mirror of
https://github.com/hneemann/Digital.git
synced 2025-09-18 09:24:42 -04:00
Its possible to load and save a truth table.
This commit is contained in:
parent
c1ea119b5d
commit
3f14865e1d
@ -1,5 +1,8 @@
|
|||||||
package de.neemann.digital.analyse;
|
package de.neemann.digital.analyse;
|
||||||
|
|
||||||
|
import com.thoughtworks.xstream.XStream;
|
||||||
|
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
|
||||||
|
import com.thoughtworks.xstream.io.xml.StaxDriver;
|
||||||
import de.neemann.digital.analyse.expression.BitSetter;
|
import de.neemann.digital.analyse.expression.BitSetter;
|
||||||
import de.neemann.digital.analyse.expression.Context;
|
import de.neemann.digital.analyse.expression.Context;
|
||||||
import de.neemann.digital.analyse.expression.ExpressionException;
|
import de.neemann.digital.analyse.expression.ExpressionException;
|
||||||
@ -7,6 +10,7 @@ import de.neemann.digital.analyse.expression.Variable;
|
|||||||
import de.neemann.digital.analyse.quinemc.BoolTable;
|
import de.neemann.digital.analyse.quinemc.BoolTable;
|
||||||
import de.neemann.digital.analyse.quinemc.BoolTableIntArray;
|
import de.neemann.digital.analyse.quinemc.BoolTableIntArray;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -18,7 +22,45 @@ public class TruthTable {
|
|||||||
|
|
||||||
private final ArrayList<Variable> variables;
|
private final ArrayList<Variable> variables;
|
||||||
private final ArrayList<Result> results;
|
private final ArrayList<Result> results;
|
||||||
private BitSetter bitSetter;
|
private transient BitSetter bitSetter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load the given file and returns a truth table instance
|
||||||
|
*
|
||||||
|
* @param filename filename
|
||||||
|
* @return the {@link TruthTable}
|
||||||
|
* @throws IOException IOException
|
||||||
|
*/
|
||||||
|
public static TruthTable readFromFile(File filename) throws IOException {
|
||||||
|
XStream xStream = getxStream();
|
||||||
|
try (InputStream in = new FileInputStream(filename)) {
|
||||||
|
return (TruthTable) xStream.fromXML(in);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes the table to the given file.
|
||||||
|
*
|
||||||
|
* @param filename the file
|
||||||
|
* @throws IOException IOException
|
||||||
|
*/
|
||||||
|
public void save(File filename) throws IOException {
|
||||||
|
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(this, new PrettyPrintWriter(out));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static XStream getxStream() {
|
||||||
|
XStream xStream = new XStream(new StaxDriver());
|
||||||
|
xStream.alias("truthTable", TruthTable.class);
|
||||||
|
xStream.alias("variable", Variable.class);
|
||||||
|
xStream.aliasAttribute(Variable.class, "identifier", "name");
|
||||||
|
xStream.alias("result", Result.class);
|
||||||
|
xStream.alias("BoolTable", BoolTableIntArray.class);
|
||||||
|
return xStream;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new instance
|
* Creates a new instance
|
||||||
|
@ -755,7 +755,14 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave, E
|
|||||||
setTitle(Lang.get("digital"));
|
setTitle(Lang.get("digital"));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static File checkSuffix(File filename, String suffix) {
|
/**
|
||||||
|
* Adds the given suffix to the file
|
||||||
|
*
|
||||||
|
* @param filename filename
|
||||||
|
* @param suffix suffix
|
||||||
|
* @return the file name with the given suffix
|
||||||
|
*/
|
||||||
|
public static File checkSuffix(File filename, String suffix) {
|
||||||
String name = filename.getName();
|
String name = filename.getName();
|
||||||
int p = name.lastIndexOf('.');
|
int p = name.lastIndexOf('.');
|
||||||
if (p >= 0)
|
if (p >= 0)
|
||||||
|
@ -61,7 +61,7 @@ public class TableDialog extends JDialog {
|
|||||||
private final JPopupMenu renamePopup;
|
private final JPopupMenu renamePopup;
|
||||||
private final Font font;
|
private final Font font;
|
||||||
private final ShapeFactory shapeFactory;
|
private final ShapeFactory shapeFactory;
|
||||||
private final File filename;
|
private File filename;
|
||||||
private TruthTableTableModel model;
|
private TruthTableTableModel model;
|
||||||
private TableColumn column;
|
private TableColumn column;
|
||||||
private int columnIndex;
|
private int columnIndex;
|
||||||
@ -122,6 +122,44 @@ public class TableDialog extends JDialog {
|
|||||||
|
|
||||||
JMenu fileMenu = new JMenu(Lang.get("menu_file"));
|
JMenu fileMenu = new JMenu(Lang.get("menu_file"));
|
||||||
|
|
||||||
|
fileMenu.add(new ToolTipAction(Lang.get("menu_open")) {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
JFileChooser fc = new JFileChooser();
|
||||||
|
if (TableDialog.this.filename != null)
|
||||||
|
fc.setSelectedFile(Main.checkSuffix(TableDialog.this.filename, "tru"));
|
||||||
|
if (fc.showOpenDialog(TableDialog.this) == JFileChooser.APPROVE_OPTION) {
|
||||||
|
try {
|
||||||
|
File file = Main.checkSuffix(fc.getSelectedFile(), "tru");
|
||||||
|
TruthTable truthTable = TruthTable.readFromFile(file);
|
||||||
|
setModel(new TruthTableTableModel(truthTable));
|
||||||
|
TableDialog.this.filename = file;
|
||||||
|
} catch (IOException e1) {
|
||||||
|
new ErrorMessage().addCause(e1).show(TableDialog.this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
fileMenu.add(new ToolTipAction(Lang.get("menu_save")) {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
JFileChooser fc = new JFileChooser();
|
||||||
|
if (TableDialog.this.filename != null)
|
||||||
|
fc.setSelectedFile(Main.checkSuffix(TableDialog.this.filename, "tru"));
|
||||||
|
if (fc.showSaveDialog(TableDialog.this) == JFileChooser.APPROVE_OPTION) {
|
||||||
|
try {
|
||||||
|
File file = Main.checkSuffix(fc.getSelectedFile(), "tru");
|
||||||
|
model.getTable().save(file);
|
||||||
|
TableDialog.this.filename = file;
|
||||||
|
} catch (IOException e1) {
|
||||||
|
new ErrorMessage().addCause(e1).show(TableDialog.this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
fileMenu.add(new ToolTipAction(Lang.get("menu_table_exportTableLaTeX")) {
|
fileMenu.add(new ToolTipAction(Lang.get("menu_table_exportTableLaTeX")) {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
@ -310,13 +348,7 @@ public class TableDialog extends JDialog {
|
|||||||
if (filename == null) {
|
if (filename == null) {
|
||||||
filename = new File("circuit.jed");
|
filename = new File("circuit.jed");
|
||||||
} else {
|
} else {
|
||||||
String name = filename.getName();
|
filename = Main.checkSuffix(filename.getParentFile(), "jed");
|
||||||
if (filename.getName().endsWith(".dig")) {
|
|
||||||
name = name.substring(0, name.length() - 3) + "jed";
|
|
||||||
} else {
|
|
||||||
name += ".jed";
|
|
||||||
}
|
|
||||||
filename = new File(filename.getParentFile(), name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
JFileChooser fileChooser = new JFileChooser();
|
JFileChooser fileChooser = new JFileChooser();
|
||||||
@ -324,7 +356,7 @@ public class TableDialog extends JDialog {
|
|||||||
fileChooser.setSelectedFile(filename);
|
fileChooser.setSelectedFile(filename);
|
||||||
if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
|
if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
|
||||||
try {
|
try {
|
||||||
try (OutputStream out = new FileOutputStream(fileChooser.getSelectedFile())) {
|
try (OutputStream out = new FileOutputStream(Main.checkSuffix(fileChooser.getSelectedFile(), "jed"))) {
|
||||||
expressionExporter.getPinMapping().addAll(pinMap);
|
expressionExporter.getPinMapping().addAll(pinMap);
|
||||||
new BuilderExpressionCreator(expressionExporter.getBuilder(), ExpressionModifier.IDENTITY).create();
|
new BuilderExpressionCreator(expressionExporter.getBuilder(), ExpressionModifier.IDENTITY).create();
|
||||||
expressionExporter.writeTo(out);
|
expressionExporter.writeTo(out);
|
||||||
@ -359,14 +391,8 @@ public class TableDialog extends JDialog {
|
|||||||
fileChooser.setFileFilter(new FileNameExtensionFilter("PLD", "PLD"));
|
fileChooser.setFileFilter(new FileNameExtensionFilter("PLD", "PLD"));
|
||||||
if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
|
if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
|
||||||
try {
|
try {
|
||||||
File f = fileChooser.getSelectedFile();
|
File f = Main.checkSuffix(fileChooser.getSelectedFile(), "PLD");
|
||||||
String name = f.getName();
|
cupl.setProjectName(f.getName());
|
||||||
if (!name.endsWith(".PLD"))
|
|
||||||
name = name + ".PLD";
|
|
||||||
|
|
||||||
f = new File(f.getParentFile(), name);
|
|
||||||
|
|
||||||
cupl.setProjectName(name);
|
|
||||||
cupl.getPinMapping().addAll(pinMap);
|
cupl.getPinMapping().addAll(pinMap);
|
||||||
new BuilderExpressionCreator(cupl.getBuilder(), ExpressionModifier.IDENTITY).create();
|
new BuilderExpressionCreator(cupl.getBuilder(), ExpressionModifier.IDENTITY).create();
|
||||||
try (FileOutputStream out = new FileOutputStream(f)) {
|
try (FileOutputStream out = new FileOutputStream(f)) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user