2,2c2,2 < * @(#)AlignCommand.java 5.2 --- > * @(#)AlignCommand.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 11,11d13 < import CH.ifa.draw.util.Command; 12,12c14,14 < import CH.ifa.draw.framework.*; --- > import CH.ifa.draw.framework.*; 12a15,16 > import CH.ifa.draw.util.UndoableAdapter; > import CH.ifa.draw.util.Undoable; 13,13d14 < 15a22,23 > * > * @version <$CURRENT_VERSION$> 17,17c25,25 < public class AlignCommand extends Command { --- > public class AlignCommand extends AbstractCommand { 18,20d25 < < private DrawingView fView; < private int fOp; 21a27,27 > public static abstract class Alignment { 24a31,37 > public final static Alignment LEFTS = new Alignment("Lefts") { > public void moveBy(Figure f, Rectangle anchor) { > Rectangle rr = f.displayBox(); > f.moveBy(anchor.x-rr.x, 0); > } > }; > 25,25d30 < public final static int LEFTS = 0; 28a41,47 > public final static Alignment CENTERS = new Alignment("Centers") { > public void moveBy(Figure f, Rectangle anchor) { > Rectangle rr = f.displayBox(); > f.moveBy((anchor.x+anchor.width/2) - (rr.x+rr.width/2), 0); > } > }; > 29,29d40 < public final static int CENTERS = 1; 32a51,57 > public final static Alignment RIGHTS = new Alignment("Rights") { > public void moveBy(Figure f, Rectangle anchor) { > Rectangle rr = f.displayBox(); > f.moveBy((anchor.x+anchor.width) - (rr.x+rr.width), 0); > } > }; > 33,33d50 < public final static int RIGHTS = 2; 36a61,67 > public final static Alignment TOPS = new Alignment("Tops") { > public void moveBy(Figure f, Rectangle anchor) { > Rectangle rr = f.displayBox(); > f.moveBy(0, anchor.y-rr.y); > } > }; > 37,37d60 < public final static int TOPS = 3; 41,41d70 < public final static int MIDDLES = 4; 44a81,86 > public final static Alignment BOTTOMS = new Alignment("Bottoms") { > public void moveBy(Figure f, Rectangle anchor) { > Rectangle rr = f.displayBox(); > f.moveBy(0, (anchor.y+anchor.height) - (rr.y+rr.height)); > } > }; 45,45d80 < public final static int BOTTOMS = 5; 46a88,109 > private String myDescription; > > private Alignment(String newDescription) { > setDescription(newDescription); > } > > public String toString() { > return getDescription(); > } > > public String getDescription() { > return myDescription; > } > > private void setDescription(String newDescription) { > myDescription = newDescription; > } > > public abstract void moveBy(Figure f, Rectangle anchor); > } > > private Alignment myAlignment; 50,50d112 < * @param name the command name 52,52c113,113 < * @param op the alignment operation (LEFTS, CENTERS, RIGHTS, etc.) --- > * @param newAlignment the alignment operation (LEFTS, CENTERS, RIGHTS, etc.) 53a116,118 > public AlignCommand(Alignment newAlignment, DrawingView view) { > super(newAlignment.getDescription(), view); > setAlignment(newAlignment); 54,57d115 < public AlignCommand(String name, DrawingView view, int op) { < super(name); < fView = view; < fOp = op; 61,61c122,122 < return fView.selectionCount() > 1; --- > return view().selectionCount() > 1; 64a126,131 > setUndoActivity(createUndoActivity()); > // get selected figures in the order the figures have been selected > getUndoActivity().setAffectedFigures(new FigureEnumerator(view().selection())); > ((AlignCommand.UndoActivity)getUndoActivity()).alignAffectedFigures(getAlignment()); > view().checkDamage(); > } 65,67d125 < FigureEnumeration selection = fView.selectionElements(); < Figure anchorFigure = selection.nextFigure(); < Rectangle r = anchorFigure.displayBox(); 68a71,72 > public final static Alignment MIDDLES = new Alignment("Middles") { > public void moveBy(Figure f, Rectangle anchor) { 68a133,134 > protected void setAlignment(Alignment newAlignment) { > myAlignment = newAlignment; 69,70d70 < while (selection.hasMoreElements()) { < Figure f = selection.nextFigure(); 71,71c73,73 < Rectangle rr = f.displayBox(); --- > Rectangle rr = f.displayBox(); 72,85d73 < switch (fOp) { < case LEFTS: < f.moveBy(r.x-rr.x, 0); < break; < case CENTERS: < f.moveBy((r.x+r.width/2) - (rr.x+rr.width/2), 0); < break; < case RIGHTS: < f.moveBy((r.x+r.width) - (rr.x+rr.width), 0); < break; < case TOPS: < f.moveBy(0, r.y-rr.y); < break; < case MIDDLES: 86a75,77 > } > }; > 86,86c74,74 < f.moveBy(0, (r.y+r.height/2) - (rr.y+rr.height/2)); --- > f.moveBy(0, (anchor.y+anchor.height/2) - (rr.y+rr.height/2)); 87,90d74 < break; < case BOTTOMS: < f.moveBy(0, (r.y+r.height) - (rr.y+rr.height)); < break; 91a136,138 > > public Alignment getAlignment() { > return myAlignment; 92a140,145 > > /** > * Factory method for undo activity > */ > protected Undoable createUndoActivity() { > return new AlignCommand.UndoActivity(view(), getAlignment()); 93,93d139 < fView.checkDamage(); 94a147,157 > > public static class UndoActivity extends UndoableAdapter { > private Hashtable myOriginalPoints; > private Alignment myAppliedAlignment; > > public UndoActivity(DrawingView newView, Alignment newAlignment) { > super(newView); > myOriginalPoints = new Hashtable(); > setAppliedAlignment(newAlignment); > setUndoable(true); > setRedoable(true); 96a160,163 > public boolean undo() { > if (!super.undo()) { > return false; > } 97a165,223 > FigureEnumeration k = getAffectedFigures(); > while (k.hasMoreElements()) { > Figure f = k.nextFigure(); > Point originalPoint = getOriginalPoint(f); > Point currentPoint = f.displayBox().getLocation(); > // substract current lcoation to get to 0,0 and then move to original location > f.moveBy(-currentPoint.x + originalPoint.x, > -currentPoint.y + originalPoint.y); > } > > return true; > } > > public boolean redo() { > if (!isRedoable()) { > return false; > } > alignAffectedFigures(getAppliedAlignment()); > return true; > } > > protected void setAppliedAlignment(Alignment newAlignment) { > myAppliedAlignment = newAlignment; > } > > public Alignment getAppliedAlignment() { > return myAppliedAlignment; > } > > protected void addOriginalPoint(Figure f) { > myOriginalPoints.put(f, f.displayBox().getLocation()); > } > > public Point getOriginalPoint(Figure f) { > return (Point)myOriginalPoints.get(f); > } > > public void alignAffectedFigures(Alignment applyAlignment) { > FigureEnumeration fe = getAffectedFigures(); > Figure anchorFigure = fe.nextFigure(); > Rectangle r = anchorFigure.displayBox(); > > while (fe.hasMoreElements()) { > Figure f = fe.nextFigure(); > applyAlignment.moveBy(f, r); > } > } > > public void setAffectedFigures(FigureEnumeration fe) { > // first make copy of FigureEnumeration in superclass > super.setAffectedFigures(fe); > // then get new FigureEnumeration of copy to save aligment > FigureEnumeration copyFe = getAffectedFigures(); > while (copyFe.hasMoreElements()) { > addOriginalPoint(copyFe.nextFigure()); > } > } > } > }