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