Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
Hotkeys.java
1 package ij.plugin;
2 import ij.*;
3 import ij.gui.*;
4 import ij.util.*;
5 import java.awt.*;
6 import java.io.*;
7 import java.util.*;
8 
10 public class Hotkeys implements PlugIn {
11 
12  private static final String TITLE = "Hotkeys";
13  private static String command = "";
14  private static String shortcut = "";
15 
16  public void run(String arg) {
17  if (arg.equals("install"))
18  installHotkey();
19  else if (arg.equals("remove"))
20  removeHotkey();
21  else {
22  Executer e = new Executer(arg);
23  e.run();
24  }
25  IJ.register(Hotkeys.class);
26  }
27 
28  void installHotkey() {
29  String[] commands = getAllCommands();
30  GenericDialog gd = new GenericDialog("Create Shortcut");
31  gd.addChoice("Command:", commands, command);
32  gd.addStringField("Shortcut:", shortcut, 3);
33  gd.showDialog();
34  if (gd.wasCanceled())
35  return;
36  command = gd.getNextChoice();
37  shortcut = gd.getNextString();
38  if (shortcut.equals("")) {
39  IJ.showMessage(TITLE, "Shortcut required");
40  return;
41  }
42  if (shortcut.length()>1)
43  shortcut = shortcut.replace('f','F');
44  String plugin = "ij.plugin.Hotkeys("+"\""+command+"\")";
45  int err = Menus.installPlugin(plugin,Menus.SHORTCUTS_MENU,"*"+command,shortcut,IJ.getInstance());
46  switch (err) {
47  case Menus.INVALID_SHORTCUT:
48  IJ.showMessage(TITLE, "The shortcut must be a single character or F1-F12.");
49  break;
50  case Menus.SHORTCUT_IN_USE:
51  IJ.showMessage("The \""+shortcut+"\" shortcut is already being used.");
52  break;
53  default:
54  shortcut = "";
55  break;
56  }
57  }
58 
59  void removeHotkey() {
60  String[] commands = getInstalledCommands();
61  if (commands==null) {
62  IJ.showMessage("Remove...", "No installed commands found.");
63  return;
64  }
65  GenericDialog gd = new GenericDialog("Remove");
66  gd.addChoice("Command:", commands, "");
67  gd.addMessage("The command is not removed\nuntil ImageJ is restarted.");
68  gd.showDialog();
69  if (gd.wasCanceled())
70  return;
71  command = gd.getNextChoice();
72  int err = Menus.uninstallPlugin(command);
73  boolean removed = true;
74  if(err==Menus.COMMAND_NOT_FOUND)
75  removed = deletePlugin(command);
76  if (removed) {
77  IJ.showStatus("\""+command + "\" removed; ImageJ restart required");
78  } else
79  IJ.showStatus("\""+command + "\" not removed");
80  }
81 
82  boolean deletePlugin(String command) {
83  String plugin = (String)Menus.getCommands().get(command);
84  String name = plugin+".class";
85  File file = new File(Menus.getPlugInsPath(), name);
86  if (file==null || !file.exists())
87  return false;
88  else
89  return IJ.showMessageWithCancel("Delete Plugin?", "Permanently delete \""+name+"\"?");
90  }
91 
92  String[] getAllCommands() {
93  Vector v = new Vector();
94  for (Enumeration en=Menus.getCommands().keys(); en.hasMoreElements();) {
95  String cmd = (String)en.nextElement();
96  if (!cmd.startsWith("*"))
97  v.addElement(cmd);
98  }
99  String[] list = new String[v.size()];
100  v.copyInto((String[])list);
101  StringSorter.sort(list);
102  return list;
103  }
104 
105  String[] getInstalledCommands() {
106  Vector v = new Vector();
107  Hashtable commandTable = Menus.getCommands();
108  for (Enumeration en=commandTable.keys(); en.hasMoreElements();) {
109  String cmd = (String)en.nextElement();
110  if (cmd.startsWith("*"))
111  v.addElement(cmd);
112  else {
113  String plugin = (String)commandTable.get(cmd);
114  if (plugin.indexOf("_")>=0 && !plugin.startsWith("ij."))
115  v.addElement(cmd);
116  }
117  }
118  if (v.size()==0)
119  return null;
120  String[] list = new String[v.size()];
121  v.copyInto((String[])list);
122  StringSorter.sort(list);
123  return list;
124  }
125 
126 }