f@0
|
1 /*
|
f@0
|
2 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
|
f@0
|
3
|
f@0
|
4 Copyright (C) 2002 Cay S. Horstmann (http://horstmann.com)
|
f@0
|
5 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
|
f@0
|
6
|
f@0
|
7 This program is free software: you can redistribute it and/or modify
|
f@0
|
8 it under the terms of the GNU General Public License as published by
|
f@0
|
9 the Free Software Foundation, either version 3 of the License, or
|
f@0
|
10 (at your option) any later version.
|
f@0
|
11
|
f@0
|
12 This program is distributed in the hope that it will be useful,
|
f@0
|
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
f@0
|
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
f@0
|
15 GNU General Public License for more details.
|
f@0
|
16
|
f@0
|
17 You should have received a copy of the GNU General Public License
|
f@0
|
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
|
f@0
|
19 */
|
f@0
|
20
|
f@0
|
21 package uk.ac.qmul.eecs.ccmi.simpletemplate;
|
f@0
|
22
|
f@0
|
23 import java.awt.Color;
|
f@0
|
24 import java.awt.Dimension;
|
f@0
|
25 import java.awt.Graphics2D;
|
f@0
|
26 import java.awt.geom.Point2D;
|
f@0
|
27 import java.awt.geom.Rectangle2D;
|
f@0
|
28
|
f@0
|
29 import javax.swing.JLabel;
|
f@0
|
30
|
f@0
|
31 import uk.ac.qmul.eecs.ccmi.diagrammodel.DiagramTreeNode;
|
f@0
|
32 import uk.ac.qmul.eecs.ccmi.gui.GraphPanel;
|
f@0
|
33
|
f@0
|
34
|
f@0
|
35 /**
|
f@0
|
36 * Provides static methods to draw a {@code SimpleShapeEdge} on a {@code Graphics}
|
f@0
|
37 *
|
f@0
|
38 */
|
f@0
|
39 public abstract class EdgeDrawSupport {
|
f@0
|
40 /**
|
f@0
|
41 * Draws a string in proximity of a edge.
|
f@0
|
42 * @param g2 the graphics context
|
f@0
|
43 * @param p an endpoint of the segment along which to
|
f@0
|
44 * draw the string
|
f@0
|
45 * @param q the other endpoint of the segment along which to
|
f@0
|
46 * draw the string
|
f@0
|
47 * @param arrow the arrow head painted on the edge end where the string
|
f@0
|
48 * is to be painted
|
f@0
|
49 * @param s the string to draw
|
f@0
|
50 * @param center true if the string should be centered
|
f@0
|
51 * along the segment
|
f@0
|
52 */
|
f@0
|
53
|
f@0
|
54 public static void drawString(Graphics2D g2,
|
f@0
|
55 Point2D p, Point2D q, ArrowHead arrow, String s, boolean center){
|
f@0
|
56 if (s == null || s.length() == 0) return;
|
f@0
|
57 label.setText("<html>" + s + "</html>");
|
f@0
|
58 label.setFont(g2.getFont());
|
f@0
|
59 Dimension d = label.getPreferredSize();
|
f@0
|
60 label.setBounds(0, 0, d.width, d.height);
|
f@0
|
61
|
f@0
|
62 Rectangle2D b = getStringBounds(g2, p, q, arrow, s, center);
|
f@0
|
63
|
f@0
|
64 Color oldColor = g2.getColor();
|
f@0
|
65 g2.setColor(g2.getBackground());
|
f@0
|
66 g2.fill(b);
|
f@0
|
67 g2.setColor(oldColor);
|
f@0
|
68
|
f@0
|
69 g2.translate(b.getX(), b.getY());
|
f@0
|
70 label.paint(g2);
|
f@0
|
71 g2.translate(-b.getX(), -b.getY());
|
f@0
|
72 }
|
f@0
|
73
|
f@0
|
74 /**
|
f@0
|
75 * Draws a graphical marker when the edge has notes associated to it
|
f@0
|
76 * @see uk.ac.qmul.eecs.ccmi.diagrammodel.TreeModel#setNotes(DiagramTreeNode, String, Object)
|
f@0
|
77 *
|
f@0
|
78 * @param g2 the graphics context
|
f@0
|
79 * @param p an endpoint of the segment along which to draw the string
|
f@0
|
80 * @param q the other endpoint of the segment along which to draw the string
|
f@0
|
81 */
|
f@0
|
82 public static void drawMarker(Graphics2D g2, Point2D p, Point2D q){
|
f@0
|
83 Point2D attach = q;
|
f@0
|
84 if (p.getX() > q.getX()){
|
f@0
|
85 drawMarker(g2, q, p);
|
f@0
|
86 return;
|
f@0
|
87 }
|
f@0
|
88 attach = new Point2D.Double((p.getX() + q.getX()) / 2,
|
f@0
|
89 (p.getY() + q.getY()) / 2);
|
f@0
|
90 Color oldColor = g2.getColor();
|
f@0
|
91 g2.setColor(GraphPanel.GRABBER_COLOR);
|
f@0
|
92 g2.fill(new Rectangle2D.Double(attach.getX() - MARKER_SIZE / 2, attach.getY() - MARKER_SIZE / 2, MARKER_SIZE, MARKER_SIZE));
|
f@0
|
93 g2.setColor(oldColor);
|
f@0
|
94 }
|
f@0
|
95
|
f@0
|
96 /*
|
f@0
|
97 Computes the attachment point for drawing a string.
|
f@0
|
98 return the point at which to draw the string
|
f@0
|
99 */
|
f@0
|
100 private static Point2D getAttachmentPoint(Graphics2D g2,
|
f@0
|
101 Point2D p, Point2D q, ArrowHead arrow, Dimension d, boolean center){
|
f@0
|
102 final int GAP = 3;
|
f@0
|
103 double xoff = GAP;
|
f@0
|
104 double yoff = -GAP - d.getHeight();
|
f@0
|
105 Point2D attach = q;
|
f@0
|
106 if (center){
|
f@0
|
107 if (p.getX() > q.getX()){
|
f@0
|
108 return getAttachmentPoint(g2, q, p, arrow, d, center);
|
f@0
|
109 }
|
f@0
|
110 attach = new Point2D.Double((p.getX() + q.getX()) / 2,
|
f@0
|
111 (p.getY() + q.getY()) / 2);
|
f@0
|
112 if (p.getY() < q.getY())
|
f@0
|
113 yoff = - GAP - d.getHeight();
|
f@0
|
114 else if (p.getY() == q.getY())
|
f@0
|
115 xoff = -d.getWidth() / 2;
|
f@0
|
116 else
|
f@0
|
117 yoff = GAP;
|
f@0
|
118 }
|
f@0
|
119 else
|
f@0
|
120 {
|
f@0
|
121 if (p.getX() < q.getX()){
|
f@0
|
122 xoff = -GAP - d.getWidth();
|
f@0
|
123 }
|
f@0
|
124 if (p.getY() > q.getY()){
|
f@0
|
125 yoff = GAP;
|
f@0
|
126 }
|
f@0
|
127 if (arrow != null){
|
f@0
|
128 Rectangle2D arrowBounds = arrow.getPath(p, q).getBounds2D();
|
f@0
|
129 if (p.getX() < q.getX()){
|
f@0
|
130 xoff -= arrowBounds.getWidth();
|
f@0
|
131 }
|
f@0
|
132 else{
|
f@0
|
133 xoff += arrowBounds.getWidth();
|
f@0
|
134 }
|
f@0
|
135 }
|
f@0
|
136 }
|
f@0
|
137 return new Point2D.Double(attach.getX() + xoff, attach.getY() + yoff);
|
f@0
|
138 }
|
f@0
|
139
|
f@0
|
140 /*
|
f@0
|
141 * Computes the extent of a string that is drawn along a line segment.
|
f@0
|
142 * The rectangle enclosing the string
|
f@0
|
143 */
|
f@0
|
144 private static Rectangle2D getStringBounds(Graphics2D g2,
|
f@0
|
145 Point2D p, Point2D q, ArrowHead arrow, String s, boolean center){
|
f@0
|
146 if (g2 == null) return new Rectangle2D.Double();
|
f@0
|
147 if (s == null || s.equals("")) return new Rectangle2D.Double(q.getX(), q.getY(), 0, 0);
|
f@0
|
148 label.setText("<html>" + s + "</html>");
|
f@0
|
149 label.setFont(g2.getFont());
|
f@0
|
150 Dimension d = label.getPreferredSize();
|
f@0
|
151 Point2D a = getAttachmentPoint(g2, p, q, arrow, d, center);
|
f@0
|
152 return new Rectangle2D.Double(a.getX(), a.getY(), d.getWidth(), d.getHeight());
|
f@0
|
153 }
|
f@0
|
154
|
f@0
|
155 /* size of the marker when an edge as notes */
|
f@0
|
156 private static final int MARKER_SIZE = 7;
|
f@0
|
157 private static JLabel label = new JLabel();
|
f@0
|
158 }
|