CircuitPopup(Project proj, Tool tool, Circuit circuit) { super(_("circuitMenu")); this.proj = proj; this.circuit = circuit; add(editLayout); editLayout.addActionListener(this); add(editAppearance); editAppearance.addActionListener(this); add(analyze); analyze.addActionListener(this); add(stats); stats.addActionListener(this); addSeparator(); add(main); main.addActionListener(this); add(remove); remove.addActionListener(this); boolean canChange = proj.getLogisimFile().contains(circuit); LogisimFile file = proj.getLogisimFile(); if (circuit == proj.getCurrentCircuit()) { if (proj.getFrame().getEditorView().equals(Frame.EDIT_APPEARANCE)) { editAppearance.setEnabled(false); } else { editLayout.setEnabled(false); } } main.setEnabled(canChange && file.getMainCircuit() != circuit); remove.setEnabled(canChange && file.getCircuitCount() > 1 && proj.getDependencies().canRemove(circuit)); }
public void actionPerformed(ActionEvent e) { Object src = e.getSource(); Project proj = menubar.getProject(); if (src == newi) { ProjectActions.doNew(proj); } else if (src == merge) { ProjectActions.doMerge(proj == null ? null : proj.getFrame().getCanvas(), proj); } else if (src == open) { ProjectActions.doOpen(proj == null ? null : proj.getFrame().getCanvas(), proj); } else if (src == close) { int result = 0; Frame frame = proj.getFrame(); if (proj.isFileDirty()) { /* Must use hardcoded strings here, because the string management is rotten */ String message = "What should happen to your unsaved changes to " + proj.getLogisimFile().getName(); String[] options = {"Save", "Discard", "Cancel"}; result = JOptionPane.showOptionDialog( JOptionPane.getFrameForComponent(this), message, "Confirm Close", 0, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); if (result == 0) { ProjectActions.doSave(proj); } } /* If "cancel" pressed do nothing, otherwise dispose the window, opening one if this was the last opened window */ if (result != 2) { // Get the list of open projects List<Project> pl = Projects.getOpenProjects(); if (pl.size() == 1) { // Since we have a single window open, before closing the // current // project open a new empty one ProjectActions.doNew(proj); } // Close the current project frame.dispose(); OptionsFrame f = proj.getOptionsFrame(false); if (f != null) f.dispose(); } } else if (src == save) { ProjectActions.doSave(proj); } else if (src == saveAs) { ProjectActions.doSaveAs(proj); } else if (src == prefs) { PreferencesFrame.showPreferences(); } else if (src == quit) { ProjectActions.doQuit(); } }
@Override public void actionPerformed(ActionEvent e) { Object src = e.getSource(); if (src == unload) { ProjectLibraryActions.doUnloadLibrary(proj, lib); } else if (src == reload) { Loader loader = proj.getLogisimFile().getLoader(); loader.reload((LoadedLibrary) lib); } }
@Override public void mouseReleased(Canvas canvas, Graphics g, MouseEvent e) { Project proj = canvas.getProject(); if (state == MOVING) { setState(proj, IDLE); computeDxDy(proj, e, g); int dx = curDx; int dy = curDy; if (dx != 0 || dy != 0) { if (!proj.getLogisimFile().contains(canvas.getCircuit())) { canvas.setErrorMessage(Strings.getter("cannotModifyError")); } else if (proj.getSelection().hasConflictWhenMoved(dx, dy)) { canvas.setErrorMessage(Strings.getter("exclusiveError")); } else { boolean connect = shouldConnect(canvas, e.getModifiersEx()); drawConnections = false; ReplacementMap repl; if (connect) { MoveGesture gesture = moveGesture; if (gesture == null) { gesture = new MoveGesture( new MoveRequestHandler(canvas), canvas.getCircuit(), canvas.getSelection().getAnchoredComponents()); } canvas.setErrorMessage(new ComputingMessage(dx, dy), COLOR_COMPUTING); MoveResult result = gesture.forceRequest(dx, dy); clearCanvasMessage(canvas, dx, dy); repl = result.getReplacementMap(); } else { repl = null; } Selection sel = proj.getSelection(); proj.doAction(SelectionActions.translate(sel, dx, dy, repl)); } } moveGesture = null; proj.repaintCanvas(); } else if (state == RECT_SELECT) { Bounds bds = Bounds.create(start).add(start.getX() + curDx, start.getY() + curDy); Circuit circuit = canvas.getCircuit(); Selection sel = proj.getSelection(); Collection<Component> in_sel = sel.getComponentsWithin(bds, g); for (Component comp : circuit.getAllWithin(bds, g)) { if (!in_sel.contains(comp)) sel.add(comp); } Action act = SelectionActions.drop(sel, in_sel); if (act != null) { proj.doAction(act); } setState(proj, IDLE); proj.repaintCanvas(); } }
@Override public void doAction(Action canvasAction) { Circuit circuit = circuitState.getCircuit(); if (!proj.getLogisimFile().contains(circuit)) { return; } if (canvasAction instanceof ModelReorderAction) { int max = getMaxIndex(getModel()); ModelReorderAction reorder = (ModelReorderAction) canvasAction; List<ReorderRequest> rs = reorder.getReorderRequests(); List<ReorderRequest> mod = new ArrayList<ReorderRequest>(rs.size()); boolean changed = false; boolean movedToMax = false; for (ReorderRequest r : rs) { CanvasObject o = r.getObject(); if (o instanceof AppearanceElement) { changed = true; } else { if (r.getToIndex() > max) { int from = r.getFromIndex(); changed = true; movedToMax = true; if (from == max && !movedToMax) { // this change is ineffective - don't add it } else { mod.add(new ReorderRequest(o, from, max)); } } else { if (r.getToIndex() == max) movedToMax = true; mod.add(r); } } } if (changed) { if (mod.isEmpty()) { return; } canvasAction = new ModelReorderAction(getModel(), mod); } } if (canvasAction instanceof ModelAddAction) { ModelAddAction addAction = (ModelAddAction) canvasAction; int cur = addAction.getDestinationIndex(); int max = getMaxIndex(getModel()); if (cur > max) { canvasAction = new ModelAddAction(getModel(), addAction.getObjects(), max + 1); } } proj.doAction(new CanvasActionAdapter(circuit, canvasAction)); }
@Override public void setValueRequested(Attribute<Object> attr, Object value) throws AttrTableSetException { if (!proj.getLogisimFile().contains(circ)) { String msg = _("cannotModifyCircuitError"); throw new AttrTableSetException(msg); } else { SetAttributeAction act = new SetAttributeAction(circ, __("changeAttributeAction")); act.set(comp, attr, value); proj.doAction(act); } }
@Override public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == editLayout) { proj.setCurrentCircuit(circuit); proj.getFrame().setEditorView(Frame.EDIT_LAYOUT); } else if (source == editAppearance) { proj.setCurrentCircuit(circuit); proj.getFrame().setEditorView(Frame.EDIT_APPEARANCE); } else if (source == analyze) { ProjectCircuitActions.doAnalyze(proj, circuit); } else if (source == stats) { JFrame frame = (JFrame) SwingUtilities.getRoot(this); StatisticsDialog.show(frame, proj.getLogisimFile(), circuit); } else if (source == main) { ProjectCircuitActions.doSetAsMainCircuit(proj, circuit); } else if (source == remove) { ProjectCircuitActions.doRemoveCircuit(proj, circuit); } }
@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); }
@Override public void mousePressed(Canvas canvas, Graphics g, MouseEvent e) { Project proj = canvas.getProject(); Circuit circ = canvas.getCircuit(); if (!proj.getLogisimFile().contains(circ)) { if (caret != null) caret.cancelEditing(); canvas.setErrorMessage(Strings.getter("cannotModifyError")); return; } // Maybe user is clicking within the current caret. if (caret != null) { if (caret.getBounds(g).contains(e.getX(), e.getY())) { // Yes caret.mousePressed(e); proj.repaintCanvas(); return; } else { // No. End the current caret. caret.stopEditing(); } } // caret will be null at this point // Otherwise search for a new caret. int x = e.getX(); int y = e.getY(); Location loc = Location.create(x, y); ComponentUserEvent event = new ComponentUserEvent(canvas, x, y); // First search in selection. for (Component comp : proj.getSelection().getComponentsContaining(loc, g)) { TextEditable editable = (TextEditable) comp.getFeature(TextEditable.class); if (editable != null) { caret = editable.getTextCaret(event); if (caret != null) { proj.getFrame().viewComponentAttributes(circ, comp); caretComponent = comp; caretCreatingText = false; break; } } } // Then search in circuit if (caret == null) { for (Component comp : circ.getAllContaining(loc, g)) { TextEditable editable = (TextEditable) comp.getFeature(TextEditable.class); if (editable != null) { caret = editable.getTextCaret(event); if (caret != null) { proj.getFrame().viewComponentAttributes(circ, comp); caretComponent = comp; caretCreatingText = false; break; } } } } // if nothing found, create a new label if (caret == null) { if (loc.getX() < 0 || loc.getY() < 0) return; AttributeSet copy = (AttributeSet) attrs.clone(); caretComponent = Text.FACTORY.createComponent(loc, copy); caretCreatingText = true; TextEditable editable = (TextEditable) caretComponent.getFeature(TextEditable.class); if (editable != null) { caret = editable.getTextCaret(event); proj.getFrame().viewComponentAttributes(circ, caretComponent); } } if (caret != null) { caretCanvas = canvas; caretCircuit = canvas.getCircuit(); caret.addCaretListener(listener); caretCircuit.addCircuitListener(listener); } proj.repaintCanvas(); }