renamed some methods

fixed utf-8 problem
This commit is contained in:
hneemann 2016-03-22 19:54:04 +01:00
parent 81ef864d66
commit 4240b60737
7 changed files with 49 additions and 37 deletions

View File

@ -99,7 +99,7 @@ public class ElementTypeDescription {
/** /**
* Returns the names of the inputs which are needed by this element. * 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. * 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 * The you get an array of <code>ObservableName</code>s, and <code>ObservableName</code> has a
* field <code>name</code>. * field <code>name</code>.
@ -117,7 +117,7 @@ public class ElementTypeDescription {
* @param elementAttributes the elements attributes * @param elementAttributes the elements attributes
* @return the Part instance * @return the Part instance
*/ */
public Element createPart(ElementAttributes elementAttributes) { public Element createElement(ElementAttributes elementAttributes) {
return partFactory.create(elementAttributes); return partFactory.create(elementAttributes);
} }
} }

View File

@ -27,10 +27,7 @@ import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.io.File; import java.io.*;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.prefs.Preferences; import java.util.prefs.Preferences;
/** /**
@ -171,7 +168,7 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
try { try {
modelDescription = new ModelDescription(circuitComponent.getCircuit(), library); modelDescription = new ModelDescription(circuitComponent.getCircuit(), library);
model = modelDescription.createModel(); model = modelDescription.createModel(false);
SpeedTest speedTest = new SpeedTest(model); SpeedTest speedTest = new SpeedTest(model);
double frequency = speedTest.calculate(); double frequency = speedTest.calculate();
JOptionPane.showMessageDialog(Main.this, "Frequency: " + frequency); JOptionPane.showMessageDialog(Main.this, "Frequency: " + frequency);
@ -229,7 +226,7 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave {
modelDescription.highLightOff(); modelDescription.highLightOff();
circuitComponent.setModeAndReset(CircuitComponent.Mode.running); circuitComponent.setModeAndReset(CircuitComponent.Mode.running);
modelDescription = new ModelDescription(circuitComponent.getCircuit(), library); modelDescription = new ModelDescription(circuitComponent.getCircuit(), library);
model = modelDescription.createModel(); model = modelDescription.createModel(true);
modelDescription.connectToGui(circuitComponent); modelDescription.connectToGui(circuitComponent);
model.init(); model.init();
} catch (NodeException e) { } catch (NodeException e) {
@ -273,7 +270,7 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave {
private void loadFile(File filename) { private void loadFile(File filename) {
XStream xStream = getxStream(); XStream xStream = getxStream();
try (FileReader in = new FileReader(filename)) { try (InputStream in = new FileInputStream(filename)) {
Circuit circuit = (Circuit) xStream.fromXML(in); Circuit circuit = (Circuit) xStream.fromXML(in);
circuitComponent.setCircuit(circuit); circuitComponent.setCircuit(circuit);
setFilename(filename); 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) { private void setFilename(File filename) {
this.filename = filename; this.filename = filename;
if (filename != null) { if (filename != null) {
@ -291,20 +303,6 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave {
setTitle("Digital"); 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 class ModeAction extends ToolTipAction {
private final CircuitComponent.Mode mode; private final CircuitComponent.Mode mode;

View File

@ -273,7 +273,7 @@ public class CircuitComponent extends JComponent implements Observer {
for (VisualElement vp : circuit.getParts()) for (VisualElement vp : circuit.getParts())
if (vp.matches(pos)) { if (vp.matches(pos)) {
String name = vp.getElementName(); String name = vp.getElementName();
ArrayList<AttributeKey> list = library.getPartType(name).getAttributeList(); ArrayList<AttributeKey> list = library.getElementType(name).getAttributeList();
if (list.size() > 0) { if (list.size() > 0) {
Point p = new Point(e.getX(), e.getY()); Point p = new Point(e.getX(), e.getY());
SwingUtilities.convertPointToScreen(p, CircuitComponent.this); SwingUtilities.convertPointToScreen(p, CircuitComponent.this);

View File

@ -67,7 +67,7 @@ public class PartLibrary implements Iterable<PartLibrary.PartContainer> {
list.add(new PartContainer(name, treePath)); list.add(new PartContainer(name, treePath));
} }
public ElementTypeDescription getPartType(String partName) { public ElementTypeDescription getElementType(String partName) {
ElementTypeDescription pd = map.get(partName); ElementTypeDescription pd = map.get(partName);
if (pd == null) if (pd == null)
throw new RuntimeException("element " + partName + " not found"); throw new RuntimeException("element " + partName + " not found");

View File

@ -16,22 +16,30 @@ import java.util.HashSet;
*/ */
public class ModelDescription { public class ModelDescription {
private final Circuit circuit;
private final NetList netList; private final NetList netList;
private final ArrayList<ModelEntry> entries; private final ArrayList<ModelEntry> entries;
private HashMap<Node, ModelEntry> map; 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 { public ModelDescription(Circuit circuit, PartLibrary library) throws PinException {
this.circuit = circuit;
entries = new ArrayList<>(); entries = new ArrayList<>();
netList = new NetList(circuit.getWires()); netList = new NetList(circuit.getWires());
for (VisualElement vp : circuit.getParts()) { for (VisualElement vp : circuit.getParts()) {
Pins pins = vp.getPins(); Pins pins = vp.getPins();
ElementTypeDescription partType = library.getPartType(vp.getElementName()); ElementTypeDescription elementType = library.getElementType(vp.getElementName());
Element element = partType.createPart(vp.getElementAttributes()); Element element = elementType.createElement(vp.getElementAttributes());
pins.setOutputs(element.getOutputs()); 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) for (Pin p : pins)
netList.add(p); netList.add(p);
} }
@ -43,10 +51,11 @@ public class ModelDescription {
* @return the model * @return the model
* @throws PinException * @throws PinException
* @throws NodeException * @throws NodeException
* @param connectWires
*/ */
public Model createModel() throws PinException, NodeException { public Model createModel(boolean connectWires) throws PinException, NodeException {
for (Net n : netList) for (Net n : netList)
n.interconnect(); n.interconnect(connectWires);
Model m = new Model(); Model m = new Model();

View File

@ -10,6 +10,10 @@ import java.util.ArrayList;
import java.util.HashSet; 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 * @author hneemann
*/ */
public class Net { public class Net {
@ -54,7 +58,7 @@ public class Net {
pins.add(pin); pins.add(pin);
} }
public void interconnect() throws PinException { public void interconnect(boolean connectWires) throws PinException {
ArrayList<Pin> inputs = new ArrayList<>(); ArrayList<Pin> inputs = new ArrayList<>();
ArrayList<Pin> outputs = new ArrayList<>(); ArrayList<Pin> outputs = new ArrayList<>();
for (Pin p : pins) { for (Pin p : pins) {
@ -77,8 +81,9 @@ public class Net {
for (Pin i : inputs) for (Pin i : inputs)
i.setValue(value); i.setValue(value);
for (Wire w : wires) if (connectWires)
w.setValue(value); for (Wire w : wires)
w.setValue(value);
} }
public void setHighLight(boolean highLight) { public void setHighLight(boolean highLight) {

View File

@ -67,7 +67,7 @@ public final class ShapeFactory {
} }
private String[] outputNames(ElementTypeDescription description, ElementAttributes attributes) { 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]; String[] names = new String[o.length];
for (int i = 0; i < names.length; i++) for (int i = 0; i < names.length; i++)
names[i] = o[i].getName(); names[i] = o[i].getName();
@ -80,7 +80,7 @@ public final class ShapeFactory {
if (library == null) if (library == null)
throw new RuntimeException("no shape for " + partName); throw new RuntimeException("no shape for " + partName);
else { else {
ElementTypeDescription pt = library.getPartType(partName); ElementTypeDescription pt = library.getElementType(partName);
return new GenericShape(pt.getShortName(), pt.getInputNames(elementAttributes), outputNames(pt, elementAttributes), true); return new GenericShape(pt.getShortName(), pt.getInputNames(elementAttributes), outputNames(pt, elementAttributes), true);
} }
} else } else