Search in sources :

Example 6 with Selection

use of com.cburch.logisim.gui.main.Selection in project logisim-evolution by reds-heig.

the class MenuTool method mousePressed.

@Override
public void mousePressed(Canvas canvas, Graphics g, MouseEvent e) {
    int x = e.getX();
    int y = e.getY();
    Location pt = Location.create(x, y);
    JPopupMenu menu;
    Project proj = canvas.getProject();
    Selection sel = proj.getSelection();
    Collection<Component> in_sel = sel.getComponentsContaining(pt, g);
    if (!in_sel.isEmpty()) {
        Component comp = in_sel.iterator().next();
        if (sel.getComponents().size() > 1) {
            menu = new MenuSelection(proj);
        } else {
            menu = new MenuComponent(proj, canvas.getCircuit(), comp);
            MenuExtender extender = (MenuExtender) comp.getFeature(MenuExtender.class);
            if (extender != null)
                extender.configureMenu(menu, proj);
        }
    } else {
        Collection<Component> cl = canvas.getCircuit().getAllContaining(pt, g);
        if (!cl.isEmpty()) {
            Component comp = cl.iterator().next();
            menu = new MenuComponent(proj, canvas.getCircuit(), comp);
            MenuExtender extender = (MenuExtender) comp.getFeature(MenuExtender.class);
            if (extender != null)
                extender.configureMenu(menu, proj);
        } else {
            menu = null;
        }
    }
    if (menu != null) {
        canvas.showPopupMenu(menu, x, y);
    }
}
Also used : Project(com.cburch.logisim.proj.Project) Selection(com.cburch.logisim.gui.main.Selection) Component(com.cburch.logisim.comp.Component) JPopupMenu(javax.swing.JPopupMenu) Location(com.cburch.logisim.data.Location)

Example 7 with Selection

use of com.cburch.logisim.gui.main.Selection in project logisim-evolution by reds-heig.

the class SelectTool method mousePressed.

@Override
public void mousePressed(Canvas canvas, Graphics g, MouseEvent e) {
    Project proj = canvas.getProject();
    Selection sel = proj.getSelection();
    Circuit circuit = canvas.getCircuit();
    start = Location.create(e.getX(), e.getY());
    curDx = 0;
    curDy = 0;
    moveGesture = null;
    // if the user clicks into the selection,
    // selection is being modified
    Collection<Component> in_sel = sel.getComponentsContaining(start, g);
    if (!in_sel.isEmpty()) {
        if ((e.getModifiers() & InputEvent.SHIFT_MASK) == 0) {
            setState(proj, MOVING);
            proj.repaintCanvas();
            return;
        } else {
            Action act = SelectionActions.drop(sel, in_sel);
            if (act != null) {
                proj.doAction(act);
            }
        }
    }
    // if the user clicks into a component outside selection, user
    // wants to add/reset selection
    Collection<Component> clicked = circuit.getAllContaining(start, g);
    if (!clicked.isEmpty()) {
        if ((e.getModifiers() & InputEvent.SHIFT_MASK) == 0) {
            if (sel.getComponentsContaining(start).isEmpty()) {
                Action act = SelectionActions.dropAll(sel);
                if (act != null) {
                    proj.doAction(act);
                }
            }
        }
        for (Component comp : clicked) {
            if (!in_sel.contains(comp)) {
                sel.add(comp);
            }
        }
        setState(proj, MOVING);
        proj.repaintCanvas();
        return;
    }
    // selection (maybe with the shift key down).
    if ((e.getModifiers() & InputEvent.SHIFT_MASK) == 0) {
        Action act = SelectionActions.dropAll(sel);
        if (act != null) {
            proj.doAction(act);
        }
    }
    setState(proj, RECT_SELECT);
    proj.repaintCanvas();
}
Also used : Project(com.cburch.logisim.proj.Project) Action(com.cburch.logisim.proj.Action) Selection(com.cburch.logisim.gui.main.Selection) Circuit(com.cburch.logisim.circuit.Circuit) Component(com.cburch.logisim.comp.Component)

Example 8 with Selection

use of com.cburch.logisim.gui.main.Selection 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 9 with Selection

use of com.cburch.logisim.gui.main.Selection 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)

Aggregations

Selection (com.cburch.logisim.gui.main.Selection)9 Component (com.cburch.logisim.comp.Component)6 Circuit (com.cburch.logisim.circuit.Circuit)5 Project (com.cburch.logisim.proj.Project)3 ReplacementMap (com.cburch.logisim.circuit.ReplacementMap)2 Wire (com.cburch.logisim.circuit.Wire)2 Bounds (com.cburch.logisim.data.Bounds)2 Location (com.cburch.logisim.data.Location)2 Canvas (com.cburch.logisim.gui.main.Canvas)2 Action (com.cburch.logisim.proj.Action)2 CircuitListener (com.cburch.logisim.circuit.CircuitListener)1 CircuitState (com.cburch.logisim.circuit.CircuitState)1 ComponentFactory (com.cburch.logisim.comp.ComponentFactory)1 Attribute (com.cburch.logisim.data.Attribute)1 AttributeSet (com.cburch.logisim.data.AttributeSet)1 Direction (com.cburch.logisim.data.Direction)1 AddTool (com.cburch.logisim.tools.AddTool)1 Tool (com.cburch.logisim.tools.Tool)1 KeyConfigurationEvent (com.cburch.logisim.tools.key.KeyConfigurationEvent)1 KeyConfigurationResult (com.cburch.logisim.tools.key.KeyConfigurationResult)1