PluginAction.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.awt.event.ActionEvent;
019 
020 import javax.swing.AbstractAction;
021 import javax.swing.ImageIcon;
022 import javax.swing.SwingUtilities;
023 
024 import org.qedeq.base.trace.Trace;
025 import org.qedeq.gui.se.pane.QedeqGuiConfig;
026 import org.qedeq.gui.se.pane.TextPaneWindow;
027 import org.qedeq.gui.se.tree.NothingSelectedException;
028 import org.qedeq.gui.se.util.GuiHelper;
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  * Execute plugin for selected QEDEQ module files.
035  */
036 public class PluginAction extends AbstractAction {
037 
038     /** This class. */
039     private static final Class CLASS = PluginAction.class;
040 
041     /** Controller reference. */
042     private final QedeqController controller;
043 
044     /** Start this plugin. */
045     private Plugin plugin;
046 
047     /** Icon resolution. */
048     private String resolution = QedeqGuiConfig.getInstance().getIconSize();
049 
050     /**
051      * Constructor.
052      *
053      @param   controller  Reference to controller.
054      @param   plugin      Start action for this plugin.
055      */
056     PluginAction(final QedeqController controller, final Plugin plugin) {
057         this.controller = controller;
058         this.plugin = plugin;
059     }
060 
061     public void actionPerformed(final ActionEvent e) {
062         final String method = "actionPerformed";
063         Trace.begin(CLASS, this, method);
064         try {
065             final QedeqBo[] props;
066             try {
067                 props = controller.getSelected();
068             catch (NothingSelectedException ex) {
069                 controller.selectionError();
070                 return;
071             }
072 
073             for (int i = 0; i < props.length; i++) {
074                 final QedeqBo prop = props[i];
075                 final Thread thread = new Thread() {
076                     public void run() {
077                         final Object result = KernelContext.getInstance().executePlugin(
078                             plugin.getPluginId(),
079                             prop.getModuleAddress()null);
080                         if (result instanceof String) {
081                             final Runnable showTextResult = new Runnable() {
082                                 public void run() {
083                                     (new TextPaneWindow(plugin.getPluginActionName(),
084                                         PluginAction.this.getIcon(),
085                                         (Stringresult)).setVisible(true);
086                                 }
087                             };
088                             SwingUtilities.invokeLater(showTextResult);
089                         }
090                     }
091                 };
092                 thread.setDaemon(true);
093                 thread.setPriority(Thread.MIN_PRIORITY);
094                 thread.start();
095             }
096         finally {
097             Trace.end(CLASS, this, method);
098         }
099     }
100 
101     /**
102      * Get plugin we work for.
103      *
104      @return  The plugin we work for.
105      */
106     public Plugin getPlugin() {
107         return plugin;
108     }
109 
110     public ImageIcon getIcon() {
111         if (plugin.getPluginActionName().endsWith("LaTeX")) {
112             return GuiHelper.readImageIcon("tango/" + resolution + "/mimetypes/x-office-document.png");
113         else if (-< plugin.getPluginActionName().indexOf("euristic")) {
114             return GuiHelper.readImageIcon("tango/" + resolution + "/apps/accessories-calculator.png");
115         else if (plugin.getPluginActionName().endsWith("earch")) {
116             return GuiHelper.readImageIcon("tango/" + resolution + "/categories/applications-system.png");
117         else if (-< plugin.getPluginActionName().indexOf("how")) {
118             return GuiHelper.readImageIcon("tango/" + resolution + "/actions/edit-find.png");
119         else if (-< plugin.getPluginActionName().indexOf("odel")) {
120             return GuiHelper.readImageIcon("oil/" + resolution + "/apps/accessories-calculator-3.png");
121         else if (-< plugin.getPluginActionName().indexOf("UTF-8")) {
122             return GuiHelper.readImageIcon("tango/" + resolution + "/mimetypes/text-x-generic.png");
123         else if (-< plugin.getPluginActionName().indexOf("heck")
124                 && -< plugin.getPluginActionName().indexOf("roofs")) {
125             return GuiHelper.readImageIcon("tango/" + resolution + "/actions/run.png");
126         else if (-< plugin.getPluginActionName().indexOf("ind")
127                 && -< plugin.getPluginActionName().indexOf("roofs")) {
128             return GuiHelper.readImageIcon("oil/" + resolution + "/apps/development-java-3.png");
129         else {
130             return GuiHelper.readImageIcon("tango/" + resolution + "/actions/edit-find.png");
131         }
132     }
133 
134 }