@Override public void duplicate() { Selection sel = canvas.getSelection(); int n = sel.getSelected().size(); List<CanvasObject> select = new ArrayList<CanvasObject>(n); List<CanvasObject> clones = new ArrayList<CanvasObject>(n); for (CanvasObject o : sel.getSelected()) { if (o.canRemove()) { CanvasObject copy = o.clone(); copy.translate(10, 10); clones.add(copy); select.add(copy); } else { select.add(o); } } if (!clones.isEmpty()) { canvas .getProject() .doAction( new SelectionAction( canvas, Strings.getter("duplicateSelectionAction"), null, clones, select, null, null)); } }
@Override public void paste() { ClipboardContents clip = Clipboard.get(); Collection<CanvasObject> contents = clip.getElements(); List<CanvasObject> add = new ArrayList<CanvasObject>(contents.size()); for (CanvasObject o : contents) { add.add(o.clone()); } if (add.isEmpty()) return; // find how far we have to translate shapes so that at least one of the // pasted shapes doesn't match what's already in the model Collection<CanvasObject> raw = canvas.getModel().getObjectsFromBottom(); MatchingSet<CanvasObject> cur = new MatchingSet<CanvasObject>(raw); int dx = 0; while (true) { // if any shapes in "add" aren't in canvas, we are done boolean allMatch = true; for (CanvasObject o : add) { if (!cur.contains(o)) { allMatch = false; break; } } if (!allMatch) break; // otherwise translate everything by 10 pixels and repeat test for (CanvasObject o : add) { o.translate(10, 10); } dx += 10; } Location anchorLocation = clip.getAnchorLocation(); if (anchorLocation != null && dx != 0) { anchorLocation = anchorLocation.translate(dx, dx); } canvas .getProject() .doAction( new SelectionAction( canvas, Strings.getter("pasteClipboardAction"), null, add, add, anchorLocation, clip.getAnchorFacing())); }