private String getToolName(byte[] data) { String text = new String(data); String name = null; Tokenizer tok = new Tokenizer(); Program pgm = tok.tokenize(text); int[] code = pgm.getCode(); Symbol[] symbolTable = pgm.getSymbolTable(); for (int i = 0; i < code.length; i++) { int token = code[i] & MacroConstants.TOK_MASK; if (token == MacroConstants.MACRO) { int nextToken = code[i + 1] & MacroConstants.TOK_MASK; if (nextToken == MacroConstants.STRING_CONSTANT) { int address = code[i + 1] >> MacroConstants.TOK_SHIFT; Symbol symbol = symbolTable[address]; name = symbol.str; break; } } } if (name == null) return null; int index = name.indexOf("Tool"); if (index == -1) return null; name = name.substring(0, index + 4); name = name.replaceAll(" ", "_"); name = name + ".ijm"; return name; }
void installPopupMenu(String name, Program pgm) { Hashtable h = pgm.getMenus(); if (h == null) return; String[] commands = (String[]) h.get(name); if (commands == null) return; PopupMenu popup = Menus.getPopupMenu(); if (popup == null) return; popup.removeAll(); for (int i = 0; i < commands.length; i++) { if (commands[i].equals("-")) popup.addSeparator(); else { MenuItem mi = new MenuItem(commands[i]); mi.addActionListener(this); popup.add(mi); } } }
void install() { subMenus.clear(); if (text != null) { Tokenizer tok = new Tokenizer(); pgm = tok.tokenize(text); } if (macrosMenu != null) IJ.showStatus(""); int[] code = pgm.getCode(); Symbol[] symbolTable = pgm.getSymbolTable(); int count = 0, token, nextToken, address; String name; Symbol symbol; shortcutsInUse = null; inUseCount = 0; nShortcuts = 0; toolCount = 0; macroStarts = new int[MAX_MACROS]; macroNames = new String[MAX_MACROS]; boolean isPluginsMacrosMenu = false; if (macrosMenu != null) { int itemCount = macrosMenu.getItemCount(); isPluginsMacrosMenu = macrosMenu == Menus.getMacrosMenu(); int baseCount = isPluginsMacrosMenu ? MACROS_MENU_COMMANDS : Editor.MACROS_MENU_ITEMS; if (itemCount > baseCount) { for (int i = itemCount - 1; i >= baseCount; i--) macrosMenu.remove(i); } } if (pgm.hasVars() && pgm.macroCount() > 0 && pgm.getGlobals() == null) new Interpreter().saveGlobals(pgm); ArrayList tools = new ArrayList(); for (int i = 0; i < code.length; i++) { token = code[i] & TOK_MASK; if (token == MACRO) { nextToken = code[i + 1] & TOK_MASK; if (nextToken == STRING_CONSTANT) { if (count == MAX_MACROS) { if (isPluginsMacrosMenu) IJ.error("Macro Installer", "Macro sets are limited to " + MAX_MACROS + " macros."); break; } address = code[i + 1] >> TOK_SHIFT; symbol = symbolTable[address]; name = symbol.str; macroStarts[count] = i + 2; macroNames[count] = name; if (name.indexOf('-') != -1 && (name.indexOf("Tool") != -1 || name.indexOf("tool") != -1)) { tools.add(name); toolCount++; } else if (name.startsWith("AutoRun")) { if (autoRunCount == 0 && !openingStartupMacrosInEditor) { new MacroRunner(pgm, macroStarts[count], name, (String) null); if (name.equals("AutoRunAndHide")) autoRunAndHideCount++; } autoRunCount++; count--; } else if (name.equals("Popup Menu")) installPopupMenu(name, pgm); else if (!name.endsWith("Tool Selected")) { if (macrosMenu != null) { addShortcut(name); int pos = name.indexOf(">"); boolean inSubMenu = name.startsWith("<") && (pos > 1); if (inSubMenu) { Menu parent = macrosMenu; Menu subMenu = null; String parentStr = name.substring(1, pos).trim(); String childStr = name.substring(pos + 1).trim(); MenuItem mnuItem = new MenuItem(); mnuItem.setActionCommand(name); mnuItem.setLabel(childStr); for (int jj = 0; jj < subMenus.size(); jj++) { String aName = subMenus.get(jj).getName(); if (aName.equals(parentStr)) subMenu = subMenus.get(jj); } if (subMenu == null) { subMenu = new Menu(parentStr); subMenu.setName(parentStr); subMenu.addActionListener(this); subMenus.add(subMenu); parent.add(subMenu); } subMenu.add(mnuItem); } else macrosMenu.add(new MenuItem(name)); } } // IJ.log(count+" "+name+" "+macroStarts[count]); count++; } } else if (token == EOF) break; } nMacros = count; if (toolCount > 0 && (isPluginsMacrosMenu || macrosMenu == null) && installTools) { Toolbar tb = Toolbar.getInstance(); if (toolCount == 1) tb.addMacroTool((String) tools.get(0), this); else { for (int i = 0; i < tools.size(); i++) { String toolName = (String) tools.get(i); if (toolName.startsWith("Abort Macro or Plugin") && toolCount > 6) toolName = "Unused " + toolName; tb.addMacroTool(toolName, this, i); } } if (toolCount > 1 && Toolbar.getToolId() >= Toolbar.CUSTOM1) tb.setTool(Toolbar.RECTANGLE); tb.repaint(); } if (macrosMenu != null) this.instance = this; if (shortcutsInUse != null && text != null) IJ.showMessage( "Install Macros", (inUseCount == 1 ? "This keyboard shortcut is" : "These keyboard shortcuts are") + " already in use:" + shortcutsInUse); if (nMacros == 0 && fileName != null) { if (text == null || text.length() == 0) return; int dotIndex = fileName.lastIndexOf('.'); if (dotIndex > 0) anonymousName = fileName.substring(0, dotIndex); else anonymousName = fileName; if (macrosMenu != null) macrosMenu.add(new MenuItem(anonymousName)); macroNames[0] = anonymousName; nMacros = 1; } String word = nMacros == 1 ? " macro" : " macros"; if (isPluginsMacrosMenu) IJ.showStatus(nMacros + word + " installed"); }