Search in sources :

Example 31 with ComponentFactory

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

the class XmlCircuitReader method getComponent.

/**
 * Get a circuit's component from a read XML file. If the component has a
 * non-null "trackercomp" field, it means that it is tracked, therefore it
 * is skipped in the non-tracked version to avoid errors.
 *
 * @param elt
 *            XML element to parse
 * @param reader
 *            XML file reader
 * @return the component built from its XML description
 * @throws XmlReaderException
 */
static Component getComponent(Element elt, XmlReader.ReadContext reader) throws XmlReaderException {
    if (elt.getAttribute("trackercomp") != "" && !Main.VERSION.hasTracker()) {
        return (null);
    }
    // Determine the factory that creates this element
    String name = elt.getAttribute("name");
    if (name == null || name.equals("")) {
        throw new XmlReaderException(Strings.get("compNameMissingError"));
    }
    String libName = elt.getAttribute("lib");
    Library lib = reader.findLibrary(libName);
    if (lib == null) {
        throw new XmlReaderException(Strings.get("compUnknownError", "no-lib"));
    }
    Tool tool = lib.getTool(name);
    if (tool == null || !(tool instanceof AddTool)) {
        if (libName == null || libName.equals("")) {
            throw new XmlReaderException(Strings.get("compUnknownError", name));
        } else {
            throw new XmlReaderException(Strings.get("compAbsentError", name, libName));
        }
    }
    ComponentFactory source = ((AddTool) tool).getFactory();
    // Determine attributes
    String loc_str = elt.getAttribute("loc");
    AttributeSet attrs = source.createAttributeSet();
    reader.initAttributeSet(elt, attrs, source);
    // Create component if location known
    if (loc_str == null || loc_str.equals("")) {
        throw new XmlReaderException(Strings.get("compLocMissingError", source.getName()));
    } else {
        try {
            Location loc = Location.parse(loc_str);
            return source.createComponent(loc, attrs);
        } catch (NumberFormatException e) {
            throw new XmlReaderException(Strings.get("compLocInvalidError", source.getName(), loc_str));
        }
    }
}
Also used : AttributeSet(com.cburch.logisim.data.AttributeSet) ComponentFactory(com.cburch.logisim.comp.ComponentFactory) Library(com.cburch.logisim.tools.Library) AddTool(com.cburch.logisim.tools.AddTool) Tool(com.cburch.logisim.tools.Tool) AddTool(com.cburch.logisim.tools.AddTool) Location(com.cburch.logisim.data.Location)

Example 32 with ComponentFactory

use of com.cburch.logisim.comp.ComponentFactory 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 33 with ComponentFactory

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

the class AddTool method draw.

@Override
public void draw(Canvas canvas, ComponentDrawContext context) {
    // next "if" suggested roughly by Kevin Walsh of Cornell to take care of
    // repaint problems on OpenJDK under Ubuntu
    int x = lastX;
    int y = lastY;
    if (x == INVALID_COORD || y == INVALID_COORD)
        return;
    ComponentFactory source = getFactory();
    if (source == null)
        return;
    /* take care of coloring the components differently that require a label */
    if (state == SHOW_GHOST) {
        source.drawGhost(context, Color.GRAY, x, y, getBaseAttributes());
    } else if (state == SHOW_ADD) {
        source.drawGhost(context, Color.BLACK, x, y, getBaseAttributes());
    }
}
Also used : ComponentFactory(com.cburch.logisim.comp.ComponentFactory)

Example 34 with ComponentFactory

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

the class AddTool method mouseReleased.

