示例#1
0
  private ModuleInfo createInfo(final String key, final URL url) {
    // set menu path
    final String[] subPaths = key.split("/");
    final MenuPath menuPath = new MenuPath();
    menuPath.add(new MenuEntry(MenuConstants.IMAGE_LABEL));
    menuPath.add(new MenuEntry("Lookup Tables"));
    for (int i = 0; i < subPaths.length - 1; i++) {
      menuPath.add(new MenuEntry(subPaths[i]));
    }
    final MenuEntry leaf = new MenuEntry(tableName(subPaths[subPaths.length - 1]));
    leaf.setWeight(50); // set menu position: TODO - do this properly
    menuPath.add(leaf);

    // hard code path to open as a preset
    final HashMap<String, Object> presets = new HashMap<String, Object>();
    presets.put("tableURL", url);
    // and create the command info
    // CTR FIXME: Avoid circular dependency between ij-data and ij-commands.
    final CommandInfo info = new CommandInfo("imagej.plugins.commands.misc.ApplyLookupTable");
    info.setPresets(presets);
    info.setMenuPath(menuPath);
    // use the default icon
    // info.setIconPath(iconPath);

    return info;
  }
 @Override
 public ModuleInfo getModuleForAccelerator(final Accelerator acc) {
   for (final ModuleInfo info : getModules()) {
     final MenuPath menuPath = info.getMenuPath();
     if (menuPath == null || menuPath.isEmpty()) continue;
     if (acc.equals(menuPath.getLeaf().getAccelerator())) return info;
   }
   return null;
 }
示例#3
0
  private PluginInfo<Command> createEntry(
      final Object key, final Hashtable<?, ?> commands, final Map<String, MenuPath> menuTable) {
    final String ij1PluginString = commands.get(key).toString();

    // NB: Check whether legacy command is on the blacklist.
    final boolean blacklisted = blacklist.contains(ij1PluginString);

    // NB: Check whether menu path is already taken by a modern ImageJ command.
    // This allows transparent override of legacy commands.
    final MenuPath menuPath = menuTable.get(key);
    final boolean overridden = menuPath != null && appMenu.getMenu(menuPath) != null;

    if (log.isDebug()) {
      // output discovery info for this legacy command
      final String status;
      if (blacklisted && overridden) status = "[BLACKLISTED, OVERRIDDEN] ";
      else if (blacklisted) status = "[BLACKLISTED] ";
      else if (overridden) status = "[OVERRIDDEN] ";
      else status = "";
      log.debug(
          "- "
              + status
              + ij1PluginString
              + " [menu = "
              + (menuPath == null
                  ? ""
                  : menuPath.getMenuString() + ", weight = " + menuPath.getLeaf().getWeight())
              + "]");
    }
    if (blacklisted && overridden) {
      log.warn("Overridden plugin " + ij1PluginString + " is blacklisted");
    }

    if (blacklisted || overridden) return null;

    final String className = parsePluginClass(ij1PluginString);
    final String arg = parseArg(ij1PluginString);

    final Map<String, Object> presets = new HashMap<String, Object>();
    presets.put("className", className);
    presets.put("arg", arg);
    final CommandInfo ci = new CommandInfo(LegacyCommand.class);
    // HACK: Make LegacyCommands a subtype of regular Commands.
    ci.setPluginType(legacyCommandClass());
    if (menuPath != null) ci.setMenuPath(menuPath);
    ci.setPresets(presets);

    // flag legacy command with special icon
    if (menuPath != null) menuPath.getLeaf().setIconPath(LEGACY_PLUGIN_ICON);

    return ci;
  }
