001: package org.enhydra.jawe.components.graph;
002:
003: import java.awt.BasicStroke;
004: import java.awt.Color;
005: import java.awt.Component;
006: import java.awt.Font;
007: import java.awt.geom.Point2D;
008: import java.awt.geom.Rectangle2D;
009: import java.util.Map;
010:
011: import javax.swing.ImageIcon;
012: import javax.swing.UIManager;
013: import javax.swing.border.Border;
014:
015: import org.jgraph.JGraph;
016: import org.jgraph.graph.CellView;
017: import org.jgraph.graph.CellViewRenderer;
018: import org.jgraph.graph.GraphConstants;
019: import org.jgraph.graph.VertexView;
020:
021: /**
022: * @author Sasa Bojanic
023: * @author Miroslav Popov
024: */
025: public class MultiLinedRenderer extends DefaultCellPanel implements
026: CellViewRenderer {
027:
028: public static boolean loaded = false;
029:
030: private static float borderWidth = 1f;
031: protected BasicStroke borderStroke = new BasicStroke(borderWidth,
032: BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND);
033:
034: /** Cache the current graph for drawing. */
035: transient protected JGraph graph;
036:
037: /** Cache the current shape for drawing. */
038: transient protected VertexView view;
039:
040: /** Cached hasFocus and selected value. */
041: transient protected boolean hasFocus, selected, preview, opaque;
042:
043: /** Cached default foreground and default background. */
044: transient protected Color defaultForeground, defaultBackground,
045: bordercolor;
046:
047: /** Cached value of the double buffered state */
048: transient boolean isDoubleBuffered = false;
049:
050: public MultiLinedRenderer() {
051: defaultForeground = UIManager.getColor("Tree.textForeground");
052: defaultBackground = UIManager.getColor("Tree.textBackground");
053: }
054:
055: public Component getRendererComponent(JGraph pGgraph,
056: CellView pView, boolean pSel, boolean pFocus,
057: boolean pPreview) {
058: this .graph = pGgraph;
059: wrapName(GraphUtilities.getGraphController().getGraphSettings()
060: .isNameWrappingEnabled());
061: wrapStyle(GraphUtilities.getGraphController()
062: .getGraphSettings().isWordWrappingEnabled());
063:
064: isDoubleBuffered = graph.isDoubleBuffered();
065: if (pView instanceof VertexView) {
066: this .view = (VertexView) pView;
067: setMainIcon(getIcon());
068: setTextPosition(GraphUtilities.getGraphController()
069: .getGraphSettings().getTextPos());
070: showIcon(GraphUtilities.getGraphController()
071: .getGraphSettings().shouldShowIcons());
072:
073: setDisplayName(view.getCell().toString());
074:
075: if (graph.getEditingCell() != view.getCell()) {
076: Object label = graph.convertValueToString(view);
077: if (label != null)
078: setDisplayName(label.toString());
079: else
080: setDisplayName(null);
081: } else
082: setDisplayName(null);
083: this .hasFocus = pFocus;
084: this .selected = pSel;
085: this .preview = pPreview;
086: Map attributes = view.getAllAttributes();
087: installAttributes(graph, attributes);
088: return this ;
089: }
090:
091: return null;
092: }
093:
094: protected void installAttributes(JGraph pGraph, Map pAttributes) {
095: setOpaque(GraphConstants.isOpaque(pAttributes));
096: Color foreground = GraphConstants.getForeground(pAttributes);
097: setForeground((foreground != null) ? foreground : pGraph
098: .getForeground());
099: Color background = GraphConstants.getBackground(pAttributes);
100: setBackground((background != null) ? background : pGraph
101: .getBackground());
102: Font font = GraphConstants.getFont(pAttributes);
103: setFont((font != null) ? font : pGraph.getFont());
104: Border border = GraphConstants.getBorder(pAttributes);
105: bordercolor = GraphConstants.getBorderColor(pAttributes);
106: if (border != null)
107: setBorder(border);
108: else if (bordercolor != null) {
109: borderWidth = Math.max(1, Math.round(GraphConstants
110: .getLineWidth(pAttributes)));
111: }
112: }
113:
114: public ImageIcon getIcon() {
115: return GraphUtilities.getGraphController().getGraphSettings()
116: .getDefaultActivityIcon();
117: }
118:
119: public Point2D getPerimeterPoint(VertexView pView, Point2D p) {
120: Rectangle2D bounds = pView.getBounds();
121: double x = bounds.getX();
122: double y = bounds.getY();
123: double width = bounds.getWidth();
124: double height = bounds.getHeight();
125: double xCenter = x + width / 2;
126: double yCenter = y + height / 2;
127: double dx = p.getX() - xCenter; // Compute Angle
128: double dy = p.getY() - yCenter;
129: double alpha = Math.atan2(dy, dx);
130: double xout = 0, yout = 0;
131: double pi = Math.PI;
132: double pi2 = Math.PI / 2.0;
133: double beta = pi2 - alpha;
134: double t = Math.atan2(height, width);
135: if (alpha < -pi + t || alpha > pi - t) { // Left edge
136: xout = x;
137: yout = yCenter - width * Math.tan(alpha) / 2;
138: } else if (alpha < -t) { // Top Edge
139: yout = y;
140: xout = xCenter - height * Math.tan(beta) / 2;
141: } else if (alpha < t) { // Right Edge
142: xout = x + width;
143: yout = yCenter + width * Math.tan(alpha) / 2;
144: } else { // Bottom Edge
145: yout = y + height;
146: xout = xCenter + height * Math.tan(beta) / 2;
147: }
148: return new Point2D.Double(xout, yout);
149: }
150: }
|