📄 basicgraphui.java
字号:
// Use Swing's scaling double scale = graph.getScale(); g2.scale(scale, scale); // Paint cells CellView[] views = graphLayoutCache.getRoots(); for (int i = 0; i < views.length; i++) { Rectangle bounds = views[i].getBounds(); if (bounds != null && real != null && bounds.intersects(real)) paintCell(g, views[i], bounds, false); } // Reset affine transform and antialias g2.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); g2.setTransform(at); // Paint Foreground (Typically Ports) paintForeground(g); // Paint Handle if (handle != null) handle.paint(g); // Paint Marquee g.setColor(graph.getMarqueeColor()); g.setXORMode(graph.getBackground()); if (marquee != null) marquee.paint(g); g.setPaintMode(); // Empty out the renderer pane, allowing renderers to be gc'ed. if (rendererPane != null) rendererPane.removeAll(); } /** * Paints the renderer of <code>view</code> to <code>g</code> * at <code>bounds</code>. Recursive implementation that * paints the children first.<p> * The reciever should NOT modify <code>clipBounds</code>, or * <code>insets</code>. The <code>preview</code> flag is passed to * the renderer, and is not used here. */ public void paintCell( Graphics g, CellView view, Rectangle bounds, boolean preview) { // First Paint View if (view != null && bounds != null) { boolean bfocus = (view == this.focus); boolean sel = graph.isCellSelected(view.getCell()); Component component = view.getRendererComponent(graph, sel, bfocus, preview); rendererPane.paintComponent( g, component, graph, bounds.x, bounds.y, bounds.width, bounds.height, true); } // Then Paint Children if (!view.isLeaf()) { CellView[] children = view.getChildViews(); for (int i = 0; i < children.length; i++) paintCell(g, children[i], children[i].getBounds(), preview); } } // // Background // /** * Paint the background of this graph. Calls paintGrid. */ protected void paintBackground(Graphics g) { if (graph.isGridVisible()) paintGrid(graph.getGridSize(), g, g.getClipBounds()); } /** * Paint the grid. */ protected void paintGrid(int gs, Graphics g, Rectangle r) { // Parameter "r" is never used: remove it. Rectangle rr = (Rectangle) g.getClipBounds(); double xl = rr.x; double yt = rr.y; double xr = xl + rr.width; double yb = yt + rr.height; double sgs = (double) gs * graph.getScale(); while (sgs < 2) sgs *= 2; if (sgs >= 0.0) { int xs = (int) (Math.floor(xl / sgs) * sgs); int xe = (int) (Math.ceil(xr / sgs) * sgs); int ys = (int) (Math.floor(yt / sgs) * sgs); int ye = (int) (Math.ceil(yb / sgs) * sgs); g.setColor(graph.getGridColor()); switch (graph.getGridMode()) { case JGraph.CROSS_GRID_MODE : { int cs = (sgs > 16.0) ? 2 : ((sgs < 8.0) ? 0 : 1); for (double x = xs; x <= xe; x += sgs) { for (double y = ys; y <= ye; y += sgs) { int ix = (int) Math.round(x); int iy = (int) Math.round(y); g.drawLine(ix - cs, iy, ix + cs, iy); g.drawLine(ix, iy - cs, ix, iy + cs); } } } break; case JGraph.LINE_GRID_MODE : { xe += (int) Math.ceil(sgs); ye += (int) Math.ceil(sgs); for (double x = xs; x <= xe; x += sgs) { int ix = (int) Math.round(x); g.drawLine(ix, ys, ix, ye); } for (double y = ys; y <= ye; y += sgs) { int iy = (int) Math.round(y); g.drawLine(xs, iy, xe, iy); } } break; case JGraph.DOT_GRID_MODE : default : for (double x = xs; x <= xe; x += sgs) { for (double y = ys; y <= ye; y += sgs) { int ix = (int) Math.round(x); int iy = (int) Math.round(y); g.drawLine(ix, iy, ix, iy); } } break; } } } // // Foreground // /** * Paint the foreground of this graph. Calls paintPorts. */ protected void paintForeground(Graphics g) { if (graph.isPortsVisible()) paintPorts(g, graphLayoutCache.getPorts()); } /** * Paint <code>ports</code>. */ public void paintPorts(Graphics g, CellView[] ports) { if (ports != null) { Rectangle r = g.getClipBounds(); for (int i = 0; i < ports.length; i++) { if (ports[i] != null) { Rectangle bounds = new Rectangle(ports[i].getBounds()); Point center = graph.toScreen( new Point( (int) bounds.getCenterX(), (int) bounds.getCenterY())); bounds.setLocation( center.x - bounds.width / 2, center.y - bounds.height / 2); if (r == null || bounds.intersects(r)) paintCell(g, ports[i], bounds, false); } } } } // // Various local methods // /** * Update the handle using createHandle. */ protected void updateHandle() { if (graphLayoutCache != null) { Object[] cells = graphLayoutCache.order(graph.getSelectionCells()); if (cells != null && cells.length > 0) handle = createHandle(createContext(graph, cells)); else handle = null; } } protected GraphContext createContext(JGraph graph, Object[] cells) { return new GraphContext(graph, cells); } /** * Constructs the "root handle" for <code>context</code>. * * @param context reference to the context of the current selection. */ public CellHandle createHandle(GraphContext context) { if (context != null && !context.isEmpty() && graph.isEnabled()) return new RootHandle(context); return null; } /** * Messages the Graph with <code>graphDidChange</code>. */ public void updateSize() { validCachedPreferredSize = false; graph.graphDidChange(); updateHandle(); } /** * Updates the <code>preferredSize</code> instance variable, * which is returned from <code>getPreferredSize()</code>. */ protected void updateCachedPreferredSize() { Rectangle size = AbstractCellView.getBounds(graphLayoutCache.getRoots()); if (size == null) size = new Rectangle(); Point psize = new Point(size.x + size.width, size.y + size.height); Dimension d = graph.getMinimumSize(); Point min = (d != null) ? graph.toScreen(new Point(d.width, d.height)) : new Point(0, 0); Point scaled = graph.toScreen(psize); preferredSize = new Dimension(Math.max(min.x, scaled.x), Math.max(min.y, scaled.y)); Insets in = graph.getInsets(); if (in != null) { preferredSize.width += in.left + in.right; preferredSize.height += in.top + in.bottom; } validCachedPreferredSize = true; } /** Sets the preferred minimum size. */ public void setPreferredMinSize(Dimension newSize) { preferredMinSize = newSize; } /** Returns the minimum preferred size. */ public Dimension getPreferredMinSize() { if (preferredMinSize == null) return null; return new Dimension(preferredMinSize); } /** Returns the preferred size to properly display the graph. */ public Dimension getPreferredSize(JComponent c) { Dimension pSize = this.getPreferredMinSize(); if (!validCachedPreferredSize) updateCachedPreferredSize(); if (graph != null) { if (pSize != null) return new Dimension( Math.max(pSize.width, preferredSize.width), Math.max(pSize.height, preferredSize.height)); return new Dimension(preferredSize.width, preferredSize.height); } else if (pSize != null) return pSize; else return new Dimension(0, 0); } /** * Returns the minimum size for this component. Which will be * the min preferred size or 0, 0. */ public Dimension getMinimumSize(JComponent c) { if (this.getPreferredMinSize() != null) return this.getPreferredMinSize(); return new Dimension(0, 0); } /** * Returns the maximum size for this component, which will be the * preferred size if the instance is currently in a JGraph, or 0, 0. */ public Dimension getMaximumSize(JComponent c) { if (graph != null) return getPreferredSize(graph); if (this.getPreferredMinSize() != null) return this.getPreferredMinSize(); return new Dimension(0, 0); } /** * Messages to stop the editing session. If the UI the receiver * is providing the look and feel for returns true from * <code>getInvokesStopCellEditing</code>, stopCellEditing will * invoked on the current editor. Then completeEditing will * be messaged with false, true, false to cancel any lingering * editing. */ protected void completeEditing() { /* If should invoke stopCellEditing, try that */ if (graph.getInvokesStopCellEditing() && stopEditingInCompleteEditing && editingComponent != null) { cellEditor.stopCellEditing(); } /* Invoke cancelCellEditing, this will do nothing if stopCellEditing was succesful. */ completeEditing(false, true, false); } /** * Stops the editing session. If messageStop is true the editor * is messaged with stopEditing, if messageCancel is true the * editor is messaged with cancelEditing. If messageGraph is true * the graphModel is messaged with valueForCellChanged. */ protected void completeEditing( boolean messageStop, boolean messageCancel, boolean messageGraph) { if (stopEditingInCompleteEditing && editingComponent != null) { Component oldComponent = editingComponent; Object oldCell = editingCell; GraphCellEditor oldEditor = cellEditor; Object newValue = oldEditor.getCellEditorValue(); boolean requestFocus = (graph != null && (graph.hasFocus() || editingComponent.hasFocus())); editingCell = null; editingComponent = null; if (messageStop) oldEditor.stopCellEditing(); else if (messageCancel) oldEditor.cancelCellEditing(); graph.remove(oldComponent); if (requestFocus) graph.requestFocus(); if (messageGraph) { Map nested = GraphConstants.createAttributes( oldCell, GraphConstants.VALUE, newValue); graphLayoutCache.edit(nested, null, null, null); } updateSize(); // Remove Editor Listener if (oldEditor != null && cellEditorListener != null) oldEditor.removeCellEditorListener(cellEditorListener); cellEditor = null; } } /** * Will start editing for cell if there is a cellEditor and * shouldSelectCell returns true.<p> * This assumes that cell is valid and visible. */ protected boolean startEditing(Object cell, MouseEvent event) { completeEditing(); if (graph.isCellEditable(cell)) { CellView tmp = graphLayoutCache.getMapping(cell, false); cellEditor = tmp.getEditor(); editingComponent = cellEditor.getGraphCellEditorComponent( graph, cell, graph.isCellSelected(cell)); if (cellEditor.isCellEditable(event)) { Rectangle cellBounds = graph.getCellBounds(cell); editingCell = cell; Dimension editorSize = editingComponent.getPreferredSize(); graph.add(editingComponent); Point p = graph.toScreen(new Point(cellBounds.x, cellBounds.y)); // Edges have different editor position and size if (graphLayoutCache.getMapping(cell, false) instanceof EdgeView) { EdgeView edgeView = (EdgeView) graphLayoutCache.getMapping(cell, false); EdgeRenderer er = edgeView.getEdgeRenderer(); p = er.getLabelPosition(edgeView); p.x = p.x - editorSize.width / 2; p.y = p.y - editorSize.height / 2; graph.toScreen(p); } editingComponent.setBounds( p.x, p.y, editorSize.width, editorSize.height); editingCell = cell; editingComponent.validate(); // Add Editor Listener if (cellEditorListener == null) cellEditorListener = createCellEditorListener(); if (cellEditor != null && cellEditorListener != null) cellEditor.addCellEditorListener(cellEditorListener); Rectangle visRect = graph.getVisibleRect(); graph.paintImmediately( p.x, p.y,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -