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(); } } } }
/** * 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); } }); }
/** Handles mouse down events and starts the corresponding tracker. */ public void mouseDown(MouseEvent e, int x, int y) { // on MS-Windows NT: AWT generates additional mouse down events // when the left button is down && right button is clicked. // To avoid dead locks we ignore such events if (fChild != null) { return; } view().freezeView(); Handle handle = view().findHandle(e.getX(), e.getY()); if (handle != null) { fChild = createHandleTracker(view(), handle); } else { Figure figure = drawing().findFigure(e.getX(), e.getY()); if (figure != null) { fChild = createDragTracker(figure); } else { if (!e.isShiftDown()) { view().clearSelection(); } fChild = createAreaTracker(); } } fChild.mouseDown(e, x, y); fChild.activate(); }
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); }
/** Handles mouse up events. The events are forwarded to the current tracker. */ public void mouseUp(MouseEvent e, int x, int y) { view().unfreezeView(); if (fChild != null) { // JDK1.1 doesn't guarantee mouseDown, mouseDrag, mouseUp fChild.mouseUp(e, x, y); fChild.deactivate(); fChild = null; } }
private void setTool(Tool t, String name) { if (fTool != null) { fTool.deactivate(); } fTool = t; if (fTool != null) { showStatus(name); fTool.activate(); } }
protected void setTracker(Tool newTracker) { if (tracker != null) { tracker.deactivate(getEditor()); tracker.removeToolListener(this); } tracker = newTracker; if (tracker != null) { tracker.activate(getEditor()); tracker.addToolListener(this); } }
/** * 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 toolDone(ToolEvent event) { // Empty Tool newTracker = getSelectAreaTracker(); if (newTracker != null) { if (tracker != null) { tracker.deactivate(getEditor()); tracker.removeToolListener(this); } tracker = newTracker; tracker.activate(getEditor()); tracker.addToolListener(this); } fireToolDone(); }
/** * 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(); }
/** * 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(); }
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())); }
/** * 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()); } }
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; }
public void keyTyped(KeyEvent evt) { if (getView() != null && getView().isEnabled()) { tracker.keyTyped(evt); } }
public void mouseDragged(MouseEvent evt) { if (getView() != null && getView().isEnabled()) { tracker.mouseDragged(evt); } }
@Override public void draw(Graphics2D g) { tracker.draw(g); }
@Override public void mousePressed(MouseEvent evt) { if (getView() != null && getView().isEnabled()) { super.mousePressed(evt); DrawingView view = getView(); Handle handle = view.findHandle(anchor); Tool newTracker = null; if (handle != null) { newTracker = getHandleTracker(handle); } else { Figure figure; Drawing drawing = view.getDrawing(); Point2D.Double p = view.viewToDrawing(anchor); if (isSelectBehindEnabled() && (evt.getModifiersEx() & (InputEvent.ALT_DOWN_MASK | InputEvent.CTRL_DOWN_MASK)) != 0) { // Select a figure behind the current selection figure = view.findFigure(anchor); while (figure != null && !figure.isSelectable()) { figure = drawing.findFigureBehind(p, figure); } HashSet<Figure> ignoredFigures = new HashSet<Figure>(view.getSelectedFigures()); ignoredFigures.add(figure); Figure figureBehind = view.getDrawing().findFigureBehind(view.viewToDrawing(anchor), ignoredFigures); if (figureBehind != null) { figure = figureBehind; } } else { // Note: The search sequence used here, must be // consistent with the search sequence used by the // DefaultHandleTracker, the DefaultSelectAreaTracker and DelegationSelectionTool. // If possible, continue to work with the current selection figure = null; if (isSelectBehindEnabled()) { for (Figure f : view.getSelectedFigures()) { if (f.contains(p)) { figure = f; break; } } } // If the point is not contained in the current selection, // search for a figure in the drawing. if (figure == null) { figure = view.findFigure(anchor); while (figure != null && !figure.isSelectable()) { figure = drawing.findFigureBehind(p, figure); } } } if (figure != null && figure.isSelectable()) { newTracker = getDragTracker(figure); } else { if (!evt.isShiftDown()) { view.clearSelection(); view.setHandleDetailLevel(0); } newTracker = getSelectAreaTracker(); } } if (newTracker != null) { setTracker(newTracker); } tracker.mousePressed(evt); } }
public void mouseExited(MouseEvent evt) { super.mouseExited(evt); tracker.mouseExited(evt); }
@Override public void mouseReleased(MouseEvent evt) { if (getView() != null && getView().isEnabled()) { tracker.mouseReleased(evt); } }
/** Handles mouse drag events. The events are forwarded to the current tracker. */ public void mouseDrag(MouseEvent e, int x, int y) { if (fChild != null) { // JDK1.1 doesn't guarantee mouseDown, mouseDrag, mouseUp fChild.mouseDrag(e, x, y); } }
/** Creates a new instance. */ public SelectionTool() { tracker = getSelectAreaTracker(); tracker.addToolListener(this); }
public void keyPressed(KeyEvent e) { if (getView() != null && getView().isEnabled()) { tracker.keyPressed(e); } }
@Override public void activate(DrawingEditor editor) { super.activate(editor); tracker.activate(editor); }
public void deactivate(DrawingEditor editor) { super.deactivate(editor); tracker.deactivate(editor); }
@Override public void mouseMoved(MouseEvent evt) { tracker.mouseMoved(evt); }