2,2c2,2 < * @(#)StandardDrawingView.java 5.2 --- > * @(#)StandardDrawingView.java 3a4,9 > * Project: JHotdraw - a GUI framework for technical drawings > * http://www.jhotdraw.org > * http://jhotdraw.sourceforge.net > * Copyright: © by the original author(s) and all contributors > * License: Lesser GNU Public License (LGPL) > * http://www.opensource.org/licenses/lgpl-license.html 14a21,21 > import CH.ifa.draw.figures.TextTool; 20a29,30 > * > * @version <$CURRENT_VERSION$> 37a48,52 > * the registered listeners for selection changes > */ > private transient Vector fSelectionListeners; > > /** 92a108,120 > /** > * > */ > private transient UndoManager myUndoManager; > > /** > * Scrolling increment > */ > public static final int MINIMUM_WIDTH = 400; > public static final int MINIMUM_HEIGHT = 300; > public static final int SCROLL_INCR = 100; > public static final int SCROLL_OFFSET = 10; > 103a132,135 > public StandardDrawingView(DrawingEditor editor) { > this(editor, MINIMUM_WIDTH, MINIMUM_HEIGHT); > } > 106a139,140 > fSelectionListeners = new Vector(); > addFigureSelectionListener(editor()); 109a144,144 > setUndoManager(new UndoManager()); 132,132c167,167 < return fEditor.tool(); --- > return editor().tool(); 167a203,239 > * Automatically adjusts the size of view (optionally) and > * scroll the invalidated rectangle to be visible in viewport > * Params r - Rectangle to be scrolled > * bSizeChange - If true, view may be resized to accomodate "r". > */ > protected void scrollToVisible(Rectangle r, boolean bSizeChange) { > if (bSizeChange) { > // Flag for size change > boolean bChanged = false; > > // New width and height > int newWidth = r.x + r.width + SCROLL_INCR; > int newHeight = r.y + r.height + SCROLL_INCR; > > // Check the adjustments in size > if (newWidth > fViewSize.width) { > fViewSize.width = newWidth; > bChanged = true; > } > > if (newHeight > fViewSize.height) { > fViewSize.height = newHeight; > bChanged = true; > } > > // Auto size > if (bChanged) { > setSize(fViewSize); > } > } > > // Compute intersection of view size and to be scrolled rectangle > // in order to prevent scrolling beyond view size > scrollRectToVisible(r.intersection(new Rectangle(0, 0, fViewSize.width, fViewSize.height))); > } > > /** 171a244,252 > Figure f = drawing().add(figure); > > // If new figure is added, check for size and scrolling > if(f != null) { > Rectangle r = f.displayBox(); > scrollToVisible(r, true); > } > > return f; 172,172c468,468 < return drawing().add(figure); --- > if (!isFigureSelected(figure) && drawing().includes(figure)) { 188,188c268,268 < while (k.hasMoreElements()) --- > while (k.hasMoreElements()) { 190a271,381 > } > > /** > * Check existance of figure in the drawing > */ > public boolean figureExists(Figure inf, FigureEnumeration e) { > while(e.hasMoreElements()) { > Figure figure = e.nextFigure(); > > if(figure.includes(inf)) { > return true; > } > } > > return false; > } > > /** > * Inserts a vector of figures and translates them by the > * given offset. This function is used to insert figures from clipboards (cut/copy) > * > * @return enumeration which has been added to the drawing. The figures in the enumeration > * can have changed during adding them (e.g. they could have been decorated). > */ > public FigureEnumeration insertFigures(FigureEnumeration fe, int dx, int dy, boolean bCheck) { > if (fe == null) { > return FigureEnumerator.getEmptyEnumeration(); > } > > Vector addedFigures = new Vector(); > Vector vCF = new Vector(10); > > while (fe.hasMoreElements()) { > Figure figure = fe.nextFigure(); > if (figure instanceof ConnectionFigure) { > vCF.addElement(figure); > } > else if (figure != null) { > figure.moveBy(dx, dy); > figure = add(figure); > addToSelection(figure); > // figure might has changed during adding so add it afterwards > addedFigures.addElement(figure); > } > } > > FigureEnumeration ecf = new FigureEnumerator(vCF); > > while (ecf.hasMoreElements()) { > ConnectionFigure cf = (ConnectionFigure) ecf.nextFigure(); > Figure sf = cf.startFigure(); > Figure ef = cf.endFigure(); > > if (figureExists(sf, drawing().figures()) && > figureExists(ef, drawing().figures()) && > (!bCheck || cf.canConnect(sf, ef))) { > > if (bCheck) { > Point sp = sf.center(); > Point ep = ef.center(); > Connector fStartConnector = cf.startFigure().connectorAt(ep.x, ep.y); > Connector fEndConnector = cf.endFigure().connectorAt(sp.x, sp.y); > > if (fEndConnector != null && fStartConnector != null) { > cf.connectStart(fStartConnector); > cf.connectEnd(fEndConnector); > cf.updateConnection(); > } > } > > Figure nf = add(cf); > addToSelection(nf); > // figure might has changed during adding so add it afterwards > addedFigures.addElement(nf); > } > } > > return new FigureEnumerator(addedFigures); > } > > /** > * Returns a vector of connectionfigures attached to this figure > */ > public Vector getConnectionFigures(Figure inFigure) { > // If no figure or figure is non connectable, just return null > if (inFigure == null || !inFigure.canConnect()) { > return null; > } > > // if (inFigure instanceof ConnectionFigure) > // return null; > > Vector result = new Vector(5); > FigureEnumeration figures = drawing().figures(); > > // Find all connection figures > while (figures.hasMoreElements()) { > Figure f= figures.nextFigure(); > > if ((f instanceof ConnectionFigure) && !(isFigureSelected(f))) { > ConnectionFigure cf = (ConnectionFigure) f; > > if (cf.startFigure().includes(inFigure) || > cf.endFigure().includes(inFigure)) { > result.addElement(f); > } > } > } > > return result; > } 214a406,413 > * Sets the current display update strategy. > * @see Painter > */ > public Painter getDisplayUpdate() { > return fUpdateStrategy; > } > > /** 228,228c427,427 < return new FigureEnumerator(fSelection); --- > return new FigureEnumerator(selectionZOrdered()); 238,238c437,437 < Vector result = new Vector(fSelection.size()); --- > Vector result = new Vector(selectionCount()); 243,243c442,442 < if (fSelection.contains(f)) { --- > if (isFigureSelected(f)) { 257a457,463 > * Test whether a given figure is selected. > */ > public boolean isFigureSelected(Figure checkFigure) { > return fSelection.contains(checkFigure); > } > > /** 258a465,465 > * it is also contained in the Drawing associated with this DrawingView. 258,258c464,464 < * Adds a figure to the current selection. --- > * Adds a figure to the current selection. The figure is only selected if 261,261c496,496 < if (!fSelection.contains(figure)) { --- > if (isFigureSelected(figure)) { 264a472,472 > fireSelectionChanged(); 265,265d471 < selectionChanged(); 272a480,489 > addToSelectionAll(new FigureEnumerator(figures)); > } > > /** > * Adds a FigureEnumeration to the current selection. > */ > public void addToSelectionAll(FigureEnumeration fe) { > while (fe.hasMoreElements()) { > addToSelection(fe.nextFigure()); > } 273,275d479 < FigureEnumeration k = new FigureEnumerator(figures); < while (k.hasMoreElements()) < addToSelection(k.nextFigure()); 282,282c509,509 < if (fSelection.contains(figure)) { --- > if (isFigureSelected(figure)) { 285a500,500 > fireSelectionChanged(); 286,286d499 < selectionChanged(); 295,295d508 < if (fSelection.contains(figure)) 296a511,511 > } 297,297c512,512 < else --- > else { 298a514,515 > } > fireSelectionChanged(); 299,299d513 < selectionChanged(); 310,310c526,526 < while (k.hasMoreElements()) --- > while (k.hasMoreElements()) { 311a528,528 > } 313a531,531 > fireSelectionChanged(); 314,314d530 < selectionChanged(); 327,327c544,544 < while (kk.hasMoreElements()) --- > while (kk.hasMoreElements()) { 330a548,548 > } 339,339c557,557 < return new FigureSelection(selectionZOrdered()); --- > return new StandardFigureSelection(new FigureEnumerator(selectionZOrdered()), selectionCount()); 352,352c570,570 < if (handle.containsPoint(x, y)) --- > if (handle.containsPoint(x, y)) { 354a573,573 > } 362a582,588 > protected void fireSelectionChanged() { > if (fSelectionListeners != null) { > for (int i = 0; i < fSelectionListeners.size(); i++) { > FigureSelectionListener l = (FigureSelectionListener)fSelectionListeners.elementAt(i); > l.figureSelectionChanged(this); > } > } 363,364d581 < protected void selectionChanged() { < fEditor.selectionChanged(this); 399,399c623,623 < if (fConstrainer != null ) --- > if (fConstrainer != null ) { 400a625,625 > } 458a684,684 > } 459,459c685,685 < } else if (code == KeyEvent.VK_DOWN || code == KeyEvent.VK_UP || --- > else if (code == KeyEvent.VK_DOWN || code == KeyEvent.VK_UP || 461a688,688 > } 462,462c689,689 < } else { --- > else { 497a25,25 > * 498,498d24 < 501,501c727,727 < while (figures.hasMoreElements()) --- > while (figures.hasMoreElements()) { 502a729,729 > } 506a734,742 > * Determines whether to auto scroll damaged rectangle. > * Override if you do not want drag scroll mechanism. > * By default this method returns true. > */ > protected boolean doDragScroll() { > return true; > } > > /** 520,520c756,756 < if (fDamage != null) { --- > if (fDamage == null) { 520a757,767 > // repaint(); > } > else { > > // TextFigures have problems with scrolling: avoid scrolling them > // TextTool does not call checkDamage at the moment to avoid scrolling > // if (doDragScroll() && !(tool() instanceof TextTool)) { > if (doDragScroll() ) { > scrollToVisible(fDamage, false); > } > 521,521c768,768 < repaint(fDamage.x, fDamage.y, fDamage.width, fDamage.height); --- > repaint(1, fDamage.x, fDamage.y, fDamage.width, fDamage.height); 528,528c775,775 < if (fDamage == null) --- > if (fDamage == null) { 529a777,777 > } 530,530c778,778 < else --- > else { 532a781,781 > } 539,545d787 < * Updates the drawing view. < */ < public void update(Graphics g) { < paint(g); < } < < /** 550,550c792,792 < public void paint(Graphics g) { --- > protected void paintComponent(Graphics g) { 551,551c793,793 < fUpdateStrategy.draw(g, this); --- > getDisplayUpdate().draw(g, this); 562,562c804,804 < if (fBackgrounds != null && !isPrinting) --- > if (fBackgrounds != null && !isPrinting) { 563a806,806 > } 565,565c808,808 < if (fForegrounds != null && !isPrinting) --- > if (fForegrounds != null && !isPrinting) { 566a810,810 > } 567,567c811,811 < if (!isPrinting) --- > if (!isPrinting) { 569a814,814 > } 580,580c825,825 < if (fBackgrounds != null && !isPrinting) --- > if (fBackgrounds != null && !isPrinting) { 581a827,827 > } 583,583c829,829 < if (fForegrounds != null && !isPrinting) --- > if (fForegrounds != null && !isPrinting) { 584a831,831 > } 585,585c832,832 < if (!isPrinting) --- > if (!isPrinting) { 587a835,835 > } 594,594c842,842 < while (k.hasMoreElements()) --- > while (k.hasMoreElements()) { 596a845,845 > } 616,616c865,865 < for (int i = 0; i < v.size(); i++) --- > for (int i = 0; i < v.size(); i++) { 618a868,868 > } 624,624c874,874 < if (fBackgrounds == null) --- > if (fBackgrounds == null) { 625a876,876 > } 634,634c885,885 < if (fBackgrounds != null) --- > if (fBackgrounds != null) { 635a887,887 > } 643,643c895,895 < if (fForegrounds != null) --- > if (fForegrounds != null) { 644a897,897 > } 652,652c905,905 < if (fForegrounds == null) --- > if (fForegrounds == null) { 653a907,907 > } 680,680c934,934 < if (fDrawing != null) --- > if (fDrawing != null) { 682a937,938 > fSelectionListeners= new Vector(); > } 693,693c949,949 < fViewSize.height = d.height+10; --- > fViewSize.height = d.height + SCROLL_OFFSET; 694,694c950,950 < fViewSize.width = d.width+10; --- > fViewSize.width = d.width + SCROLL_OFFSET; 708a965,988 > > /** > * Add a listener for selection changes. > * @param fsl jhotdraw.framework.FigureSelectionListener > */ > public void addFigureSelectionListener(FigureSelectionListener fsl) { > fSelectionListeners.add(fsl); > } > > /** > * Remove a listener for selection changes. > * @param fsl jhotdraw.framework.FigureSelectionListener > */ > public void removeFigureSelectionListener(FigureSelectionListener fsl) { > fSelectionListeners.remove(fsl); > } > > protected void setUndoManager(UndoManager newUndoManager) { > myUndoManager = newUndoManager; > } > > public UndoManager getUndoManager() { > return myUndoManager; > }