001    /**
002     * Copyright (C) 2007-2009, Jens Lehmann
003     *
004     * This file is part of DL-Learner.
005     * 
006     * DL-Learner is free software; you can redistribute it and/or modify
007     * it under the terms of the GNU General Public License as published by
008     * the Free Software Foundation; either version 3 of the License, or
009     * (at your option) any later version.
010     *
011     * DL-Learner is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014     * GNU General Public License for more details.
015     *
016     * You should have received a copy of the GNU General Public License
017     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018     *
019     */
020    package org.dllearner.tools.protege;
021    
022    import java.awt.BorderLayout;
023    import java.awt.Color;
024    import java.awt.Dimension;
025    import java.awt.event.ActionListener;
026    import java.net.URL;
027    import java.util.Set;
028    
029    import javax.swing.DefaultListModel;
030    import javax.swing.ImageIcon;
031    import javax.swing.JButton;
032    import javax.swing.JComponent;
033    import javax.swing.JLabel;
034    import javax.swing.JPanel;
035    import javax.swing.JScrollPane;
036    import javax.swing.JTextArea;
037    import javax.swing.JToggleButton;
038    
039    import org.protege.editor.owl.OWLEditorKit;
040    import org.semanticweb.owl.model.OWLDescription;
041    /**
042     * This class is responsible for the view of the dllearner. It renders the
043     * output for the user and is the graphical component of the plugin.
044     * 
045     * @author Christian Koetteritzsch
046     * 
047     */
048    public class DLLearnerView {
049    
050            
051            private static final  long serialVersionUID = 624829578325729385L; 
052            // this is the Component which shows the view of the dllearner
053            private final JComponent learner;
054    
055            // Accept button to add the learned concept to the owl
056    
057            private final JButton accept;
058    
059            // Runbutton to start the learning algorithm
060    
061            private final JButton run;
062    
063            // This is the label for the advanced button.
064    
065            private final JLabel adv;
066    
067            // This is the color for the error message. It is red.
068    
069            private final Color colorRed = Color.red;
070    
071            // This is the text area for the error message when an error occurred
072    
073            private final JTextArea errorMessage;
074    
075            // Advanced Button to activate/deactivate the example select panel
076    
077            private final JToggleButton advanced;
078    
079            // Action Handler that manages the Button actions
080    
081            private final ActionHandler action;
082    
083            // This is the model of the dllearner plugin which includes all data
084    
085            private final DLLearnerModel model;
086    
087            // Panel for the suggested concepts
088    
089            private SuggestClassPanel sugPanel;
090    
091            // Selection panel for the positive and negative examples
092    
093            private final PosAndNegSelectPanel posPanel;
094    
095            // Picture for the advanced button when it is not toggled
096    
097            private final ImageIcon icon;
098    
099            // Picture of the advanced button when it is toggled
100            private final JPanel addButtonPanel;
101            private final JLabel wikiPane;
102            private final ImageIcon toggledIcon;
103            private final JTextArea hint;
104            private boolean isInconsistent;
105            // This is the Panel for more details of the suggested concept
106            private final MoreDetailForSuggestedConceptsPanel detail;
107            private ReadingOntologyThread readThread;
108            private final OWLEditorKit editorKit;
109            private final JPanel learnerPanel;
110            private final JScrollPane learnerScroll;
111            private static  final int SCROLL_SPPED = 10;
112    
113            /**
114             * The constructor for the DL-Learner tab in the class description
115             * editor.
116             * 
117             * @param editor OWLEditorKit
118             * @param label String
119             */
120            public DLLearnerView(String label, OWLEditorKit editor) {
121                    editorKit = editor;
122                    model = new DLLearnerModel(editorKit, this);
123                    model.setID(label);
124                    sugPanel = new SuggestClassPanel();
125                    learnerPanel = new JPanel();
126                    learnerPanel.setLayout(new BorderLayout());
127                    learnerScroll = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
128                    action = new ActionHandler(model, this, label);
129                    wikiPane = new JLabel("<html>See <a href=\"http://dl-learner.org/wiki/ProtegePlugin\">http://dl-learner.org/wiki/ProtegePlugin</a> for an introduction.</html>");
130                    URL iconUrl = this.getClass().getResource("arrow.gif");
131                    icon = new ImageIcon(iconUrl);
132                    URL toggledIconUrl = this.getClass().getResource("arrow2.gif");
133                    toggledIcon = new ImageIcon(toggledIconUrl);
134                    adv = new JLabel("Advanced Settings");
135                    advanced = new JToggleButton(icon);
136                    advanced.setVisible(true);
137                    run = new JButton("suggest class expression");
138                    accept = new JButton("ADD");
139                    addButtonPanel = new JPanel(new BorderLayout());
140                    sugPanel.addSuggestPanelMouseListener(action);
141                    errorMessage = new JTextArea();
142                    errorMessage.setEditable(false);
143                    hint = new JTextArea();
144                    hint.setEditable(false);
145                    hint.setText("To get suggestions for class expression, please click the button above.");
146                    learner = new JPanel();
147                    advanced.setSize(20, 20);
148                    learner.setLayout(null);
149                    accept.setPreferredSize(new Dimension(260, 50));
150                    advanced.setName("Advanced");
151                    learnerScroll.setPreferredSize(new Dimension(600, 400));
152                    learnerScroll.getVerticalScrollBar().setUnitIncrement(SCROLL_SPPED);
153                    posPanel = new PosAndNegSelectPanel(model, action);
154                    detail = new MoreDetailForSuggestedConceptsPanel(model);
155                    this.addAcceptButtonListener(this.action);
156                    this.addRunButtonListener(this.action);
157                    this.addAdvancedButtonListener(this.action);
158                    
159            }
160            
161            /**
162             * This method returns the SuggestClassPanel.
163             * @return SuggestClassPanel
164             */
165            public SuggestClassPanel getSuggestClassPanel() {
166                    return sugPanel;
167            }
168            /**
169             * This method returns the PosAndNegSelectPanel.
170             * @return PosAndNegSelectPanel
171             */
172            public PosAndNegSelectPanel getPosAndNegSelectPanel() {
173                    return posPanel;
174            }
175            
176            /**
177             * This Method renders the view of the plugin.
178             */
179            public void makeView() {
180                    learner.remove(detail);
181                    run.setEnabled(false);
182                    detail.unsetPanel();
183                    learnerPanel.setPreferredSize(new Dimension(575, 350));
184                    detail.setVisible(false);
185                    hint.setText("");
186                    isInconsistent = false;
187                    readThread = new ReadingOntologyThread(editorKit, this, model);
188                    readThread.start();
189                    hint.setVisible(true);
190                    advanced.setIcon(icon);
191                    accept.setEnabled(false);
192                    action.resetToggled();
193                    addButtonPanel.add("North", accept);
194                    sugPanel.setSuggestList(new DefaultListModel());
195                    sugPanel = sugPanel.updateSuggestClassList();
196                    advanced.setSelected(false);
197                    sugPanel.setBounds(10, 35, 470, 110);
198                    adv.setBounds(40, 195, 200, 20);
199                    wikiPane.setBounds(220, 0, 350, 30);
200                    addButtonPanel.setBounds(485, 40, 80, 70);
201                    run.setBounds(10, 0, 200, 30);
202                    advanced.setBounds(10, 195, 20, 20);
203                    detail.setBounds(10, 195, 600, 300);
204                    detail.setVisible(true);
205                    sugPanel.setVisible(true);
206                    posPanel.setVisible(false);
207                    posPanel.setBounds(10, 225, 490, 250);
208                    accept.setBounds(510, 40, 80, 80);
209                    hint.setBounds(10, 150, 490, 35);
210                    errorMessage.setBounds(485, 110, 80, 80);
211                    learner.add(run);
212                    learner.add(wikiPane);
213                    learner.add(adv);
214                    learner.add(advanced);
215                    learner.add(sugPanel);
216                    learner.add(addButtonPanel);
217                    learner.add(hint);
218                    learner.add(errorMessage);
219                    learner.add(posPanel);
220                    learnerPanel.add(learner);
221                    learnerScroll.setViewportView(learnerPanel);
222                    this.renderErrorMessage("");
223                            
224            }
225            /**
226             * This method sets the right icon for the advanced Panel.
227             * @param toggled boolean
228             */
229            public void setIconToggled(boolean toggled) {
230                    if (toggled) {
231                            advanced.setIcon(toggledIcon);
232                            learnerPanel.setPreferredSize(new Dimension(575, 400));
233                    }
234                    if (!toggled) {
235                            advanced.setIcon(icon);
236                            learnerPanel.setPreferredSize(new Dimension(575, 350));
237                    }
238            }
239            
240            public void setGraphicalPanel() {
241                    learner.remove(posPanel);
242                    learner.remove(advanced);
243                    learner.remove(adv);
244                    learner.repaint();
245                    posPanel.setBounds(10, 435, 490, 250);
246                    adv.setBounds(40, 405, 200, 20);
247                    advanced.setBounds(10, 405, 20, 20);
248                    detail.setBounds(10, 195, 590, 200);
249                    detail.setVisible(true);
250                    learner.add(adv);
251                    learner.add(advanced);
252                    learner.add(posPanel);
253                    learner.add(detail);
254                    learnerPanel.setPreferredSize(new Dimension(575, 620));
255                    learnerPanel.removeAll();
256                    learnerPanel.add(learner);
257                    learnerScroll.setViewportView(learnerPanel);
258                    learnerScroll.repaint();
259                    
260                    
261            }
262            /**
263             * This Method changes the hint message. 
264             * @param message String hintmessage
265             */
266            public void setHintMessage(String message) {
267                    hint.setText(message);
268            }
269            
270            /**
271             * This method returns the hint panel.
272             * @return hint panel
273             */
274            public JTextArea getHintPanel() {
275                    return hint;
276            }
277            
278            /**
279             * Sets the panel to select/deselect the examples visible/invisible.
280             * @param visible boolean
281             */
282            public void setExamplePanelVisible(boolean visible) {
283                    posPanel.setVisible(visible);
284                    detail.repaint();
285            }
286    
287            /**
288             * Returns the AddButton.
289             * @return JButton
290             */
291            public JButton getAddButton() {
292                    return accept;
293            }
294    
295            /**
296             * Returns all added descriptions.
297             * @return Set(OWLDescription) 
298             */
299            public Set<OWLDescription> getSolutions() {
300    
301                    return model.getNewOWLDescription();
302            }
303            
304            public void dispose() {
305                    this.unsetEverything();
306                    sugPanel.getSuggestList().removeAll();
307                    learner.removeAll();
308                    sugPanel = null;
309                    model.getSuggestModel().clear();
310                    model.getIndividual().clear();
311            }
312    
313            /**
314             * Returns the last added description.
315             * @return OWLDescription
316             */
317            public OWLDescription getSolution() {
318                    return model.getSolution();
319            }
320    
321            /**
322        * Destroys everything in the view after the plugin is closed.
323        */
324            public void unsetEverything() {
325                    run.setEnabled(true);
326                    model.getNewOWLDescription().clear();
327                    action.destroyDLLearnerThread();
328                    errorMessage.setText("");
329                    learner.removeAll();
330            }
331    
332            /**
333             * Renders the error message when an error occured.
334             * @param s String 
335             */
336            public void renderErrorMessage(String s) {
337                    errorMessage.setForeground(colorRed);
338                    errorMessage.setText(s);
339            }
340            /**
341             * This Method returns the panel for more details for the chosen concept.
342             * @return MoreDetailForSuggestedConceptsPanel
343             */
344            public MoreDetailForSuggestedConceptsPanel getMoreDetailForSuggestedConceptsPanel() {
345                    return detail;
346            }
347    
348            /**
349             * This Method returns the run button.
350             * @return JButton
351             */
352            public JButton getRunButton() {
353                    return run;
354            }
355            
356            /**
357             * This method sets if ontology is inconsistent or not.
358             * @param isIncon boolean if ontology is consisten
359             */
360            public void setIsInconsistent(boolean isIncon) {
361                    this.isInconsistent = isIncon;
362            }
363            
364            /**
365             * Adds Actionlistener to the run button.
366             * @param a ActionListener
367             */
368            public void addRunButtonListener(ActionListener a) {
369                    run.addActionListener(a);
370            }
371    
372            /**
373             * Adds Actionlistener to the add button.
374             * @param a ActionListener
375             */
376            public void addAcceptButtonListener(ActionListener a) {
377                    accept.addActionListener(a);
378            }
379    
380            /**
381             * Adds Actionlistener to the advanced button.
382             * @param a ActionListener
383             */
384            public void addAdvancedButtonListener(ActionListener a) {
385                    advanced.addActionListener(a);
386            }
387            
388            /**
389             * This method sets the run button enable after learning.
390             */
391            public void algorithmTerminated() {
392                    String error = "learning\nsuccessful";
393                    String message = "";
394                    if(isInconsistent) {
395                            message = "Class expressions marked red will lead to an inconsistent ontology. \nPlease double click on them to view detail information.";
396                    } else {
397                            message = "To view details about why a class expression was suggested, please click on it.";
398                    }
399                    run.setEnabled(true);
400                    // start the algorithm and print the best concept found
401                    renderErrorMessage(error);
402                    setHintMessage(message);
403            }
404            
405            public JComponent getLearnerView() {
406                    return learnerScroll;
407            }
408            
409            public DLLearnerModel getDLLearnerModel() {
410                    return model;
411            }
412            
413            public ReadingOntologyThread getReadingOntologyThread() {
414                    return readThread;
415            }
416    }