2,2c2,2 < * @(#)TextTool.java 5.2 --- > * @(#)TextTool.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 7a17,18 > import CH.ifa.draw.util.UndoableAdapter; > import CH.ifa.draw.util.Undoable; 8,8c19,19 < import java.awt.*; --- > import java.awt.*; 9a21,21 > import java.util.Vector; 9,9c20,20 < import java.awt.event.*; --- > import java.awt.event.*; 22a32,33 > * > * @version <$CURRENT_VERSION$> 39a112,112 > } 40,40d111 < 52,52c63,63 < editor().toolDone(); --- > editor().toolDone(); 55a66,70 > // update view so the created figure is drawn before the floating text > // figure is overlaid. (Note, fDamage should be null in StandardDrawingView > // when the overlay figure is drawn because a JTextField cannot be scrolled) > view().checkDamage(); > textHolder = (TextHolder)getCreatedFigure(); 56,56d65 < textHolder = (TextHolder)createdFigure(); 96,96c110,110 < if (fTextField == null) --- > if (fTextField == null) { 99,99c114,114 < if (figure != getTypingTarget() && getTypingTarget() != null) --- > if (figure != getTypingTarget() && getTypingTarget() != null) { 100a116,116 > } 104,104d119 < setTypingTarget(figure); 105a121,123 > setTypingTarget(figure); > setUndoActivity(createUndoActivity()); > System.out.println("UndoActivity (1): " + getUndoActivity()); 106,106c147,147 < view().checkDamage(); --- > // view().checkDamage(); 111,111c128,128 < if (fTextField.getText().length() > 0) --- > if (fTextField.getText().length() > 0) { 112a130,130 > } 113a132,135 > drawing().orphan((Figure)getAddedFigure()); > > // nothing to undo > // setUndoActivity(null); 114,114d131 < drawing().remove((Figure)getTypingTarget()); 115a137,144 > > // put created figure into a figure enumeration > System.out.println("UndoActivity (2): " + getUndoActivity()); > getUndoActivity().setAffectedFigures( > new SingleFigureEnumerator(getAddedFigure())); > ((TextTool.UndoActivity)getUndoActivity()).setBackupText( > getTypingTarget().getText()); > 118,118d146 < view().checkDamage(); 125a155,155 > //System.out.println("fieldBounds: " + box.x + " .. " + box.y + " -- " + d.width +" .. "+ d.height); 135a166,283 > > /** > * Factory method for undo activity > */ > protected Undoable createUndoActivity() { > return new TextTool.UndoActivity(view(), getTypingTarget().getText()); > } > > public static class UndoActivity extends UndoableAdapter { > private String myOriginalText; > private String myBackupText; > > public UndoActivity(DrawingView newDrawingView, String newOriginalText) { > super(newDrawingView); > setOriginalText(newOriginalText); > setUndoable(true); > setRedoable(true); > } > > /* > * Undo the activity > * @return true if the activity could be undone, false otherwise > */ > public boolean undo() { > if (!super.undo()) { > return false; > } > > getDrawingView().clearSelection(); > > if (!isValidText(getOriginalText())) { > FigureEnumeration fe = getAffectedFigures(); > while (fe.hasMoreElements()) { > getDrawingView().drawing().orphan(fe.nextFigure()); > } > } > // add text figure if it has been removed (no backup text) > else if (!isValidText(getBackupText())) { > FigureEnumeration fe = getAffectedFigures(); > while (fe.hasMoreElements()) { > getDrawingView().add(fe.nextFigure()); > } > setText(getOriginalText()); > } > else { > setText(getOriginalText()); > } > > > return true; > } > > /* > * Redo the activity > * @return true if the activity could be redone, false otherwise > */ > public boolean redo() { > if (!super.redo()) { > return false; > } > > getDrawingView().clearSelection(); > > // the text figure did exist but was remove > if (!isValidText(getBackupText())) { > FigureEnumeration fe = getAffectedFigures(); > while (fe.hasMoreElements()) { > getDrawingView().drawing().orphan(fe.nextFigure()); > } > } > // the text figure didn't exist before > else if (!isValidText(getOriginalText())) { > FigureEnumeration fe = getAffectedFigures(); > while (fe.hasMoreElements()) { > getDrawingView().drawing().add(fe.nextFigure()); > setText(getBackupText()); > } > } > else { > setText(getBackupText()); > } > > return true; > } > > protected boolean isValidText(String toBeChecked) { > return ((toBeChecked != null) && (toBeChecked.length() > 0)); > } > > protected void setText(String newText) { > FigureEnumeration fe = getAffectedFigures(); > while (fe.hasMoreElements()) { > Figure currentFigure = fe.nextFigure(); > if (currentFigure instanceof DecoratorFigure) { > currentFigure = ((DecoratorFigure)currentFigure).getDecoratedFigure(); > } > if (currentFigure instanceof TextHolder) { > ((TextHolder)currentFigure).setText(newText); > } > } > } > > public void setBackupText(String newBackupText) { > myBackupText = newBackupText; > } > > public String getBackupText() { > return myBackupText; > } > > public void setOriginalText(String newOriginalText) { > myOriginalText = newOriginalText; > } > > public String getOriginalText() { > return myOriginalText; > } > }