Search in sources :

Example 51 with AttributeSet

use of com.cburch.logisim.data.AttributeSet in project logisim-evolution by reds-heig.

the class XmlWriter method fromLibrary.

Element fromLibrary(Library lib) {
    Element ret = doc.createElement("lib");
    if (libs.containsKey(lib))
        return null;
    String name = "" + libs.size();
    String desc = loader.getDescriptor(lib);
    if (desc == null) {
        loader.showError("library location unknown: " + lib.getName());
        return null;
    }
    libs.put(lib, name);
    ret.setAttribute("name", name);
    ret.setAttribute("desc", desc);
    for (Tool t : lib.getTools()) {
        AttributeSet attrs = t.getAttributeSet();
        if (attrs != null) {
            Element toAdd = doc.createElement("tool");
            toAdd.setAttribute("name", t.getName());
            addAttributeSetContent(toAdd, attrs, t);
            if (toAdd.getChildNodes().getLength() > 0) {
                ret.appendChild(toAdd);
            }
        }
    }
    return ret;
}
Also used : AttributeSet(com.cburch.logisim.data.AttributeSet) Element(org.w3c.dom.Element) Tool(com.cburch.logisim.tools.Tool)

Example 52 with AttributeSet

use of com.cburch.logisim.data.AttributeSet 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 53 with AttributeSet

use of com.cburch.logisim.data.AttributeSet in project logisim-evolution by reds-heig.

the class PokeTool method mousePressed.

@Override
public void mousePressed(Canvas canvas, Graphics g, MouseEvent e) {
    int x = e.getX();
    int y = e.getY();
    Location loc = Location.create(x, y);
    boolean dirty = false;
    canvas.setHighlightedWires(WireSet.EMPTY);
    if (pokeCaret != null && !pokeCaret.getBounds(g).contains(loc)) {
        dirty = true;
        removeCaret(true);
    }
    if (pokeCaret == null) {
        ComponentUserEvent event = new ComponentUserEvent(canvas, x, y);
        Circuit circ = canvas.getCircuit();
        for (Component c : circ.getAllContaining(loc, g)) {
            if (pokeCaret != null)
                break;
            if (c instanceof Wire) {
                Caret caret = new WireCaret(canvas, (Wire) c, x, y, canvas.getProject().getOptions().getAttributeSet());
                setPokedComponent(circ, c, caret);
                canvas.setHighlightedWires(circ.getWireSet((Wire) c));
            } else {
                Pokable p = (Pokable) c.getFeature(Pokable.class);
                if (p != null) {
                    Caret caret = p.getPokeCaret(event);
                    setPokedComponent(circ, c, caret);
                    AttributeSet attrs = c.getAttributeSet();
                    if (attrs != null && attrs.getAttributes().size() > 0) {
                        Project proj = canvas.getProject();
                        proj.getFrame().viewComponentAttributes(circ, c);
                    }
                }
            }
        }
    }
    if (pokeCaret != null) {
        dirty = true;
        pokeCaret.mousePressed(e);
    }
    if (dirty)
        canvas.getProject().repaintCanvas();
}
Also used : Project(com.cburch.logisim.proj.Project) AttributeSet(com.cburch.logisim.data.AttributeSet) ComponentUserEvent(com.cburch.logisim.comp.ComponentUserEvent) Circuit(com.cburch.logisim.circuit.Circuit) Component(com.cburch.logisim.comp.Component) Wire(com.cburch.logisim.circuit.Wire) Location(com.cburch.logisim.data.Location)

Example 54 with AttributeSet

use of com.cburch.logisim.data.AttributeSet 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 55 with AttributeSet

use of com.cburch.logisim.data.AttributeSet 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)

Aggregations

AttributeSet (com.cburch.logisim.data.AttributeSet)65 ComponentFactory (com.cburch.logisim.comp.ComponentFactory)16 Component (com.cburch.logisim.comp.Component)13 TreeMap (java.util.TreeMap)13 Attribute (com.cburch.logisim.data.Attribute)12 Location (com.cburch.logisim.data.Location)9 HashMap (java.util.HashMap)7 Value (com.cburch.logisim.data.Value)6 Circuit (com.cburch.logisim.circuit.Circuit)5 Direction (com.cburch.logisim.data.Direction)4 Graphics (java.awt.Graphics)4 Map (java.util.Map)4 CircuitMutation (com.cburch.logisim.circuit.CircuitMutation)3 Wire (com.cburch.logisim.circuit.Wire)3 AbstractAttributeSet (com.cburch.logisim.data.AbstractAttributeSet)3 BitWidth (com.cburch.logisim.data.BitWidth)3 Bounds (com.cburch.logisim.data.Bounds)3 ToolAttributeAction (com.cburch.logisim.gui.main.ToolAttributeAction)3 Action (com.cburch.logisim.proj.Action)3 Project (com.cburch.logisim.proj.Project)3