Search in sources :

Example 6 with Component

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

the class SelectionActions method getReplacementMap.

private static HashMap<Component, Component> getReplacementMap(Project proj) {
    HashMap<Component, Component> replMap;
    replMap = new HashMap<Component, Component>();
    LogisimFile file = proj.getLogisimFile();
    ArrayList<Library> libs = new ArrayList<Library>();
    libs.add(file);
    libs.addAll(file.getLibraries());
    ArrayList<String> dropped = null;
    Clipboard clip = Clipboard.get();
    Collection<Component> comps = clip.getComponents();
    HashMap<ComponentFactory, ComponentFactory> factoryReplacements;
    factoryReplacements = new HashMap<ComponentFactory, ComponentFactory>();
    for (Component comp : comps) {
        if (comp instanceof Wire)
            continue;
        ComponentFactory compFactory = comp.getFactory();
        ComponentFactory copyFactory = findComponentFactory(compFactory, libs, false);
        if (factoryReplacements.containsKey(compFactory)) {
            copyFactory = factoryReplacements.get(compFactory);
        } else if (copyFactory == null) {
            ComponentFactory candidate = findComponentFactory(compFactory, libs, true);
            if (candidate == null) {
                if (dropped == null) {
                    dropped = new ArrayList<String>();
                }
                dropped.add(compFactory.getDisplayName());
            } else {
                String msg = Strings.get("pasteCloneQuery", compFactory.getName());
                Object[] opts = { Strings.get("pasteCloneReplace"), Strings.get("pasteCloneIgnore"), Strings.get("pasteCloneCancel") };
                int select = JOptionPane.showOptionDialog(proj.getFrame(), msg, Strings.get("pasteCloneTitle"), 0, JOptionPane.QUESTION_MESSAGE, null, opts, opts[0]);
                if (select == 0) {
                    copyFactory = candidate;
                } else if (select == 1) {
                    copyFactory = null;
                } else {
                    return null;
                }
                factoryReplacements.put(compFactory, copyFactory);
            }
        }
        if (copyFactory == null) {
            replMap.put(comp, null);
        } else if (copyFactory != compFactory) {
            Location copyLoc = comp.getLocation();
            AttributeSet copyAttrs = (AttributeSet) comp.getAttributeSet().clone();
            Component copy = copyFactory.createComponent(copyLoc, copyAttrs);
            replMap.put(comp, copy);
        }
    }
    if (dropped != null) {
        Collections.sort(dropped);
        StringBuilder droppedStr = new StringBuilder();
        droppedStr.append(Strings.get("pasteDropMessage"));
        String curName = dropped.get(0);
        int curCount = 1;
        int lines = 1;
        for (int i = 1; i <= dropped.size(); i++) {
            String nextName = i == dropped.size() ? "" : dropped.get(i);
            if (nextName.equals(curName)) {
                curCount++;
            } else {
                lines++;
                droppedStr.append("\n  ");
                droppedStr.append(curName);
                if (curCount > 1) {
                    droppedStr.append(" \u00d7 " + curCount);
                }
                curName = nextName;
                curCount = 1;
            }
        }
        lines = Math.max(3, Math.min(7, lines));
        JTextArea area = new JTextArea(lines, 60);
        area.setEditable(false);
        area.setText(droppedStr.toString());
        area.setCaretPosition(0);
        JScrollPane areaPane = new JScrollPane(area);
        JOptionPane.showMessageDialog(proj.getFrame(), areaPane, Strings.get("pasteDropTitle"), JOptionPane.WARNING_MESSAGE);
    }
    return replMap;
}
Also used : JScrollPane(javax.swing.JScrollPane) JTextArea(javax.swing.JTextArea) ComponentFactory(com.cburch.logisim.comp.ComponentFactory) ArrayList(java.util.ArrayList) Wire(com.cburch.logisim.circuit.Wire) LogisimFile(com.cburch.logisim.file.LogisimFile) AttributeSet(com.cburch.logisim.data.AttributeSet) Library(com.cburch.logisim.tools.Library) Component(com.cburch.logisim.comp.Component) Location(com.cburch.logisim.data.Location)

Example 7 with Component

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

the class SelectionAttributes method updateList.

