📄 vertexview.java
字号:
/* * @(#)VertexView.java 1.0 1/1/02 * * Copyright (c) 2001-2004, Gaudenz Alder * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * - Neither the name of JGraph nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * */package org.jgraph.graph;import java.awt.Color;import java.awt.Cursor;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Image;import java.awt.Point;import java.awt.Rectangle;import java.awt.event.MouseEvent;import java.awt.geom.AffineTransform;import java.awt.image.BufferedImage;import java.io.Serializable;import java.util.HashSet;import java.util.LinkedList;import java.util.Map;import java.util.Set;import javax.swing.tree.DefaultMutableTreeNode;import javax.swing.tree.TreeNode;import org.jgraph.JGraph;import org.jgraph.plaf.GraphUI;import org.jgraph.plaf.basic.BasicGraphUI;/** * The default implementation of a vertex view. * * @version 1.0 1/1/02 * @author Gaudenz Alder */public class VertexView extends AbstractCellView { /** Renderer for the class. */ public static VertexRenderer renderer = new VertexRenderer(); /** Reference to the bounds attribute */ protected Rectangle bounds; /** Cached bounds of all children if vertex is a group */ protected Rectangle groupBounds = DefaultGraphCell.defaultBounds; /** * Constructs a vertex view for the specified model object * and the specified child views. * * @param cell reference to the model object */ public VertexView(Object cell, JGraph graph, CellMapper mapper) { super(cell, graph, mapper); } // // CellView Interface // /** * Overrides the parent method to udpate the cached points. */ public void update() { super.update(); bounds = GraphConstants.getBounds(allAttributes); groupBounds = null; } public void childUpdated() { super.childUpdated(); groupBounds = null; } /** * Returns the cached bounds for the vertex. */ public Rectangle getBounds() { if (!isLeaf()) { if (groupBounds == null) updateGroupBounds(); return groupBounds; } return bounds; } public Rectangle getCachedBounds() { return bounds; } public void setCachedBounds(Rectangle bounds) { this.bounds = bounds; } protected void updateGroupBounds() { // Note: Prevent infinite recursion by removing // child edges that point to their parent. CellView[] childViews = getChildViews(); LinkedList result = new LinkedList(); for (int i = 0; i < childViews.length; i++) if (includeInGroupBounds(childViews[i])) result.add(childViews[i]); childViews = new CellView[result.size()]; result.toArray(childViews); groupBounds = getBounds(childViews); } private boolean includeInGroupBounds(CellView view) { if (view instanceof EdgeView) { GraphModel model = graph.getModel(); EdgeView edgeView = (EdgeView) view; if (edgeView.getCell() instanceof DefaultMutableTreeNode) { DefaultMutableTreeNode edge = (DefaultMutableTreeNode) edgeView.getCell(); if (model.getSource(edge) instanceof TreeNode) { TreeNode source = (TreeNode) model.getSource(edge); if (((DefaultMutableTreeNode) source.getParent()) .isNodeDescendant(edge)) { return false; } } if (model.getTarget(edge) instanceof TreeNode) { TreeNode target = (TreeNode) model.getTarget(edge); if (((DefaultMutableTreeNode) target.getParent()) .isNodeDescendant(edge)) { return false; } } } } return true; } /** * Returns a renderer for the class. */ public CellViewRenderer getRenderer() { return renderer; } /** * Returns a cell handle for the view, if the graph and the view * are sizeable. */ public CellHandle getHandle(GraphContext context) { if (GraphConstants.isSizeable(getAllAttributes()) && context.getGraph().isSizeable()) return new SizeHandle(this, context); return null; } // // Special Methods // /** * Returns the center of this vertex. */ public Point getCenterPoint() { Rectangle r = getBounds(); return new Point((int) r.getCenterX(), (int) r.getCenterY()); } /** * Returns the intersection of the bounding rectangle and the * straight line between the source and the specified point p. * The specified point is expected not to intersect the bounds. * Note: You must override this method if you use a different * renderer. This is because this method relies on the * VertexRenderer interface, which can not be safely assumed * for subclassers. */ public Point getPerimeterPoint(Point source, Point p) { return renderer.getPerimeterPoint(this, source, p); } public boolean isConstrainedSizeEvent(MouseEvent e) { GraphUI ui = graph.getUI(); if (ui instanceof BasicGraphUI) return ((BasicGraphUI) ui).isConstrainedMoveEvent(e); return false; } public class SizeHandle implements CellHandle, Serializable { // Double Buffer protected transient Image offscreen; protected transient Graphics offgraphics; protected transient boolean firstDrag = true; protected transient JGraph graph; /* Reference to the temporary view for this handle. */ protected transient VertexView vertex; protected transient CellView[] portViews; protected transient Rectangle cachedBounds; /* Reference to the context for the specified view. */ protected transient GraphContext context; protected transient Rectangle initialBounds; protected transient CellView[] contextViews; /* Index of the active control point. -1 if none is active. */ protected transient int index = -1; /* Array of control points represented as rectangles. */ protected transient Rectangle[] r = new Rectangle[8]; protected boolean firstOverlayInvocation = true; /** Array that holds the cursors for the different control points. */ public transient int[] cursors = new int[] { Cursor.NW_RESIZE_CURSOR, Cursor.N_RESIZE_CURSOR, Cursor.NE_RESIZE_CURSOR, Cursor.W_RESIZE_CURSOR, Cursor.E_RESIZE_CURSOR, Cursor.SW_RESIZE_CURSOR, Cursor.S_RESIZE_CURSOR, Cursor.SE_RESIZE_CURSOR }; public SizeHandle(VertexView vertexview, GraphContext ctx) { graph = ctx.getGraph(); vertex = vertexview; // PortView Preview portViews = ctx.createTemporaryPortViews(); initialBounds = new Rectangle(vertex.getBounds()); context = ctx; for (int i = 0; i < r.length; i++) r[i] = new Rectangle(); invalidate(); } public void paint(Graphics g) { invalidate(); g.setColor(graph.getHandleColor()); for (int i = 0; i < r.length; i++) g.fill3DRect(r[i].x, r[i].y, r[i].width, r[i].height, true); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -