Search in sources :

Example 1 with AutoLabel

use of com.cburch.logisim.util.AutoLabel in project logisim-evolution by reds-heig.

the class AttrTableSelectionModel method setValueRequested.

@Override
public void setValueRequested(Attribute<Object> attr, Object value) throws AttrTableSetException {
    Selection selection = frame.getCanvas().getSelection();
    Circuit circuit = frame.getCanvas().getCircuit();
    if (selection.isEmpty() && circuit != null) {
        AttrTableCircuitModel circuitModel = new AttrTableCircuitModel(project, circuit);
        circuitModel.setValueRequested(attr, value);
    } else {
        SetAttributeAction act = new SetAttributeAction(circuit, Strings.getter("selectionAttributeAction"));
        AutoLabel labler = null;
        if (attr.equals(StdAttr.LABEL)) {
            labler = new AutoLabel((String) value, circuit);
        }
        SortedSet<Component> comps = new TreeSet<Component>(new PositionComparator());
        comps.addAll(selection.getComponents());
        for (Component comp : comps) {
            if (!(comp instanceof Wire)) {
                if (comp.getFactory() instanceof SubcircuitFactory) {
                    SubcircuitFactory fac = (SubcircuitFactory) comp.getFactory();
                    if (attr.equals(CircuitAttributes.NAMED_CIRCUIT_BOX) || attr.equals(CircuitAttributes.NAME_ATTR)) {
                        try {
                            CircuitMutation mutation = new CircuitMutation(fac.getSubcircuit());
                            mutation.setForCircuit(attr, value);
                            Action action = mutation.toAction(null);
                            project.doAction(action);
                        } catch (CircuitException ex) {
                            JOptionPane.showMessageDialog(project.getFrame(), ex.getMessage());
                        }
                        return;
                    }
                }
                if (attr.equals(StdAttr.LABEL)) {
                    if (labler.hasNext(circuit)) {
                        if (comps.size() > 1) {
                            act.set(comp, attr, labler.GetNext(circuit, comp.getFactory()));
                        } else {
                            if (getAttributeSet().getValue(StdAttr.LABEL).equals((String) value))
                                return;
                            else
                                act.set(comp, attr, labler.GetCurrent(circuit, comp.getFactory()));
                        }
                    } else
                        act.set(comp, attr, "");
                } else
                    act.set(comp, attr, value);
            }
        }
        project.doAction(act);
    }
}
Also used : Action(com.cburch.logisim.proj.Action) SetAttributeAction(com.cburch.logisim.tools.SetAttributeAction) CircuitException(com.cburch.logisim.circuit.CircuitException) AutoLabel(com.cburch.logisim.util.AutoLabel) Circuit(com.cburch.logisim.circuit.Circuit) SetAttributeAction(com.cburch.logisim.tools.SetAttributeAction) Wire(com.cburch.logisim.circuit.Wire) TreeSet(java.util.TreeSet) SubcircuitFactory(com.cburch.logisim.circuit.SubcircuitFactory) CircuitMutation(com.cburch.logisim.circuit.CircuitMutation) Component(com.cburch.logisim.comp.Component)

Example 2 with AutoLabel

use of com.cburch.logisim.util.AutoLabel in project logisim-evolution by reds-heig.

the class Circuit method Annotate.

