📄 edgeview.java
字号:
/* * @(#)EdgeView.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.Cursor;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Point;import java.awt.Rectangle;import java.awt.Shape;import java.awt.event.MouseEvent;import java.awt.geom.AffineTransform;import java.awt.geom.GeneralPath;import java.awt.geom.Line2D;import java.io.Serializable;import java.util.Map;import javax.swing.SwingUtilities;import org.jgraph.JGraph;import org.jgraph.plaf.GraphUI;import org.jgraph.plaf.basic.BasicGraphUI;/** * The default implementation of an edge view. * * @version 1.0 1/1/02 * @author Gaudenz Alder */public class EdgeView extends AbstractCellView { /** Renderer for the class. */ public static EdgeRenderer renderer = new EdgeRenderer(); /** List of points of the edge. May contain ports. */ protected java.util.List points; /** Cached source and target portview of the edge. */ protected CellView source, target; /** Cached label position of the edge. */ protected Point labelPosition; /** Drawing attributes that are created on the fly */ public Shape beginShape, endShape, lineShape; /** Shared-path tune-up. */ public transient GeneralPath sharedPath = null; private Rectangle cachedLabelBounds = null; private Rectangle cachedBounds = null; /** * Constructs an edge view for the specified model object. * * @param cell reference to the model object */ public EdgeView(Object cell, JGraph graph, CellMapper mapper) { super(cell, graph, mapper); } // // Data Source // /** * Overrides the parent method to udpate the cached points, * source and target port. If the source or target is removed, * a point is inserted into the array of points. */ public void refresh(boolean createDependentViews) { super.refresh(createDependentViews); // Sync Source- and Targetport if (points != null) { Object modelSource = getModel().getSource(cell); Object modelTarget = getModel().getTarget(cell); setSource(mapper.getMapping(modelSource, createDependentViews)); setTarget(mapper.getMapping(modelTarget, createDependentViews)); // Re-Route Edge.Routing routing = GraphConstants.getRouting(allAttributes); if (routing != null) routing.route(this, points); } } /** * Update attributes and recurse children. */ public void update() { super.update(); points = GraphConstants.getPoints(allAttributes); labelPosition = GraphConstants.getLabelPosition(allAttributes); Edge.Routing routing = GraphConstants.getRouting(allAttributes); if (routing != null) routing.route(this, points); // Synchronize Points and PortViews if (getModel().getSource(cell) != null) setSource(getSource()); if (getModel().getTarget(cell) != null) setTarget(getTarget()); // Clear cached shapes beginShape = null; endShape = null; lineShape = null; sharedPath = null; cachedBounds = null; cachedLabelBounds = null; } void invalidate() { sharedPath = null; cachedBounds = null; cachedLabelBounds = null; } public static long shapeHits = 0; /** * Returns the shape of the view according to the last rendering state */ public final Shape getShape() { if (sharedPath != null) { return sharedPath; } else { shapeHits++; return sharedPath = (GeneralPath) getEdgeRenderer().createShape(); } } /** * Returns the bounds of label according to the last rendering state */ public final Rectangle getLabelBounds() { if (cachedLabelBounds != null) { return cachedLabelBounds; } else { return cachedLabelBounds = getEdgeRenderer().getLabelBounds(this); } } // // View Methods // /** * Returns true if this view intersects the given rectangle. */ public boolean intersects(Graphics g, Rectangle rect) { return getEdgeRenderer().intersects(g, this, rect); } /** * Returns the location for this portview. */ public Rectangle getBounds() { if (cachedBounds != null) { return cachedBounds; } else { return cachedBounds = getEdgeRenderer().getBounds(this); } } /** * Returns the local renderer. Do not access the renderer * field directly. Use this method instead! */ public EdgeRenderer getEdgeRenderer() { return (EdgeRenderer) getRenderer(); } /** * Returns a renderer for the class. */ public CellViewRenderer getRenderer() { return renderer; } /** * Returns a cell handle for the view. */ public CellHandle getHandle(GraphContext context) { return new EdgeHandle(this, context); } // // Cached Values // /** * Returns the CellView that represents the source of the edge. */ public CellView getSource() { return source; } /** * Sets the <code>sourceView</code> of the edge. */ public void setSource(CellView sourceView) { source = sourceView; if (source != null) points.set(0, source); else points.set(0, getPoint(0)); invalidate(); } /** * Returns the CellView that represents the target of the edge. */ public CellView getTarget() { return target; } /** * Sets the <code>targetView</code> of the edge. */ public void setTarget(CellView targetView) { target = targetView; int n = points.size() - 1; if (target != null) points.set(n, target); else points.set(n, getPoint(n)); invalidate(); } /** * Returns a point that describes the position of the label. */ public Point getLabelPosition() { return labelPosition; } /** * Sets the description of the label position. */ public void setLabelPosition(Point pos) { labelPosition.setLocation(pos); invalidate(); } // // Points // /** * Returns the points. * @return java.util.List */ public java.util.List getPoints() { return points; } /** * Returns the number of point for this edge. */ public int getPointCount() { return points.size(); } /** * Returns the cached points for this edge. */ public Point getPoint(int index) { Object obj = points.get(index); if (obj instanceof PortView) // Port Location Seen From This Edge return ((PortView) obj).getLocation(this); else if (obj instanceof CellView) return ((CellView) obj).getBounds().getLocation(); else if (obj instanceof Point) // Regular Point return (Point) obj; return null; } /** * Sets the point at <code>index</code> to <code>p</code>. */ public void setPoint(int index, Point p) { points.set(index, p); invalidate(); } /** * Adds <code>p</code> at position <code>index</code>. */ public void addPoint(int index, Point p) { points.add(index, p); invalidate(); } /** * Removes the point at position <code>index</code>. */ public void removePoint(int index) { points.remove(index); invalidate(); } /** * Returning true signifies a mouse event adds a new point to an edge. */ public boolean isAddPointEvent(MouseEvent event) { return SwingUtilities.isRightMouseButton(event); } /** * Returning true signifies a mouse event removes a given point. */ public boolean isRemovePointEvent(MouseEvent event) { return SwingUtilities.isRightMouseButton(event); } // // Routing // public static double getLength(CellView view) { double cost = 1; if (view instanceof EdgeView) { EdgeView edge = (EdgeView) view; Point last = null, current = null; for (int i = 0; i < edge.getPointCount(); i++) { current = edge.getPoint(i); if (last != null) cost += last.distance(current); last = current; } } return cost; } public boolean isConstrainedMoveEvent(MouseEvent e) { GraphUI ui = graph.getUI(); if (ui instanceof BasicGraphUI) return ((BasicGraphUI) ui).isConstrainedMoveEvent(e); return false; } // // Handle // // This implementation uses the point instance to make the change. No index // is used for the current point because routing could change the index during // the move operation. public class EdgeHandle implements CellHandle, Serializable { protected JGraph graph; /* Pointer to the edge and its clone. */ protected EdgeView edge, orig; /* Boolean indicating whether the source, target or label is being edited. */ protected boolean label = false, source = false, target = false; /* Pointer to the currently selected point. */ protected Point currentPoint; /* Array of control points represented as rectangles. */ protected transient Rectangle[] r; /* A control point for the label position. */ protected transient Rectangle loc;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -