1 18 19 package org.objectweb.jac.ide.diagrams; 20 21 import CH.ifa.draw.contrib.AutoscrollHelper; 22 import CH.ifa.draw.framework.ConnectionFigure; 23 import CH.ifa.draw.framework.Connector; 24 import CH.ifa.draw.framework.Drawing; 25 import CH.ifa.draw.framework.DrawingChangeEvent; 26 import CH.ifa.draw.framework.DrawingEditor; 27 import CH.ifa.draw.framework.DrawingView; 28 import CH.ifa.draw.framework.Figure; 29 import CH.ifa.draw.framework.FigureEnumeration; 30 import CH.ifa.draw.framework.FigureSelection; 31 import CH.ifa.draw.framework.FigureSelectionListener; 32 import CH.ifa.draw.framework.Handle; 33 import CH.ifa.draw.framework.Painter; 34 import CH.ifa.draw.framework.PointConstrainer; 35 import CH.ifa.draw.framework.Tool; 36 import CH.ifa.draw.standard.FigureEnumerator; 37 import CH.ifa.draw.standard.SimpleUpdateStrategy; 38 import CH.ifa.draw.standard.StandardFigureSelection; 39 import CH.ifa.draw.util.Geom; 40 import java.awt.Color ; 41 import java.awt.Dimension ; 42 import java.awt.Graphics ; 43 import java.awt.Insets ; 44 import java.awt.Point ; 45 import java.awt.PrintGraphics ; 46 import java.awt.Rectangle ; 47 import java.awt.dnd.Autoscroll ; 48 import java.awt.event.KeyEvent ; 49 import java.awt.event.KeyListener ; 50 import java.awt.event.MouseEvent ; 51 import java.awt.event.MouseListener ; 52 import java.awt.event.MouseMotionListener ; 53 import java.util.Enumeration ; 54 import java.util.Vector ; 55 import javax.swing.JPanel ; 56 import javax.swing.Scrollable ; 57 58 62 public class IDEDrawingView extends JPanel 63 implements DrawingView, KeyListener , Autoscroll , Scrollable { 64 65 70 transient private DrawingEditor fEditor; 71 72 73 private transient Vector fSelectionListeners; 74 75 76 private Drawing fDrawing; 77 78 79 private transient Rectangle fDamage = null; 80 81 84 transient private Vector fSelection; 85 86 89 transient private Vector fSelectionHandles; 90 91 94 private Dimension fViewSize; 95 96 100 private Point fLastClick; 101 102 107 private Vector fBackgrounds = null; 108 109 114 private Vector fForegrounds = null; 115 116 119 private Painter fUpdateStrategy; 120 121 125 private PointConstrainer fConstrainer; 126 127 130 public static final int MINIMUM_WIDTH = 400; 131 public static final int MINIMUM_HEIGHT = 300; 132 public static final int SCROLL_INCR = 100; 133 public static final int SCROLL_OFFSET = 10; 134 135 public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) { 136 return SCROLL_OFFSET; 137 } 138 public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) { 139 return SCROLL_INCR; 140 } 141 142 public boolean getScrollableTracksViewportWidth() { 143 return false; 144 } 145 public boolean getScrollableTracksViewportHeight() { 146 return false; 147 } 148 public Dimension getPreferredScrollableViewportSize() { 149 return getPreferredSize(); 150 } 151 152 157 private static final long serialVersionUID = -3878153366174603336L; 158 159 162 public IDEDrawingView(DrawingEditor editor) { 163 this(editor, MINIMUM_WIDTH, MINIMUM_HEIGHT); 164 } 165 166 public IDEDrawingView(DrawingEditor editor, int width, int height) { 167 setAutoscrolls(true); 168 counter++; 169 fEditor = editor; 170 fViewSize = new Dimension (width,height); 171 fSelectionListeners = new Vector (); 172 addFigureSelectionListener(editor()); 173 fLastClick = new Point (0, 0); 174 fConstrainer = null; 175 fSelection = new Vector (); 176 setDisplayUpdate(new SimpleUpdateStrategy()); 178 setBackground(Color.lightGray); 181 182 addMouseListener(ml); 183 addMouseMotionListener(mml); 184 addKeyListener(this); 185 } 186 187 MouseListener ml = new MouseListener () { 188 public void mouseClicked(MouseEvent e) {} 190 public void mouseEntered(MouseEvent e) {} 191 public void mouseExited(MouseEvent e) {} 192 197 public void mousePressed(MouseEvent e) { 198 if (tool()!=null) { 199 requestFocus(); Point p = constrainPoint(new Point (e.getX(), e.getY())); 201 fLastClick = new Point (e.getX(), e.getY()); 202 tool().mouseDown(e, p.x, p.y); 203 checkDamage(); 204 } 205 } 206 211 public void mouseReleased(MouseEvent e) { 212 if (tool()!=null) { 213 Point p = constrainPoint(new Point (e.getX(), e.getY())); 214 tool().mouseUp(e, p.x, p.y); 215 checkDamage(); 216 } 217 } 218 }; 219 220 MouseMotionListener mml = new MouseMotionListener () { 221 226 public void mouseDragged(MouseEvent e) { 227 if (tool()!=null) { 228 Point p = constrainPoint(new Point (e.getX(), e.getY())); 229 tool().mouseDrag(e, p.x, p.y); 230 checkDamage(); 231 } 232 } 233 234 239 public void mouseMoved(MouseEvent e) { 240 if (tool()!=null) { 241 tool().mouseMove(e, e.getX(), e.getY()); 242 } 243 } 244 }; 245 246 249 public void setEditor(DrawingEditor editor) { 250 fEditor = editor; 251 } 252 253 256 public Tool tool() { 257 return editor().tool(); 258 } 259 260 263 public Drawing drawing() { 264 return fDrawing; 265 } 266 267 270 public void setDrawing(Drawing d) { 271 if (fDrawing != null) { 272 clearSelection(); 273 fDrawing.removeDrawingChangeListener(this); 274 } 275 fDrawing = d; 276 if (fDrawing != null) { 277 fDrawing.addDrawingChangeListener(this); 278 } 279 checkMinimumSize(); 280 repaint(); 281 } 282 283 286 public DrawingEditor editor() { 287 return fEditor; 288 } 289 290 294 public Figure add(Figure figure) { 295 return drawing().add(figure); 296 } 297 298 302 public Figure remove(Figure figure) { 303 return drawing().remove(figure); 304 } 305 306 309 public void addAll(Vector figures) { 310 FigureEnumeration k = new FigureEnumerator(figures); 311 while (k.hasMoreElements()) { 312 add(k.nextFigure()); 313 } 314 } 315 316 319 public boolean figureExists(Figure inf, FigureEnumeration e) { 320 while(e.hasMoreElements()) { 321 Figure figure = e.nextFigure(); 322 323 if(figure.includes(inf)) { 324 return true; 325 } 326 } 327 328 return false; 329 } 330 331 338 public FigureEnumeration insertFigures(FigureEnumeration fe, int dx, int dy, boolean bCheck) { 339 if (fe == null) { 340 return FigureEnumerator.getEmptyEnumeration(); 341 } 342 343 Vector addedFigures = new Vector (); 344 Vector vCF = new Vector (10); 345 346 while (fe.hasMoreElements()) { 347 Figure figure = fe.nextFigure(); 348 if (figure instanceof ConnectionFigure) { 349 vCF.addElement(figure); 350 } 351 else if (figure != null) { 352 figure.moveBy(dx, dy); 353 figure = add(figure); 354 addToSelection(figure); 355 addedFigures.addElement(figure); 357 } 358 } 359 360 FigureEnumeration ecf = new FigureEnumerator(vCF); 361 362 while (ecf.hasMoreElements()) { 363 ConnectionFigure cf = (ConnectionFigure) ecf.nextFigure(); 364 Figure sf = cf.startFigure(); 365 Figure ef = cf.endFigure(); 366 367 if (figureExists(sf, drawing().figures()) && 368 figureExists(ef, drawing().figures()) && 369 (!bCheck || cf.canConnect(sf, ef))) { 370 371 if (bCheck) { 372 Point sp = sf.center(); 373 Point ep = ef.center(); 374 Connector fStartConnector = cf.startFigure().connectorAt(ep.x, ep.y); 375 Connector fEndConnector = cf.endFigure().connectorAt(sp.x, sp.y); 376 377 if (fEndConnector != null && fStartConnector != null) { 378 cf.connectStart(fStartConnector); 379 cf.connectEnd(fEndConnector); 380 cf.updateConnection(); 381 } 382 } 383 384 Figure nf = add(cf); 385 addToSelection(nf); 386 addedFigures.addElement(nf); 388 } 389 } 390 391 return new FigureEnumerator(addedFigures); 392 } 393 394 397 public Vector getConnectionFigures(Figure inFigure) { 398 if (inFigure == null || !inFigure.canConnect()) { 400 return null; 401 } 402 403 406 Vector result = new Vector (5); 407 FigureEnumeration figures = drawing().figures(); 408 409 while (figures.hasMoreElements()) { 411 Figure f= figures.nextFigure(); 412 413 if ((f instanceof ConnectionFigure) && !(isFigureSelected(f))) { 414 ConnectionFigure cf = (ConnectionFigure) f; 415 416 if (cf.startFigure().includes(inFigure) || 417 cf.endFigure().includes(inFigure)) { 418 result.addElement(f); 419 } 420 } 421 } 422 423 return result; 424 } 425 426 429 public Dimension getMinimumSize() { 430 return fViewSize; 431 } 432 433 436 public Dimension getPreferredSize() { 437 return getMinimumSize(); 438 } 439 440 444 public void setDisplayUpdate(Painter updateStrategy) { 445 fUpdateStrategy = updateStrategy; 446 } 447 448 452 public Painter getDisplayUpdate() { 453 return fUpdateStrategy; 454 } 455 456 461 public Vector selection() { 462 return (Vector )fSelection.clone(); 464 } 465 466 469 public FigureEnumeration selectionElements() { 470 return new FigureEnumerator(selectionZOrdered()); 471 } 472 473 479 public Vector selectionZOrdered() { 480 Vector result = new Vector (selectionCount()); 481 FigureEnumeration figures = drawing().figures(); 482 483 while (figures.hasMoreElements()) { 484 Figure f= figures.nextFigure(); 485 if (isFigureSelected(f)) { 486 result.addElement(f); 487 } 488 } 489 return result; 490 } 491 492 495 public int selectionCount() { 496 return fSelection.size(); 497 } 498 499 502 public boolean isFigureSelected(Figure checkFigure) { 503 return fSelection.contains(checkFigure); 504 } 505 506 510 public void addToSelection(Figure figure) { 511 if (!isFigureSelected(figure) && drawing().includes(figure)) { 512 fSelection.addElement(figure); 513 fSelectionHandles = null; 514 figure.invalidate(); 515 fireSelectionChanged(); 516 } 517 } 518 519 522 public void addToSelectionAll(Vector figures) { 523 addToSelectionAll(new FigureEnumerator(figures)); 524 } 525 526 529 public void addToSelectionAll(FigureEnumeration fe) { 530 while (fe.hasMoreElements()) { 531 addToSelection(fe.nextFigure()); 532 } 533 } 534 535 538 public void removeFromSelection(Figure figure) { 539 if (isFigureSelected(figure)) { 540 fSelection.removeElement(figure); 541 fSelectionHandles = null; 542 figure.invalidate(); 543 fireSelectionChanged(); 544 } 545 } 546 547 551 public void toggleSelection(Figure figure) { 552 if (isFigureSelected(figure)) { 553 removeFromSelection(figure); 554 } 555 else { 556 addToSelection(figure); 557 } 558 fireSelectionChanged(); 559 } 560 561 564 public void clearSelection() { 565 if (fSelectionHandles == null) { 567 return; 569 } 570 571 FigureEnumeration fe = selectionElements(); 572 while (fe.hasMoreElements()) { 573 fe.nextFigure().invalidate(); 574 } 575 fSelection = new Vector (); 576 fSelectionHandles = null; 577 fireSelectionChanged(); 578 } 579 580 583 private Enumeration selectionHandles() { 584 if (fSelectionHandles == null) { 585 fSelectionHandles = new Vector (); 586 FigureEnumeration k = selectionElements(); 587 while (k.hasMoreElements()) { 588 Figure figure = k.nextFigure(); 589 Enumeration kk = figure.handles().elements(); 590 while (kk.hasMoreElements()) { 591 fSelectionHandles.addElement(kk.nextElement()); 592 } 593 } 594 } 595 return fSelectionHandles.elements(); 596 } 597 598 602 public FigureSelection getFigureSelection() { 603 return new StandardFigureSelection(new FigureEnumerator(selectionZOrdered()), selectionCount()); 604 } 605 606 610 public Handle findHandle(int x, int y) { 611 Handle handle; 612 613 Enumeration k = selectionHandles(); 614 while (k.hasMoreElements()) { 615 handle = (Handle) k.nextElement(); 616 if (handle.containsPoint(x, y)) { 617 return handle; 618 } 619 } 620 return null; 621 } 622 623 628 protected void fireSelectionChanged() { 629 if (fSelectionListeners != null) { 630 for (int i = 0; i < fSelectionListeners.size(); i++) { 631 FigureSelectionListener l = (FigureSelectionListener)fSelectionListeners.elementAt(i); 632 l.figureSelectionChanged(this); 633 } 634 } 635 } 636 637 640 public Point lastClick() { 641 return fLastClick; 642 } 643 644 647 public void setConstrainer(PointConstrainer c) { 648 fConstrainer = c; 649 } 650 651 654 public PointConstrainer getConstrainer() { 655 return fConstrainer; 656 } 657 658 661 protected Point constrainPoint(Point p) { 662 Dimension size = getSize(); 664 p.x = Geom.range(1, size.width, p.x); 667 p.y = Geom.range(1, size.height, p.y); 668 669 if (fConstrainer != null ) { 670 return fConstrainer.constrainPoint(p); 671 } 672 return p; 673 } 674 675 676 682 public void keyPressed(KeyEvent e) { 683 int code = e.getKeyCode(); 684 if ((code == KeyEvent.VK_BACK_SPACE) || (code == KeyEvent.VK_DELETE)) { 685 } else if (code == KeyEvent.VK_DOWN || code == KeyEvent.VK_UP || 687 code == KeyEvent.VK_RIGHT || code == KeyEvent.VK_LEFT) { 688 handleCursorKey(code); 689 } else { 690 tool().keyDown(e, code); 691 } 692 checkDamage(); 693 } 694 695 699 protected void handleCursorKey(int key) { 700 int dx = 0, dy = 0; 701 int stepX = 1, stepY = 1; 702 if (fConstrainer != null) { 704 stepX = fConstrainer.getStepX(); 705 stepY = fConstrainer.getStepY(); 706 } 707 708 switch (key) { 709 case KeyEvent.VK_DOWN: 710 dy = stepY; 711 break; 712 case KeyEvent.VK_UP: 713 dy = -stepY; 714 break; 715 case KeyEvent.VK_RIGHT: 716 dx = stepX; 717 break; 718 case KeyEvent.VK_LEFT: 719 dx = -stepX; 720 break; 721 } 722 moveSelection(dx, dy); 723 } 724 725 private void moveSelection(int dx, int dy) { 726 FigureEnumeration figures = selectionElements(); 727 while (figures.hasMoreElements()) { 728 figures.nextFigure().moveBy(dx, dy); 729 } 730 checkDamage(); 731 } 732 733 736 public synchronized void checkDamage() { 737 } 738 739 public void repairDamage() { 740 if (fDamage != null) { 741 repaint(fDamage.x, fDamage.y, fDamage.width, fDamage.height); 742 fDamage = null; 743 } 744 } 745 746 public void drawingInvalidated(DrawingChangeEvent e) { 747 Rectangle r = e.getInvalidatedRectangle(); 748 if (fDamage == null) { 749 fDamage = r; 750 } else { 751 fDamage.add(r); 752 } 753 } 754 755 public void drawingRequestUpdate(DrawingChangeEvent e) { 756 repairDamage(); 757 } 758 759 764 protected void paintComponent(Graphics g) { 765 getDisplayUpdate().draw(g, this); 766 } 767 768 773 public void drawAll(Graphics g) { 774 boolean isPrinting = g instanceof PrintGraphics ; 775 drawBackground(g); 776 if (fBackgrounds != null && !isPrinting) { 777 drawPainters(g, fBackgrounds); 778 } 779 drawDrawing(g); 780 if (fForegrounds != null && !isPrinting) { 781 drawPainters(g, fForegrounds); 782 } 783 if (!isPrinting) { 784 drawHandles(g); 785 } 786 } 787 788 794 public void draw(Graphics g, FigureEnumeration fe) { 795 boolean isPrinting = g instanceof PrintGraphics ; 796 if (fBackgrounds != null && !isPrinting) { 798 drawPainters(g, fBackgrounds); 799 } 800 fDrawing.draw(g, fe); 801 if (fForegrounds != null && !isPrinting) { 802 drawPainters(g, fForegrounds); 803 } 804 if (!isPrinting) { 805 drawHandles(g); 806 } 807 } 808 809 812 public void drawHandles(Graphics g) { 813 Enumeration k = selectionHandles(); 814 while (k.hasMoreElements()) { 815 ((Handle) k.nextElement()).draw(g); 816 } 817 } 818 819 822 public void drawDrawing(Graphics g) { 823 fDrawing.draw(g); 824 } 825 826 831 public void drawBackground(Graphics g) { 832 g.setColor(getBackground()); 833 g.fillRect(0, 0, getBounds().width, getBounds().height); 834 } 835 836 private void drawPainters(Graphics g, Vector v) { 837 for (int i = 0; i < v.size(); i++) { 838 ((Painter)v.elementAt(i)).draw(g, this); 839 } 840 } 841 842 845 public void addBackground(Painter painter) { 846 if (fBackgrounds == null) { 847 fBackgrounds = new Vector (3); 848 } 849 fBackgrounds.addElement(painter); 850 repaint(); 851 } 852 853 856 public void removeBackground(Painter painter) { 857 if (fBackgrounds != null) { 858 fBackgrounds.removeElement(painter); 859 } 860 repaint(); 861 } 862 863 866 public void removeForeground(Painter painter) { 867 if (fForegrounds != null) { 868 fForegrounds.removeElement(painter); 869 } 870 repaint(); 871 } 872 873 876 public void addForeground(Painter painter) { 877 if (fForegrounds == null) { 878 fForegrounds = new Vector (3); 879 } 880 fForegrounds.addElement(painter); 881 repaint(); 882 } 883 884 888 public void freezeView() { 889 drawing().lock(); 890 } 891 892 896 public void unfreezeView() { 897 drawing().unlock(); 898 } 899 900 private void checkMinimumSize() { 901 FigureEnumeration k = drawing().figures(); 902 Dimension d = new Dimension (0, 0); 903 while (k.hasMoreElements()) { 904 Rectangle r = k.nextFigure().displayBox(); 905 d.width = Math.max(d.width, r.x+r.width); 906 d.height = Math.max(d.height, r.y+r.height); 907 } 908 if (fViewSize.height < d.height || fViewSize.width < d.width) { 909 fViewSize.height = d.height + SCROLL_OFFSET; 910 fViewSize.width = d.width + SCROLL_OFFSET; 911 setSize(fViewSize); 912 } 913 } 914 915 public boolean isFocusTraversable() { 916 return true; 917 } 918 919 public boolean isInteractive() { 920 return true; 921 } 922 923 public void keyTyped(KeyEvent e) {} 924 public void keyReleased(KeyEvent e) {} 925 926 930 public void addFigureSelectionListener(FigureSelectionListener fsl) { 931 fSelectionListeners.add(fsl); 932 } 933 934 938 public void removeFigureSelectionListener(FigureSelectionListener fsl) { 939 fSelectionListeners.remove(fsl); 940 } 941 942 public int getDefaultDNDActions() { 943 return java.awt.dnd.DnDConstants.ACTION_COPY_OR_MOVE; 944 } 945 946 947 948 private ASH ash = new ASH(10); 949 950 public void autoscroll(java.awt.Point p) { 951 ash.autoscroll(p); 952 } 953 public Insets getAutoscrollInsets() { 954 return ash.getAutoscrollInsets(); 955 } 956 class ASH extends AutoscrollHelper { 957 public ASH(int margin) { 958 super(margin); 959 } 960 public Dimension getSize() { 961 return IDEDrawingView.this.getSize(); 962 } 963 public Rectangle getVisibleRect() { 964 return IDEDrawingView.this.getVisibleRect(); 965 } 966 public void scrollRectToVisible(Rectangle aRect) { 967 IDEDrawingView.this.scrollRectToVisible(aRect); 968 } 969 } 970 971 public String toString() { 972 return "DrawingView Nr: " + myCounter; 973 } 974 975 static int counter; 976 int myCounter = counter; 977 } 978 | Popular Tags |