Search in sources :

Example 86 with Component

use of com.cburch.logisim.comp.Component in project logisim-evolution by reds-heig.

the class SelectTool method processKeyEvent.

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);
            }
        }
    }
}
Also used : KeyConfigurationResult(com.cburch.logisim.tools.key.KeyConfigurationResult) Attribute(com.cburch.logisim.data.Attribute) Selection(com.cburch.logisim.gui.main.Selection) ComponentFactory(com.cburch.logisim.comp.ComponentFactory) AttributeSet(com.cburch.logisim.data.AttributeSet) KeyConfigurationEvent(com.cburch.logisim.tools.key.KeyConfigurationEvent) Component(com.cburch.logisim.comp.Component) KeyConfigurator(com.cburch.logisim.tools.key.KeyConfigurator) HashMap(java.util.HashMap) Map(java.util.Map) ReplacementMap(com.cburch.logisim.circuit.ReplacementMap)

Example 87 with Component

use of com.cburch.logisim.comp.Component in project logisim-evolution by reds-heig.

the class SelectTool method mouseReleased.

@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();
    }
    if (e.getClickCount() >= 2) {
        Set<Component> comps = canvas.getProject().getSelection().getComponents();
        if (comps.size() == 1) {
            for (Component comp : comps) {
                if (comp.getAttributeSet().containsAttribute(StdAttr.LABEL)) {
                    String OldLabel = comp.getAttributeSet().getValue(StdAttr.LABEL);
                    SetAttributeAction act = new SetAttributeAction(canvas.getCircuit(), Strings.getter("changeComponentAttributesAction"));
                    AutoLabler.AskAndSetLabel(comp.getFactory().getDisplayName(), OldLabel, canvas.getCircuit(), comp, comp.getFactory(), comp.getAttributeSet(), act, true);
                    if (!act.isEmpty())
                        canvas.getProject().doAction(act);
                }
            }
        }
    }
}
Also used : Action(com.cburch.logisim.proj.Action) Selection(com.cburch.logisim.gui.main.Selection) Bounds(com.cburch.logisim.data.Bounds) Circuit(com.cburch.logisim.circuit.Circuit) Project(com.cburch.logisim.proj.Project) Collection(java.util.Collection) MoveGesture(com.cburch.logisim.tools.move.MoveGesture) MoveResult(com.cburch.logisim.tools.move.MoveResult) ReplacementMap(com.cburch.logisim.circuit.ReplacementMap) Component(com.cburch.logisim.comp.Component)

Example 88 with Component

use of com.cburch.logisim.comp.Component in project logisim-evolution by reds-heig.

the class SelectTool method getHiddenComponents.

@Override
public Set<Component> getHiddenComponents(Canvas canvas) {
    if (state == MOVING) {
        int dx = curDx;
        int dy = curDy;
        if (dx == 0 && dy == 0) {
            return null;
        }
        Set<Component> sel = canvas.getSelection().getComponents();
        MoveGesture gesture = moveGesture;
        if (gesture != null && drawConnections) {
            MoveResult result = gesture.findResult(dx, dy);
            if (result != null) {
                HashSet<Component> ret = new HashSet<Component>(sel);
                ret.addAll(result.getReplacementMap().getRemovals());
                return ret;
            }
        }
        return sel;
    } else {
        return null;
    }
}
Also used : MoveGesture(com.cburch.logisim.tools.move.MoveGesture) MoveResult(com.cburch.logisim.tools.move.MoveResult) Component(com.cburch.logisim.comp.Component) HashSet(java.util.HashSet)

Example 89 with Component

use of com.cburch.logisim.comp.Component in project logisim-evolution by reds-heig.

the class SetAttributeAction method doIt.

@Override
public void doIt(Project proj) {
    CircuitMutation xn = new CircuitMutation(circuit);
    int len = values.size();
    oldValues.clear();
    for (int i = 0; i < len; i++) {
        Component comp = comps.get(i);
        Attribute<Object> attr = attrs.get(i);
        Object value = values.get(i);
        if (circuit.contains(comp)) {
            oldValues.add(null);
            xn.set(comp, attr, value);
        } else {
            AttributeSet compAttrs = comp.getAttributeSet();
            oldValues.add(compAttrs.getValue(attr));
            compAttrs.setValue(attr, value);
        }
    }
    if (!xn.isEmpty()) {
        CircuitTransactionResult result = xn.execute();
        xnReverse = result.getReverseTransaction();
    }
}
Also used : CircuitTransactionResult(com.cburch.logisim.circuit.CircuitTransactionResult) AttributeSet(com.cburch.logisim.data.AttributeSet) CircuitMutation(com.cburch.logisim.circuit.CircuitMutation) Component(com.cburch.logisim.comp.Component)

Example 90 with Component

use of com.cburch.logisim.comp.Component in project logisim-evolution by reds-heig.

the class SetAttributeAction method undo.

@Override
public void undo(Project proj) {
    if (xnReverse != null)
        xnReverse.execute();
    for (int i = oldValues.size() - 1; i >= 0; i--) {
        Component comp = comps.get(i);
        Attribute<Object> attr = attrs.get(i);
        Object value = oldValues.get(i);
        if (value != null) {
            comp.getAttributeSet().setValue(attr, value);
        }
    }
}
Also used : Component(com.cburch.logisim.comp.Component)

Aggregations

Component (com.cburch.logisim.comp.Component)97 Location (com.cburch.logisim.data.Location)26 ArrayList (java.util.ArrayList)20 Circuit (com.cburch.logisim.circuit.Circuit)19 AttributeSet (com.cburch.logisim.data.AttributeSet)13 Wire (com.cburch.logisim.circuit.Wire)12 Bounds (com.cburch.logisim.data.Bounds)11 HashMap (java.util.HashMap)11 HashSet (java.util.HashSet)11 SubcircuitFactory (com.cburch.logisim.circuit.SubcircuitFactory)8 ComponentFactory (com.cburch.logisim.comp.ComponentFactory)8 EndData (com.cburch.logisim.comp.EndData)8 InstanceComponent (com.cburch.logisim.instance.InstanceComponent)8 Project (com.cburch.logisim.proj.Project)7 Pin (com.cburch.logisim.std.wiring.Pin)7 Graphics (java.awt.Graphics)7 CircuitState (com.cburch.logisim.circuit.CircuitState)6 Splitter (com.cburch.logisim.circuit.Splitter)6 Selection (com.cburch.logisim.gui.main.Selection)6 InstanceState (com.cburch.logisim.instance.InstanceState)5