📄 basicgraphui.java
字号:
visRect.width + visRect.x - cellBounds.x, editorSize.height); if (cellEditor.shouldSelectCell(event)) { stopEditingInCompleteEditing = false; try { graph.setSelectionCell(cell); } catch (Exception e) { System.err.println("Editing exception: " + e); } stopEditingInCompleteEditing = true; } if (event instanceof MouseEvent) { /* Find the component that will get forwarded all the mouse events until mouseReleased. */ Point componentPoint = SwingUtilities.convertPoint( graph, new Point(event.getX(), event.getY()), editingComponent); /* Create an instance of BasicTreeMouseListener to handle passing the mouse/motion events to the necessary component. */ // We really want similiar behavior to getMouseEventTarget, // but it is package private. Component activeComponent = SwingUtilities.getDeepestComponentAt( editingComponent, componentPoint.x, componentPoint.y); if (activeComponent != null) { new MouseInputHandler(graph, activeComponent, event); } } return true; } else editingComponent = null; } return false; } /** * Scroll the graph for an event at <code>p</code>. */ public static void autoscroll(JGraph graph, Point p) { Rectangle view = graph.getBounds(); if (graph.getParent() instanceof JViewport) view = ((JViewport) graph.getParent()).getViewRect(); if (view.contains(p)) { Point target = new Point(p); int dx = (int) (graph.getWidth() * SCROLLSTEP); int dy = (int) (graph.getHeight() * SCROLLSTEP); if (target.x - view.x < SCROLLBORDER) target.x -= dx; if (target.y - view.y < SCROLLBORDER) target.y -= dy; if (view.x + view.width - target.x < SCROLLBORDER) target.x += dx; if (view.y + view.height - target.y < SCROLLBORDER) target.y += dy; graph.scrollPointToVisible(target); } } /** * Updates the preferred size when scrolling (if necessary). */ public class ComponentHandler extends ComponentAdapter implements ActionListener { /** Timer used when inside a scrollpane and the scrollbar is * adjusting. */ protected Timer timer; /** ScrollBar that is being adjusted. */ protected JScrollBar scrollBar; public void componentMoved(ComponentEvent e) { if (timer == null) { JScrollPane scrollPane = getScrollPane(); if (scrollPane == null) updateSize(); else { scrollBar = scrollPane.getVerticalScrollBar(); if (scrollBar == null || !scrollBar.getValueIsAdjusting()) { // Try the horizontal scrollbar. if ((scrollBar = scrollPane.getHorizontalScrollBar()) != null && scrollBar.getValueIsAdjusting()) startTimer(); else updateSize(); } else startTimer(); } } } /** * Creates, if necessary, and starts a Timer to check if need to * resize the bounds. */ protected void startTimer() { if (timer == null) { timer = new Timer(200, this); timer.setRepeats(true); } timer.start(); } /** * Returns the JScrollPane housing the JGraph, or null if one isn't * found. */ protected JScrollPane getScrollPane() { Component c = graph.getParent(); while (c != null && !(c instanceof JScrollPane)) c = c.getParent(); if (c instanceof JScrollPane) return (JScrollPane) c; return null; } /** * Public as a result of Timer. If the scrollBar is null, or * not adjusting, this stops the timer and updates the sizing. */ public void actionPerformed(ActionEvent ae) { if (scrollBar == null || !scrollBar.getValueIsAdjusting()) { if (timer != null) timer.stop(); updateSize(); timer = null; scrollBar = null; } } } // End of BasicGraphUI.ComponentHandler /** * Listens for changes in the graph model and updates the view * accordingly. */ public class GraphModelHandler implements GraphModelListener, Serializable { public void graphChanged(GraphModelEvent e) { if (graphLayoutCache != null) graphLayoutCache.graphChanged(e.getChange()); // Get arrays Object[] inserted = e.getChange().getInserted(); Object[] removed = e.getChange().getRemoved(); // Insert if (inserted != null && inserted.length > 0) // Update focus to first inserted cell focus = graphLayoutCache.getMapping(inserted[0], false); // Remove if (removed != null && removed.length > 0) { // Update Focus If Necessary for (int i = 0; i < removed.length && focus != null; i++) if (removed[i] == focus.getCell()) focus = null; // Remove from selection graph.getSelectionModel().removeSelectionCells(removed); } updateSize(); // Select new root cells if (inserted != null && inserted.length > 0 && graph.isSelectNewCells()) { java.util.List toSelect = new ArrayList(); for (int i = 0; i < inserted.length; i++) { if (graphModel.getParent(inserted[i]) == null && graphLayoutCache.isVisible(inserted[i])) { toSelect.add(inserted[i]); } } graph.setSelectionCells(toSelect.toArray()); graph.requestFocus(); } } } // End of BasicGraphUI.GraphModelHandler /** * Listens for changes in the graph view and updates the size * accordingly. */ public class GraphViewObserver implements Observer, Serializable { public void update(Observable o, Object arg) { updateSize(); } } // End of BasicGraphUI.GraphModelHandler /** * Listens for changes in the selection model and updates the display * accordingly. */ public class GraphSelectionHandler implements GraphSelectionListener, Serializable { /** * Messaged when the selection changes in the graph we're displaying * for. Stops editing, updates handles and displays the changed cells. */ public void valueChanged(GraphSelectionEvent event) { //cancelEditing(graph); updateHandle(); Object[] cells = event.getCells(); if (cells != null && cells.length <= MAXCLIPCELLS) { Rectangle r = graph.toScreen(graph.getCellBounds(cells)); if (r != null) { int hsize = graph.getHandleSize(); r.grow(hsize, hsize); //padding to paint handles updateHandle(); graph.repaint(r); } } else graph.repaint(); } } /** * Listener responsible for getting cell editing events and updating * the graph accordingly. */ public class CellEditorHandler implements CellEditorListener, Serializable { /** Messaged when editing has stopped in the graph. */ public void editingStopped(ChangeEvent e) { completeEditing(false, false, true); } /** Messaged when editing has been canceled in the graph. */ public void editingCanceled(ChangeEvent e) { completeEditing(false, false, false); } } // BasicGraphUI.CellEditorHandler /** * This is used to get mutliple key down events to appropriately generate * events. */ public class KeyHandler extends KeyAdapter implements Serializable { /** Key code that is being generated for. */ protected Action repeatKeyAction; /** Set to true while keyPressed is active. */ protected boolean isKeyDown; public void keyPressed(KeyEvent e) { if (graph != null && graph.hasFocus() && graph.isEnabled()) { KeyStroke keyStroke = KeyStroke.getKeyStroke(e.getKeyCode(), e.getModifiers()); if (graph.getConditionForKeyStroke(keyStroke) == JComponent.WHEN_FOCUSED) { ActionListener listener = graph.getActionForKeyStroke(keyStroke); if (listener instanceof Action) repeatKeyAction = (Action) listener; else repeatKeyAction = null; } else { repeatKeyAction = null; if (keyStroke.getKeyCode() == KeyEvent.VK_ESCAPE) { if (marquee != null) marquee.mouseReleased(null); if (mouseListener != null) mouseListener.mouseReleased(null); updateHandle(); graph.repaint(); } } if (isKeyDown && repeatKeyAction != null) { repeatKeyAction.actionPerformed( new ActionEvent( graph, ActionEvent.ACTION_PERFORMED, "")); e.consume(); } else isKeyDown = true; } } public void keyReleased(KeyEvent e) { isKeyDown = false; } } // End of BasicGraphUI.KeyHandler /** * TreeMouseListener is responsible for updating the selection * based on mouse events. */ public class MouseHandler extends MouseAdapter implements MouseMotionListener, Serializable { /* The cell under the mousepointer. */ protected CellView cell; /* The object that handles mouse operations. */ protected Object handler; protected transient Cursor previousCursor = null; /** * Invoked when a mouse button has been pressed on a component. */ public void mousePressed(MouseEvent e) { handler = null; if (!e.isConsumed() && graph.isEnabled()) { graph.requestFocus(); int s = graph.getTolerance(); Rectangle r = graph.fromScreen( new Rectangle( e.getX() - s, e.getY() - s, 2 * s, 2 * s)); Point point = graph.fromScreen(new Point(e.getPoint())); focus = (focus != null && focus.intersects(graph.getGraphics(), r)) ? focus : null; cell = graph.getNextViewAt(focus, point.x, point.y); if (focus == null) focus = cell; completeEditing(); if (!isForceMarqueeEvent(e)) { if (e.getClickCount() == graph.getEditClickCount() && focus != null && focus.isLeaf() && focus.getParentView() == null) { // Start Editing handleEditTrigger(focus.getCell(), e); e.consume(); cell = null; } else if (!isToggleSelectionEvent(e)) { if (handle != null) { handle.mousePressed(e); handler = handle; } // Immediate Selection if (!e.isConsumed() && cell != null && !graph.isCellSelected(cell)) { selectCellForEvent(cell.getCell(), e); focus = cell; if (handle != null) { handle.mousePressed(e); handler = handle; } e.consume(); cell = null; } } } //Marquee Selection if (!e.isConsumed() && (!isToggleSelectionEvent(e) || focus == null)) { if (marquee != null) { marquee.mousePressed(e); handler = marquee; } } } } protected void handleEditTrigger(Object cell, MouseEvent e) { graph.scrollCellToVisible(cell); if (cell != null) startEditing(cell, e); } public void mouseDragged(MouseEvent e) { try { autoscroll(graph, e.getPoint()); if (handler != null && handler == marquee) marquee.mouseDragged(e); else if ( handler == null && !isEditing(graph) && focus != null) { if (!graph.isCellSelected(focus.getCell())) { selectCellForEvent(focus.getCell(), e); cell = null; } if (handle != null) handle.mousePressed(e); handler = handle; } if (handle != null && handler == handle) handle.mouseDragged(e); } finally { //cell = null; } } /** * Invoked when the mouse pointer has been moved on a component * (with no buttons down). */ public void mouseMoved(MouseEvent e) { if (previousCursor == null) previousCursor = graph.getCursor(); if (graph != null && graph.isEnabled()) { if (marquee != null) marquee.mouseMoved(e); if (handle != null) handle.mouseMoved(e); if (!e.isConsumed() && previousCursor != null) { graph.setCursor(previousCursor); previousCursor = null; } } } // Event may be null when called to cancel the current operation. public void mouseReleased(MouseEvent e) { try { if (e != null && !e.isConsumed()) { if (handler == marquee && marquee != null) marquee.mouseReleased(e); else if (handler == handle && handle != null) handle.mouseReleased(e); if (isDescendant(cell, focus) && e.getModifiers() != 0) { // Do not switch to parent if Special Selection cell = focus; } if (!e.isConsumed() && cell != null) { Object tmp = cell.getCell(); boolean wasSelected = graph.isCellSelected(tmp); selectCellForEvent(tmp, e); focus = cell; postProcessSelection(e, tmp, wasSelected); } } } finally { handler = null; cell = null; } } /** * Invoked after a cell has been selected in the mouseReleased method.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -