1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
/*
* Copyright (c) 2005, David Benson
*
* See LICENSE file in distribution for licensing details of this source file
*/
package com.jgraph.example.fastgraph;
import java.awt.Rectangle;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Map;
import org.jgraph.JGraph;
import org.jgraph.graph.AttributeMap;
import org.jgraph.graph.CellViewRenderer;
import org.jgraph.graph.Edge;
import org.jgraph.graph.EdgeRenderer;
import org.jgraph.graph.EdgeView;
import org.jgraph.graph.GraphConstants;
import org.jgraph.graph.GraphLayoutCache;
import org.jgraph.graph.GraphModel;
public class FastEdgeView extends EdgeView {
public CellViewRenderer getRenderer() {
return renderer;
}
/**
* @param cell
*/
public FastEdgeView(Object cell) {
super(cell);
}
/**
* Hook from AbstractCellView, avoid creating attribute map until as late
* as possible
*/
protected AttributeMap createAttributeMap() {
return AttributeMap.emptyAttributeMap;
}
/**
* If the model is a FastGraphModel check the undoDisabled flag. If set,
* copy the reference from the model attributes. This will cause the
* preview of a drag to edit in-place, but since undo is disabled this
* will only cause a performance improvement.
*/
protected AttributeMap getCellAttributes(GraphModel model) {
if (model instanceof FastGraphModel) {
if (((FastGraphModel)model).undoDisabled) {
return model.getAttributes(cell);
}
}
return (AttributeMap) model.getAttributes(cell).clone();
}
protected void mergeAttributes() {
}
public void setAttributes(AttributeMap attributes) {
if (allAttributes == null)
allAttributes = new AttributeMap(1, 1.0f);
this.allAttributes = attributes;
}
public Map changeAttributes(GraphLayoutCache cache, Map change) {
if (change != null) {
Map undo = allAttributes.applyMap(change);
update(cache);
return undo;
}
return null;
}
/**
* Returns the local renderer. Do not access the renderer field directly.
* Use this method instead. Note: This method is package private.
*/
private EdgeRenderer getFastEdgeRenderer() {
return (EdgeRenderer) getRenderer();
}
//
// View Methods
//
/**
* Returns true if this view intersects the given rectangle.
*/
public boolean intersects(JGraph graph, Rectangle2D rect) {
boolean intersects = false;
Rectangle2D bounds = getBounds();
if (bounds != null) {
intersects = bounds.intersects(rect);
}
if (intersects) {
Rectangle r = new Rectangle((int) rect.getX(), (int) rect.getY(),
(int) rect.getWidth(), (int) rect.getHeight());
return getFastEdgeRenderer().intersects(graph, this, r);
}
return false;
}
/**
* Update attributes and recurse children.
*/
public void update(GraphLayoutCache cache) {
// Save the reference to the points so they can be changed
// in-place by use of setPoint, setSource, setTarget methods.
points = GraphConstants.getPoints(allAttributes);
if (points == null) {
points = new ArrayList(2);
points.add(getAllAttributes().createPoint(10, 10));
points.add(getAllAttributes().createPoint(20, 20));
GraphConstants.setPoints(allAttributes, points);
}
checkDefaultLabelPosition();
Edge.Routing routing = GraphConstants.getRouting(allAttributes);
if (routing != null)
routing.route(cache, this);
// Clear cached shapes
beginShape = null;
endShape = null;
lineShape = null;
invalidateFastEdge();
}
private void invalidateFastEdge() {
labelVector = null;
sharedPath = null;
cachedBounds = null;
}
/**
* Returns the location for this portview.
*/
public Rectangle2D getBounds() {
Rectangle2D rect = null;
if (cachedBounds == null) {
String label = String.valueOf(getCell());
if (label != null) {
// If there is a label get the bounds from the renderer
cachedBounds = getFastEdgeRenderer().getBounds(this);
} else {
// Otherwise make simpe calculation for bounds
int n = points.size();
double minX, minY, maxX, maxY;
minX = maxX = getPoint(0).getX();
minY = maxY = getPoint(0).getY();
for (int i = 1; i < n; i++) {
Point2D currentPoint = getPoint(i);
double currentX = currentPoint.getX();
double currentY = currentPoint.getY();
if (currentX < minX) {
minX = currentX;
} else if (currentX > maxX) {
maxX = currentX;
}
if (currentY < minY) {
minY = currentY;
} else if (currentY > maxY) {
maxY = currentY;
}
}
cachedBounds = new Rectangle2D.Double(minX, minY, maxX-minX+1, maxY-minY+1);
}
}
rect = cachedBounds;
return rect;
}
}
|