Author: scooter
Date: 2011-01-19 09:29:13 -0800 (Wed, 19 Jan 2011)
New Revision: 23510
Added:
cytoscape/trunk/application/src/main/java/cytoscape/actions/PluginInstallAction.java
Modified:
cytoscape/trunk/application/src/main/java/cytoscape/plugin/PluginManager.java
cytoscape/trunk/application/src/main/java/cytoscape/view/CyMenus.java
Log:
Provide ability to load plugins from a file.
Added:
cytoscape/trunk/application/src/main/java/cytoscape/actions/PluginInstallAction.java
===================================================================
---
cytoscape/trunk/application/src/main/java/cytoscape/actions/PluginInstallAction.java
(rev 0)
+++
cytoscape/trunk/application/src/main/java/cytoscape/actions/PluginInstallAction.java
2011-01-19 17:29:13 UTC (rev 23510)
@@ -0,0 +1,93 @@
+/**
+ *
+ */
+package cytoscape.actions;
+
+import java.awt.event.ActionEvent;
+
+import cytoscape.Cytoscape;
+import cytoscape.logger.CyLogger;
+
+import cytoscape.task.ui.JTaskConfig;
+import cytoscape.util.CyFileFilter;
+import cytoscape.util.CytoscapeAction;
+import cytoscape.util.FileUtil;
+
+import cytoscape.dialogs.plugins.PluginUpdateDialog;
+
+import cytoscape.plugin.DownloadableInfo;
+import cytoscape.plugin.PluginInfo;
+import cytoscape.plugin.PluginManager;
+import cytoscape.plugin.ManagerException;
+import cytoscape.plugin.PluginStatus;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.MalformedURLException;
+import java.util.List;
+import java.util.ArrayList;
+
+import javax.swing.JOptionPane;
+
+public class PluginInstallAction extends CytoscapeAction {
+ protected static CyLogger logger =
CyLogger.getLogger(PluginInstallAction.class);
+
+ public PluginInstallAction() {
+ super("Install Plugin from File");
+ setPreferredMenu("Plugins");
+
+ if (PluginManager.usingWebstartManager()) {
+ setEnabled(false);
+ }
+ }
+
+ public void actionPerformed(ActionEvent e) {
+ if (PluginManager.usingWebstartManager())
+ return;
+
+ PluginManager manager = PluginManager.getPluginManager();
+
+ // Get the jar file
+ File[] jarFiles = FileUtil.getFiles("Select Plugin File",
FileUtil.LOAD,
+ new CyFileFilter[] {new
CyFileFilter("jar")});
+
+ if (jarFiles == null || jarFiles.length == 0) return;
+
+ File tempDir = manager.getPluginManageDirectory();
+
+ for (File file: jarFiles) {
+ // Copy the file into the temp directory
+ File outputFile = new File(tempDir, file.getName());
+ try {
+ copyfile(file, outputFile);
+
+ // Now we know what we want to load -- load it.
+ manager.loadPlugin(outputFile);
+ } catch (Exception ex) {
+
JOptionPane.showMessageDialog(Cytoscape.getDesktop(), "Unable to install
plugin: "+ex,
+ "Plugin install
error", JOptionPane.ERROR_MESSAGE);
+ logger.error("Unable to install plugin:
"+ex.getMessage(), ex);
+ }
+ }
+ return;
+ }
+
+ private void copyfile(File src, File dest) throws
FileNotFoundException, IOException {
+ InputStream in = new FileInputStream(src);
+ OutputStream out = new FileOutputStream(dest);
+
+ byte [] buf = new byte[4096];
+ int len;
+ while ((len = in.read(buf)) > 0) {
+ out.write(buf, 0, len);
+ }
+ in.close();
+ out.close();
+ }
+
+}
Modified:
cytoscape/trunk/application/src/main/java/cytoscape/plugin/PluginManager.java
===================================================================
---
cytoscape/trunk/application/src/main/java/cytoscape/plugin/PluginManager.java
2011-01-19 17:28:09 UTC (rev 23509)
+++
cytoscape/trunk/application/src/main/java/cytoscape/plugin/PluginManager.java
2011-01-19 17:29:13 UTC (rev 23510)
@@ -691,6 +691,57 @@
}
/**
+ * Load a single plugin based on the File object given
+ *
+ * @param plugin
+ * The plugin to load
+ * @throws MalformedURLException
+ */
+ public void loadPlugin(File plugin) throws MalformedURLException,
PluginException {
+ String fileName = plugin.getAbsolutePath();
+ if (fileName.endsWith(".jar")) {
+ try {
+ String className =
JarUtil.getPluginClass(fileName,
+
PluginInfo.FileType.JAR);
+
+ // See if we already have this className
registered
+ try {
+ Class pluginClass =
getPluginClass(className); // Get the plugin class
+ // If this succeeded, we're in trouble
-- this class is already
+ // registered
+ throw new PluginException("Duplicate
class name: "+className+". \n"+
+ "You may need
to delete a previously installed plugin.");
+ } catch (ClassNotFoundException cnfe1) {
+ // This is what we want....
+ }
+
+ // We don't want to register because we're
going to play a little
+ // fast and loose with the uniqueID, so we need
to contruct things
+ // ourselves
+
+ try {
+ addClassPath(jarURL(fileName));
+
+ // OK, now hand-craft the registration
+ Class pluginClass =
getPluginClass(className); // Get the plugin class
+ Object obj =
CytoscapePlugin.loadPlugin(pluginClass);
+ PluginInfo base = new
PluginInfo(plugin.getName(), className);
+ registerPlugin((CytoscapePlugin)obj,
new JarFile(plugin), base, true);
+ } catch (Throwable t) {
+ throw new PluginException("Classloader
Error: " + jarURL(fileName), t);
+ }
+ } catch (IOException ioe) {
+ throw new PluginException("Unable to read
plugin jar: "+ioe.getMessage(), ioe);
+ }
+
+ }
+
+ if (duplicateLoadError) {
+ addDuplicateError();
+ }
+ }
+
+ /**
* Parses the plugin input strings and transforms them into the
appropriate
* URLs or resource names. The method first checks to see if the
*/
Modified: cytoscape/trunk/application/src/main/java/cytoscape/view/CyMenus.java
===================================================================
--- cytoscape/trunk/application/src/main/java/cytoscape/view/CyMenus.java
2011-01-19 17:28:09 UTC (rev 23509)
+++ cytoscape/trunk/application/src/main/java/cytoscape/view/CyMenus.java
2011-01-19 17:29:13 UTC (rev 23510)
@@ -488,6 +488,7 @@
//
addAction(new PluginManagerAction());
addAction(new PluginUpdateAction());
+ addAction(new PluginInstallAction());
opsMenu.addSeparator();
--
You received this message because you are subscribed to the Google Groups
"cytoscape-cvs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/cytoscape-cvs?hl=en.