2,2c2,2 < * @(#)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 10a15,16 > import CH.ifa.draw.standard.*; > import CH.ifa.draw.util.UndoableAdapter; 10,10c14,14 < import CH.ifa.draw.framework.*; --- > import CH.ifa.draw.framework.*; 11,11c17,17 < import CH.ifa.draw.util.Command; --- > import CH.ifa.draw.util.Undoable; 14a23,23 > * 15a25,26 > * > * @version <$CURRENT_VERSION$> 17,17c28,28 < public class UngroupCommand extends Command { --- > public class UngroupCommand extends AbstractCommand { 18,19d28 < < private DrawingView fView; 27,27c36,36 < super(name); --- > super(name, view); 28,28d36 < fView = view; 31a40,43 > setUndoActivity(createUndoActivity()); > // selection of group figures > getUndoActivity().setAffectedFigures(view().selectionElements()); > view().clearSelection(); 32,33d39 < FigureEnumeration selection = fView.selectionElements(); < fView.clearSelection(); 34a45,46 > ((UngroupCommand.UndoActivity)getUndoActivity()).ungroupFigures(); > view().checkDamage(); 35,43d44 < 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(); 46a50,59 > 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; > } 47,47d49 < return fView.selectionCount() > 0; 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()); > } > } > } > }