public void editingStopped(CaretEvent e) { if (e.getCaret() != caret) { e.getCaret().removeCaretListener(this); return; } caret.removeCaretListener(this); caretCircuit.removeCircuitListener(this); String val = caret.getText(); boolean isEmpty = (val == null || val.equals("")); Action a; Project proj = caretCanvas.getProject(); if (caretCreatingText) { if (!isEmpty) { CircuitMutation xn = new CircuitMutation(caretCircuit); xn.add(caretComponent); a = xn.toAction(Strings.getter("addComponentAction", Text.FACTORY.getDisplayGetter())); } else { a = null; // don't add the blank text field } } else { if (isEmpty && caretComponent.getFactory() instanceof Text) { CircuitMutation xn = new CircuitMutation(caretCircuit); xn.add(caretComponent); a = xn.toAction(Strings.getter("removeComponentAction", Text.FACTORY.getDisplayGetter())); } else { Object obj = caretComponent.getFeature(TextEditable.class); if (obj == null) { // should never happen a = null; } else { TextEditable editable = (TextEditable) obj; a = editable.getCommitAction(caretCircuit, e.getOldText(), e.getText()); } } } caretCircuit = null; caretComponent = null; caretCreatingText = false; caret = null; if (a != null) proj.doAction(a); }
@Override public void paintIcon(ComponentDrawContext c, int x, int y) { Text.FACTORY.paintIcon(c, x, y, null); }
@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(); }
public TextTool() { attrs = Text.FACTORY.createAttributeSet(); }