2c2 < * @(#)GroupCommand.java 5.2 --- > * @(#)GroupCommand.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.util.*; < import CH.ifa.draw.util.Command; 11a16,18 > import CH.ifa.draw.util.UndoableAdapter; > import CH.ifa.draw.util.Undoable; > import java.util.*; 16a24,25 > * > * @version <$CURRENT_VERSION$> 18,20c27 < public class GroupCommand extends Command { < < private DrawingView fView; --- > public class GroupCommand extends AbstractCommand { 28,29c35 < super(name); < fView = view; --- > super(name, view); 33,37c39,43 < Vector selected = fView.selectionZOrdered(); < Drawing drawing = fView.drawing(); < if (selected.size() > 0) { < fView.clearSelection(); < drawing.orphanAll(selected); --- > setUndoActivity(createUndoActivity()); > getUndoActivity().setAffectedFigures(view().selectionElements()); > ((GroupCommand.UndoActivity)getUndoActivity()).groupFigures(); > view().checkDamage(); > } 39,41c45,46 < GroupFigure group = new GroupFigure(); < group.addAll(selected); < fView.addToSelection(drawing.add(group)); --- > public boolean isExecutable() { > return view().selectionCount() > 1; 43c48,53 < fView.checkDamage(); --- > > /** > * Factory method for undo activity > */ > protected Undoable createUndoActivity() { > return new GroupCommand.UndoActivity(view()); 46,47c56,60 < public boolean isExecutable() { < return fView.selectionCount() > 0; --- > public static class UndoActivity extends UndoableAdapter { > public UndoActivity(DrawingView newDrawingView) { > super(newDrawingView); > setUndoable(true); > setRedoable(true); 49a63,91 > public boolean undo() { > if (!super.undo()) { > return false; > } > > getDrawingView().clearSelection(); > > // duplicate before we orphan affected figures > // FigureEnumeration fe = StandardFigureSelection.duplicateFigures( > // getAffectedFigures(), getAffectedFiguresCount()); > > // orphan group figure(s) > // getDrawingView().drawing().removeAll(getAffectedFigures()); > getDrawingView().drawing().orphanAll(getAffectedFigures()); > > // create a new vector with the grouped figures as elements > Vector affectedFigures = new Vector(); > > FigureEnumeration fe =getAffectedFigures(); > while (fe.hasMoreElements()) { > Figure currentFigure = fe.nextFigure(); > // add contained figures > getDrawingView().drawing().addAll(currentFigure.figures()); > getDrawingView().addToSelectionAll(currentFigure.figures()); > > FigureEnumeration groupedFigures = currentFigure.figures(); > while (groupedFigures.hasMoreElements()) { > affectedFigures.addElement(groupedFigures.nextFigure()); > } 51a94,126 > setAffectedFigures(new FigureEnumerator(affectedFigures)); > > return true; > } > > public boolean redo() { > // do not call execute directly as the selection might has changed > if (isRedoable()) { > groupFigures(); > return true; > } > > return false; > } > > public void groupFigures() { > getDrawingView().drawing().orphanAll(getAffectedFigures()); > getDrawingView().clearSelection(); > > // add new group figure instead > GroupFigure group = new GroupFigure(); > group.addAll(getAffectedFigures()); > > Figure figure = getDrawingView().drawing().add(group); > getDrawingView().addToSelection(figure); > > // create a new vector with the new group figure as element > Vector affectedFigures = new Vector(); > affectedFigures.addElement(figure); > setAffectedFigures(new FigureEnumerator(affectedFigures)); > } > } > }