Пример #1
0
  /*
   * Get the item with the given name either from the menuBar, or if
   * that is null, from the menu.
   */
  protected static MenuItem getMenuItem(
      MenuBar menuBar, Menu menu, String name, boolean createIfNecessary) {
    if (menuBar == null && menu == null) return null;
    if (menuBar != null && name.equals("Help")) {
      menu = menuBar.getHelpMenu();
      if (menu == null && createIfNecessary) {
        menu = new PopupMenu("Help");
        menuBar.setHelpMenu(menu);
      }
      return menu;
    }

    int count = menuBar != null ? menuBar.getMenuCount() : menu.getItemCount();
    for (int i = 0; i < count; i++) {
      MenuItem current = menuBar != null ? menuBar.getMenu(i) : menu.getItem(i);
      if (name.equals(current.getLabel())) return current;
    }

    if (createIfNecessary) {
      Menu newMenu = new PopupMenu(name);
      if (menuBar != null) menuBar.add(newMenu);
      else menu.add(newMenu);
      return newMenu;
    } else return null;
  }
Пример #2
0
 @SuppressWarnings("deprecation")
 public void addItem(MenuItem item) {
   // the current awt way of adding a separator appears to be adding an item with "-" as the label
   if ("-".equals(item.getLabel())) {
     addSeparator();
   } else {
     item.addNotify();
     jComponent.add(((SwingBaseMenuItemPeer<?, ?>) item.getPeer()).jComponent);
   }
 }
Пример #3
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);
    }
  }
Пример #4
0
  /** Install the plugins now */
  public void run(String arg) {
    if ("update".equals(arg)) {
      Menus.updateImageJMenus();
      ClassLoader loader = IJ.getClassLoader();
      if (loader != null && (loader instanceof FijiClassLoader)) return;
    }
    FijiClassLoader classLoader = new FijiClassLoader(true);
    try {
      classLoader.addPath(path);
    } catch (IOException e) {
    }

    try {
      // IJ.setClassLoader(classLoader);
      Class ij = Class.forName("ij.IJ");
      java.lang.reflect.Method method =
          ij.getDeclaredMethod("setClassLoader", new Class[] {ClassLoader.class});
      method.setAccessible(true);
      method.invoke(null, new Object[] {classLoader});
    } catch (Exception e) {
      e.printStackTrace();
    }

    installScripts();
    installPlugins(path, ".", menuPath);
    /* make sure "Update Menus" runs _this_ plugin */
    Menus.getCommands().put("Update Menus", "fiji.User_Plugins(\"update\")");
    Menus.getCommands().put("Refresh Menus", "fiji.User_Plugins(\"update\")");
    Menus.getCommands().put("Compile and Run...", "fiji.Compile_and_Run");
    if (IJ.getInstance() != null) {
      Menu help = Menus.getMenuBar().getHelpMenu();
      for (int i = help.getItemCount() - 1; i >= 0; i--) {
        MenuItem item = help.getItem(i);
        String name = item.getLabel();
        if (name.equals("Update Menus")) item.setLabel("Refresh Menus");
      }
    }

    // make sure "Edit>Options>Memory & Threads runs Fiji's plugin
    Menus.getCommands().put("Memory & Threads...", "fiji.Memory");

    SampleImageLoader.install();
    Main.installRecentCommands();
  }
