Exemplo n.º 1
0
  /**
   * Installs a Tool in the Toolbar
   *
   * @param toolbar as JToolbar
   * @param tool, Tool to install
   */
  public void installToolInToolBar(JToolBar toolbar, final Tool tool) {
    final JButton button;
    button = new JButton();

    button.setMargin(new Insets(0, 0, 0, 0));

    if (tool.getItemType() != null) {
      button.setIcon(tool.getItemType().getIcon());
      button.setToolTipText(tool.getItemType().getDescription());

    } else {
      button.setText("Tool"); // For Debugging
    }
    toolbar.add(button);
    toolButtons.add(button);
    button.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            WorkingView.this.setTool(tool, button);
          }
        });
    button.setContentAreaFilled(false);
    button.setBorderPainted(false);
    button.addMouseListener(
        new MouseAdapter() {
          public void mouseEntered(MouseEvent e) {
            ((JButton) e.getSource()).setBorderPainted(true);
          }

          public void mouseExited(MouseEvent e) {
            ((JButton) e.getSource()).setBorderPainted(false);
          }
        });
  }
  private void editSelected() {
    CheckedTreeNode node = getSelectedToolNode();
    if (node != null && node.getUserObject() instanceof Tool) {
      Tool selected = (Tool) node.getUserObject();
      if (selected != null) {
        String oldGroupName = selected.getGroup();
        ToolEditorDialog dlg = new ToolEditorDialog(this);
        dlg.setData(selected, getGroups());
        dlg.show();
        if (dlg.isOK()) {
          selected.copyFrom(dlg.getData());
          String newGroupName = selected.getGroup();
          if (!Comparing.equal(oldGroupName, newGroupName)) {
            CheckedTreeNode oldGroupNode = (CheckedTreeNode) node.getParent();
            removeNodeFromParent(node);
            ((ToolsGroup) oldGroupNode.getUserObject()).removeElement(selected);
            if (oldGroupNode.getChildCount() == 0) {
              removeNodeFromParent(oldGroupNode);
            }

            insertNewTool(selected, true);
          } else {
            (getModel()).nodeChanged(node);
          }
          myIsModified = true;
          update();
        }
      }
    }
  }
Exemplo n.º 3
0
  public ToolButton(PaletteListener listener, String iconName, String name, Tool tool) {
    super(listener);
    tool.addToolListener(this);
    setEnabled(tool.isUsable());

    // use a Mediatracker to ensure that all the images are initially loaded
    Iconkit kit = Iconkit.instance();
    if (kit == null) {
      throw new JHotDrawRuntimeException("Iconkit instance isn't set");
    }

    Image im[] = new Image[3];
    im[0] = kit.loadImageResource(iconName + "1.gif");
    im[1] = kit.loadImageResource(iconName + "2.gif");
    im[2] = kit.loadImageResource(iconName + "3.gif");

    MediaTracker tracker = new MediaTracker(this);
    for (int i = 0; i < 3; i++) {
      tracker.addImage(im[i], i);
    }
    try {
      tracker.waitForAll();
    } catch (Exception e) {
      // ignore exception
    }

    fIcon = new PaletteIcon(new Dimension(24, 24), im[0], im[1], im[2]);
    fTool = tool;
    fName = name;

    setIcon(new ImageIcon(im[0]));
    setPressedIcon(new ImageIcon(im[1]));
    setSelectedIcon(new ImageIcon(im[2]));
    setToolTipText(name);
  }
Exemplo n.º 4
0
 private void setTool(Tool t, String name) {
   if (fTool != null) {
     fTool.deactivate();
   }
   fTool = t;
   if (fTool != null) {
     showStatus(name);
     fTool.activate();
   }
 }
