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;
}
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();
}
}
}
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();
}
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;
}
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;
}
Aggregations