diff --git a/src/main/java/de/neemann/digital/core/element/ElementTypeDescription.java b/src/main/java/de/neemann/digital/core/element/ElementTypeDescription.java index 68cfc5ef5..2bdfb1c5c 100644 --- a/src/main/java/de/neemann/digital/core/element/ElementTypeDescription.java +++ b/src/main/java/de/neemann/digital/core/element/ElementTypeDescription.java @@ -99,7 +99,7 @@ public class ElementTypeDescription { /** * Returns the names of the inputs which are needed by this element. - * If you need a list of outputs names you can create a element using createPart() + * If you need a list of outputs names you can create a element using createElement() * and request the outputs by calling the elements getOutputs() method. * The you get an array of ObservableNames, and ObservableName has a * field name. @@ -117,7 +117,7 @@ public class ElementTypeDescription { * @param elementAttributes the elements attributes * @return the Part instance */ - public Element createPart(ElementAttributes elementAttributes) { + public Element createElement(ElementAttributes elementAttributes) { return partFactory.create(elementAttributes); } } diff --git a/src/main/java/de/neemann/digital/gui/Main.java b/src/main/java/de/neemann/digital/gui/Main.java index 44d5cf663..87e260856 100644 --- a/src/main/java/de/neemann/digital/gui/Main.java +++ b/src/main/java/de/neemann/digital/gui/Main.java @@ -27,10 +27,7 @@ import javax.swing.*; import javax.swing.filechooser.FileNameExtensionFilter; import java.awt.*; import java.awt.event.ActionEvent; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; +import java.io.*; import java.util.prefs.Preferences; /** @@ -171,7 +168,7 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave { public void actionPerformed(ActionEvent e) { try { modelDescription = new ModelDescription(circuitComponent.getCircuit(), library); - model = modelDescription.createModel(); + model = modelDescription.createModel(false); SpeedTest speedTest = new SpeedTest(model); double frequency = speedTest.calculate(); JOptionPane.showMessageDialog(Main.this, "Frequency: " + frequency); @@ -229,7 +226,7 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave { modelDescription.highLightOff(); circuitComponent.setModeAndReset(CircuitComponent.Mode.running); modelDescription = new ModelDescription(circuitComponent.getCircuit(), library); - model = modelDescription.createModel(); + model = modelDescription.createModel(true); modelDescription.connectToGui(circuitComponent); model.init(); } catch (NodeException e) { @@ -273,7 +270,7 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave { private void loadFile(File filename) { XStream xStream = getxStream(); - try (FileReader in = new FileReader(filename)) { + try (InputStream in = new FileInputStream(filename)) { Circuit circuit = (Circuit) xStream.fromXML(in); circuitComponent.setCircuit(circuit); setFilename(filename); @@ -282,6 +279,21 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave { } } + private void saveFile(File filename) { + if (!filename.getName().endsWith(".dig")) + filename = new File(filename.getPath() + ".dig"); + + XStream xStream = getxStream(); + try (Writer out = new OutputStreamWriter(new FileOutputStream(filename), "utf-8")) { + out.write("\n"); + xStream.marshal(circuitComponent.getCircuit(), new PrettyPrintWriter(out)); + setFilename(filename); + circuitComponent.getCircuit().saved(); + } catch (IOException e) { + new ErrorMessage("error writing a file").addCause(e).show(); + } + } + private void setFilename(File filename) { this.filename = filename; if (filename != null) { @@ -291,20 +303,6 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave { setTitle("Digital"); } - private void saveFile(File filename) { - if (!filename.getName().endsWith(".dig")) - filename = new File(filename.getPath() + ".dig"); - - XStream xStream = getxStream(); - try (FileWriter out = new FileWriter(filename)) { - xStream.marshal(circuitComponent.getCircuit(), new PrettyPrintWriter(out)); - setFilename(filename); - circuitComponent.getCircuit().saved(); - } catch (IOException e) { - new ErrorMessage("error writing a file").addCause(e).show(); - } - } - private class ModeAction extends ToolTipAction { private final CircuitComponent.Mode mode; diff --git a/src/main/java/de/neemann/digital/gui/components/CircuitComponent.java b/src/main/java/de/neemann/digital/gui/components/CircuitComponent.java index afbbf43fb..d2868c8a6 100644 --- a/src/main/java/de/neemann/digital/gui/components/CircuitComponent.java +++ b/src/main/java/de/neemann/digital/gui/components/CircuitComponent.java @@ -273,7 +273,7 @@ public class CircuitComponent extends JComponent implements Observer { for (VisualElement vp : circuit.getParts()) if (vp.matches(pos)) { String name = vp.getElementName(); - ArrayList list = library.getPartType(name).getAttributeList(); + ArrayList list = library.getElementType(name).getAttributeList(); if (list.size() > 0) { Point p = new Point(e.getX(), e.getY()); SwingUtilities.convertPointToScreen(p, CircuitComponent.this); diff --git a/src/main/java/de/neemann/digital/gui/draw/library/PartLibrary.java b/src/main/java/de/neemann/digital/gui/draw/library/PartLibrary.java index a9370fac2..c7c2d285a 100644 --- a/src/main/java/de/neemann/digital/gui/draw/library/PartLibrary.java +++ b/src/main/java/de/neemann/digital/gui/draw/library/PartLibrary.java @@ -67,7 +67,7 @@ public class PartLibrary implements Iterable { list.add(new PartContainer(name, treePath)); } - public ElementTypeDescription getPartType(String partName) { + public ElementTypeDescription getElementType(String partName) { ElementTypeDescription pd = map.get(partName); if (pd == null) throw new RuntimeException("element " + partName + " not found"); diff --git a/src/main/java/de/neemann/digital/gui/draw/model/ModelDescription.java b/src/main/java/de/neemann/digital/gui/draw/model/ModelDescription.java index 5f2b428f8..8207c8b79 100644 --- a/src/main/java/de/neemann/digital/gui/draw/model/ModelDescription.java +++ b/src/main/java/de/neemann/digital/gui/draw/model/ModelDescription.java @@ -16,22 +16,30 @@ import java.util.HashSet; */ public class ModelDescription { - private final Circuit circuit; private final NetList netList; private final ArrayList entries; private HashMap map; + /** + * Creates the ModelDescription. + * After the the NetList is complete, so all pins connected together are registered + * to the Net instances in the NetList. Evenry group of connected Pins is represented + * by a Net-instance in the NetList. + * + * @param circuit the circuit + * @param library the library + * @throws PinException + */ public ModelDescription(Circuit circuit, PartLibrary library) throws PinException { - this.circuit = circuit; entries = new ArrayList<>(); netList = new NetList(circuit.getWires()); for (VisualElement vp : circuit.getParts()) { Pins pins = vp.getPins(); - ElementTypeDescription partType = library.getPartType(vp.getElementName()); - Element element = partType.createPart(vp.getElementAttributes()); + ElementTypeDescription elementType = library.getElementType(vp.getElementName()); + Element element = elementType.createElement(vp.getElementAttributes()); pins.setOutputs(element.getOutputs()); - entries.add(new ModelEntry(element, pins, vp, partType.getInputNames(vp.getElementAttributes()))); + entries.add(new ModelEntry(element, pins, vp, elementType.getInputNames(vp.getElementAttributes()))); for (Pin p : pins) netList.add(p); } @@ -43,10 +51,11 @@ public class ModelDescription { * @return the model * @throws PinException * @throws NodeException + * @param connectWires */ - public Model createModel() throws PinException, NodeException { + public Model createModel(boolean connectWires) throws PinException, NodeException { for (Net n : netList) - n.interconnect(); + n.interconnect(connectWires); Model m = new Model(); diff --git a/src/main/java/de/neemann/digital/gui/draw/model/Net.java b/src/main/java/de/neemann/digital/gui/draw/model/Net.java index 885a91c03..d72ad6fa7 100644 --- a/src/main/java/de/neemann/digital/gui/draw/model/Net.java +++ b/src/main/java/de/neemann/digital/gui/draw/model/Net.java @@ -10,6 +10,10 @@ import java.util.ArrayList; import java.util.HashSet; /** + * Contains all pins which are connected tigether. + * Is created and filled by the ModelDescription constructor. + * After creation all the ObservableValues belonging to the outputs are set. + * * @author hneemann */ public class Net { @@ -54,7 +58,7 @@ public class Net { pins.add(pin); } - public void interconnect() throws PinException { + public void interconnect(boolean connectWires) throws PinException { ArrayList inputs = new ArrayList<>(); ArrayList outputs = new ArrayList<>(); for (Pin p : pins) { @@ -77,8 +81,9 @@ public class Net { for (Pin i : inputs) i.setValue(value); - for (Wire w : wires) - w.setValue(value); + if (connectWires) + for (Wire w : wires) + w.setValue(value); } public void setHighLight(boolean highLight) { diff --git a/src/main/java/de/neemann/digital/gui/draw/shapes/ShapeFactory.java b/src/main/java/de/neemann/digital/gui/draw/shapes/ShapeFactory.java index 5a4cb2931..58df6512d 100644 --- a/src/main/java/de/neemann/digital/gui/draw/shapes/ShapeFactory.java +++ b/src/main/java/de/neemann/digital/gui/draw/shapes/ShapeFactory.java @@ -67,7 +67,7 @@ public final class ShapeFactory { } private String[] outputNames(ElementTypeDescription description, ElementAttributes attributes) { - ObservableValue[] o = description.createPart(attributes).getOutputs(); + ObservableValue[] o = description.createElement(attributes).getOutputs(); String[] names = new String[o.length]; for (int i = 0; i < names.length; i++) names[i] = o[i].getName(); @@ -80,7 +80,7 @@ public final class ShapeFactory { if (library == null) throw new RuntimeException("no shape for " + partName); else { - ElementTypeDescription pt = library.getPartType(partName); + ElementTypeDescription pt = library.getElementType(partName); return new GenericShape(pt.getShortName(), pt.getInputNames(elementAttributes), outputNames(pt, elementAttributes), true); } } else