2,2c2,2 < * @(#)ChangeAttributeCommand.java 5.2 --- > * @(#)ChangeAttributeCommand.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,8d13 < import java.awt.Color; 9a15,15 > import CH.ifa.draw.util.UndoableAdapter; 10,10c16,16 < import CH.ifa.draw.util.*; --- > import CH.ifa.draw.util.Undoable; 10a17,18 > import java.awt.Color; > import java.util.Hashtable; 13a22,23 > * > * @version <$CURRENT_VERSION$> 15,15c25,25 < public class ChangeAttributeCommand --- > public class ChangeAttributeCommand extends AbstractCommand { 16,16d25 < extends Command { 18,18d26 < private DrawingView fView; 31,31c39,39 < super(name); --- > super(name, view); 34,34d41 < fView = view; 37a45,84 > setUndoActivity(createUndoActivity()); > getUndoActivity().setAffectedFigures(view().selectionElements()); > FigureEnumeration fe = getUndoActivity().getAffectedFigures(); > while (fe.hasMoreElements()) { > fe.nextFigure().setAttribute(fAttribute, fValue); > } > view().checkDamage(); > } > > public boolean isExecutable() { > return view().selectionCount() > 0; > } > > /** > * Factory method for undo activity > */ > protected Undoable createUndoActivity() { > return new ChangeAttributeCommand.UndoActivity(view(), fAttribute, fValue); > } > > public static class UndoActivity extends UndoableAdapter { > private Hashtable myOriginalValues; > private String myUndoAttribute; > private Object myUndoValue; > > public UndoActivity(DrawingView newDrawingView, String newUndoAttribute, Object newUndoValue) { > super(newDrawingView); > myOriginalValues = new Hashtable(); > setAttributeName(newUndoAttribute); > setBackupValue(newUndoValue); > setUndoable(true); > setRedoable(true); > } > > public boolean undo() { > if (!super.undo()) { > return false; > } > > FigureEnumeration k = getAffectedFigures(); 38,38d44 < FigureEnumeration k = fView.selectionElements(); 41,41c87,87 < f.setAttribute(fAttribute, fValue); --- > f.setAttribute(getAttributeName(), getOriginalValue(f)); 42a89,90 > > return true; 43,43d88 < fView.checkDamage(); 45a93,124 > public boolean redo() { > if (!isRedoable()) { > return false; > } > > FigureEnumeration k = getAffectedFigures(); > while (k.hasMoreElements()) { > Figure f = k.nextFigure(); > f.setAttribute(getAttributeName(), getBackupValue()); > } > > return true; > } > > protected void addOriginalValue(Figure affectedFigure, Object newOriginalValue) { > myOriginalValues.put(affectedFigure, newOriginalValue); > } > > protected Object getOriginalValue(Figure lookupAffectedFigure) { > return myOriginalValues.get(lookupAffectedFigure); > } > > protected void setAttributeName(String newUndoAttribute) { > myUndoAttribute = newUndoAttribute; > } > > public String getAttributeName() { > return myUndoAttribute; > } > > protected void setBackupValue(Object newUndoValue) { > myUndoValue = newUndoValue; 46,47d92 < public boolean isExecutable() { < return fView.selectionCount() > 0; 49a127,128 > public Object getBackupValue() { > return myUndoValue; 51a131,134 > public void release() { > super.release(); > myOriginalValues = null; > } 52a136,147 > public void setAffectedFigures(FigureEnumeration fe) { > // first make copy of FigureEnumeration in superclass > super.setAffectedFigures(fe); > // then get new FigureEnumeration of copy to save attributes > FigureEnumeration copyFe = getAffectedFigures(); > while (copyFe.hasMoreElements()) { > Figure f = copyFe.nextFigure(); > addOriginalValue(f, f.getAttribute(getAttributeName())); > } > } > } > }