📄 basicgraphui.java
字号:
/** * Returning true signifies the marquee handler has precedence over * other handlers, and is receiving subsequent mouse events. */ public boolean isForceMarqueeEvent(MouseEvent event) { if (marquee != null) return marquee.isForceMarqueeEvent(event); return false; } /** * Returning true signifies a move should only be * applied to one direction. */ public boolean isConstrainedMoveEvent(MouseEvent event) { if (event != null) return event.isShiftDown(); return false; } // // Editing // /** * Returns true if the graph is being edited. The item that is being * edited can be returned by getEditingPath(). */ public boolean isEditing(JGraph graph) { return (editingComponent != null); } /** * Stops the current editing session. This has no effect if the * graph isn't being edited. Returns true if the editor allows the * editing session to stop. */ public boolean stopEditing(JGraph graph) { if (editingComponent != null && cellEditor.stopCellEditing()) { completeEditing(false, false, true); return true; } return false; } /** * Cancels all current editing sessions. */ public void cancelEditing(JGraph graph) { if (editingComponent != null) completeEditing(false, true, false); // Escape key is handled by the KeyHandler.keyPressed inner class method } /** * Selects the cell and tries to edit it. Editing will * fail if the CellEditor won't allow it for the selected item. */ public void startEditingAtCell(JGraph graph, Object cell) { graph.scrollCellToVisible(cell); if (cell != null) startEditing(cell, null); } /** * Returns the element that is being edited. */ public Object getEditingCell(JGraph graph) { return editingCell; } // // Install methods // public void installUI(JComponent c) { if (c == null) throw new NullPointerException("null component passed to BasicGraphUI.installUI()"); graph = (JGraph) c; marquee = graph.getMarqueeHandler(); prepareForUIInstall(); // Boilerplate install block installDefaults(); installListeners(); installKeyboardActions(); installComponents(); completeUIInstall(); } /** * Invoked after the <code>graph</code> instance variable has been * set, but before any defaults/listeners have been installed. */ protected void prepareForUIInstall() { // Data member initializations stopEditingInCompleteEditing = true; preferredSize = new Dimension(); setGraphLayoutCache(graph.getGraphLayoutCache()); setModel(graph.getModel()); } /** * Invoked from installUI after all the defaults/listeners have been * installed. */ protected void completeUIInstall() { // Custom install code setSelectionModel(graph.getSelectionModel()); updateSize(); } /** * Invoked as part from the boilerplate install block. This * sets the look and feel specific variables in JGraph. */ protected void installDefaults() { if (graph.getBackground() == null || graph.getBackground() instanceof UIResource) { graph.setBackground(UIManager.getColor("Tree.background")); } if (graph.getFont() == null || graph.getFont() instanceof UIResource) graph.setFont(UIManager.getFont("Tree.font")); // Set JGraph's laf-specific colors graph.setMarqueeColor(UIManager.getColor("Table.gridColor")); graph.setHandleColor( UIManager.getColor("MenuItem.selectionBackground")); graph.setLockedHandleColor(UIManager.getColor("MenuItem.background")); graph.setGridColor(UIManager.getColor("Tree.selectionBackground")); graph.setOpaque(true); } /** * Invoked as part from the boilerplate install block. This * installs the listeners from BasicGraphUI in the graph. */ protected void installListeners() { // Install Local Handlers TransferHandler th = graph.getTransferHandler(); if (th == null || th instanceof UIResource) { defaultTransferHandler = createTransferHandler(); graph.setTransferHandler(defaultTransferHandler); } if (graphLayoutCache != null) { graphViewObserver = createGraphViewObserver(); graphLayoutCache.addObserver(graphViewObserver); } DropTarget dropTarget = graph.getDropTarget(); try { if (dropTarget != null) { defaultDropTargetListener = new GraphDropTargetListener(); dropTarget.addDropTargetListener(defaultDropTargetListener); } } catch (TooManyListenersException tmle) { // should not happen... swing drop target is multicast } // Install Listeners if ((propertyChangeListener = createPropertyChangeListener()) != null) graph.addPropertyChangeListener(propertyChangeListener); if ((mouseListener = createMouseListener()) != null) { graph.addMouseListener(mouseListener); if (mouseListener instanceof MouseMotionListener) { graph.addMouseMotionListener( (MouseMotionListener) mouseListener); } } if ((keyListener = createKeyListener()) != null) { graph.addKeyListener(keyListener); } if ((graphModelListener = createGraphModelListener()) != null && graphModel != null) graphModel.addGraphModelListener(graphModelListener); if ((graphSelectionListener = createGraphSelectionListener()) != null && graphSelectionModel != null) graphSelectionModel.addGraphSelectionListener( graphSelectionListener); } /** * Invoked as part from the boilerplate install block. */ protected void installKeyboardActions() { InputMap km = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); SwingUtilities.replaceUIInputMap( graph, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, km); km = getInputMap(JComponent.WHEN_FOCUSED); SwingUtilities.replaceUIInputMap(graph, JComponent.WHEN_FOCUSED, km); SwingUtilities.replaceUIActionMap(graph, createActionMap()); } /** * Return JTree's input map. */ InputMap getInputMap(int condition) { if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) return (InputMap) UIManager.get("Tree.ancestorInputMap"); else if (condition == JComponent.WHEN_FOCUSED) return (InputMap) UIManager.get("Tree.focusInputMap"); return null; } /** * Return the mapping between JTree's input map and JGraph's actions. */ ActionMap createActionMap() { // 1: Up, 2: Right, 3: Down, 4: Left ActionMap map = new ActionMapUIResource(); map.put( "selectPrevious", new GraphIncrementAction(1, "selectPrevious")); map.put( "selectPreviousChangeLead", new GraphIncrementAction(1, "selectPreviousLead")); map.put( "selectPreviousExtendSelection", new GraphIncrementAction(1, "selectPreviousExtendSelection")); map.put("selectParent", new GraphIncrementAction(4, "selectParent")); map.put( "selectParentChangeLead", new GraphIncrementAction(4, "selectParentChangeLead")); map.put("selectNext", new GraphIncrementAction(3, "selectNext")); map.put( "selectNextChangeLead", new GraphIncrementAction(3, "selectNextLead")); map.put( "selectNextExtendSelection", new GraphIncrementAction(3, "selectNextExtendSelection")); map.put("selectChild", new GraphIncrementAction(2, "selectChild")); map.put( "selectChildChangeLead", new GraphIncrementAction(2, "selectChildChangeLead")); map.put("cancel", new GraphCancelEditingAction("cancel")); map.put("startEditing", new GraphEditAction("startEditing")); map.put("selectAll", new GraphSelectAllAction("selectAll", true)); map.put( "clearSelection", new GraphSelectAllAction("clearSelection", false)); map.put( TransferHandler.getCutAction().getValue(Action.NAME), TransferHandler.getCutAction()); map.put( TransferHandler.getCopyAction().getValue(Action.NAME), TransferHandler.getCopyAction()); map.put( TransferHandler.getPasteAction().getValue(Action.NAME), TransferHandler.getPasteAction()); return map; } /** * Intalls the subcomponents of the graph, which is the renderer pane. */ protected void installComponents() { if ((rendererPane = createCellRendererPane()) != null) graph.add(rendererPane); } // // Create methods. // /** * Creates an instance of TransferHandler. Used for subclassers * to provide different TransferHandler. */ protected TransferHandler createTransferHandler() { return new GraphTransferHandler(); } /** * Creates a listener that is responsible to update the UI based on * how the graph's bounds properties change. */ protected PropertyChangeListener createPropertyChangeListener() { return new PropertyChangeHandler(); } /** * Creates the listener responsible for calling the correct handlers * based on mouse events, and to select invidual cells. */ protected MouseListener createMouseListener() { return new MouseHandler(); } /** * Creates the listener reponsible for getting key events from * the graph. */ protected KeyListener createKeyListener() { return new KeyHandler(); } /** * Creates the listener that updates the display based on selection change * methods. */ protected GraphSelectionListener createGraphSelectionListener() { return new GraphSelectionHandler(); } /** * Creates a listener to handle events from the current editor. */ protected CellEditorListener createCellEditorListener() { return new CellEditorHandler(); } /** * Creates and returns a new ComponentHandler. */ protected ComponentListener createComponentListener() { return new ComponentHandler(); } /** * Returns the renderer pane that renderer components are placed in. */ protected CellRendererPane createCellRendererPane() { return new CellRendererPane(); } /** * Returns a listener that can update the graph when the view changes. */ protected Observer createGraphViewObserver() { return new GraphViewObserver(); } /** * Returns a listener that can update the graph when the model changes. */ protected GraphModelListener createGraphModelListener() { return new GraphModelHandler(); } // // Uninstall methods // public void uninstallUI(JComponent c) { cancelEditing(graph); uninstallListeners(); uninstallKeyboardActions(); uninstallComponents(); completeUIUninstall(); } protected void completeUIUninstall() { graphLayoutCache = null; rendererPane = null; componentListener = null; propertyChangeListener = null; keyListener = null; setSelectionModel(null); graph = null; graphModel = null; graphSelectionModel = null; graphSelectionListener = null; } protected void uninstallListeners() { // Uninstall Handlers TransferHandler th = graph.getTransferHandler(); if (th == defaultTransferHandler) graph.setTransferHandler(null); if (graphViewObserver != null) graphLayoutCache.deleteObserver(graphViewObserver); if (graph.getDropTarget() != null && defaultDropTargetListener != null) graph.getDropTarget().removeDropTargetListener( defaultDropTargetListener); if (componentListener != null) graph.removeComponentListener(componentListener); if (propertyChangeListener != null) graph.removePropertyChangeListener(propertyChangeListener); if (mouseListener != null) { graph.removeMouseListener(mouseListener); if (mouseListener instanceof MouseMotionListener) graph.removeMouseMotionListener( (MouseMotionListener) mouseListener); } if (keyListener != null) graph.removeKeyListener(keyListener); if (graphModel != null && graphModelListener != null) graphModel.removeGraphModelListener(graphModelListener); if (graphSelectionListener != null && graphSelectionModel != null) graphSelectionModel.removeGraphSelectionListener( graphSelectionListener); } protected void uninstallKeyboardActions() { SwingUtilities.replaceUIActionMap(graph, null); SwingUtilities.replaceUIInputMap( graph, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, null); SwingUtilities.replaceUIInputMap(graph, JComponent.WHEN_FOCUSED, null); } /** * Uninstalls the renderer pane. */ protected void uninstallComponents() { if (rendererPane != null) graph.remove(rendererPane); } // // Painting routines. // /** * Main painting routine. */ public void paint(Graphics g, JComponent c) { if (graph != c) throw new InternalError( "BasicGraphUI cannot paint " + c.toString() + "; " + graph + " was expected."); Graphics2D g2 = (Graphics2D) g; Rectangle paintBounds = new Rectangle(g.getClipBounds()); Rectangle real = graph.fromScreen(new Rectangle(paintBounds)); // Paint Background (Typically Grid) paintBackground(g); // Remember current affine transform AffineTransform at = g2.getTransform(); // Use anti aliasing if (graph.isAntiAliased()) g2.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -