1 30 package com.genimen.djeneric.tools.modeler.diagrammer; 31 32 import java.awt.BasicStroke ; 33 import java.awt.Color ; 34 import java.awt.FontMetrics ; 35 import java.awt.Graphics2D ; 36 import java.awt.Rectangle ; 37 import java.awt.Shape ; 38 import java.awt.Stroke ; 39 import java.awt.geom.AffineTransform ; 40 import java.awt.geom.Rectangle2D ; 41 import java.util.Arrays ; 42 import java.util.HashMap ; 43 44 import com.genimen.djeneric.repository.DjExtent; 45 import com.genimen.djeneric.repository.DjProperty; 46 import com.genimen.djeneric.repository.DjRelation; 47 48 public class ExtentViewer extends DiagramObject 49 { 50 DjExtent _extent; 51 52 Color _extentColor = new Color (58, 110, 165); 53 Color _columnColor = Color.lightGray; 54 Color _nameColor = Color.white; 55 Color BOXFRAME_COLOR = new Color (130, 130, 130); 56 public final static Color _lighterGray = new Color (230, 230, 230); 57 58 final static int CORNER_SIZE = 11; 59 final static int COLUMN_MARGIN = 12; 60 final static int TYPE_MARGIN = 120; 61 final static int COLUMN_Y_OFFSET = 40; 62 63 static float dash[] = {4.0f}; 64 static BasicStroke _dashStroke = new BasicStroke (1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 65 10.0f, dash, 0.0f); 66 67 int _relsCountTop = 0; 68 int _relsCountBottom = 0; 69 70 public ExtentViewer(DjExtent extent) 71 { 72 _extent = extent; 73 } 74 75 public void setExtentColor(Color c) 76 { 77 _extentColor = c; 78 } 79 80 public void setNameColor(Color c) 81 { 82 _nameColor = c; 83 } 84 85 public void setPropertyColor(Color c) 86 { 87 _columnColor = c; 88 } 89 90 public Color getExtentColor() 91 { 92 return _extentColor; 93 } 94 95 public Color getNameColor() 96 { 97 return _nameColor; 98 } 99 100 public Color getPropertyColor() 101 { 102 return _columnColor; 103 } 104 105 public DjExtent getExtent() 106 { 107 return _extent; 108 } 109 110 public void setExtent(DjExtent extent) 111 { 112 _extent = extent; 113 } 114 115 public final int getCorner(int x, int y) 116 { 117 if ((x > getX()) && (x < getX() + CORNER_SIZE) && (y > getY()) && (y < getY() + CORNER_SIZE)) return 1; 118 if ((x > getX2() - CORNER_SIZE) && (x < getX2()) && (y > getY()) && (y < getY() + CORNER_SIZE)) return 2; 119 if ((x > getX()) && (x < getX() + CORNER_SIZE) && (y > getY2() - CORNER_SIZE) && (y < getY2())) return 3; 120 if ((x > getX2() - CORNER_SIZE) && (x < getX2()) && (y > getY2() - CORNER_SIZE) && (y < getY2())) return 4; 121 122 return -1; 123 } 124 125 protected final void drawRecursiveRelation(Graphics2D g2D, AffineTransform afx, ExtentViewer master, 126 DjRelation currentRelation, int counter) 127 { 128 String relName = currentRelation.getName(); 129 130 relName += " (" + currentRelation.getDetailProperty().getName() + ")"; 131 132 int s1 = 5; 133 int s2 = s1 * 2; 134 135 int step = 20; 136 137 int x1 = getX2() - step * counter; 138 int y1 = getY(); 139 140 if (x1 < getX()) x1 = getX(); 141 142 int x2 = x1; 143 int y2 = getY() - step * counter; 144 145 int x3 = getX2() + step * counter; 146 int y3 = y2; 147 148 int x4 = x3; 149 int y4 = getY() + step * counter; 150 151 if (y4 > getY2()) y4 = getY2(); 152 153 int x5 = getX2(); 154 int y5 = y4; 155 156 g2D.drawString(relName, x2, y2 - 7); 157 158 g2D.drawString(currentRelation.isOneToMany() ? "*" : "1", x5 + 10, y5 - 2); 159 160 if (!currentRelation.isDetailsContained()) 161 { 162 Stroke org = g2D.getStroke(); 163 if (!currentRelation.getDetailProperty().isRequired()) g2D.setStroke(_dashStroke); 164 165 g2D.drawLine(x1, y1, x2, y2); 166 g2D.drawLine(x2, y2, x3, y3); 167 g2D.drawLine(x3, y3, x4, y4); 168 g2D.drawLine(x4, y4, x5, y5); 169 g2D.setStroke(org); 170 } 171 else 172 { 173 Stroke org = g2D.getStroke(); 174 if (!currentRelation.getDetailProperty().isRequired()) g2D.setStroke(_dashStroke); 175 176 g2D.drawLine(x1, y1 - s2, x2, y2); 177 g2D.drawLine(x2, y2, x3, y3); 178 g2D.drawLine(x3, y3, x4, y4); 179 g2D.drawLine(x4, y4, x5, y5); 180 181 g2D.setStroke(org); 182 183 g2D.drawLine(x1, y1, x1 - s1, y1 - s1); 184 g2D.drawLine(x1 - s1, y1 - s1, x1, y1 - s2); 185 g2D.drawLine(x1, y1 - s2, x1 + s1, y1 - s1); 186 g2D.drawLine(x1 + s1, y1 - s1, x1, y1); 187 } 188 } 189 190 protected final void drawNormalRelation(Graphics2D g2D, AffineTransform afx, ExtentViewer master, 191 DjRelation currentRelation, HashMap horizontalMastersDone, 192 HashMap verticalMastersDone) 193 { 194 int drawingOffset = 0; 195 196 String relName = currentRelation.getName(); 197 relName += " (" + currentRelation.getDetailProperty().getName() + ")"; 198 199 FontMetrics fontMetrics = g2D.getFontMetrics(_editor._font); 200 Rectangle2D r = fontMetrics.getStringBounds(relName, _editor.getPaintPanel().getGraphics()); 201 int nameWidth = (int) (r.getWidth()) + 20; 202 int nameHeight = (int) (r.getHeight()); 203 LineSpecifics ls = new LineSpecifics(master, this, 0); 204 205 if (!ls.horizontal) 206 { 207 String key1 = master.getExtent().getName(); 210 String key2 = getExtent().getName(); 211 String key; 212 213 if (key1.compareTo(key2) <= 0) key = ls.horizontal + key1 + key2; 214 else key = ls.horizontal + key2 + key1; 215 216 if (verticalMastersDone.containsKey(key)) 217 { 218 int count = ((Integer ) verticalMastersDone.get(key)).intValue() + 1; 219 verticalMastersDone.put(key, new Integer (count)); 220 drawingOffset += count * nameHeight; 221 } 222 else 223 { 224 verticalMastersDone.put(key, new Integer (0)); 225 } 226 227 ls = new LineSpecifics(master, this, drawingOffset); 228 229 drawingOffset = 10; 230 231 key = "*" + ls.direction + key1; 233 if (verticalMastersDone.containsKey(key)) 234 { 235 int count = ((Integer ) verticalMastersDone.get(key)).intValue() + 1; 236 verticalMastersDone.put(key, new Integer (count)); 237 drawingOffset += count * nameHeight; 238 } 239 else 240 { 241 verticalMastersDone.put(key, new Integer (0)); 242 } 243 } 244 245 int x1 = ls.startX1 + getX(); 246 int y1 = ls.startY1 + getY(); 247 int x2 = ls.startX2 + master.getX(); 248 int y2 = ls.startY2 + master.getY(); 249 boolean horizontal = ls.horizontal; 250 251 if (horizontal) 252 { 253 255 String key1 = master.getExtent().getName(); 258 String key2 = getExtent().getName(); 259 String key; 260 261 if (key1.compareTo(key2) <= 0) key = ls.horizontal + key1 + key2; 262 else key = ls.horizontal + key2 + key1; 263 264 if (horizontalMastersDone.containsKey(key)) 265 { 266 int count = ((Integer ) horizontalMastersDone.get(key)).intValue() + 1; 267 horizontalMastersDone.put(key, new Integer (count)); 268 drawingOffset += count * nameHeight; 269 } 270 else 271 { 272 horizontalMastersDone.put(key, new Integer (0)); 273 } 274 275 y1 += drawingOffset; 276 y2 += drawingOffset; 277 278 int s1 = 5; 279 if (x1 > x2) 280 { 281 s1 *= -1; 282 nameWidth = -20; 283 } 284 285 int s2 = s1 * 2; 286 287 g2D.drawString(relName, x2 - nameWidth, y2 - 4); 288 289 g2D.drawString(currentRelation.isOneToMany() ? "*" : "1", x1 + s1, y1 - 2); 290 291 if (!currentRelation.isDetailsContained()) 292 { 293 Stroke org = g2D.getStroke(); 294 if (!currentRelation.getDetailProperty().isRequired()) g2D.setStroke(_dashStroke); 295 g2D.drawLine(x1, y1, x2, y2); 296 g2D.setStroke(org); 297 } 298 else 299 { 300 Stroke org = g2D.getStroke(); 301 if (!currentRelation.getDetailProperty().isRequired()) g2D.setStroke(_dashStroke); 302 g2D.drawLine(x2 - s2, y2, x1, y1); 303 g2D.setStroke(org); 304 305 g2D.drawLine(x2 - s2, y2, x2 - s1, y2 - s1); 306 g2D.drawLine(x2 - s1, y2 - s1, x2, y2); 307 g2D.drawLine(x2 - s2, y2, x2 - s1, y2 + s1); 308 g2D.drawLine(x2 - s1, y2 + s1, x2, y2); 309 } 310 } 311 else 312 { 313 315 int s1 = 5; 316 int starHeight = nameHeight; 317 318 if (y1 > y2) 319 { 320 s1 *= -1; 321 nameHeight *= -1; 322 starHeight = -2; 323 drawingOffset *= -1; 324 } 325 326 int s2 = s1 * 2; 327 328 g2D.drawString(relName, x2 + 4, (y2 - nameHeight) - drawingOffset); 329 g2D.drawString(currentRelation.isOneToMany() ? "*" : "1", x1 - s1, y1 + starHeight); 330 331 if (!currentRelation.isDetailsContained()) 332 { 333 Stroke org = g2D.getStroke(); 334 if (!currentRelation.getDetailProperty().isRequired()) g2D.setStroke(_dashStroke); 335 g2D.drawLine(x1, y1, x2, y2); 336 g2D.setStroke(org); 337 } 338 else 339 { 340 Stroke org = g2D.getStroke(); 341 if (!currentRelation.getDetailProperty().isRequired()) g2D.setStroke(_dashStroke); 342 g2D.drawLine(x2, y2 - s2, x1, y1); 343 g2D.setStroke(org); 344 345 g2D.drawLine(x2, y2, x2 - s1, y2 - s1); 346 g2D.drawLine(x2 - s1, y2 - s1, x2, y2 - s2); 347 g2D.drawLine(x2, y2 - s2, x2 + s1, y2 - s1); 348 g2D.drawLine(x2 + s1, y2 - s1, x2, y2); 349 } 350 } 351 } 352 353 public final void paintInheritance(Graphics2D g2D, AffineTransform afx) 354 { 355 DjExtent superExtent = _extent.getSuper(); 356 if (superExtent == null) return; 357 358 ExtentViewer superViewer = _editor.getExtentViewer(superExtent); 359 if (superViewer == null) return; 361 362 LineSpecifics ls = new LineSpecifics(superViewer, this, 0); 363 364 int x1 = ls.startX1 + getX(); 365 int y1 = ls.startY1 + getY(); 366 int x2 = ls.startX2 + superViewer.getX(); 367 int y2 = ls.startY2 + superViewer.getY(); 368 boolean horizontal = ls.horizontal; 369 370 g2D.setTransform(afx); 371 g2D.setPaint(Color.black); 372 373 int s = 8; 374 if (horizontal) 375 { 376 y1 += 5; 377 y2 += 5; 378 if (x1 > x2) s *= -1; 379 380 g2D.drawLine(x2 - s, y2, x1, y1); 381 382 g2D.drawLine(x2 - s, y2 - s, x2, y2); 383 g2D.drawLine(x2 - s, y2 + s, x2, y2); 384 g2D.drawLine(x2 - s, y2 - s, x2 - s, y2 + s); 385 } 386 else 387 { 388 x1 += 5; 389 x2 += 5; 390 if (y2 > y1) s *= -1; 391 392 g2D.drawLine(x2, y2 + s, x1, y1); 393 394 g2D.drawLine(x2 - s, y2 + s, x2, y2); 395 g2D.drawLine(x2 + s, y2 + s, x2, y2); 396 g2D.drawLine(x2 - s, y2 + s, x2 + s, y2 + s); 397 } 398 399 g2D.setTransform(afx); 400 } 401 402 public final void paintRelations(Graphics2D g2D, AffineTransform afx, HashMap horizontalMastersDone, 403 HashMap verticalMastersDone) 404 { 405 g2D.setTransform(afx); 406 g2D.setPaint(Color.black); 407 408 int recursiveRelationCounter = 1; 409 410 DjRelation[] rels = _extent.getPersonalMasterRelations(); 411 for (int i = 0; i < rels.length; i++) 412 { 413 DjRelation currentRelation = rels[i]; 414 ExtentViewer master = _editor.getExtentViewer(currentRelation.getMasterExtent()); 415 if (master != null && master.getExtent() == getExtent()) 416 { 417 drawRecursiveRelation(g2D, afx, master, currentRelation, recursiveRelationCounter++); 419 } 420 else if (master != null) 421 { 422 drawNormalRelation(g2D, afx, master, currentRelation, horizontalMastersDone, verticalMastersDone); 424 } 425 } 426 g2D.setTransform(afx); 427 } 428 429 public void paint(Graphics2D g2D, AffineTransform afx, HashMap horizontalMastersDone, HashMap verticalMastersDone, 430 int viewMode) 431 { 432 g2D.setPaint(_extentColor); 433 434 Rectangle rect = new Rectangle (); 435 rect.setFrame(getX(), getY(), getWidth(), getHeight()); 436 437 Shape orgClip = g2D.getClip(); 438 439 Rectangle clipRect = new Rectangle (); 440 clipRect.setFrame(getX(), getY(), getWidth() + 1, getHeight() + 1); 441 g2D.setClip(clipRect); 442 443 g2D.fill(rect); 444 g2D.draw(rect); 445 446 g2D.setColor(BOXFRAME_COLOR); 447 g2D.draw(rect); 448 449 g2D.setTransform(afx); 450 451 g2D.setColor(_nameColor); 452 g2D.setFont(_editor._largerFont); 453 454 if (viewMode == PaintPanel.SHOW_ALIAS) g2D.drawString(_extent.getAlias(), getX() + CORNER_SIZE + 2, 455 getY() + _editor._largerFont.getSize() + 2); 456 else if (viewMode == PaintPanel.SHOW_CLASS) g2D.drawString(_extent.getObjectType(), getX() + CORNER_SIZE + 2, 457 getY() + _editor._largerFont.getSize() + 2); 458 else if (viewMode == PaintPanel.SHOW_EXTENT) g2D.drawString(_extent.getName(), getX() + CORNER_SIZE + 2, 459 getY() + _editor._largerFont.getSize() + 2); 460 else if (viewMode == PaintPanel.SHOW_QUALIFIED) g2D.drawString(_extent.getQualifiedObjectType(), getX() 461 + CORNER_SIZE + 2, 462 getY() + _editor._largerFont.getSize() + 2); 463 464 g2D.setFont(_editor._smallfont); 465 g2D.setColor(_columnColor); 466 467 g2D.setFont(_editor._font); 468 g2D.setColor(_columnColor); 469 Rectangle colClipRect = new Rectangle (); 470 471 int maxWidth = TYPE_MARGIN - 4; 472 if (maxWidth > getWidth()) maxWidth = getWidth(); 473 colClipRect.setFrame(getX(), getY(), maxWidth, getHeight() + 1); 474 475 int fSize = _editor._font.getSize(); 476 DjProperty[] props = _extent.getPersonalProperties(); 477 for (int i = 0; i < props.length; i++) 478 { 479 g2D.setClip(colClipRect); 480 481 if (viewMode == PaintPanel.SHOW_ALIAS) g2D.drawString(props[i].getAlias(), _x + COLUMN_MARGIN + 15, 482 _y + fSize * i + COLUMN_Y_OFFSET); 483 else g2D.drawString(props[i].getName(), _x + COLUMN_MARGIN + 15, _y + fSize * i + COLUMN_Y_OFFSET); 484 485 g2D.setClip(clipRect); 486 g2D.drawString(props[i].getTypeName(), _x + TYPE_MARGIN, _y + fSize * i + COLUMN_Y_OFFSET); 487 488 if (props[i].isRequired()) 489 { 490 g2D.drawString("*", _x + 5, _y + fSize * i + COLUMN_Y_OFFSET + 2); 491 } 492 if (props[i].isPartOfUID()) 493 { 494 g2D.drawString("u", _x + 10, _y + fSize * i + COLUMN_Y_OFFSET); 495 } 496 } 497 498 if (isSelected()) 499 { 500 g2D.setColor(_editor._selectionColor); 501 g2D.fillRect(getX(), getY(), CORNER_SIZE, CORNER_SIZE); 502 g2D.fillRect(getX2() - (CORNER_SIZE - 1), getY(), CORNER_SIZE, CORNER_SIZE); 503 g2D.fillRect(getX(), getY2() - (CORNER_SIZE - 1), CORNER_SIZE, CORNER_SIZE); 504 g2D.fillRect(getX2() - (CORNER_SIZE - 1), getY2() - (CORNER_SIZE - 1), CORNER_SIZE, CORNER_SIZE); 505 } 506 507 g2D.setTransform(afx); 508 g2D.setClip(orgClip); 509 } 510 511 public void paintLines(Graphics2D g2D, AffineTransform afx, HashMap horizontalMastersDone, HashMap verticalMastersDone) 512 { 513 paintRelations(g2D, afx, horizontalMastersDone, verticalMastersDone); 514 paintInheritance(g2D, afx); 515 } 516 517 public boolean equals(Object obj) 518 { 519 if (!(obj instanceof ExtentViewer)) return false; 520 ExtentViewer other = (ExtentViewer) obj; 521 522 return _extent == other._extent; 523 } 524 525 public int hashCode() 526 { 527 int result = 0; 528 if (_extent != null) result += _extent.hashCode(); 529 530 return result; 531 } 532 533 public void paintShadow(Graphics2D g2D) 534 { 535 g2D.setPaint(_lighterGray); 536 Rectangle rect = new Rectangle (); 537 rect.setFrame(getX() + 4, getY() + 4, getWidth(), getHeight()); 538 g2D.fill(rect); 539 g2D.draw(rect); 540 541 g2D.setPaint(Color.lightGray); 542 rect = new Rectangle (); 543 rect.setFrame(getX() + 2, getY() + 2, getWidth(), getHeight()); 544 g2D.fill(rect); 545 g2D.draw(rect); 546 } 547 } 548 549 class LineSpecifics 550 { 551 552 public int startX1 = 0, startY1 = 0, startX2 = 0, startY2 = 0; 553 public boolean horizontal; 554 public char direction = ' '; 555 556 public LineSpecifics(ExtentViewer master, ExtentViewer detail, int drawingOffset) 557 { 558 horizontal = true; 559 if (master.getY2() < detail.getY()) 560 { 561 direction = 'T'; horizontal = false; 564 startY1 = 0; 565 startY2 = master.getHeight(); 566 567 int middle = getMiddleOfSpan(detail.getX(), detail.getX2(), master.getX(), master.getX2()) + drawingOffset; 568 startX1 = middle - detail.getX(); 569 startX2 = middle - master.getX(); 570 if (startX1 > detail.getWidth() - 5) startX1 = (detail.getWidth() - 5) - drawingOffset; 571 if (startX1 < 5) startX1 = 5; 572 if (startX2 > master.getWidth() - 5) startX2 = (master.getWidth() - 5) - drawingOffset; 573 if (startX2 < 5) startX2 = 5; 574 } 575 else if (master.getY() > detail.getY2()) 576 { 577 direction = 'B'; horizontal = false; 580 startY1 = detail.getHeight(); 581 startY2 = 0; 582 583 int middle = getMiddleOfSpan(detail.getX(), detail.getX2(), master.getX(), master.getX2()) + drawingOffset; 584 startX1 = middle - detail.getX(); 585 startX2 = middle - master.getX(); 586 587 if (startX1 > detail.getWidth() - 5) startX1 = (detail.getWidth() - 5) - drawingOffset; 588 if (startX1 < 5) startX1 = 5; 589 if (startX2 > master.getWidth() - 5) startX2 = (master.getWidth() - 5) - drawingOffset; 590 if (startX2 < 5) startX2 = 5; 591 } 592 else 593 if (master.getX() > detail.getX2()) 595 { 596 direction = 'R'; startX1 = detail.getWidth(); 599 startX2 = 0; 600 601 startY1 = detail.getHeight() / 2; 602 startY2 = master.getHeight() / 2; 603 604 int middle = getMiddleOfSpan(detail.getY(), detail.getY2(), master.getY(), master.getY2()) + drawingOffset; 605 startY1 = middle - detail.getY(); 606 startY2 = middle - master.getY(); 607 608 if (startY1 > detail.getHeight() - 5) startY1 = (detail.getHeight() - 5) - drawingOffset; 609 if (startY1 < 5) startY1 = 5; 610 if (startY2 > master.getHeight() - 5) startY2 = (master.getHeight() - 5) - drawingOffset; 611 if (startY2 < 5) startY2 = 5; 612 613 } 614 else if (master.getX() < detail.getX2()) 615 { 616 direction = 'L'; startX1 = 0; 619 startX2 = master.getWidth(); 620 621 int middle = getMiddleOfSpan(detail.getY(), detail.getY2(), master.getY(), master.getY2()) + drawingOffset; 622 startY1 = middle - detail.getY(); 623 startY2 = middle - master.getY(); 624 625 if (startY1 > detail.getHeight() - 5) startY1 = (detail.getHeight() - 5) - drawingOffset; 626 if (startY1 < 5) startY1 = 5; 627 if (startY2 > master.getHeight() - 5) startY2 = (master.getHeight() - 5) - drawingOffset; 628 if (startY2 < 5) startY2 = 5; 629 } 630 } 631 632 public final int getMiddleOfSpan(int x1, int x2, int x3, int x4) 633 { 634 int[] p = new int[4]; 635 p[0] = x1; 636 p[1] = x2; 637 p[2] = x3; 638 p[3] = x4; 639 640 Arrays.sort(p); 641 return (p[1] + p[2]) / 2; 642 } 643 644 } | Popular Tags |