Exemplo n.º 5
0
  /**
   * Searches for the tool with the given name.
   *
   * @param aToolName the name of the tool to search for, cannot be <code>null</code>.
   * @return the tool with the given name, can be <code>null</code> if no such tool can be found.
   */
  private Tool findToolByName(final String aToolName) {
    Tool tool = null;

    final Collection<Tool> tools = getTools();
    for (Tool _tool : tools) {
      if (aToolName.equals(_tool.getName())) {
        tool = _tool;
        break;
      }
    }
    return tool;
  }
 private void insertNewTool(final Tool newTool, boolean setSelection) {
   CheckedTreeNode groupNode = findGroupNode(newTool.getGroup());
   if (groupNode == null) {
     groupNode = insertNewGroup(new ToolsGroup(newTool.getGroup()));
     nodeWasInserted(groupNode);
   }
   CheckedTreeNode tool = insertNewTool(groupNode, newTool);
   if (setSelection) {
     TreePath treePath = new TreePath(tool.getPath());
     myTree.expandPath(treePath);
     myTree.getSelectionModel().setSelectionPath(treePath);
   }
   myIsModified = true;
 }
 public void selectTool(final String actionId) {
   Object root = myTree.getModel().getRoot();
   if (root == null || !(root instanceof CheckedTreeNode)) {
     return;
   }
   final List<CheckedTreeNode> nodes = new ArrayList<CheckedTreeNode>();
   new Object() {
     @SuppressWarnings("unchecked")
     public void collect(CheckedTreeNode node) {
       if (node.isLeaf()) {
         Object userObject = node.getUserObject();
         if (userObject instanceof Tool && actionId.equals(((Tool) userObject).getActionId())) {
           nodes.add(node);
         }
       } else {
         for (int i = 0; i < node.getChildCount(); i++) {
           final TreeNode child = node.getChildAt(i);
           if (child instanceof CheckedTreeNode) {
             collect((CheckedTreeNode) child);
           }
         }
       }
     }
   }.collect((CheckedTreeNode) root);
   if (nodes.isEmpty()) {
     return;
   }
   myTree.getSelectionModel().setSelectionPath(new TreePath(nodes.get(0).getPath()));
 }
Exemplo n.º 8
0
  /**
   * Sets the current Tool, the right listeners and the Cursor
   *
   * @param tool, Tool to set as current
   * @param button, JButton to set as current
   */
  public void setTool(Tool tool, JButton button) {
    // IF NO CURSORTOOL
    if (this.currentTool instanceof CursorTool && !(tool instanceof CursorTool)) {
      board.removeMouseListener(listeners[0]);

      // IF CURSORTOOL
    } else if (tool instanceof CursorTool && !(this.currentTool instanceof CursorTool)) {

      board.addMouseListener(listeners[0]);
    }
    if (tool == null) throw new IllegalArgumentException("Tool must not be null.");

    if (this.currentTool != tool) {
      if (this.currentButton != null) {
        this.currentButton.setEnabled(true);
      }
      this.currentButton = button;
      this.currentTool = tool;
    }
    if (tool instanceof CursorTool || tool instanceof ArrowTool || tool instanceof TextBoxTool) {
      board.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
    } else {
      board.setCursor(tool.getItemType().getCursor());
    }
  }
Exemplo n.º 9
0
  /**
   * Runs the tool denoted by the given name.
   *
   * @param aToolName the name of the tool to run, cannot be <code>null</code>;
   * @param aParent the parent window to use, can be <code>null</code>.
   */
  public void runTool(final String aToolName, final Window aParent) {
    if (LOG.isLoggable(Level.INFO)) {
      LOG.log(Level.INFO, "Running tool: \"{0}\" ...", aToolName);
    }

    final Tool tool = findToolByName(aToolName);
    if (tool == null) {
      JOptionPane.showMessageDialog(
          aParent, "No such tool found: " + aToolName, "Error ...", JOptionPane.ERROR_MESSAGE);
    } else {
      final ToolContext context = createToolContext();
      tool.process(aParent, this.dataContainer, context, this);
    }

    updateActions();
  }
Exemplo n.º 10
0
  /**
   * Removes the given tool from the list of tools.
   *
   * @param aTool the tool to remove, cannot be <code>null</code>.
   */
  public void removeTool(final Tool aTool) {
    if (this.mainFrame != null) {
      this.mainFrame.removeToolMenuItem(aTool.getName());
    }

    updateActions();
  }
 private CheckedTreeNode insertNewTool(final CheckedTreeNode groupNode, final Tool toolCopy) {
   CheckedTreeNode toolNode = new CheckedTreeNode(toolCopy);
   toolNode.setChecked(toolCopy.isEnabled());
   ((ToolsGroup) groupNode.getUserObject()).addElement(toolCopy);
   groupNode.add(toolNode);
   nodeWasInserted(toolNode);
   return toolNode;
 }