private void processKeyEvent(Canvas canvas, KeyEvent e, int type) { HashMap<Component, KeyConfigurator> handlers = keyHandlers; if (handlers == null) { handlers = new HashMap<Component, KeyConfigurator>(); Selection sel = canvas.getSelection(); for (Component comp : sel.getComponents()) { ComponentFactory factory = comp.getFactory(); AttributeSet attrs = comp.getAttributeSet(); Object handler = factory.getFeature(KeyConfigurator.class, attrs); if (handler != null) { KeyConfigurator base = (KeyConfigurator) handler; handlers.put(comp, base.clone()); } } keyHandlers = handlers; } if (!handlers.isEmpty()) { boolean consume = false; ArrayList<KeyConfigurationResult> results; results = new ArrayList<KeyConfigurationResult>(); for (Map.Entry<Component, KeyConfigurator> entry : handlers.entrySet()) { Component comp = entry.getKey(); KeyConfigurator handler = entry.getValue(); KeyConfigurationEvent event = new KeyConfigurationEvent(type, comp.getAttributeSet(), e, comp); KeyConfigurationResult result = handler.keyEventReceived(event); consume |= event.isConsumed(); if (result != null) { results.add(result); } } if (consume) { e.consume(); } if (!results.isEmpty()) { SetAttributeAction act = new SetAttributeAction( canvas.getCircuit(), Strings.getter("changeComponentAttributesAction")); for (KeyConfigurationResult result : results) { Component comp = (Component) result.getEvent().getData(); Map<Attribute<?>, Object> newValues = result.getAttributeValues(); for (Map.Entry<Attribute<?>, Object> entry : newValues.entrySet()) { act.set(comp, entry.getKey(), entry.getValue()); } } if (!act.isEmpty()) { canvas.getProject().doAction(act); } } } }
@Override public String getTitle() { return comp.getFactory().getDisplayName(); }
AttrTableComponentModel(Project proj, Circuit circ, Component comp) { super(comp.getAttributeSet()); this.proj = proj; this.circ = circ; this.comp = comp; }
@Override public void draw(Canvas canvas, ComponentDrawContext context) { Project proj = canvas.getProject(); int dx = curDx; int dy = curDy; if (state == MOVING) { proj.getSelection().drawGhostsShifted(context, dx, dy); MoveGesture gesture = moveGesture; if (gesture != null && drawConnections && (dx != 0 || dy != 0)) { MoveResult result = gesture.findResult(dx, dy); if (result != null) { Collection<Wire> wiresToAdd = result.getWiresToAdd(); Graphics g = context.getGraphics(); GraphicsUtil.switchToWidth(g, 3); g.setColor(Color.GRAY); for (Wire w : wiresToAdd) { Location loc0 = w.getEnd0(); Location loc1 = w.getEnd1(); g.drawLine(loc0.getX(), loc0.getY(), loc1.getX(), loc1.getY()); } GraphicsUtil.switchToWidth(g, 1); g.setColor(COLOR_UNMATCHED); for (Location conn : result.getUnconnectedLocations()) { int connX = conn.getX(); int connY = conn.getY(); g.fillOval(connX - 3, connY - 3, 6, 6); g.fillOval(connX + dx - 3, connY + dy - 3, 6, 6); } } } } else if (state == RECT_SELECT) { int left = start.getX(); int right = left + dx; if (left > right) { int i = left; left = right; right = i; } int top = start.getY(); int bot = top + dy; if (top > bot) { int i = top; top = bot; bot = i; } Graphics gBase = context.getGraphics(); int w = right - left - 1; int h = bot - top - 1; if (w > 2 && h > 2) { gBase.setColor(BACKGROUND_RECT_SELECT); gBase.fillRect(left + 1, top + 1, w - 1, h - 1); } Circuit circ = canvas.getCircuit(); Bounds bds = Bounds.create(left, top, right - left, bot - top); for (Component c : circ.getAllWithin(bds)) { Location cloc = c.getLocation(); Graphics gDup = gBase.create(); context.setGraphics(gDup); c.getFactory() .drawGhost(context, COLOR_RECT_SELECT, cloc.getX(), cloc.getY(), c.getAttributeSet()); gDup.dispose(); } gBase.setColor(COLOR_RECT_SELECT); GraphicsUtil.switchToWidth(gBase, 2); if (w < 0) w = 0; if (h < 0) h = 0; gBase.drawRect(left, top, w, h); } }
@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(); }