1 package org.coach.tracing.server.viewer; 2 3 import javax.swing.*; 4 import java.awt.*; 5 import java.awt.geom.*; 6 7 public class MessageGui extends JComponent { 8 public final static Integer NW_SE = new Integer (0); 9 public final static Integer NE_SW = new Integer (1); 10 public final static Integer SE_NW = new Integer (2); 11 public final static Integer SW_NE = new Integer (3); 12 public final static Integer UP = new Integer (4); 13 public final static Integer DOWN = new Integer (5); 14 public final static int arrow_size = 6; 15 public final static int delta = 2; 16 public final static Color normal_color = Color.blue; 18 public final static Color trail_color = Color.magenta; 19 public final static Color text_color = Color.black; 20 private static Diagram view; 21 private static ScaleManager sm; 22 23 private EventGui start; 24 private EventGui end; 25 private Integer orientation = null; 26 private Point[] arrow_head = new Point[3]; 27 private Polygon poly = null; 29 private Dimension text_dimension = null; 30 private Point text_origin = null; 31 private String text = ""; 32 private int identityGuiWidth; 33 34 public MessageGui(Diagram v, EventGui e1, EventGui e2, int identityGuiWidth) { 35 super(); 36 if (e1.getPosition() < e2.getPosition()) { 37 start = e1; 38 end = e2; 39 } else { 40 start = e2; 41 end = e1; 42 } 43 this.identityGuiWidth = identityGuiWidth; 44 if (view == null) { 45 view = v; 46 sm = view.getScaleManager(); 47 } 48 text = start.getOpName(); 49 setLocation(calcOrigin(start.getCenter(), end.getCenter(), start.getRadius())); 50 setSize(calcDimension(start.getCenter(), end.getCenter(), start.getRadius())); 51 orientation = calcOrientation(start.getCenter(), end.getCenter()); 52 poly = calcPolygon(start.getCenter(), end.getCenter()); 53 calcArrow(arrow_head); 54 calcText(); 55 ToolTipManager.sharedInstance().registerComponent(this); 56 setToolTipText(text); 57 } 58 59 public void paint(Graphics g) { 60 super.paint(g); 62 float scale = sm.getScale(); 63 Graphics2D g2 = (Graphics2D)g; 64 g2.setRenderingHints(sm.getHints()); 65 66 if (start.isReturn()) { 68 g2.setStroke(sm.getStrokeDashed()); 69 } else { 70 g2.setStroke(sm.getStroke()); 71 } 73 int x1 = 0; 74 int y1 = 0; 75 int x2 = 0; 76 int y2 = 0; 77 78 if ((orientation == NW_SE) || (orientation == SE_NW)) { 79 x2 = getWidth() - 1; 80 y2 = getH() - 1; 81 } 82 83 if ((orientation == NE_SW) || (orientation == SW_NE)) { 84 y1 = getH() - 1; 85 x2 = getWidth() - 1; 86 } 87 88 if (start.getColor() != null && end.getColor() != null) { 90 g2.setColor(start.getColor()); 91 } else { 92 g2.setColor(normal_color); 93 } 94 95 if ((orientation == UP) || (orientation == DOWN)) { 96 g2.draw(new Line2D.Float(0, 0, (getWidth() - 1) / 2, 0)); 98 g2.draw(new Line2D.Float((getWidth() - 1) / 2, 0, (getWidth() - 1) / 2, getH() - 1)); 99 g2.draw(new Line2D.Float(0, getH() - 1, (getWidth() - 1) / 2, getH() - 1)); 100 } else { 101 g2.draw(new Line2D.Float(x1, y1, x2, y2)); 103 } 104 g2.setStroke(sm.getStroke()); 105 106 if (arrow_head[0] != null) { 108 g2.draw(new Line2D.Float(arrow_head[0].x, arrow_head[0].y, arrow_head[1].x, arrow_head[1].y)); 109 g2.draw(new Line2D.Float(arrow_head[0].x, arrow_head[0].y, arrow_head[2].x, arrow_head[2].y)); 110 } 111 g2.setFont(sm.getFont()); 113 g2.setColor(Color.white); 114 String t = text; 115 t = getTrimmedText(text, g2); 116 g2.setColor(text_color); 117 g2.drawString(t, text_origin.x, text_origin.y); 118 } 119 120 private void calcText() { 121 double W = getWidth(); 122 double H = getH(); 123 text_dimension = sm.getStringDimension(text + "_", this); 124 double x = (W - text_dimension.width) / 2; 125 126 if (orientation == DOWN || orientation == UP) { 127 x = 0; 128 } 129 double y = H / 2 + text_dimension.height / 3; 130 131 if (x < 0) { 133 x = 0; 134 } 135 text_origin = new Point((int)x, (int)y); 136 } 137 138 private String getTrimmedText(String s, Graphics2D g) { 139 Dimension td = sm.getStringDimension(s, g); 140 double W = getWidth(); 141 142 if (td.width > W && (s.length() > 0)) { 143 while (td.width > W && (s.length() > 0)) { 144 s = s.substring(0, s.length() - 1); 145 td = sm.getStringDimension(s + "____", this); 147 } 149 s += "..."; 150 } 151 return s; 152 } 153 154 private void calcArrow(Point[] pl) { 155 float scale = sm.getScale(); 156 double px, py; 157 double x1, y1; 159 double x2, y2; 161 double W = getWidth(); 163 double H = getH(); 164 double Z = Math.sqrt(W * W + H * H); 165 double sin_a = H / Z; 166 double cos_a = W / Z; 167 double sin_30 = 0.5; 168 double cos_30 = Math.sqrt(3) / 2; 169 double sin_a_minus_30 = sin_a * cos_30 - cos_a * sin_30; 171 double cos_a_minus_30 = cos_a * cos_30 + sin_a * sin_30; 173 double sin_a_plus_30 = sin_a * cos_30 + cos_a * sin_30; 175 double cos_a_plus_30 = cos_a * cos_30 + sin_a * sin_30; 177 x1 = (arrow_size + 1) * scale * cos_a_minus_30; 178 y1 = (arrow_size + 1) * scale * sin_a_minus_30; 179 x2 = arrow_size * scale * cos_a_plus_30; 180 y2 = arrow_size * scale * sin_a_plus_30; 181 182 if (orientation == NW_SE) { 183 pl[0] = new Point((int)(W - 1), (int)(H - 1)); 184 pl[1] = new Point((int)(W - x1 - 1), (int)(H - y1 - 1)); 185 pl[2] = new Point((int)(W - x2 - 1), (int)(H - y2 - 1)); 186 } 187 188 if (orientation == NE_SW) { 189 pl[0] = new Point(0, (int)(H - 1)); 190 pl[1] = new Point((int)(x1), (int)(H - y1 - 1)); 191 pl[2] = new Point((int)(x2), (int)(H - y2 - 1)); 192 } 193 194 if (orientation == DOWN) { 195 pl[0] = new Point(0, (int)(H - 1)); 196 pl[1] = new Point((int)((scale * arrow_size * Math.sqrt(3) / 2)), (int)(H + (scale * arrow_size / 2) - 1)); 197 pl[2] = new Point((int)((scale * arrow_size * Math.sqrt(3) / 2)), (int)(H - (scale * arrow_size / 2) - 1)); 198 } 199 200 if (orientation == UP) { 202 pl[0] = null; 203 pl[1] = null; 204 pl[2] = null; 205 } 206 207 if (orientation == SW_NE) { 208 pl[0] = null; 209 pl[1] = null; 210 pl[2] = null; 211 } 212 213 if (orientation == SE_NW) { 214 pl[0] = null; 215 pl[1] = null; 216 pl[2] = null; 217 } 218 } 219 220 private Point calcOrigin(Point p1, Point p2, int r) { 221 double W = Math.abs(p2.x - p1.x); 223 224 if (W < 3) { 225 return new Point((int)(Math.min(p1.x, p2.x) + r), (int)Math.min(p1.y, p2.y)); 226 } else { 227 double H = Math.abs(p2.y - p1.y); 228 double Z = Math.sqrt(W * W + H * H); 229 double rx = r * W / Z; 230 double ry = r * H / Z; 231 return new Point((int)(Math.min(p1.x, p2.x) + rx + 2), (int)(Math.min(p1.y, p2.y) + ry)); 232 } 233 } 234 235 private Dimension calcDimension(Point p1, Point p2, int r) { 236 float scale = sm.getScale(); 238 double W = Math.abs(p2.x - p1.x); 239 240 if (W < 3) { 241 243 int ww = identityGuiWidth; 244 return new Dimension(ww, (int)(Math.abs(p1.y - p2.y) + (scale * arrow_size / 2 + 1))); 245 } else { 247 double H = Math.abs(p2.y - p1.y); 248 double Z = Math.sqrt(W * W + H * H); 249 double rx = r * W / Z; 250 double ry = r * H / Z; 251 return new Dimension((int)(W - (2 * rx)), (int)((H - 2 * ry) + (scale * arrow_size / 2 + 1))); 253 } 254 } 255 256 private Integer calcOrientation(Point p1, Point p2) { 257 258 if (p1.x < p2.x) { 260 261 if (p1.y < p2.y) { 263 return NW_SE; 265 } else { 266 return SW_NE; 268 } 269 } else { 270 271 if ((p1.x - p2.x) < 3) { 273 274 if (p1.y < p2.y) { 276 return DOWN; 278 } else { 279 return UP; 281 } 282 } 283 { 284 285 if (p1.y < p2.y) { 287 return NE_SW; 289 } else { 290 return SE_NW; 292 } 293 } 294 } 295 } 296 297 private Polygon calcPolygon(Point p1, Point p2) { 298 Polygon p = new Polygon(); 299 int x1 = 0; 301 int y1 = 0; 302 int x2 = 0; 303 int y2 = 0; 304 305 if ((orientation == NW_SE) || (orientation == SE_NW)) { 306 x2 = getWidth() - 1; 307 y2 = getH() - 1; 308 } 309 310 if ((orientation == NE_SW) || (orientation == SW_NE)) { 311 y1 = getH() - 1; 312 x2 = getWidth() - 1; 313 } 314 315 if ((orientation == UP) || (orientation == DOWN)) { 316 p.addPoint(0, -delta); 317 p.addPoint((getWidth() - 1) / 2 + delta, -delta); 318 p.addPoint((getWidth() - 1) / 2 + delta, getH() - 1 + delta); 319 p.addPoint(0, getH() - 1 + delta); 320 p.addPoint(0, getH() - 1 - delta); 321 p.addPoint((getWidth() - 1) / 2 - delta, getH() - 1 - delta); 322 p.addPoint((getWidth() - 1) / 2 - delta, +delta); 323 p.addPoint(0, +delta); 324 } else { 325 float scale = sm.getScale(); 326 double W = getWidth() - 1; 327 double H = getH() - 1; 328 double Z = Math.sqrt(W * W + H * H); 329 double sin_a = H / Z; 330 double cos_a = W / Z; 331 double dx = (delta * scale) * sin_a; 332 double dy = (delta * scale) * cos_a; 333 334 if ((orientation == NW_SE) || (orientation == SE_NW)) { 335 p.addPoint((int)(x1 - dx), (int)(y1 + dy)); 336 p.addPoint((int)(x1 + dx), (int)(y1 - dy)); 337 p.addPoint((int)(x2 + dx), (int)(y2 - dy)); 338 p.addPoint((int)(x2 - dx), (int)(y2 + dy)); 339 } 340 341 if ((orientation == NE_SW) || (orientation == SW_NE)) { 342 p.addPoint((int)(x1 - dx), (int)(y1 - dy)); 343 p.addPoint((int)(x1 + dx), (int)(y1 + dy)); 344 p.addPoint((int)(x2 + dx), (int)(y2 + dy)); 345 p.addPoint((int)(x2 - dx), (int)(y2 - dy)); 346 } 347 } 348 return p; 349 } 350 351 public int getH() { 352 float scale = sm.getScale(); 353 return (int)(getHeight() - (scale * arrow_size / 2)); 354 } 355 } 356 | Popular Tags |