public void setSelected(Collection<CanvasObject> shapes, boolean value) { if (value) { ArrayList<CanvasObject> added; added = new ArrayList<>(shapes.size()); for (CanvasObject shape : shapes) { if (selected.add(shape)) { added.add(shape); } } if (!added.isEmpty()) { fireChanged(SelectionEvent.ACTION_ADDED, added); } } else { ArrayList<CanvasObject> removed; removed = new ArrayList<>(shapes.size()); for (CanvasObject shape : shapes) { if (selected.remove(shape)) { suppressed.remove(shape); Handle h = selectedHandle; if (h != null && h.getObject() == shape) setHandleSelected(null); removed.add(shape); } } if (!removed.isEmpty()) { fireChanged(SelectionEvent.ACTION_REMOVED, removed); } } }
void modelChanged(CanvasModelEvent event) { int action = event.getAction(); switch (action) { case CanvasModelEvent.ACTION_REMOVED: Collection<? extends CanvasObject> affected = event.getAffected(); if (affected != null) { selected.removeAll(affected); suppressed.keySet().removeAll(affected); Handle h = selectedHandle; if (h != null && affected.contains(h.getObject())) { setHandleSelected(null); } } break; case CanvasModelEvent.ACTION_HANDLE_DELETED: if (event.getHandle().equals(selectedHandle)) { setHandleSelected(null); } break; case CanvasModelEvent.ACTION_HANDLE_MOVED: HandleGesture gesture = event.getHandleGesture(); if (gesture.getHandle().equals(selectedHandle)) { setHandleSelected(gesture.getResultingHandle()); } } }
public void setHandleGesture(HandleGesture gesture) { HandleGesture g = curHandleGesture; if (g != null) suppressed.remove(g.getHandle().getObject()); Handle h = gesture.getHandle(); suppressed.put(h.getObject(), MOVING_HANDLE); curHandleGesture = gesture; }
public void setHandleSelected(Handle handle) { Handle cur = selectedHandle; boolean same = cur == null ? handle == null : cur.equals(handle); if (!same) { selectedHandle = handle; curHandleGesture = null; Collection<CanvasObject> objs; if (handle == null) { objs = Collections.emptySet(); } else { objs = Collections.singleton(handle.getObject()); } fireChanged(SelectionEvent.ACTION_HANDLE, objs); } }
@Override public Collection<CanvasObject> getObjects() { return Collections.singleton(handle.getObject()); }
@Override public void computeEnabled() { Project proj = canvas.getProject(); Circuit circ = canvas.getCircuit(); Selection sel = canvas.getSelection(); boolean selEmpty = sel.isEmpty(); boolean canChange = proj.getLogisimFile().contains(circ); boolean clipExists = !Clipboard.isEmpty(); boolean selHasRemovable = false; for (CanvasObject o : sel.getSelected()) { if (!(o instanceof AppearanceElement)) { selHasRemovable = true; } } boolean canRaise; boolean canLower; if (!selEmpty && canChange) { Map<CanvasObject, Integer> zs = ZOrder.getZIndex(sel.getSelected(), canvas.getModel()); int zmin = Integer.MAX_VALUE; int zmax = Integer.MIN_VALUE; int count = 0; for (Map.Entry<CanvasObject, Integer> entry : zs.entrySet()) { if (!(entry.getKey() instanceof AppearanceElement)) { count++; int z = entry.getValue().intValue(); if (z < zmin) zmin = z; if (z > zmax) zmax = z; } } int maxPoss = AppearanceCanvas.getMaxIndex(canvas.getModel()); if (count > 0 && count <= maxPoss) { canRaise = zmin <= maxPoss - count; canLower = zmax >= count; } else { canRaise = false; canLower = false; } } else { canRaise = false; canLower = false; } boolean canAddCtrl = false; boolean canRemCtrl = false; Handle handle = sel.getSelectedHandle(); if (handle != null && canChange) { CanvasObject o = handle.getObject(); canAddCtrl = o.canInsertHandle(handle.getLocation()) != null; canRemCtrl = o.canDeleteHandle(handle.getLocation()) != null; } setEnabled(LogisimMenuBar.CUT, selHasRemovable && canChange); setEnabled(LogisimMenuBar.COPY, !selEmpty); setEnabled(LogisimMenuBar.PASTE, canChange && clipExists); setEnabled(LogisimMenuBar.DELETE, selHasRemovable && canChange); setEnabled(LogisimMenuBar.DUPLICATE, !selEmpty && canChange); setEnabled(LogisimMenuBar.SELECT_ALL, true); setEnabled(LogisimMenuBar.RAISE, canRaise); setEnabled(LogisimMenuBar.LOWER, canLower); setEnabled(LogisimMenuBar.RAISE_TOP, canRaise); setEnabled(LogisimMenuBar.LOWER_BOTTOM, canLower); setEnabled(LogisimMenuBar.ADD_CONTROL, canAddCtrl); setEnabled(LogisimMenuBar.REMOVE_CONTROL, canRemCtrl); }
private CanvasModelEvent(CanvasModel source, int action, Handle handle) { this(source, action, Collections.singleton(handle.getObject())); this.handle = handle; }