2c2 < * @(#)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 > import CH.ifa.draw.figures.TextTool; 17a25 > * 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 > setUndoManager(new UndoManager()); 132c167 < 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))); > } > > /** 172c244,252 < return drawing().add(figure); --- > 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; 188c268 < 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; > } > > /** 228c427 < return new FigureEnumerator(fSelection); --- > return new FigureEnumerator(selectionZOrdered()); 238c437 < Vector result = new Vector(fSelection.size()); --- > Vector result = new Vector(selectionCount()); 243c442 < if (fSelection.contains(f)) { --- > if (isFigureSelected(f)) { 258c457,465 < * Adds a figure to the current selection. --- > * Test whether a given figure is selected. > */ > public boolean isFigureSelected(Figure checkFigure) { > return fSelection.contains(checkFigure); > } > > /** > * Adds a figure to the current selection. The figure is only selected if > * it is also contained in the Drawing associated with this DrawingView. 261c468 < if (!fSelection.contains(figure)) { --- > if (!isFigureSelected(figure) && drawing().includes(figure)) { 265c472 < selectionChanged(); --- > fireSelectionChanged(); 273,275c480,489 < FigureEnumeration k = new FigureEnumerator(figures); < while (k.hasMoreElements()) < addToSelection(k.nextFigure()); --- > addToSelectionAll(new FigureEnumerator(figures)); > } > > /** > * Adds a FigureEnumeration to the current selection. > */ > public void addToSelectionAll(FigureEnumeration fe) { > while (fe.hasMoreElements()) { > addToSelection(fe.nextFigure()); > } 282c496 < if (fSelection.contains(figure)) { --- > if (isFigureSelected(figure)) { 286c500 < selectionChanged(); --- > fireSelectionChanged(); 295c509 < if (fSelection.contains(figure)) --- > if (isFigureSelected(figure)) { 297c511,512 < else --- > } > else { 299c514,515 < selectionChanged(); --- > } > fireSelectionChanged(); 310c526 < while (k.hasMoreElements()) --- > while (k.hasMoreElements()) { 311a528 > } 314c531 < selectionChanged(); --- > fireSelectionChanged(); 327c544 < while (kk.hasMoreElements()) --- > while (kk.hasMoreElements()) { 330a548 > } 339c557 < return new FigureSelection(selectionZOrdered()); --- > return new StandardFigureSelection(new FigureEnumerator(selectionZOrdered()), selectionCount()); 352c570 < if (handle.containsPoint(x, y)) --- > if (handle.containsPoint(x, y)) { 354a573 > } 363,364c582,588 < protected void selectionChanged() { < fEditor.selectionChanged(this); --- > protected void fireSelectionChanged() { > if (fSelectionListeners != null) { > for (int i = 0; i < fSelectionListeners.size(); i++) { > FigureSelectionListener l = (FigureSelectionListener)fSelectionListeners.elementAt(i); > l.figureSelectionChanged(this); > } > } 399c623 < if (fConstrainer != null ) --- > if (fConstrainer != null ) { 400a625 > } 459c684,685 < } else if (code == KeyEvent.VK_DOWN || code == KeyEvent.VK_UP || --- > } > else if (code == KeyEvent.VK_DOWN || code == KeyEvent.VK_UP || 462c688,689 < } else { --- > } > else { 498d724 < 501c727 < while (figures.hasMoreElements()) --- > while (figures.hasMoreElements()) { 502a729 > } 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,521c756,768 < if (fDamage != null) { < repaint(fDamage.x, fDamage.y, fDamage.width, fDamage.height); --- > if (fDamage == null) { > // 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); > } > > repaint(1, fDamage.x, fDamage.y, fDamage.width, fDamage.height); 528c775 < if (fDamage == null) --- > if (fDamage == null) { 530c777,778 < else --- > } > else { 532a781 > } 539,545d787 < * Updates the drawing view. < */ < public void update(Graphics g) { < paint(g); < } < < /** 550,551c792,793 < public void paint(Graphics g) { < fUpdateStrategy.draw(g, this); --- > protected void paintComponent(Graphics g) { > getDisplayUpdate().draw(g, this); 562c804 < if (fBackgrounds != null && !isPrinting) --- > if (fBackgrounds != null && !isPrinting) { 563a806 > } 565c808 < if (fForegrounds != null && !isPrinting) --- > if (fForegrounds != null && !isPrinting) { 567c810,811 < if (!isPrinting) --- > } > if (!isPrinting) { 569a814 > } 580c825 < if (fBackgrounds != null && !isPrinting) --- > if (fBackgrounds != null && !isPrinting) { 581a827 > } 583c829 < if (fForegrounds != null && !isPrinting) --- > if (fForegrounds != null && !isPrinting) { 585c831,832 < if (!isPrinting) --- > } > if (!isPrinting) { 587a835 > } 594c842 < while (k.hasMoreElements()) --- > while (k.hasMoreElements()) { 596a845 > } 616c865 < for (int i = 0; i < v.size(); i++) --- > for (int i = 0; i < v.size(); i++) { 618a868 > } 624c874 < if (fBackgrounds == null) --- > if (fBackgrounds == null) { 625a876 > } 634c885 < if (fBackgrounds != null) --- > if (fBackgrounds != null) { 635a887 > } 643c895 < if (fForegrounds != null) --- > if (fForegrounds != null) { 644a897 > } 652c905 < if (fForegrounds == null) --- > if (fForegrounds == null) { 653a907 > } 680c934 < if (fDrawing != null) --- > if (fDrawing != null) { 682a937,938 > fSelectionListeners= new Vector(); > } 693,694c949,950 < fViewSize.height = d.height+10; < fViewSize.width = d.width+10; --- > fViewSize.height = d.height + SCROLL_OFFSET; > 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; > }