use of com.cburch.logisim.circuit.CircuitMutation 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);
}
}
use of com.cburch.logisim.circuit.CircuitMutation in project logisim-evolution by reds-heig.
the class WiringTool method performShortening.
private boolean performShortening(Canvas canvas, Location drag0, Location drag1) {
Wire shorten = willShorten(drag0, drag1);
if (shorten == null) {
return false;
} else {
CircuitMutation xn = new CircuitMutation(canvas.getCircuit());
StringGetter actName;
Wire result = getShortenResult(shorten, drag0, drag1);
if (result == null) {
xn.remove(shorten);
actName = Strings.getter("removeComponentAction", shorten.getFactory().getDisplayGetter());
} else {
xn.replace(shorten, result);
actName = Strings.getter("shortenWireAction");
}
canvas.getProject().doAction(xn.toAction(actName));
return true;
}
}
use of com.cburch.logisim.circuit.CircuitMutation in project logisim-evolution by reds-heig.
the class LoadedLibrary method replaceAll.
private static void replaceAll(Circuit circuit, Map<ComponentFactory, ComponentFactory> compMap) {
ArrayList<Component> toReplace = null;
for (Component comp : circuit.getNonWires()) {
if (compMap.containsKey(comp.getFactory())) {
if (toReplace == null)
toReplace = new ArrayList<Component>();
toReplace.add(comp);
}
}
if (toReplace != null) {
CircuitMutation xn = new CircuitMutation(circuit);
for (Component comp : toReplace) {
xn.remove(comp);
ComponentFactory factory = compMap.get(comp.getFactory());
if (factory != null) {
AttributeSet newAttrs = createAttributes(factory, comp.getAttributeSet());
xn.add(factory.createComponent(comp.getLocation(), newAttrs));
}
}
xn.execute();
}
}
use of com.cburch.logisim.circuit.CircuitMutation in project logisim-evolution by reds-heig.
the class CircuitBuilder method build.
public static CircuitMutation build(Circuit destCirc, AnalyzerModel model, boolean twoInputs, boolean useNands) {
CircuitMutation result = new CircuitMutation(destCirc);
result.clear();
Layout[] layouts = new Layout[model.getOutputs().size()];
int maxWidth = 0;
for (int i = 0; i < layouts.length; i++) {
String output = model.getOutputs().get(i);
Expression expr = model.getOutputExpressions().getExpression(output);
CircuitDetermination det = CircuitDetermination.create(expr);
if (det != null) {
if (twoInputs)
det.convertToTwoInputs();
if (useNands)
det.convertToNands();
det.repair();
layouts[i] = layoutGates(det);
maxWidth = Math.max(maxWidth, layouts[i].width);
} else {
layouts[i] = null;
}
}
InputData inputData = computeInputData(model);
int x = inputData.getStartX();
int y = 10;
int outputX = x + maxWidth + 20;
for (int i = 0; i < layouts.length; i++) {
String outputName = model.getOutputs().get(i);
Layout layout = layouts[i];
Location output;
int height;
if (layout == null) {
output = Location.create(outputX, y + 20);
height = 40;
} else {
int dy = 0;
if (layout.outputY < 20)
dy = 20 - layout.outputY;
height = Math.max(dy + layout.height, 40);
output = Location.create(outputX, y + dy + layout.outputY);
placeComponents(result, layouts[i], x, y + dy, inputData, output);
}
placeOutput(result, output, outputName);
y += height + 10;
}
placeInputs(result, inputData);
return result;
}
use of com.cburch.logisim.circuit.CircuitMutation in project logisim-evolution by reds-heig.
the class BuildCircuitButton method performAction.
private void performAction(Project dest, String name, boolean replace, final boolean twoInputs, final boolean useNands) {
if (replace) {
final Circuit circuit = dest.getLogisimFile().getCircuit(name);
if (circuit == null) {
JOptionPane.showMessageDialog(parent, "Internal error prevents replacing circuit.", "Internal Error", JOptionPane.ERROR_MESSAGE);
return;
}
CircuitMutation xn = CircuitBuilder.build(circuit, model, twoInputs, useNands);
dest.doAction(xn.toAction(Strings.getter("replaceCircuitAction")));
} else {
// add the circuit
Circuit circuit = new Circuit(name, dest.getLogisimFile(), dest);
CircuitMutation xn = CircuitBuilder.build(circuit, model, twoInputs, useNands);
xn.execute();
dest.doAction(LogisimFileActions.addCircuit(circuit));
dest.setCurrentCircuit(circuit);
}
}
Aggregations