Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
Installer.java
1 package ij.plugin;
2 import ij.*;
3 import ij.gui.*;
4 import ij.io.*;
5 import ij.util.*;
6 import java.awt.*;
7 import java.io.*;
8 import java.util.*;
9 import java.lang.reflect.*;
10 
12 public class Installer implements PlugIn {
13 
14  private static String[] menus = {"Shortcuts", "Plugins", "Import", "Save As",
15  "Filters", "Tools", "Utilities"};
16  private static final String TITLE = "Installer";
17  private static String command = "";
18  private static String shortcut = "";
19  private static String defaultPlugin = "";
20  private static String menuStr = menus[0];
21 
22  public void run(String arg) {
23  installPlugin();
24  }
25 
26  void installPlugin() {
27  String[] plugins = getPlugins();
28  if (plugins==null || plugins.length==0) {
29  IJ.error("No plugins found");
30  return;
31  }
32  GenericDialog gd = new GenericDialog("Install Plugin", IJ.getInstance());
33  gd.addChoice("Plugin:", plugins, defaultPlugin);
34  gd.addChoice("Menu:", menus, menuStr);
35  gd.addStringField("Command:", command, 16);
36  gd.addStringField("Shortcut:", shortcut, 3);
37  gd.addStringField("Argument:", "", 12);
38  gd.showDialog();
39  if (gd.wasCanceled())
40  return;
41  String plugin = gd.getNextChoice();
42  menuStr = gd.getNextChoice();
43  command = gd.getNextString();
44  shortcut = gd.getNextString();
45  String argument = gd.getNextString();
46  IJ.register(Installer.class);
47  defaultPlugin = plugin;
48  if (command.equals("")) {
49  IJ.showMessage(TITLE, "Command required");
50  return;
51  }
52  if (shortcut.length()>1)
53  shortcut = shortcut.replace('f','F');
54  char menu = ' ';
55  if (menuStr.equals(menus[0]))
56  menu = Menus.SHORTCUTS_MENU;
57  else if (menuStr.equals(menus[1]))
58  menu = Menus.PLUGINS_MENU;
59  else if (menuStr.equals(menus[2]))
60  menu = Menus.IMPORT_MENU;
61  else if (menuStr.equals(menus[3]))
62  menu = Menus.SAVE_AS_MENU;
63  else if (menuStr.equals(menus[4]))
64  menu = Menus.FILTERS_MENU;
65  else if (menuStr.equals(menus[5]))
66  menu = Menus.TOOLS_MENU;
67  else if (menuStr.equals(menus[6]))
68  menu = Menus.UTILITIES_MENU;
69  if (!argument.equals(""))
70  plugin += "(\"" + argument +"\")";
71  int err = Menus.installPlugin(plugin,menu,command,shortcut,IJ.getInstance());
72  switch (err) {
73  case Menus.COMMAND_IN_USE:
74  IJ.showMessage(TITLE, "The command \""+command+"\" \nis already being used.");
75  return;
76  case Menus.INVALID_SHORTCUT:
77  IJ.showMessage(TITLE, "The shortcut must be a single character or \"F1\"-\"F12\".");
78  return;
79  case Menus.SHORTCUT_IN_USE:
80  IJ.showMessage("The \""+shortcut+"\" shortcut is already being used.");
81  return;
82  default:
83  command = "";
84  shortcut = "";
85  break;
86  }
87  if (!plugin.endsWith(")"))
88  installAbout(plugin);
89  }
90 
91  void installAbout(String plugin) {
92  boolean hasShowAboutMethod=false;
93  PluginClassLoader loader = new PluginClassLoader(Menus.getPlugInsPath());
94  try {
95  Class c = loader.loadClass(plugin);
96  Method m = c.getDeclaredMethod("showAbout", new Class[0]);
97  if (m!=null)
98  hasShowAboutMethod = true;
99  }
100  catch (Exception e) {}
101  //IJ.write("installAbout: "+plugin+" "+hasShowAboutMethod);
102  if (hasShowAboutMethod)
103  Menus.installPlugin(plugin+"(\"about\")",Menus.ABOUT_MENU,plugin+"...","",IJ.getInstance());
104  }
105 
106  String[] getPlugins() {
107  String path = Menus.getPlugInsPath();
108  if (path==null)
109  return null;
110  File f = new File(path);
111  String[] list = f.list();
112  if (list==null) return null;
113  Vector v = new Vector();
114  for (int i=0; i<list.length; i++) {
115  String className = list[i];
116  boolean isClassFile = className.endsWith(".class");
117  if (className.indexOf('_')>=0 && isClassFile && className.indexOf('$')<0 ) {
118  className = className.substring(0, className.length()-6);
119  v.addElement(className);
120  } else {
121  if (!isClassFile)
122  getSubdirectoryPlugins(path, className, v);
123  }
124  }
125  list = new String[v.size()];
126  v.copyInto((String[])list);
127  StringSorter.sort(list);
128  return list;
129  }
130 
132  void getSubdirectoryPlugins(String path, String dir, Vector v) {
133  //IJ.write("getSubdirectoryPlugins: "+path+dir);
134  if (dir.endsWith(".java"))
135  return;
136  File f = new File(path, dir);
137  if (!f.isDirectory())
138  return;
139  String[] list = f.list();
140  if (list==null)
141  return;
142  dir += "/";
143  for (int i=0; i<list.length; i++) {
144  String name = list[i];
145  if (name.indexOf('_')>=0 && name.endsWith(".class") && name.indexOf('$')<0 ) {
146  name = name.substring(0, name.length()-6); // remove ".class"
147  v.addElement(name);
148  //IJ.write("File: "+f+"/"+name);
149  }
150  }
151  }
152 
153 }