2c2 < * @(#)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 8,9d13 < import java.awt.*; < import java.awt.event.*; 12a17,21 > import CH.ifa.draw.util.UndoableAdapter; > import CH.ifa.draw.util.Undoable; > import java.awt.*; > import java.awt.event.*; > import java.util.Vector; 22a32,33 > * > * @version <$CURRENT_VERSION$> 40d50 < 52d61 < editor().toolDone(); 53a63 > editor().toolDone(); 56c66,70 < textHolder = (TextHolder)createdFigure(); --- > // 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(); 96c110 < if (fTextField == null) --- > if (fTextField == null) { 97a112 > } 99c114 < if (figure != getTypingTarget() && getTypingTarget() != null) --- > if (figure != getTypingTarget() && getTypingTarget() != null) { 100a116 > } 104d119 < setTypingTarget(figure); 106c121,123 < view().checkDamage(); --- > setTypingTarget(figure); > setUndoActivity(createUndoActivity()); > System.out.println("UndoActivity (1): " + getUndoActivity()); 111c128 < if (fTextField.getText().length() > 0) --- > if (fTextField.getText().length() > 0) { 112a130 > } 114c132,135 < drawing().remove((Figure)getTypingTarget()); --- > drawing().orphan((Figure)getAddedFigure()); > > // nothing to undo > // setUndoActivity(null); 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()); > 118c147 < view().checkDamage(); --- > // view().checkDamage(); 125a155 > //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; > } > }