@Override public void execute() { // duplicate the selection final List<SNode> dupes = new ArrayList<SNode>(); SketchDocument doc = (SketchDocument) context.getDocument(); for (SNode node : context.getSelection().sortedItems(doc)) { SNode dupe = node.duplicate(null); if (offset) { dupe.setTranslateX(dupe.getTranslateX() + 100); dupe.setTranslateY(dupe.getTranslateY() + 100); } dupes.add(dupe); } // make it undoable context .getUndoManager() .pushAction( new UndoManager.UndoableAction() { public void executeUndo() { SketchDocument doc = (SketchDocument) context.getDocument(); for (SNode dupe : dupes) { doc.getCurrentPage().remove(dupe); } context.getSelection().clear(); } public void executeRedo() { SketchDocument doc = (SketchDocument) context.getDocument(); for (SNode dupe : dupes) { doc.getCurrentPage().add(dupe); doc.setDirty(true); } } public String getName() { return "duplicate"; } }); // clear selection context.getSelection().clear(); // add to the doc for (SNode dupe : dupes) { doc.getCurrentPage().add(dupe); doc.setDirty(true); context.getSelection().addSelectedNode(dupe); } }
@Override public void execute() { if (context.getSelection().size() < 2) return; SketchDocument doc = context.getDocument(); final List<SNode> model = doc.getCurrentPage().getModel(); final List<SNode> nodes = new ArrayList<SNode>(); for (SNode node : model) { if (context.getSelection().contains(node)) { nodes.add(node); } } model.removeAll(nodes); final SGroup group = new SGroup(); group.addAll(nodes); model.add(group); context.getSelection().clear(); context.getSelection().setSelectedNode(group); context.redraw(); UndoManager.UndoableAction action = new UndoManager.UndoableAction() { public void executeUndo() { model.remove(group); for (SNode node : nodes) { model.add(node); node.setTranslateX(node.getTranslateX() + group.getTranslateX()); node.setTranslateY(node.getTranslateY() + group.getTranslateY()); } context.getSelection().setSelectedNodes(nodes); context.redraw(); } public void executeRedo() { model.removeAll(nodes); group.addAll(nodes); model.add(group); context.getSelection().setSelectedNode(group); context.redraw(); } public CharSequence getName() { return "group shapes"; } }; context.getUndoManager().pushAction(action); }
@Override public void execute() { if (context.getSelection().size() != 1) return; SNode n = context.getSelection().items().iterator().next(); if (!(n instanceof SGroup)) return; final SGroup group = (SGroup) n; SketchDocument doc = context.getDocument(); final List<SNode> model = doc.getCurrentPage().getModel(); model.remove(group); model.addAll(group.getNodes()); context.getSelection().clear(); for (SNode node : group.getNodes()) { node.setTranslateX(node.getTranslateX() + group.getTranslateX()); node.setTranslateY(node.getTranslateY() + group.getTranslateY()); context.getSelection().addSelectedNode(node); } context.redraw(); UndoManager.UndoableAction action = new UndoManager.UndoableAction() { public void executeUndo() { model.removeAll(group.getNodes()); for (SNode node : group.getNodes()) { node.setTranslateX(node.getTranslateX() - group.getTranslateX()); node.setTranslateY(node.getTranslateY() - group.getTranslateY()); } model.add(group); context.getSelection().setSelectedNode(group); context.redraw(); } public void executeRedo() { model.remove(group); for (SNode node : group.getNodes()) { model.add(node); node.setTranslateX(node.getTranslateX() + group.getTranslateX()); node.setTranslateY(node.getTranslateY() + group.getTranslateY()); } context.getSelection().setSelectedNodes(group.getNodes()); context.redraw(); } public CharSequence getName() { return "ungroup shapes"; } }; context.getUndoManager().pushAction(action); }