2c2 < * @(#)UngroupCommand.java 5.2 --- > * @(#)UngroupCommand.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,17 > import CH.ifa.draw.framework.*; > import CH.ifa.draw.standard.*; > import CH.ifa.draw.util.UndoableAdapter; > import CH.ifa.draw.util.Undoable; 10,11d19 < import CH.ifa.draw.framework.*; < import CH.ifa.draw.util.Command; 14a23 > * 15a25,26 > * > * @version <$CURRENT_VERSION$> 17,19c28 < public class UngroupCommand extends Command { < < private DrawingView fView; --- > public class UngroupCommand extends AbstractCommand { 27,28c36 < super(name); < fView = view; --- > super(name, view); 32,33c40,43 < FigureEnumeration selection = fView.selectionElements(); < fView.clearSelection(); --- > setUndoActivity(createUndoActivity()); > // selection of group figures > getUndoActivity().setAffectedFigures(view().selectionElements()); > view().clearSelection(); 35,43c45,46 < Vector parts = new Vector(); < while (selection.hasMoreElements()) { < Figure selected = selection.nextFigure(); < Figure group = fView.drawing().orphan(selected); < FigureEnumeration k = group.decompose(); < while (k.hasMoreElements()) < fView.addToSelection(fView.add(k.nextFigure())); < } < fView.checkDamage(); --- > ((UngroupCommand.UndoActivity)getUndoActivity()).ungroupFigures(); > view().checkDamage(); 47c50,59 < return fView.selectionCount() > 0; --- > FigureEnumeration fe = view().selectionElements(); > while (fe.hasMoreElements()) { > Figure currentFigure = fe.nextFigure(); > if (currentFigure instanceof DecoratorFigure) { > currentFigure = ((DecoratorFigure)currentFigure).getDecoratedFigure(); > } > > if (!(currentFigure instanceof GroupFigure)) { > return false; > } 49a62,123 > return true; > // return view().selectionCount() > 0; > > } > > /** > * Factory method for undo activity > */ > protected Undoable createUndoActivity() { > return new UngroupCommand.UndoActivity(view()); > } > > public static class UndoActivity extends UndoableAdapter { > public UndoActivity(DrawingView newDrawingView) { > super(newDrawingView); > setUndoable(true); > setRedoable(true); > } > > public boolean undo() { > if (!super.undo()) { > return false; > } > getDrawingView().clearSelection(); > > FigureEnumeration groupFigures = getAffectedFigures(); > while (groupFigures.hasMoreElements()) { > Figure groupFigure = groupFigures.nextFigure(); > // orphan individual figures from the group > getDrawingView().drawing().orphanAll(groupFigure.figures()); > > Figure figure = getDrawingView().drawing().add(groupFigure); > getDrawingView().addToSelection(figure); > } > > return true; > } > > public boolean redo() { > // do not call execute directly as the selection might has changed > if (isRedoable()) { > getDrawingView().drawing().orphanAll(getAffectedFigures()); > getDrawingView().clearSelection(); > ungroupFigures(); > return true; > } > return false; > } > > protected void ungroupFigures() { > FigureEnumeration fe = getAffectedFigures(); > if (fe.hasMoreElements()) { > while (fe.hasMoreElements()) { > Figure selected = fe.nextFigure(); > Figure group = getDrawingView().drawing().orphan(selected); > > getDrawingView().drawing().addAll(group.figures()); > getDrawingView().addToSelectionAll(group.figures()); > } > } > } > }