001    /**
002     * Copyright (C) 2007-2008, 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    
021    package org.dllearner.tools.ore;
022    
023    
024    import java.awt.event.ActionListener;
025    import java.util.List;
026    import java.util.Set;
027    import java.util.concurrent.ExecutionException;
028    
029    import javax.swing.DefaultListModel;
030    import javax.swing.JOptionPane;
031    import javax.swing.SwingWorker;
032    
033    import org.dllearner.core.owl.Description;
034    import org.dllearner.core.owl.NamedClass;
035    import org.semanticweb.owl.model.OWLOntologyChange;
036    
037    /**
038     * This class is responsible for reacting to events generated by pushing any of the
039     * three buttons, 'Next', 'Previous', and 'Cancel.' Based on what button is pressed,
040     * the controller will update the model to show a new panel and reset the state of
041     * the buttons as necessary.
042     * @author Lorenz Buehmann
043     */
044    public class WizardController implements ActionListener {
045        
046        private Wizard wizard;
047        
048        /**
049         * This constructor accepts a reference to the Wizard component that created it,
050         * which it uses to update the button components and access the WizardModel.
051         * @param w A callback to the Wizard component that created this controller.
052         */    
053        public WizardController(Wizard w) {
054            wizard = w;
055        }
056    
057        /**
058         * Calling method for the action listener interface. This class listens for actions
059         * performed by the buttons in the Wizard class, and calls methods below to determine
060         * the correct course of action.
061         * @param evt The ActionEvent that occurred.
062         */    
063        public void actionPerformed(java.awt.event.ActionEvent evt) {
064            
065            if (evt.getActionCommand().equals(Wizard.CANCEL_BUTTON_ACTION_COMMAND)){
066                cancelButtonPressed();
067            } else if (evt.getActionCommand().equals(Wizard.BACK_BUTTON_ACTION_COMMAND)){
068                backButtonPressed();
069            } else if (evt.getActionCommand().equals(Wizard.NEXT_BUTTON_ACTION_COMMAND)){
070                nextButtonPressed();
071            }
072        }
073        
074        
075        
076        private void cancelButtonPressed() {
077            
078            wizard.close(Wizard.CANCEL_RETURN_CODE);
079        }
080    
081        private void nextButtonPressed() {
082     
083            WizardModel model = wizard.getModel();
084            WizardPanelDescriptor descriptor = model.getCurrentPanelDescriptor();
085            ORE ore = model.getOre();
086            //  If it is a finishable panel, close down the dialog. Otherwise,
087            //  get the ID that the current panel identifies as the next panel,
088            //  and display it.
089            
090            Object nextPanelDescriptor = descriptor.getNextPanelDescriptor();
091            WizardPanelDescriptor nextDescriptor = model.getPanelHashMap().get(nextPanelDescriptor);
092          
093            if(nextPanelDescriptor.equals("CLASS_CHOOSE_OWL_PANEL")){
094    //              model.getOre().getOwlReasoner().isSatisfiable()
095                    
096                    ((ClassPanelOWLDescriptor) nextDescriptor).getOwlClassPanel().getModel().clear();
097                    new ConceptRetriever(nextPanelDescriptor).execute();
098            }
099           
100            if(nextPanelDescriptor.equals("LEARNING_PANEL")){
101                    ore.init();
102                    LearningPanelDescriptor learnDescriptor = ((LearningPanelDescriptor) model.getPanelHashMap().get(nextPanelDescriptor));
103                    learnDescriptor.setPanelDefaults();
104                                    
105            }
106            
107            if(nextPanelDescriptor.equals("REPAIR_PANEL")){
108                    RepairPanelDescriptor repair = ((RepairPanelDescriptor) model.getPanelHashMap().get(nextPanelDescriptor));
109                    repair.refreshExampleLists();
110                    
111    //              OWLOntologyChange change = model.getOre().getModi().addAxiomToOWL(model.getOre().getConceptToAdd(), model.getOre().getIgnoredConcept());
112    //              repair.getOntologyChanges().add(change);
113    
114            }
115            
116            if(nextPanelDescriptor.equals("SAVE_PANEL")){
117    
118                    Description newDesc = model.getOre().getNewClassDescription().getDescription();
119                    Description oldClass = model.getOre().getIgnoredConcept();
120                    
121                    List<OWLOntologyChange> changes = ore.getModifier().rewriteClassDescription(newDesc, oldClass);
122                    ((RepairPanelDescriptor) descriptor).getOntologyChanges().addAll(changes);
123                            
124            }
125            
126            
127            if (nextPanelDescriptor instanceof WizardPanelDescriptor.FinishIdentifier) {
128                wizard.close(Wizard.FINISH_RETURN_CODE);
129            } else {        
130                wizard.setCurrentPanel(nextPanelDescriptor);
131            }
132            
133            //TODO
134            refreshLeftPanel(nextPanelDescriptor);
135            
136            
137            
138            
139            
140        }
141        
142        
143        private void backButtonPressed() {
144     
145            WizardModel model = wizard.getModel();
146            WizardPanelDescriptor descriptor = model.getCurrentPanelDescriptor();
147     
148            //  Get the descriptor that the current panel identifies as the previous
149            //  panel, and display it.
150            
151            Object backPanelDescriptor = descriptor.getBackPanelDescriptor();        
152            
153            
154            if(backPanelDescriptor.equals("LEARNING_PANEL")){
155                    RepairPanelDescriptor repairDescriptor = (RepairPanelDescriptor) descriptor;
156                    if(repairDescriptor.getOntologyChanges().size() > 0){
157                            if (JOptionPane.showConfirmDialog(wizard.getDialog(),
158                                            "All changes will be lost!", "Warning!", 
159                                            JOptionPane.YES_NO_OPTION)
160                                         == JOptionPane.YES_OPTION){
161            
162                                    model.getOre().getModifier().undoChanges(repairDescriptor.getOntologyChanges());
163                                    repairDescriptor.getOntologyChanges().clear();
164                                            wizard.setCurrentPanel(backPanelDescriptor);
165                                    refreshLeftPanel(backPanelDescriptor);
166                                    }
167                    } else{
168                            wizard.setCurrentPanel(backPanelDescriptor);
169                            refreshLeftPanel(backPanelDescriptor);
170                    }
171                    
172                    
173                    
174            } else if(backPanelDescriptor.equals("CLASS_CHOOSE_OWL_PANEL")){
175                    LearningPanelDescriptor learnDescriptor = (LearningPanelDescriptor) descriptor;
176                    if(learnDescriptor.getLa() != null){
177                            learnDescriptor.getLa().stop();
178                            learnDescriptor.getTimer().cancel();
179                    }
180                    
181                    wizard.setCurrentPanel(backPanelDescriptor);
182                    refreshLeftPanel(backPanelDescriptor);
183            
184            }else{
185                    wizard.setCurrentPanel(backPanelDescriptor);
186                refreshLeftPanel(backPanelDescriptor);
187            }
188            
189            
190        }
191        
192        void refreshLeftPanel(Object panelDescriptor){
193            
194            if(panelDescriptor.equals("INTRODUCTION_PANEL")){
195                    wizard.setLeftPanel(0);
196            }
197            if(panelDescriptor.equals("KNOWLEDGESOURCE_CHOOSE_PANEL")){
198                    wizard.setLeftPanel(1);
199            }
200            if(panelDescriptor.equals("CLASS_CHOOSE_OWL_PANEL") || panelDescriptor.equals("CLASS_CHOOSE_SPARQL_PANEL")){
201                    wizard.setLeftPanel(2);
202            }
203            if(panelDescriptor.equals("LEARNING_PANEL")){
204                    wizard.setLeftPanel(3);
205            }
206            if(panelDescriptor.equals("REPAIR_PANEL")){
207                    wizard.setLeftPanel(4);
208            }
209            if(panelDescriptor.equals("SAVE_PANEL")){
210                    wizard.setLeftPanel(5);
211            }
212            
213            
214            
215        }
216    
217        
218        void resetButtonsToPanelRules() {
219        
220            //  Reset the buttons to support the original panel rules,
221            //  including whether the next or back buttons are enabled or
222            //  disabled, or if the panel is finishable.
223            
224            WizardModel model = wizard.getModel();
225            WizardPanelDescriptor descriptor = model.getCurrentPanelDescriptor();
226            
227            model.setCancelButtonText(Wizard.CANCEL_TEXT);
228    //        model.setCancelButtonIcon(Wizard.CANCEL_ICON);
229            
230            //  If the panel in question has another panel behind it, enable
231            //  the back button. Otherwise, disable it.
232            
233            model.setBackButtonText(Wizard.BACK_TEXT);
234    
235            
236            if (descriptor.getBackPanelDescriptor() != null){
237                model.setBackButtonEnabled(Boolean.TRUE);
238            } else{
239                model.setBackButtonEnabled(Boolean.FALSE);
240            }
241            //  If the panel in question has one or more panels in front of it,
242            //  enable the next button. Otherwise, disable it.
243     
244            if (descriptor.getNextPanelDescriptor() != null){
245                model.setNextFinishButtonEnabled(Boolean.TRUE);
246            } else{
247                model.setNextFinishButtonEnabled(Boolean.FALSE);
248            }
249            //  If the panel in question is the last panel in the series, change
250            //  the Next button to Finish. Otherwise, set the text back to Next.
251            
252            if (descriptor.getNextPanelDescriptor() instanceof WizardPanelDescriptor.FinishIdentifier) {
253                model.setNextFinishButtonText(Wizard.FINISH_TEXT);
254    
255            } else {
256                model.setNextFinishButtonText(Wizard.NEXT_TEXT);
257    
258            }
259            
260        }
261        /**
262         * Inner class to get all atomic classes in a background thread.
263         * @author Lorenz Buehmann
264         *
265         */
266        class ConceptRetriever extends SwingWorker<Set<NamedClass>, NamedClass> {
267                    private Object nextPanelID;
268                    private ClassPanelOWL owlClassPanel;
269                    
270                    public ConceptRetriever(Object nextPanelDescriptor) {
271    
272                            nextPanelID = nextPanelDescriptor;
273                            owlClassPanel = ((ClassPanelOWLDescriptor) wizard.getModel().getPanelHashMap().get(nextPanelID)).getOwlClassPanel();
274                    }
275    
276                    @Override
277                    public Set<NamedClass> doInBackground() {
278    
279                            owlClassPanel.getStatusLabel().setText("Loading atomic classes");
280                            owlClassPanel.getLoadingLabel().setBusy(true);
281                            owlClassPanel.getList().setCellRenderer(new ColorListCellRenderer(wizard.getModel().getOre()));
282    
283                            wizard.getModel().getOre().initReasoners();
284    
285                            Set<NamedClass> ind = wizard.getModel().getOre().getOwlReasoner().getNamedClasses();
286                                            
287    
288                            return ind;
289                    }
290    
291                    @Override
292                    public void done() {
293                            Set<NamedClass> ind = null;
294                            try {
295                                    ind = get();
296                            } catch (InterruptedException e) {
297                                    // TODO Auto-generated catch block
298                                    e.printStackTrace();
299                            } catch (ExecutionException e) {
300                                    // TODO Auto-generated catch block
301                                    e.printStackTrace();
302                            }
303                            
304                            DefaultListModel dm = new DefaultListModel();
305                            
306                            if(ind != null){
307                                    for (NamedClass cl : ind) {
308                                            dm.addElement(cl);
309                                            
310                                            //nextPanel.panel3.getModel().addElement(cl);
311                                            
312                                    }
313                            }
314    //                      wizard.getModel().getOre().setAllAtomicConcepts(ind);
315                            owlClassPanel.getList().setModel(dm);
316                            owlClassPanel.getStatusLabel().setText("Atomic classes loaded");
317                            owlClassPanel.getLoadingLabel().setBusy(false);
318                    }
319    
320            }
321       
322    
323        
324        
325    }