Пример #5
0
  public boolean action(Event event, Object what) {
    if (event.target instanceof Checkbox) {
      CheckboxGroup cbgrp = ((Checkbox) (event.target)).getCheckboxGroup();
      Checkbox selected = cbgrp.getCurrent();
      String label = selected.getLabel();

      int mode = GraphCanvas.CREATE_NODES;
      if (label.equals("Create Edges")) mode = GraphCanvas.CREATE_EDGES;
      else if (label.equals("Select Nodes")) mode = GraphCanvas.SELECT_NODES;
      else if (label.equals("Select Edges")) mode = GraphCanvas.SELECT_EDGES;
      else if (label.equals("Select Nodes or Edges")) mode = GraphCanvas.SELECT_BOTH;

      graphCanvas_.setMouseMode(mode);
    } else if (event.target instanceof CheckboxMenuItem) {
      String label = ((CheckboxMenuItem) (event.target)).getLabel();
      boolean state = ((CheckboxMenuItem) (event.target)).getState();

      if (label.equals("Scale Node Size")) graphCanvas_.scaleBounds(state);
      else if (label.equals("Show Controls  [s]")) {
        if (state) controls_.show();
        else controls_.hide();
        validate();
      } else if (label.equals("Directed")) graphCanvas_.setDirected(state);
      else if (label.equals("Use Node ID As Default Label")) {
        Node.setDefaultLabel(state);
        graphCanvas_.update(false);
      } else if (label.equals("High Quality Display"))
        graphCanvas_.setQuality(state == false ? 1 : 2);
    } else if (event.target instanceof MenuItem) {
      GraphAlgorithm alg;

      MenuItem menuitem = (MenuItem) event.target;

      alg = (GraphAlgorithm) algHashTable_.get(menuitem);
      if (alg != null) {
        applyAlgorithm_(alg);
      } else {
        String label = menuitem.getLabel();

        if (label.equals("Open Example Graph")) {
          URL src = null;
          try {
            src = new URL(context_, "example.gml");
            InputStream strm = src.openStream();
            GMLlexer lexer = new GMLlexer(strm);
            GMLfile_ = new GMLobject(lexer, null);
            GMLobject gmlgraph = GMLfile_.getGMLSubObject("graph", GMLobject.GMLlist, false);

            Graph newgraph = null;
            newgraph = new Graph(gmlgraph);
            graph_.copy(newgraph);
            graphCanvas_.update(true);

            filename_ = null;
            setTitle_();
          } catch (Exception e) {
            new MessageDialog(this, "Error", "Error loading example graph.", true);
          }
          ;

        } else if (label.equals("Open (GML)")) {
          String filename = null;

          FileDialog fd;
          try {
            fd = new FileDialog(this, "Open VGJ File (GML)", FileDialog.LOAD);
            fd.show();
          } catch (Throwable e) {
            MessageDialog dg =
                new MessageDialog(
                    this, "Error", "It appears your VM does not allow file loading.", true);
            return true;
          }
          filename = fd.getFile();
          if (filename == null) return true;
          filename = fd.getDirectory() + filename;

          loadFile(filename);
          return true;
        } else if (label.equals("Save (GML)") || label.equals("Save As (GML)")) {
          try {
            String filename;
            if (label.equals("Save As (GML)") || filename_ == null) {
              FileDialog fd;
              try {
                fd = new FileDialog(this, "Save VGJ File (GML)", FileDialog.SAVE);
                fd.show();
              } catch (Throwable e) {
                MessageDialog dg =
                    new MessageDialog(
                        this, "Error", "It appears your VM does not allow file saving.", true);
                return true;
              }

              filename = fd.getFile();
              if (filename == null) return true;

              // Work around JDK Windows bug.
              if (filename.endsWith(".*.*")) {
                String tmpstr = filename.substring(0, filename.length() - 4);
                filename = tmpstr;
              }
              filename = fd.getDirectory() + filename;
            } else filename = filename_;

            PrintStream ps = new PrintStream(new FileOutputStream(filename));
            if (GMLfile_ == null) {
              GMLfile_ = new GMLobject(null, GMLobject.GMLfile);
              GMLfile_.addObjectToEnd(new GMLobject("graph", GMLobject.GMLlist));
            }
            GMLobject gmlgraph = GMLfile_.getGMLSubObject("graph", GMLobject.GMLlist, false);
            graph_.setGMLvalues(gmlgraph);
            gmlgraph.prune();

            ps.println(GMLfile_.toString(0));
            ps.close();

            filename_ = filename;
            setTitle_();
          } catch (IOException e) {
            MessageDialog dg = new MessageDialog(this, "Error", e.getMessage(), true);
          }

        } else if (label.equals("Exit This Window")) Destroy();
        else if (label.startsWith("Exit Application")) System.exit(0);
        else if (label.equals("Delete Selected Items")) graphCanvas_.deleteSelected(true);
        else if (label.equals("Select All")) graphCanvas_.selectAll();
        else if (label.equals("Remove All Edge Bends")) graphCanvas_.removeEdgeBends();
        else if (label.equals("Remove All Groups")) graphCanvas_.removeGroups();
        else if (label.equals("Group Control")) {
          if (groupControl_ == null) groupControl_ = new GroupControl(this, graphCanvas_);
          else groupControl_.showMe();
        } else if (label.equals("Set New Node Properties")) graphCanvas_.setNodeProperties(true);
        else if (label.equals("Set Node Spacing")) {
          if (algPropDialog_ == null) algPropDialog_ = new AlgPropDialog(this, graphCanvas_);
          else algPropDialog_.showMe();
        } else if (label.equals("Set Font")) {
          if (fontPropDialog_ == null) fontPropDialog_ = new FontPropDialog(this, graphCanvas_);
          else fontPropDialog_.showMe();
        } else if (label.equals("Edit Text Representation (GML)")) {
          graphCanvas_.unselectItems();
          GraphEdit ge = new GraphEdit(graph_, graphCanvas_);
          ge.pack();
          ge.show();
        } else if (label.equals("PostScript Output")) {
          if (psDialog_ == null) psDialog_ = new PSdialog(this, graphCanvas_);
          else {
            psDialog_.pack();
            psDialog_.show();
          }
        }
      }
    } else if (event.target instanceof Button) {
      if (((String) what).equals("Scale / 2") // mod
          || ((String) what).equals("Zoom Out") // mod
      ) // mod
      {
        // scale_ /= 2.0;        // mod
        scale_ /= 1.2;
        graphCanvas_.setScale(scale_);

        scaleLabel_.setText("Scale: " + scale_);
      } else if (((String) what).equals("Scale * 2") // mod
          || ((String) what).equals("Zoom In") // mod
      ) // mod
      {
        // scale_ *= 2.0;       // mod
        scale_ *= 1.2;
        graphCanvas_.setScale(scale_);

        scaleLabel_.setText("Scale: " + scale_);
      } else if (((String) what).equals("Scale = 1")) {
        scale_ = 1.0;
        graphCanvas_.setScale(scale_);

        scaleLabel_.setText("Scale: " + scale_);
      } else if (((String) what).equals("Center")) {
        DDimension port_dim = viewingPanel_.getPortSize();
        DDimension cont_dim = viewingPanel_.getContentSize();

        double x = (cont_dim.width - port_dim.width) / 2.0;
        double y = (cont_dim.height - port_dim.height) / 2.0;

        viewingPanel_.scrollTo((int) x, (int) y);
        portScroller_.setOffset(x, y);
      } else if (((String) what).equals("Update")) {
        System.out.println("Update button pressed. " + myAction);
        if (myAction != null) {
          System.out.println("Performing action.");
          myAction.execute();
        }
      }
    }
    return true;
  }