Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
Menus.java
1 package ij;
2 import ij.process.*;
3 import ij.util.*;
4 import java.awt.*;
5 import java.awt.image.*;
6 import java.awt.event.*;
7 import java.util.*;
8 import java.io.*;
9 import java.applet.Applet;
10 import java.awt.event.*;
11 import java.util.zip.*;
12 import javax.swing.*;
13 
24 public class Menus {
25 
26  public static final char PLUGINS_MENU = 'p';
27  public static final char IMPORT_MENU = 'i';
28  public static final char SAVE_AS_MENU = 's';
29  public static final char SHORTCUTS_MENU = 'h'; // 'h'=hotkey
30  public static final char ABOUT_MENU = 'a';
31  public static final char FILTERS_MENU = 'f';
32  public static final char TOOLS_MENU = 't';
33  public static final char UTILITIES_MENU = 'u';
34 
35  public static final int WINDOW_MENU_ITEMS = 5; // fixed items at top of Window menu
36 
37  public static final int NORMAL_RETURN = 0;
38  public static final int COMMAND_IN_USE = -1;
39  public static final int INVALID_SHORTCUT = -2;
40  public static final int SHORTCUT_IN_USE = -3;
41  public static final int NOT_INSTALLED = -4;
42  public static final int COMMAND_NOT_FOUND = -5;
43 
44  private static JMenuBar mbar;
45  private static JCheckBoxMenuItem gray8Item,gray16Item,gray32Item,
46  color256Item,colorRGBItem;
47  private static JPopupMenu popup;
48 
49  private static ImageJ ij;
50  private static Applet applet;
51  private static Hashtable demoImagesTable = new Hashtable();
52  private static String pluginsPath;
53  private static JMenu pluginsMenu, importMenu, saveAsMenu, shortcutsMenu,
54  aboutMenu, filtersMenu, toolsMenu, utilitiesMenu, optionsMenu;
55  private static Hashtable pluginsTable;
56 
57  static JMenu window;
58  int nPlugins;
59  private static Hashtable shortcuts = new Hashtable();
60  private static Vector pluginsPrefs = new Vector(); // commands saved in IJ_Prefs
61  static int windowMenuItems2; // non-image windows listed in Window menu + separator
62  private static String error;
63  private String jarError;
64  private boolean isJarErrorHeading;
65  private boolean installingJars, duplicateCommand;
66  private static Vector jarFiles; // JAR files in plugins folder with "_" in their name
67  private int importCount, saveAsCount, toolsCount, optionsCount;
68  private static Hashtable menusTable; // Submenus of Plugins menu
69  private int userPluginsIndex; // First user plugin or submenu in Plugins menu
70  private boolean addSorted;
71 
72  Menus(ImageJ ijInstance, JApplet appletInstance) {
73  ij = ijInstance;
74  applet = appletInstance;
75  }
76 
77  String addMenuBar() {
78  error = null;
79  pluginsTable = new Hashtable();
80 
81  JMenu file = new JMenu("File");
82  addItem(file, "Open Local", KeyEvent.VK_O, false);
83  importMenu = addSubMenu(file, "Import");
84  file.addSeparator();
85  //addItem(file, "Save Local", KeyEvent.VK_S, false);
86  addItem(file, "Revert", KeyEvent.VK_R, false);
87 
88  JMenu edit = new JMenu("Edit");
89  addItem(edit, "Undo", KeyEvent.VK_Z, false);
90  edit.addSeparator();
91  addItem(edit, "Cut", KeyEvent.VK_X, false);
92  addItem(edit, "Copy", KeyEvent.VK_C, false);
93  addItem(edit, "Paste", KeyEvent.VK_V, false);
94  edit.addSeparator();
95  addPlugInItem(edit, "Clear", "ij.plugin.filter.Filler(\"clear\")", 0, false);
96  addPlugInItem(edit, "Clear Outside", "ij.plugin.filter.Filler(\"outside\")", 0, false);
97  addPlugInItem(edit, "Fill", "ij.plugin.filter.Filler(\"fill\")", KeyEvent.VK_F, false);
98  addPlugInItem(edit, "Draw", "ij.plugin.filter.Filler(\"draw\")", KeyEvent.VK_D, false);
99  addPlugInItem(edit, "Invert", "ij.plugin.filter.Filters(\"invert\")", KeyEvent.VK_I, true);
100  edit.addSeparator();
101  addSubMenu(edit, "Selection");
102  optionsMenu = addSubMenu(edit, "Options");
103 
104  JMenu image = new JMenu("Image");
105  JMenu imageType = new JMenu("Set Type");
106  gray8Item = addCheckboxItem(imageType, "8-bit Greyscale", "ij.plugin.Converter(\"8-bit\")");
107  gray16Item = addCheckboxItem(imageType, "16-bit Greyscale", "ij.plugin.Converter(\"16-bit\")");
108  gray32Item = addCheckboxItem(imageType, "32-bit Greyscale", "ij.plugin.Converter(\"32-bit\")");
109  color256Item = addCheckboxItem(imageType, "8-bit Colour", "ij.plugin.Converter(\"8-bit Color\")");
110  colorRGBItem = addCheckboxItem(imageType, "RGB Colour", "ij.plugin.Converter(\"RGB Color\")");
111  imageType.add(new JMenuItem("-"));
112  image.add(imageType);
113 
114  image.addSeparator();
115  addSubMenu(image, "Adjust");
116  image.addSeparator();
117  addPlugInItem(image, "Set Size...", "ij.plugin.filter.Resizer", 0, false);
118  addPlugInItem(image, "Scale Size...", "ij.plugin.filter.Scaler", KeyEvent.VK_E, false);
119  addPlugInItem(image, "Resize Canvas", "ij.plugin.CanvasResizer", 0, false);
120  addPlugInItem(image, "Crop", "ij.plugin.filter.Resizer(\"crop\")", 0, false);
121  addSubMenu(image, "Rotate");
122 
123  JMenu process = new JMenu("Process");
124  addPlugInItem(process, "Smooth", "ij.plugin.filter.Filters(\"smooth\")", KeyEvent.VK_S, true);
125  addPlugInItem(process, "Sharpen", "ij.plugin.filter.Filters(\"sharpen\")", 0, false);
126  addPlugInItem(process, "Find Edges", "ij.plugin.filter.Filters(\"edge\")", KeyEvent.VK_F, true);
127  addPlugInItem(process, "Enhance Contrast", "ij.plugin.ContrastEnhancer", 0, false);
128  addSubMenu(process, "Noise");
129  addSubMenu(process, "Shadows");
130  addSubMenu(process, "Binary");
131  addSubMenu(process, "Math");
132  process.addSeparator();
133  addPlugInItem(process, "Subtract Background...", "ij.plugin.filter.BackgroundSubtracter", 0, false);
134  addItem(process, "Repeat Command", KeyEvent.VK_R, true);
135  JMenu help = new JMenu("Help");
136  addPlugInItem(help, "About Image Editor", "ij.plugin.AboutBox", 0, false);
137 
138  mbar = new JMenuBar();
139  mbar.setBorder(BorderFactory.createMatteBorder(1, 1, 0, 1, Color.BLACK));
140  mbar.add(file);
141  mbar.add(edit);
142  mbar.add(image);
143  mbar.add(process);
144  mbar.add(help);
145  if (ij!=null)
146  ij.setJMenuBar(mbar);
147 
148  if (jarError!=null)
149  error = error!=null?error+="\n"+jarError:jarError;
150  return error;
151  }
152 
153  // TB Removed keyboard shortcut support when moving to Swing
154  void addItem(JMenu menu, String label, int shortcut, boolean shift) {
155  if (menu==null)
156  return;
157  JMenuItem item;
158  item = new JMenuItem(label);
159  if (addSorted && menu==pluginsMenu)
160  addItemSorted(menu, item, userPluginsIndex);
161  else
162  menu.add(item);
163  item.addActionListener(ij);
164  }
165 
166  void addPlugInItem(JMenu menu, String label, String className, int shortcut, boolean shift) {
167  pluginsTable.put(label, className);
168  nPlugins++;
169  addItem(menu, label, shortcut, shift);
170  }
171 
172  JCheckBoxMenuItem addCheckboxItem(JMenu menu, String label, String className) {
173  pluginsTable.put(label, className);
174  nPlugins++;
175  JCheckBoxMenuItem item = new JCheckBoxMenuItem(label);
176  menu.add(item);
177  item.addItemListener(ij);
178  item.setState(false);
179  return item;
180  }
181 
182  JMenu addSubMenu(JMenu menu, String name)
183  {
184  String value;
185  String key = name.toLowerCase(Locale.US);
186  int index;
187  JMenu submenu=new JMenu(name.replace('_', ' '));
188 
189  index = key.indexOf(' ');
190  if (index > 0) {
191  key = key.substring(0, index);
192  }
193  for (int count=1; count<100; count++) {
194  value = Prefs.getString(key + (count/10)%10 + count%10);
195  if (value==null)
196  break;
197  if (count==1)
198  menu.add(submenu);
199  if (value.equals("-"))
200  submenu.addSeparator();
201  else
202  addPluginItem(submenu, value);
203  }
204  return submenu;
205 
206  }//end addSubMenu()
207 
208  void addPluginItem(JMenu submenu, String s) {
209  if (s.equals("\"-\"")) {
210  // add menu separator if command="-"
211  addSeparator(submenu);
212  return;
213  }
214  int lastComma = s.lastIndexOf(',');
215  if (lastComma<=0)
216  return;
217  String command = s.substring(1,lastComma-1);
218  int keyCode = 0;
219  boolean shift = false;
220  if (command.endsWith("]")) {
221  int openBracket = command.lastIndexOf('[');
222  if (openBracket>0) {
223  String shortcut = command.substring(openBracket+1,command.length()-1);
224  keyCode = convertShortcutToCode(shortcut);
225  boolean functionKey = keyCode>=KeyEvent.VK_F1 && keyCode<=KeyEvent.VK_F12;
226  if (keyCode>0 && !functionKey)
227  command = command.substring(0,openBracket);
228  //IJ.write(command+": "+shortcut);
229  }
230  }
231  if (keyCode>=KeyEvent.VK_F1 && keyCode<=KeyEvent.VK_F12) {
232  shortcuts.put(new Integer(keyCode),command);
233  keyCode = 0;
234  } else if (keyCode>200) {
235  keyCode -= 200;
236  shift = true;
237  }
238  addItem(submenu,command,keyCode,shift);
239  while(s.charAt(lastComma+1)==' ' && lastComma+2<s.length())
240  lastComma++; // remove leading spaces
241  String className = s.substring(lastComma+1,s.length());
242  //IJ.log(command+" "+className);
243  if (installingJars)
244  duplicateCommand = pluginsTable.get(command)!=null;
245  pluginsTable.put(command, className);
246  nPlugins++;
247  }
248 
249  void checkForDuplicate(String command) {
250  if (pluginsTable.get(command)!=null) {
251  }
252  }
253 
254  void addPluginsMenu() {
255  String value,label,className;
256  int index;
257  pluginsMenu = new JMenu("Plugins");
258  for (int count=1; count<100; count++) {
259  value = Prefs.getString("plug-in" + (count/10)%10 + count%10);
260  if (value==null)
261  break;
262  char firstChar = value.charAt(0);
263  if (firstChar=='-')
264  pluginsMenu.addSeparator();
265  else if (firstChar=='>') {
266  String submenu = value.substring(2,value.length()-1);
267  JMenu menu = addSubMenu(pluginsMenu, submenu);
268  if (submenu.equals("Shortcuts"))
269  shortcutsMenu = menu;
270  else if (submenu.equals("Utilities"))
271  utilitiesMenu = menu;
272  } else
273  addPluginItem(pluginsMenu, value);
274  }
275  userPluginsIndex = pluginsMenu.getItemCount();
276  if (userPluginsIndex<0) userPluginsIndex = 0;
277  }
278 
282  void installPlugins() {
283  String value, className;
284  char menuCode;
285  JMenu menu;
286  String[] plugins = getPlugins();
287  String[] plugins2 = null;
288  Hashtable skipList = new Hashtable();
289  for (int index=0; index<100; index++) {
290  value = Prefs.getString("plugin" + (index/10)%10 + index%10);
291  if (value==null)
292  break;
293  menuCode = value.charAt(0);
294  switch (menuCode) {
295  case PLUGINS_MENU: default: menu = pluginsMenu; break;
296  case IMPORT_MENU: menu = importMenu; break;
297  case SAVE_AS_MENU: menu = saveAsMenu; break;
298  case SHORTCUTS_MENU: menu = shortcutsMenu; break;
299  case ABOUT_MENU: menu = aboutMenu; break;
300  case FILTERS_MENU: menu = filtersMenu; break;
301  case TOOLS_MENU: menu = toolsMenu; break;
302  case UTILITIES_MENU: menu = utilitiesMenu; break;
303  }
304  String prefsValue = value;
305  value = value.substring(2,value.length()); //remove menu code and coma
306  className = value.substring(value.lastIndexOf(',')+1,value.length());
307  boolean found = className.startsWith("ij.");
308  if (!found && plugins!=null) { // does this plugin exist?
309  if (plugins2==null)
310  plugins2 = getStrippedPlugins(plugins);
311  for (int i=0; i<plugins2.length; i++) {
312  if (className.startsWith(plugins2[i])) {
313  found = true;
314  break;
315  }
316  }
317  }
318  if (found) {
319  addPluginItem(menu, value);
320  pluginsPrefs.addElement(prefsValue);
321  if (className.endsWith("\")")) { // remove any argument
322  int argStart = className.lastIndexOf("(\"");
323  if (argStart>0)
324  className = className.substring(0, argStart);
325  }
326  skipList.put(className, "");
327  }
328  }
329  if (plugins!=null) {
330  for (int i=0; i<plugins.length; i++) {
331  if (!skipList.containsKey(plugins[i]))
332  installUserPlugin(plugins[i]);
333  }
334  }
335  installJarPlugins();
336  }
337 
338 
340  void installJarPlugins() {
341  if (jarFiles==null)
342  return;
343  installingJars = true;
344  for (int i=0; i<jarFiles.size(); i++) {
345  isJarErrorHeading = false;
346  String jar = (String)jarFiles.elementAt(i);
347  InputStream is = getConfigurationFile(jar);
348  if (is==null) continue;
349  LineNumberReader lnr = new LineNumberReader(new InputStreamReader(is));
350  try {
351  while(true) {
352  String s = lnr.readLine();
353  if (s==null) break;
354  installJarPlugin(jar, s);
355  }
356  }
357  catch (IOException e) {}
358  finally {
359  try {if (lnr!=null) lnr.close();}
360  catch (IOException e) {}
361  }
362  }
363  }
364 
366  void installJarPlugin(String jar, String s) {
367  //IJ.log(s);
368  if (s.length()<3) return;
369  char firstChar = s.charAt(0);
370  if (firstChar=='#') return;
371  addSorted = false;
372  JMenu menu;
373  if (s.startsWith("Plugins>")) {
374  int firstComma = s.indexOf(',');
375  if (firstComma==-1 || firstComma<=8)
376  menu = null;
377  else {
378  String name = s.substring(8, firstComma);
379  menu = getPluginsSubmenu(name);
380  }
381  } else if (firstChar=='"' || s.startsWith("Plugins")) {
382  String name = getSubmenuName(jar);
383  if (name!=null)
384  menu = getPluginsSubmenu(name);
385  else {
386  menu = pluginsMenu;
387  addSorted = true;
388  }
389  } else if (s.startsWith("File>Import")) {
390  menu = importMenu;
391  if (importCount==0) addSeparator(menu);
392  importCount++;
393  } else if (s.startsWith("File>Save")) {
394  menu = saveAsMenu;
395  if (saveAsCount==0) addSeparator(menu);
396  saveAsCount++;
397  } else if (s.startsWith("Analyze>Tools")) {
398  menu = toolsMenu;
399  if (toolsCount==0) addSeparator(menu);
400  toolsCount++;
401  } else if (s.startsWith("Help>About")) {
402  menu = aboutMenu;
403  } else if (s.startsWith("Edit>Options")) {
404  menu = optionsMenu;
405  if (optionsCount==0) addSeparator(menu);
406  optionsCount++;
407  } else {
408  if (jarError==null) jarError = "";
409  addJarErrorHeading(jar);
410  jarError += " Invalid menu: " + s + "\n";
411  return;
412  }
413  int firstQuote = s.indexOf('"');
414  if (firstQuote==-1)
415  return;
416  s = s.substring(firstQuote, s.length()); // remove menu
417  if (menu!=null) {
418  addPluginItem(menu, s);
419  addSorted = false;
420  }
421  if (duplicateCommand) {
422  if (jarError==null) jarError = "";
423  addJarErrorHeading(jar);
424  jarError += " Duplicate command: " + s + "\n";
425  }
426  duplicateCommand = false;
427  }
428 
429  void addJarErrorHeading(String jar) {
430  if (!isJarErrorHeading) {
431  if (!jarError.equals(""))
432  jarError += " \n";
433  jarError += "Plugin configuration error: " + jar + "\n";
434  isJarErrorHeading = true;
435  }
436  }
437 
438  JMenu getPluginsSubmenu(String submenuName) {
439  if (menusTable!=null) {
440  JMenu menu = (JMenu)menusTable.get(submenuName);
441  if (menu!=null)
442  return menu;
443  }
444  JMenu menu = new JMenu(submenuName);
445  //pluginsMenu.add(menu);
446  addItemSorted(pluginsMenu, menu, userPluginsIndex);
447  if (menusTable==null) menusTable = new Hashtable();
448  menusTable.put(submenuName, menu);
449  //IJ.log("getPluginsSubmenu: "+submenuName);
450  return menu;
451  }
452 
453  String getSubmenuName(String jarPath) {
454  //IJ.log("getSubmenuName: "+jarPath);
455  int index = jarPath.lastIndexOf(File.separatorChar);
456  if (index<0) return null;
457  String name = jarPath.substring(0, index);
458  index = name.lastIndexOf(File.separatorChar);
459  if (index<0) return null;
460  name = name.substring(index+1);
461  if (name.equals("plugins")) return null;
462  //IJ.log("getSubmenuName: "+jarPath+" \""+name+"\"");
463  return name;
464  }
465 
466  void addItemSorted(JMenu menu, JMenuItem item, int startingIndex) {
467  String itemLabel = item.getText();
468  int count = menu.getItemCount();
469  boolean inserted = false;
470  for (int i=startingIndex; i<count; i++) {
471  JMenuItem mi = menu.getItem(i);
472  String label = mi.getText();
473  //IJ.log(i+ " "+itemLabel+" "+label + " "+(itemLabel.compareTo(label)));
474  if (itemLabel.compareTo(label)<0) {
475  menu.insert(item, i);
476  inserted = true;
477  break;
478  }
479  }
480  if (!inserted) menu.add(item);
481  }
482 
483  void addSeparator(JMenu menu) {
484  menu.addSeparator();
485  }
486 
488  InputStream getConfigurationFile(String jar) {
489  try {
490  ZipFile jarFile = new ZipFile(jar);
491  Enumeration entries = jarFile.entries();
492  while (entries.hasMoreElements()) {
493  ZipEntry entry = (ZipEntry) entries.nextElement();
494  if (entry.getName().endsWith("plugins.config"))
495  return jarFile.getInputStream(entry);
496  }
497  }
498  catch (Exception e) {}
499  return null;
500  }
501 
503  String[] getStrippedPlugins(String[] plugins) {
504  String[] plugins2 = new String[plugins.length];
505  int slashPos;
506  for (int i=0; i<plugins2.length; i++) {
507  plugins2[i] = plugins[i];
508  slashPos = plugins2[i].lastIndexOf('/');
509  if (slashPos>=0)
510  plugins2[i] = plugins[i].substring(slashPos+1,plugins2[i].length());
511  }
512  return plugins2;
513  }
514 
516  public static synchronized String[] getPlugins() {
517  String homeDir = Prefs.getHomeDir();
518  if (homeDir==null)
519  return null;
520  if (homeDir.endsWith("plugins"))
521  pluginsPath = homeDir;
522  else {
523  String pluginsDir = System.getProperty("plugins.dir");
524  if (pluginsDir==null)
525  pluginsDir = homeDir;
526  else if (pluginsDir.equals("user.home"))
527  pluginsDir = System.getProperty("user.home");
528  pluginsPath = pluginsDir+Prefs.separator+"plugins"+Prefs.separator;
529  }
530  File f = pluginsPath!=null?new File(pluginsPath):null;
531  if (f==null || (f!=null && !f.isDirectory())) {
532  error = "Plugins folder not found at "+pluginsPath;
533  pluginsPath = null;
534  return null;
535  }
536  String[] list = f.list();
537  if (list==null)
538  return null;
539  Vector v = new Vector();
540  jarFiles = null;
541  for (int i=0; i<list.length; i++) {
542  String name = list[i];
543  boolean isClassFile = name.endsWith(".class");
544  boolean hasUnderscore = name.indexOf('_')>=0;
545  if (hasUnderscore && isClassFile && name.indexOf('$')<0 ) {
546  name = name.substring(0, name.length()-6); // remove ".class"
547  v.addElement(name);
548  } else if (hasUnderscore && (name.endsWith(".jar") || name.endsWith(".zip"))) {
549  if (jarFiles==null) jarFiles = new Vector();
550  jarFiles.addElement(pluginsPath + name);
551  } else if (hasUnderscore && (name.endsWith(".txt"))) {
552  } else {
553  if (!isClassFile)
554  checkSubdirectory(pluginsPath, name, v);
555  }
556  }
557  list = new String[v.size()];
558  v.copyInto((String[])list);
559  StringSorter.sort(list);
560  return list;
561  }
562 
564  static void checkSubdirectory(String path, String dir, Vector v) {
565  if (dir.endsWith(".java"))
566  return;
567  File f = new File(path, dir);
568  if (!f.isDirectory())
569  return;
570  String[] list = f.list();
571  if (list==null)
572  return;
573  dir += "/";
574  for (int i=0; i<list.length; i++) {
575  String name = list[i];
576  boolean hasUnderscore = name.indexOf('_')>=0;
577  if (hasUnderscore && name.endsWith(".class") && name.indexOf('$')<0) {
578  name = name.substring(0, name.length()-6); // remove ".class"
579  v.addElement(dir+name);
580  //IJ.write("File: "+f+"/"+name);
581  } else if (hasUnderscore && (name.endsWith(".jar") || name.endsWith(".zip"))) {
582  if (jarFiles==null) jarFiles = new Vector();
583  jarFiles.addElement(f.getPath() + File.separator + name);
584  } else if (hasUnderscore && (name.endsWith(".txt"))) {
585  }
586  }
587  }
588 
589  static String submenuName;
590  static JMenu submenu;
591 
594  void installUserPlugin(String className) {
595  JMenu menu = pluginsMenu;
596  int slashIndex = className.indexOf('/');
597  if (slashIndex>0) {
598  String dir = className.substring(0, slashIndex);
599  className = className.substring(slashIndex+1, className.length());
600  //className = className.replace('/', '.');
601  if (submenu==null || !submenuName.equals(dir)) {
602  submenuName = dir;
603  submenu = new JMenu(submenuName);
604  pluginsMenu.add(submenu);
605  if (menusTable==null) menusTable = new Hashtable();
606  menusTable.put(submenuName, submenu);
607  }
608  menu = submenu;
609  //IJ.write(dir + " " + className);
610  }
611  String command = className.replace('_',' ');
612  command.trim();
613  JMenuItem item = new JMenuItem(command);
614  menu.add(item);
615  item.addActionListener(ij);
616  pluginsTable.put(command, className);
617  nPlugins++;
618  }
619 
620  void installPopupMenu(ImageJ ij) {
621  String s;
622  int count = 0;
623  JMenuItem mi;
624  popup = new JPopupMenu("");
625 
626  while (true) {
627  count++;
628  s = Prefs.getString("popup" + (count/10)%10 + count%10);
629  if (s==null)
630  break;
631  if (s.equals("-"))
632  popup.addSeparator();
633  else if (!s.equals("")) {
634  mi = new JMenuItem(s);
635  mi.addActionListener(ij);
636  popup.add(mi);
637  }
638  }
639  }
640 
641  public static JMenuBar getMenuBar() {
642  return mbar;
643  }
644 
645 
647  public static void updateMenus() {
648 
649  if (ij==null) return;
650  gray8Item.setState(false);
651  gray16Item.setState(false);
652  gray32Item.setState(false);
653  color256Item.setState(false);
654  colorRGBItem.setState(false);
655  ImagePlus imp = IJ.getInstance().getImagePlus();
656  if (imp==null)
657  return;
658  int type = imp.getType();
659  if (type==ImagePlus.GRAY8) {
660  ImageProcessor ip = imp.getProcessor();
661  if (ip!=null && ip.getMinThreshold()==ImageProcessor.NO_THRESHOLD && ip.isColorLut()) {
662  type = ImagePlus.COLOR_256;
663  if (!ip.isPseudoColorLut())
664  imp.setType(ImagePlus.COLOR_256);
665  }
666  }
667  switch (type) {
668  case ImagePlus.GRAY8:
669  gray8Item.setState(true);
670  break;
671  case ImagePlus.GRAY16:
672  gray16Item.setState(true);
673  break;
674  case ImagePlus.GRAY32:
675  gray32Item.setState(true);
676  break;
677  case ImagePlus.COLOR_256:
678  color256Item.setState(true);
679  break;
680  case ImagePlus.COLOR_RGB:
681  colorRGBItem.setState(true);
682  break;
683  }
684 
685  }
686 
687  static boolean isColorLut(ImagePlus imp) {
688  ImageProcessor ip = imp.getProcessor();
689  IndexColorModel cm = (IndexColorModel)ip.getColorModel();
690  if (cm==null) return false;
691  int mapSize = cm.getMapSize();
692  byte[] reds = new byte[mapSize];
693  byte[] greens = new byte[mapSize];
694  byte[] blues = new byte[mapSize];
695  cm.getReds(reds);
696  cm.getGreens(greens);
697  cm.getBlues(blues);
698  boolean isColor = false;
699  for (int i=0; i<mapSize; i++) {
700  if ((reds[i] != greens[i]) || (greens[i] != blues[i])) {
701  isColor = true;
702  break;
703  }
704  }
705  return isColor;
706  }
707 
708 
711  public static String getPlugInsPath() {
712  return pluginsPath;
713  }
714 
715 
717  public static Hashtable getCommands() {
718  return pluginsTable;
719  }
720 
723  public static Hashtable getShortcuts() {
724  return shortcuts;
725  }
726 
727 
728  public static JPopupMenu getPopupMenu() {
729  return popup;
730  }
731 
741  public static int installPlugin(String plugin, char menuCode, String command, String shortcut, ImageJ ij) {
742  if (command.equals("")) { //uninstall
743  //Object o = pluginsPrefs.remove(plugin);
744  //if (o==null)
745  // return NOT_INSTALLED;
746  //else
747  return NORMAL_RETURN;
748  }
749 
750  if (commandInUse(command))
751  return COMMAND_IN_USE;
752  if (!validShortcut(shortcut))
753  return INVALID_SHORTCUT;
754  if (shortcutInUse(shortcut))
755  return SHORTCUT_IN_USE;
756 
757  JMenu menu;
758  switch (menuCode) {
759  case PLUGINS_MENU: menu = pluginsMenu; break;
760  case IMPORT_MENU: menu = importMenu; break;
761  case SAVE_AS_MENU: menu = saveAsMenu; break;
762  case SHORTCUTS_MENU: menu = shortcutsMenu; break;
763  case ABOUT_MENU: menu = aboutMenu; break;
764  case FILTERS_MENU: menu = filtersMenu; break;
765  case TOOLS_MENU: menu = toolsMenu; break;
766  case UTILITIES_MENU: menu = utilitiesMenu; break;
767  default: return 0;
768  }
769  int code = convertShortcutToCode(shortcut);
770  JMenuItem item;
771  boolean functionKey = code>=KeyEvent.VK_F1 && code<=KeyEvent.VK_F12;
772  if (code==0)
773  item = new JMenuItem(command);
774  else if (functionKey) {
775  command += " [F"+(code-KeyEvent.VK_F1+1)+"]";
776  shortcuts.put(new Integer(code),command);
777  item = new JMenuItem(command);
778  }else {
779  shortcuts.put(new Integer(code),command);
780  int keyCode = code;
781  boolean shift = false;
782  if (keyCode>200) {
783  keyCode -= 200;
784  shift = true;
785  }
786  item = new JMenuItem(command);
787  }
788  menu.add(item);
789  item.addActionListener(ij);
790  pluginsTable.put(command, plugin);
791  shortcut = code>0 && !functionKey?"["+shortcut+"]":"";
792  //IJ.write("installPlugin: "+menuCode+",\""+command+shortcut+"\","+plugin);
793  pluginsPrefs.addElement(menuCode+",\""+command+shortcut+"\","+plugin);
794  return NORMAL_RETURN;
795  }
796 
798  public static int uninstallPlugin(String command) {
799  boolean found = false;
800  for (Enumeration en=pluginsPrefs.elements(); en.hasMoreElements();) {
801  String cmd = (String)en.nextElement();
802  if (cmd.indexOf(command)>0) {
803  pluginsPrefs.removeElement((Object)cmd);
804  found = true;
805  break;
806  }
807  }
808  if (found)
809  return NORMAL_RETURN;
810  else
811  return COMMAND_NOT_FOUND;
812 
813  }
814 
815  public static boolean commandInUse(String command) {
816  if (pluginsTable.get(command)!=null)
817  return true;
818  else
819  return false;
820  }
821 
822  public static int convertShortcutToCode(String shortcut) {
823  int code = 0;
824  int len = shortcut.length();
825  if (len==2 && shortcut.startsWith("F")) {
826  code = KeyEvent.VK_F1+(int)shortcut.charAt(1)-49;
827  if (code>=KeyEvent.VK_F1 && code<=KeyEvent.VK_F9)
828  return code;
829  else
830  return 0;
831  }
832  if (len==3 && shortcut.startsWith("F")) {
833  code = KeyEvent.VK_F10+(int)shortcut.charAt(2)-48;
834  if (code>=KeyEvent.VK_F10 && code<=KeyEvent.VK_F12)
835  return code;
836  else
837  return 0;
838  }
839  if (len!=1)
840  return 0;
841  int c = (int)shortcut.charAt(0);
842  if (c>=65&&c<=90) //A-Z
843  code = KeyEvent.VK_A+c-65 + 200;
844  else if (c>=97&&c<=122) //a-z
845  code = KeyEvent.VK_A+c-97;
846  else if (c>=48&&c<=57) //0-9
847  code = KeyEvent.VK_0+c-48;
848  return code;
849  }
850 
851  static boolean validShortcut(String shortcut) {
852  int len = shortcut.length();
853  if (shortcut.equals(""))
854  return true;
855  else if (len==1)
856  return true;
857  else if (shortcut.startsWith("F") && (len==2 || len==3))
858  return true;
859  else
860  return false;
861  }
862 
863  public static boolean shortcutInUse(String shortcut) {
864  int code = convertShortcutToCode(shortcut);
865  if (shortcuts.get(new Integer(code))!=null)
866  return true;
867  else
868  return false;
869  }
870 
872  public static void savePreferences(Properties prefs) {
873  int index = 0;
874  for (Enumeration en=pluginsPrefs.elements(); en.hasMoreElements();) {
875  String key = "plugin" + (index/10)%10 + index%10;
876  prefs.put(key, (String)en.nextElement());
877  index++;
878  }
879  }
880 
881 }