private void updateList(boolean ignoreIfSelectionSame) {
    Selection sel = selection;
    Set<Component> oldSel = selected;
    Set<Component> newSel;
    if (sel == null) {
        newSel = Collections.emptySet();
    } else {
        newSel = createSet(sel.getComponents());
    }
    if (haveSameElements(newSel, oldSel)) {
        if (ignoreIfSelectionSame) {
            return;
        }
        newSel = oldSel;
    } else {
        for (Component o : oldSel) {
            if (!newSel.contains(o)) {
                o.getAttributeSet().removeAttributeListener(listener);
            }
        }
        for (Component o : newSel) {
            if (!oldSel.contains(o)) {
                o.getAttributeSet().addAttributeListener(listener);
            }
        }
    }
    LinkedHashMap<Attribute<Object>, Object> attrMap = computeAttributes(newSel);
    boolean same = isSame(attrMap, this.attrs, this.values);
    if (same) {
        if (newSel != oldSel) {
            this.selected = newSel;
        }
    } else {
        Attribute<?>[] oldAttrs = this.attrs;
        Object[] oldValues = this.values;
        Attribute<?>[] newAttrs = new Attribute[attrMap.size()];
        Object[] newValues = new Object[newAttrs.length];
        boolean[] newReadOnly = new boolean[newAttrs.length];
        int i = -1;
        for (Map.Entry<Attribute<Object>, Object> entry : attrMap.entrySet()) {
            i++;
            newAttrs[i] = entry.getKey();
            newValues[i] = entry.getValue();
            newReadOnly[i] = computeReadOnly(newSel, newAttrs[i]);
        }
        if (newSel != oldSel) {
            this.selected = newSel;
        }
        this.attrs = newAttrs;
        this.attrsView = new UnmodifiableList<Attribute<?>>(newAttrs);
        this.values = newValues;
        this.readOnly = newReadOnly;
        boolean listSame = oldAttrs != null && oldAttrs.length == newAttrs.length;
        if (listSame) {
            for (i = 0; i < oldAttrs.length; i++) {
                if (!oldAttrs[i].equals(newAttrs[i])) {
                    listSame = false;
                    break;
                }
            }
        }
        if (listSame) {
            for (i = 0; i < oldValues.length; i++) {
                Object oldVal = oldValues[i];
                Object newVal = newValues[i];
                boolean sameVals = oldVal == null ? newVal == null : oldVal.equals(newVal);
                if (!sameVals) {
                    @SuppressWarnings("unchecked") Attribute<Object> attr = (Attribute<Object>) oldAttrs[i];
                    fireAttributeValueChanged(attr, newVal, oldVal);
                }
            }
        } else {
            fireAttributeListChanged();
        }
    }
}
Also used : Attribute(com.cburch.logisim.data.Attribute) Component(com.cburch.logisim.comp.Component) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 8 with Component

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

the class SelectionBase method deleteAllHelper.

void deleteAllHelper(CircuitMutation xn) {
    for (Component comp : selected) {
        xn.remove(comp);
    }
    selected.clear();
    lifted.clear();
    fireSelectionChanged();
}
Also used : Component(com.cburch.logisim.comp.Component)

Example 9 with Component

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

the class SelectionBase method copyComponents.

private HashMap<Component, Component> copyComponents(Collection<Component> components, int dx, int dy) {
    HashMap<Component, Component> ret = new HashMap<Component, Component>();
    for (Component comp : components) {
        Location oldLoc = comp.getLocation();
        AttributeSet attrs = (AttributeSet) comp.getAttributeSet().clone();
        int newX = oldLoc.getX() + dx;
        int newY = oldLoc.getY() + dy;
        Object snap = comp.getFactory().getFeature(ComponentFactory.SHOULD_SNAP, attrs);
        if (snap == null || ((Boolean) snap).booleanValue()) {
            newX = Canvas.snapXToGrid(newX);
            newY = Canvas.snapYToGrid(newY);
        }
        Location newLoc = Location.create(newX, newY);
        Component copy = comp.getFactory().createComponent(newLoc, attrs);
        ret.put(comp, copy);
    }
    return ret;
}
Also used : HashMap(java.util.HashMap) AttributeSet(com.cburch.logisim.data.AttributeSet) Component(com.cburch.logisim.comp.Component) Location(com.cburch.logisim.data.Location)

Example 10 with Component

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

the class SelectionBase method getBounds.

public Bounds getBounds(Graphics g) {
    Iterator<Component> it = unionSet.iterator();
    if (it.hasNext()) {
        bounds = it.next().getBounds(g);
        while (it.hasNext()) {
            Component comp = it.next();
            Bounds bds = comp.getBounds(g);
            bounds = bounds.add(bds);
        }
    } else {
        bounds = Bounds.EMPTY_BOUNDS;
    }
    return bounds;
}
Also used : Bounds(com.cburch.logisim.data.Bounds) 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