2c2 < * @(#)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 7a14,16 > import CH.ifa.draw.framework.*; > import CH.ifa.draw.util.UndoableAdapter; > import CH.ifa.draw.util.Undoable; 11,13d19 < import CH.ifa.draw.util.Command; < import CH.ifa.draw.framework.*; < 15a22,23 > * > * @version <$CURRENT_VERSION$> 17,20c25 < public class AlignCommand extends Command { < < private DrawingView fView; < private int fOp; --- > public class AlignCommand extends AbstractCommand { 21a27 > public static abstract class Alignment { 25c31,37 < public final static int LEFTS = 0; --- > 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); > } > }; > 29c41,47 < public final static int CENTERS = 1; --- > 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); > } > }; > 33c51,57 < public final static int RIGHTS = 2; --- > 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); > } > }; > 37c61,67 < public final static int TOPS = 3; --- > 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); > } > }; > 41c71,77 < public final static int MIDDLES = 4; --- > public final static Alignment MIDDLES = new Alignment("Middles") { > public void moveBy(Figure f, Rectangle anchor) { > Rectangle rr = f.displayBox(); > f.moveBy(0, (anchor.y+anchor.height/2) - (rr.y+rr.height/2)); > } > }; > 45c81,86 < public final static int BOTTOMS = 5; --- > 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)); > } > }; 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; 50c113 < * @param name the command name --- > * @param newAlignment the alignment operation (LEFTS, CENTERS, RIGHTS, etc.) 52d114 < * @param op the alignment operation (LEFTS, CENTERS, RIGHTS, etc.) 54,57c116,118 < public AlignCommand(String name, DrawingView view, int op) { < super(name); < fView = view; < fOp = op; --- > public AlignCommand(Alignment newAlignment, DrawingView view) { > super(newAlignment.getDescription(), view); > setAlignment(newAlignment); 61c122 < return fView.selectionCount() > 1; --- > return view().selectionCount() > 1; 65,67c126,131 < FigureEnumeration selection = fView.selectionElements(); < Figure anchorFigure = selection.nextFigure(); < Rectangle r = anchorFigure.displayBox(); --- > 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(); > } 69,90c133,134 < while (selection.hasMoreElements()) { < Figure f = selection.nextFigure(); < Rectangle rr = f.displayBox(); < 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: < f.moveBy(0, (r.y+r.height/2) - (rr.y+rr.height/2)); < break; < case BOTTOMS: < f.moveBy(0, (r.y+r.height) - (rr.y+rr.height)); < break; --- > protected void setAlignment(Alignment newAlignment) { > myAlignment = newAlignment; 91a136,138 > > public Alignment getAlignment() { > return myAlignment; 93c140,145 < fView.checkDamage(); --- > > /** > * Factory method for undo activity > */ > protected Undoable createUndoActivity() { > return new AlignCommand.UndoActivity(view(), getAlignment()); 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()); > } > } > } > }