QedeqController.java
001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002  *
003  * Copyright 2000-2013,  Michael Meyling <mime@qedeq.org>.
004  *
005  * "Hilbert II" is free software; you can redistribute
006  * it and/or modify it under the terms of the GNU General Public
007  * License as published by the Free Software Foundation; either
008  * version 2 of the License, or (at your option) any later version.
009  *
010  * This program is distributed in the hope that it will be useful,
011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013  * GNU General Public License for more details.
014  */
015 
016 package org.qedeq.gui.se.control;
017 
018 import java.util.ArrayList;
019 
020 import javax.swing.Action;
021 import javax.swing.JFrame;
022 import javax.swing.JMenuItem;
023 import javax.swing.JOptionPane;
024 
025 import org.qedeq.gui.se.tree.NothingSelectedException;
026 import org.qedeq.gui.se.tree.QedeqTreeCtrl;
027 import org.qedeq.gui.se.util.DataDictionary;
028 import org.qedeq.gui.se.util.MenuHelper;
029 import org.qedeq.kernel.bo.KernelContext;
030 import org.qedeq.kernel.bo.common.QedeqBo;
031 import org.qedeq.kernel.se.common.Plugin;
032 
033 
034 /**
035  * Controller for a the GUI application.
036  *
037  * A Controller, which represents the classes connecting the model and the view, and is used to
038  * communicate between classes in the model and view.
039  * Connects the models and the views. Handles program flow.
040  *
041  * LATER mime 20070605: encapsulate actions that need another thread
042  *                     for later java versions: use Executor framework
043  *
044  @author  Michael Meyling
045  */
046 public class QedeqController {
047 
048     /** Tree controller. */
049     private QedeqTreeCtrl treeCtrl;
050 
051     /** About action. */
052     private final Action aboutAction;
053 
054     /** Help action. */
055     private final Action helpAction;
056 
057     /** Exit action. */
058     private final Action exitAction;
059 
060     /** Add QEDEQ module from web. */
061     private final Action addAction;
062 
063     /** Add QEDEQ module from local file. */
064     private final Action addFileAction;
065 
066     /** Remove all QEDEQ modules from memory. */
067     private final Action removeAllAction;
068 
069     /** Remove all QEDEQ modules from local buffer. */
070     private final Action removeLocalBufferAction;
071 
072     /** Remove all selected QEDEQ modules from memory. */
073     private final Action removeModuleAction;
074 
075     /** Add all modules from <a href="http://www.qedeq.org/">Hilbert II</a> webpage. */
076     private final Action addAllModulesFromQedeqAction;
077 
078     /** Show all service processes. */
079     private final Action processViewAction;
080 
081     /** Terminate all service processes. */
082     private final Action terminateAllAction;
083 
084     /** Check logical correctness of QEDEQ module. */
085     private final Action checkLogicAction;
086 
087     /** Remove all plugin results for QEDEQ module. */
088     private final Action removePluginResultsAction;
089 
090     /** Show preferences window. */
091     private final Action preferencesAction;
092 
093     /** Show plugin preferences window. */
094     private final Action pluginPreferencesAction;
095 
096     /** Show parser window. */
097     private final Action latexParserAction;
098 
099     /** Show parser window. */
100     private final Action textParserAction;
101 
102     /** Show parser window. */
103     private final Action proofTextParserAction;
104 
105     /** List of already wanted QEDEQ modules.*/
106     private final ArrayList moduleHistory;
107 
108     /** Reference to main frame. */
109     private final JFrame main;
110 
111     /** Maximum number of entries in history. */
112     static final int MAXIMUM_MODULE_HISTORY = 20// LATER 20070606: put into properties
113 
114     /**
115      * Constructor.
116      *
117      @param   main    Reference to main frame.
118      */
119     public QedeqController(final JFrame main) {
120         this.main = main;
121         aboutAction = new AboutAction(this);
122         helpAction = new HelpAction(this);
123         preferencesAction = new PreferencesAction(this);
124         pluginPreferencesAction = new PluginPreferencesAction(this);
125         latexParserAction = new LatexParserAction(this);
126         textParserAction = new TextParserAction(this);
127         proofTextParserAction = new ProofTextParserAction(this);
128         exitAction = new ExitAction();
129         addAction = new AddAction(this);
130         addFileAction = new AddFileAction(this);
131         addAllModulesFromQedeqAction = new AddAllModulesFromQedeqAction();
132         removeAllAction = new RemoveAllAction();
133         removeModuleAction = new RemoveModuleAction(this);
134         removeLocalBufferAction = new RemoveLocalBufferAction();
135         checkLogicAction = new CheckLogicAction(this);
136         removePluginResultsAction = new RemovePluginResultsAction(this);
137         processViewAction = new ProcessViewAction();
138         terminateAllAction = new TerminateAllAction();
139 
140         final String[] list = KernelContext.getInstance().getConfig().getModuleHistory();
141         moduleHistory = new ArrayList();
142         for (int i = 0; i < list.length; i++) {
143             getModuleHistory().add(list[i]);
144         }
145 
146         // LATER mime 20070606: dynamic evaluation from web page?
147         if (getModuleHistory().size() == 0) {
148             final String prefix = "http://wwww.qedeq.org/"
149                 + KernelContext.getInstance().getKernelVersionDirectory() "/doc/";
150             getModuleHistory().add(prefix + "math/qedeq_logic_v1.xml");
151             getModuleHistory().add(prefix + "math/qedeq_formal_logic_v1.xml");
152             getModuleHistory().add(prefix + "math/qedeq_set_theory_v1.xml");
153             getModuleHistory().add(prefix + "project/qedeq_basic_concept.xml");
154             getModuleHistory().add(prefix + "project/qedeq_logic_language.xml");
155             getModuleHistory().add(prefix + "sample/qedeq_sample1.xml");
156             getModuleHistory().add(prefix + "sample/qedeq_sample2.xml");
157             getModuleHistory().add(prefix + "sample/qedeq_sample3.xml");
158             getModuleHistory().add(prefix + "sample/qedeq_sample4.xml");
159         }
160     }
161 
162     /**
163      * Set tree controller.
164      *
165      @param   treeCtrl    Tree controller.
166      */
167     public void setTreeCtrl(final QedeqTreeCtrl treeCtrl) {
168         this.treeCtrl = treeCtrl;
169     }
170 
171     /**
172      * Get selected module.
173      *
174      @return  Selected modules.
175      @throws  NothingSelectedException    No modules were selected.
176      */
177     public QedeqBo[] getSelected() throws NothingSelectedException {
178         return treeCtrl.getSelected();
179     }
180 
181     /**
182      * Get about action.
183      *
184      @return  Action.
185      */
186     public Action getAboutAction() {
187         return aboutAction;
188     }
189 
190     /**
191      * Get help action.
192      *
193      @return  Action.
194      */
195     public Action getHelpAction() {
196         return helpAction;
197     }
198 
199     /**
200      * Get preferences window startup action.
201      *
202      @return  Action.
203      */
204     public Action getPreferencesAction() {
205         return preferencesAction;
206     }
207 
208     /**
209      * Get plugin preferences window startup action.
210      *
211      @return  Action.
212      */
213     public Action getPluginPreferencesAction() {
214         return pluginPreferencesAction;
215     }
216 
217     /**
218      * Get parser window startup action.
219      *
220      @return  Action.
221      */
222     public Action getLatexParserAction() {
223         return latexParserAction;
224     }
225 
226     /**
227      * Get parser window startup action.
228      *
229      @return  Action.
230      */
231     public Action getTextParserAction() {
232         return textParserAction;
233     }
234 
235     /**
236      * Get parser window startup action.
237      *
238      @return  Action.
239      */
240     public Action getProofTextParserAction() {
241         return proofTextParserAction;
242     }
243 
244     /**
245      * Get exit action.
246      *
247      @return  Action.
248      */
249     public Action getExitAction() {
250         return exitAction;
251     }
252 
253     /**
254      * Get action for adding a new QEDEQ module out of the web.
255      *
256      @return  Action.
257      */
258     public Action getAddAction() {
259         return addAction;
260     }
261 
262     /**
263      * Get action for adding a new QEDEQ module from a local file.
264      *
265      @return  Action.
266      */
267     public Action getAddFileAction() {
268         return addFileAction;
269     }
270 
271     /**
272      * Get all plugin actions as menu entries.
273      *
274      @return  Menu entries for plugins.
275      */
276     public JMenuItem[] getPluginMenuEntries() {
277         final Plugin[] plugins = KernelContext.getInstance().getPlugins();
278         final PluginAction[] pluginActions = new PluginAction[plugins.length];
279         for (int i = 0; i < plugins.length; i++) {
280             pluginActions[inew PluginAction(this, plugins[i]);
281         }
282         JMenuItem[] result = new JMenuItem[pluginActions.length];
283         for (int i = 0; i < pluginActions.length; i++) {
284             final JMenuItem item = MenuHelper.createMenuItem(pluginActions[i].getPlugin()
285                 .getPluginActionName());
286             item.addActionListener(pluginActions[i]);
287             item.setIcon(pluginActions[i].getIcon());
288             item.setToolTipText(pluginActions[i].getPlugin().getPluginDescription());
289             result[i= item;
290         }
291         return result;
292     }
293 
294     /**
295      * Get action for checking the logical correctness of the selected QEDEQ modules.
296      *
297      @return  Action.
298      */
299     public Action getCheckLogicAction() {
300         return checkLogicAction;
301     }
302 
303     /**
304      * Get action for removing all plugin results for the selected QEDEQ modules.
305      *
306      @return  Action.
307      */
308     public Action getRemovePluginResultsAction() {
309         return removePluginResultsAction;
310     }
311 
312     /**
313      * Get action for removing all QEDEQ modules from memory.
314      *
315      @return  Action.
316      */
317     public Action getRemoveAllAction() {
318         return removeAllAction;
319     }
320 
321     /**
322      * Get action for removing all seelected QEDEQ modules from memory.
323      *
324      @return  Action.
325      */
326     public Action getRemoveModuleAction() {
327         return removeModuleAction;
328     }
329 
330     /**
331      * Get action for removing all QEDEQ modules from memory and local buffer.
332      *
333      @return  Action.
334      */
335     public Action getRemoveLocalBufferAction() {
336         return removeLocalBufferAction;
337     }
338 
339     /**
340      * Get action for loading all QEDEQ modules for the current release from the QEDEQ web site.
341      *
342      @return  Action.
343      */
344     public Action getAddAllModulesFromQedeqAction() {
345         return addAllModulesFromQedeqAction;
346     }
347 
348     /**
349      * Get action for starting the service process viewer.
350      *
351      @return  Action.
352      */
353     public Action getProcessViewAction() {
354         return processViewAction;
355     }
356 
357     /**
358      * Get action for stopping all currently running plugin actions.
359      *
360      @return  Action.
361      */
362     public Action getTerminateAllAction() {
363         return terminateAllAction;
364     }
365 
366     /**
367      * This method returns a string from the resource bundle.
368      *
369      @param   key     Name to look for.
370      @return  Value.
371      */
372     public String getString(final String key) {
373         return DataDictionary.getInstance().getString(key);
374     }
375 
376     /**
377      * Returns a mnemonic from the resource bundle. Typically used as
378      * keyboard shortcuts in menu items.
379      *
380      @param   key     Name to look for.
381      @return  Mnemonic.
382      */
383     public char getMnemonic(final String key) {
384         return DataDictionary.getInstance().getMnemonic(key);
385     }
386 
387     void addToModuleHistory(final String url) {
388         getModuleHistory().add(0, url);
389         for (int i = 1; i < getModuleHistory().size(); i++) {
390             if (url.equals(getModuleHistory().get(i))) {
391                 getModuleHistory().remove(i);
392                 i--;
393             }
394         }
395         for (int i = getModuleHistory().size() 1; i >= QedeqController.MAXIMUM_MODULE_HISTORY;
396                 i--) {
397             getModuleHistory().remove(i);
398         }
399         KernelContext.getInstance().getConfig().saveModuleHistory(getModuleHistory());
400     }
401 
402     ArrayList getModuleHistory() {
403         return moduleHistory;
404     }
405 
406     JFrame getMainFrame() {
407         return main;
408     }
409 
410     void selectionError() {
411         JOptionPane.showMessageDialog(
412             QedeqController.this.main,
413             "No QEDEQ module selected! In the tree at least one QEDEQ module must be selected",
414             "Error",
415             JOptionPane.ERROR_MESSAGE,
416             null);
417     }
418 
419 }