@Override
public void mouseReleased(Canvas canvas, Graphics g, MouseEvent e) {
    Component added = null;
    if (state == SHOW_ADD) {
        Circuit circ = canvas.getCircuit();
        if (!canvas.getProject().getLogisimFile().contains(circ))
            return;
        if (shouldSnap)
            Canvas.snapToGrid(e);
        moveTo(canvas, g, e.getX(), e.getY());
        Location loc = Location.create(e.getX(), e.getY());
        ComponentFactory source = getFactory();
        AttributeSet attrsCopy = (AttributeSet) attrs.clone();
        if (attrsCopy.containsAttribute(StdAttr.LABEL)) {
            attrsCopy.setValue(StdAttr.LABEL, AutoLabler.GetCurrent(canvas.getCircuit(), source));
            if (AutoLabler.IsActive(canvas.getCircuit())) {
                if (AutoLabler.hasNext(canvas.getCircuit()))
                    AutoLabler.GetNext(canvas.getCircuit(), source);
                else
                    AutoLabler.Stop(canvas.getCircuit());
            } else
                AutoLabler.SetLabel("", canvas.getCircuit(), source);
        }
        if (source == null)
            return;
        Component c = source.createComponent(loc, attrsCopy);
        if (circ.hasConflict(c)) {
            canvas.setErrorMessage(Strings.getter("exclusiveError"));
            return;
        }
        Bounds bds = c.getBounds(g);
        if (bds.getX() < 0 || bds.getY() < 0) {
            canvas.setErrorMessage(Strings.getter("negativeCoordError"));
            return;
        }
        try {
            CircuitMutation mutation = new CircuitMutation(circ);
            mutation.add(c);
            Action action = mutation.toAction(Strings.getter("addComponentAction", factory.getDisplayGetter()));
            canvas.getProject().doAction(action);
            lastAddition = action;
            added = c;
            canvas.repaint();
        } catch (CircuitException ex) {
            JOptionPane.showMessageDialog(canvas.getProject().getFrame(), ex.getMessage());
        }
        setState(canvas, SHOW_GHOST);
    } else if (state == SHOW_ADD_NO) {
        setState(canvas, SHOW_NONE);
    }
    Project proj = canvas.getProject();
    Tool next = determineNext(proj);
    if (next != null) {
        proj.setTool(next);
        Action act = SelectionActions.dropAll(canvas.getSelection());
        if (act != null) {
            proj.doAction(act);
        }
        if (added != null)
            canvas.getSelection().add(added);
    }
}
Also used : Project(com.cburch.logisim.proj.Project) Action(com.cburch.logisim.proj.Action) ToolAttributeAction(com.cburch.logisim.gui.main.ToolAttributeAction) CircuitException(com.cburch.logisim.circuit.CircuitException) AttributeSet(com.cburch.logisim.data.AttributeSet) ComponentFactory(com.cburch.logisim.comp.ComponentFactory) Bounds(com.cburch.logisim.data.Bounds) CircuitMutation(com.cburch.logisim.circuit.CircuitMutation) Circuit(com.cburch.logisim.circuit.Circuit) Component(com.cburch.logisim.comp.Component) Location(com.cburch.logisim.data.Location)

Example 35 with ComponentFactory

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

the class AddTool method getFacing.

private Direction getFacing() {
    ComponentFactory source = getFactory();
    if (source == null)
        return Direction.NORTH;
    AttributeSet base = getBaseAttributes();
    Object feature = source.getFeature(ComponentFactory.FACING_ATTRIBUTE_KEY, base);
    @SuppressWarnings("unchecked") Attribute<Direction> attr = (Attribute<Direction>) feature;
    if (attr != null)
        return base.getValue(attr);
    else
        return Direction.NORTH;
}
Also used : AttributeSet(com.cburch.logisim.data.AttributeSet) Attribute(com.cburch.logisim.data.Attribute) ComponentFactory(com.cburch.logisim.comp.ComponentFactory) Direction(com.cburch.logisim.data.Direction)

Aggregations

ComponentFactory (com.cburch.logisim.comp.ComponentFactory)37 AttributeSet (com.cburch.logisim.data.AttributeSet)16 AddTool (com.cburch.logisim.tools.AddTool)9 Tool (com.cburch.logisim.tools.Tool)9 Circuit (com.cburch.logisim.circuit.Circuit)8 SubcircuitFactory (com.cburch.logisim.circuit.SubcircuitFactory)8 Component (com.cburch.logisim.comp.Component)8 Library (com.cburch.logisim.tools.Library)8 ArrayList (java.util.ArrayList)6 Wire (com.cburch.logisim.circuit.Wire)4 Location (com.cburch.logisim.data.Location)4 ProjectExplorerToolNode (com.cburch.logisim.gui.generic.ProjectExplorerToolNode)4 HashSet (java.util.HashSet)4 Attribute (com.cburch.logisim.data.Attribute)3 Bounds (com.cburch.logisim.data.Bounds)3 ToolAttributeAction (com.cburch.logisim.gui.main.ToolAttributeAction)3 Action (com.cburch.logisim.proj.Action)3 HashMap (java.util.HashMap)3 CircuitMutation (com.cburch.logisim.circuit.CircuitMutation)2 Direction (com.cburch.logisim.data.Direction)2