示例#4
0
  private ShadowMenu addChild(final ModuleInfo info, final int depth) {
    final MenuPath menuPath = info.getMenuPath();
    final MenuEntry entry = menuPath.get(depth);
    final boolean leaf = isLeaf(depth, menuPath);

    // retrieve existing child
    final ShadowMenu existingChild = children.get(entry.getName());

    final ShadowMenu child;
    if (existingChild == null) {
      // create new child and add to table
      final String menuName = entry.getName();
      final ShadowMenu newChild = new ShadowMenu(getContext(), info, depth, this);
      children.put(menuName, newChild);
      child = newChild;
    } else {
      // fill in any missing menu properties of existing child
      final MenuEntry childMenuEntry = existingChild.getMenuEntry();
      childMenuEntry.assignProperties(entry);
      child = existingChild;
    }

    // recursively add remaining child menus
    if (!leaf) child.addChild(info, depth + 1);
    else if (existingChild != null) {
      if (log != null) {
        final ModuleInfo childInfo = existingChild.getModuleInfo();
        if (childInfo != null && info.getPriority() == childInfo.getPriority()) {
          log.warn(
              "ShadowMenu: menu item already exists:\n"
                  + //
                  "\texisting: "
                  + details(childInfo)
                  + "\n"
                  + //
                  "\t ignored: "
                  + details(info));
        } else {
          log.debug(
              "ShadowMenu: higher-priority menu item already exists:\n"
                  + "\texisting: "
                  + details(childInfo)
                  + "\n"
                  + //
                  "\t ignored: "
                  + details(info));
        }
      }
    }
    return child;
  }
示例#5
0
 private ShadowMenu(
     final Context context,
     final ModuleInfo moduleInfo,
     final int menuDepth,
     final ShadowMenu parent) {
   setContext(context);
   if (moduleInfo == null) {
     this.moduleInfo = null;
     menuEntry = null;
   } else {
     final MenuPath menuPath = moduleInfo.getMenuPath();
     // preserve moduleInfo reference only for leaf items
     final boolean leaf = menuDepth == menuPath.size() - 1;
     this.moduleInfo = leaf ? moduleInfo : null;
     menuEntry = menuPath.get(menuDepth);
   }
   this.menuDepth = menuDepth;
   this.parent = parent;
   children = new HashMap<>();
 }
示例#6
0
  private ShadowMenu getMenu(final MenuPath menuPath, final int index) {
    final MenuEntry entry = menuPath.get(index);

    // search for a child with matching menu entry
    for (final ShadowMenu child : children.values()) {
      if (entry.getName().equals(child.getMenuEntry().getName())) {
        // found matching child
        if (isLeaf(index, menuPath)) {
          // return child directly
          return child;
        }
        // recurse downward
        return child.getMenu(menuPath, index + 1);
      }
    }
    return null;
  }
示例#7
0
  private void parseMenu(
      final MenuItem menuItem,
      final double weight,
      final MenuPath path,
      final Map<String, MenuPath> menuTable) {
    // build menu entry
    final String name = menuItem.getLabel();
    final MenuEntry entry = new MenuEntry(name, weight);
    final MenuShortcut shortcut = menuItem.getShortcut();
    if (shortcut != null) {
      // convert AWT MenuShortcut to ImageJ Accelerator
      final int code = shortcut.getKey();
      final boolean meta = Accelerator.isCtrlReplacedWithMeta();
      final boolean ctrl = !meta;
      final boolean shift = shortcut.usesShiftModifier();
      final KeyCode keyCode = KeyCode.get(code);
      final InputModifiers modifiers =
          new InputModifiers(false, false, ctrl, meta, shift, false, false, false);
      final Accelerator acc = new Accelerator(keyCode, modifiers);
      entry.setAccelerator(acc);
    }
    path.add(entry);

    if (menuItem instanceof Menu) { // non-leaf
      // recursively process child menu items
      final Menu menu = (Menu) menuItem;
      final int itemCount = menu.getItemCount();
      double w = -1;
      for (int i = 0; i < itemCount; i++) {
        final MenuItem item = menu.getItem(i);
        final boolean isSeparator = item.getLabel().equals("-");
        if (isSeparator) w += 10;
        else w += 1;
        parseMenu(item, w, new MenuPath(path), menuTable);
      }
    } else { // leaf item
      // add menu item to table
      menuTable.put(menuItem.getLabel(), path);
    }
  }
示例#8
0
 private boolean isLeaf(final int depth, final MenuPath path) {
   return depth == path.size() - 1;
 }