mirror of
https://github.com/hneemann/Digital.git
synced 2025-09-18 09:24:42 -04:00
renamed some methods
fixed utf-8 problem
This commit is contained in:
parent
81ef864d66
commit
4240b60737
@ -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 <code>createPart()</code>
|
||||
* If you need a list of outputs names you can create a element using <code>createElement()</code>
|
||||
* and request the outputs by calling the elements <code>getOutputs()</code> method.
|
||||
* The you get an array of <code>ObservableName</code>s, and <code>ObservableName</code> has a
|
||||
* field <code>name</code>.
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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("<?xml version=\"1.0\" encoding=\"utf-8\"?>\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;
|
||||
|
||||
|
@ -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<AttributeKey> list = library.getPartType(name).getAttributeList();
|
||||
ArrayList<AttributeKey> list = library.getElementType(name).getAttributeList();
|
||||
if (list.size() > 0) {
|
||||
Point p = new Point(e.getX(), e.getY());
|
||||
SwingUtilities.convertPointToScreen(p, CircuitComponent.this);
|
||||
|
@ -67,7 +67,7 @@ public class PartLibrary implements Iterable<PartLibrary.PartContainer> {
|
||||
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");
|
||||
|
@ -16,22 +16,30 @@ import java.util.HashSet;
|
||||
*/
|
||||
public class ModelDescription {
|
||||
|
||||
private final Circuit circuit;
|
||||
private final NetList netList;
|
||||
private final ArrayList<ModelEntry> entries;
|
||||
private HashMap<Node, ModelEntry> 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();
|
||||
|
||||
|
@ -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<Pin> inputs = new ArrayList<>();
|
||||
ArrayList<Pin> outputs = new ArrayList<>();
|
||||
for (Pin p : pins) {
|
||||
@ -77,6 +81,7 @@ public class Net {
|
||||
for (Pin i : inputs)
|
||||
i.setValue(value);
|
||||
|
||||
if (connectWires)
|
||||
for (Wire w : wires)
|
||||
w.setValue(value);
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user