public void Annotate(boolean ClearExistingLabels, FPGAReport reporter) {
    /* If I am already completely annotated, return */
    if (Annotated) {
        reporter.AddInfo("Nothing to do !");
        return;
    }
    SortedSet<Component> comps = new TreeSet<Component>(new AnnotateComparator());
    HashMap<String, AutoLabel> lablers = new HashMap<String, AutoLabel>();
    Set<String> LabelNames = new HashSet<String>();
    Set<String> Subcircuits = new HashSet<String>();
    for (Component comp : getNonWires()) {
        if (comp.getFactory() instanceof Tunnel)
            continue;
        /* we are directly going to remove duplicated labels */
        AttributeSet attrs = comp.getAttributeSet();
        if (attrs.containsAttribute(StdAttr.LABEL)) {
            String label = attrs.getValue(StdAttr.LABEL);
            if (!label.isEmpty()) {
                if (LabelNames.contains(label.toUpperCase())) {
                    SetAttributeAction act = new SetAttributeAction(this, Strings.getter("changeComponentAttributesAction"));
                    act.set(comp, StdAttr.LABEL, "");
                    proj.doAction(act);
                    reporter.AddSevereWarning("Removed duplicated label " + this.getName() + "/" + label);
                } else {
                    LabelNames.add(label.toUpperCase());
                }
            }
        }
        /* now we only process those that require a label */
        if (comp.getFactory().RequiresNonZeroLabel()) {
            if (ClearExistingLabels) {
                /* in case of label cleaning, we clear first the old label */
                reporter.AddInfo("Cleared " + this.getName() + "/" + comp.getAttributeSet().getValue(StdAttr.LABEL));
                SetAttributeAction act = new SetAttributeAction(this, Strings.getter("changeComponentAttributesAction"));
                act.set(comp, StdAttr.LABEL, "");
                proj.doAction(act);
            }
            if (comp.getAttributeSet().getValue(StdAttr.LABEL).isEmpty()) {
                comps.add(comp);
                String ComponentName = GetAnnotationName(comp);
                if (!lablers.containsKey(ComponentName)) {
                    lablers.put(ComponentName, new AutoLabel(ComponentName + "_0", this));
                }
            }
        }
        /* if the current component is a sub-circuit, recurse into it */
        if (comp.getFactory() instanceof SubcircuitFactory) {
            SubcircuitFactory sub = (SubcircuitFactory) comp.getFactory();
            if (!Subcircuits.contains(sub.getName()))
                Subcircuits.add(sub.getName());
        }
    }
    /* Now Annotate */
    for (Component comp : comps) {
        String ComponentName = GetAnnotationName(comp);
        if (!lablers.containsKey(ComponentName) || !lablers.get(ComponentName).hasNext(this)) {
            /* This should never happen! */
            reporter.AddFatalError("Annotate internal Error: Either there exists duplicate labels or the label syntax is incorrect!\nPlease try annotation on labeled components also\n");
            return;
        } else {
            String NewLabel = lablers.get(ComponentName).GetNext(this, comp.getFactory());
            SetAttributeAction act = new SetAttributeAction(this, Strings.getter("changeComponentAttributesAction"));
            act.set(comp, StdAttr.LABEL, NewLabel);
            proj.doAction(act);
            reporter.AddInfo("Labeled " + this.getName() + "/" + NewLabel);
        }
    }
    Annotated = true;
    /* Now annotate all circuits below me */
    for (String subs : Subcircuits) {
        Circuit circ = proj.getLogisimFile().getCircuit(subs);
        circ.Annotate(ClearExistingLabels, reporter);
    }
}
Also used : HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap) AutoLabel(com.cburch.logisim.util.AutoLabel) SetAttributeAction(com.cburch.logisim.tools.SetAttributeAction) Tunnel(com.cburch.logisim.std.wiring.Tunnel) AttributeSet(com.cburch.logisim.data.AttributeSet) TreeSet(java.util.TreeSet) Component(com.cburch.logisim.comp.Component) HashSet(java.util.HashSet)

Aggregations

Component (com.cburch.logisim.comp.Component)2 SetAttributeAction (com.cburch.logisim.tools.SetAttributeAction)2 AutoLabel (com.cburch.logisim.util.AutoLabel)2 TreeSet (java.util.TreeSet)2 Circuit (com.cburch.logisim.circuit.Circuit)1 CircuitException (com.cburch.logisim.circuit.CircuitException)1 CircuitMutation (com.cburch.logisim.circuit.CircuitMutation)1 SubcircuitFactory (com.cburch.logisim.circuit.SubcircuitFactory)1 Wire (com.cburch.logisim.circuit.Wire)1 AttributeSet (com.cburch.logisim.data.AttributeSet)1 Action (com.cburch.logisim.proj.Action)1 Tunnel (com.cburch.logisim.std.wiring.Tunnel)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 WeakHashMap (java.util.WeakHashMap)1