mirror of
https://github.com/hneemann/Digital.git
synced 2025-09-18 01:14: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;
|
||||
|
||||
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.Context;
|
||||
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.BoolTableIntArray;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
@ -18,7 +22,45 @@ public class TruthTable {
|
||||
|
||||
private final ArrayList<Variable> variables;
|
||||
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
|
||||
|
@ -755,7 +755,14 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave, E
|
||||
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();
|
||||
int p = name.lastIndexOf('.');
|
||||
if (p >= 0)
|
||||
|
@ -61,7 +61,7 @@ public class TableDialog extends JDialog {
|
||||
private final JPopupMenu renamePopup;
|
||||
private final Font font;
|
||||
private final ShapeFactory shapeFactory;
|
||||
private final File filename;
|
||||
private File filename;
|
||||
private TruthTableTableModel model;
|
||||
private TableColumn column;
|
||||
private int columnIndex;
|
||||
@ -122,6 +122,44 @@ public class TableDialog extends JDialog {
|
||||
|
||||
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")) {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
@ -310,13 +348,7 @@ public class TableDialog extends JDialog {
|
||||
if (filename == null) {
|
||||
filename = new File("circuit.jed");
|
||||
} else {
|
||||
String name = filename.getName();
|
||||
if (filename.getName().endsWith(".dig")) {
|
||||
name = name.substring(0, name.length() - 3) + "jed";
|
||||
} else {
|
||||
name += ".jed";
|
||||
}
|
||||
filename = new File(filename.getParentFile(), name);
|
||||
filename = Main.checkSuffix(filename.getParentFile(), "jed");
|
||||
}
|
||||
|
||||
JFileChooser fileChooser = new JFileChooser();
|
||||
@ -324,7 +356,7 @@ public class TableDialog extends JDialog {
|
||||
fileChooser.setSelectedFile(filename);
|
||||
if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
|
||||
try {
|
||||
try (OutputStream out = new FileOutputStream(fileChooser.getSelectedFile())) {
|
||||
try (OutputStream out = new FileOutputStream(Main.checkSuffix(fileChooser.getSelectedFile(), "jed"))) {
|
||||
expressionExporter.getPinMapping().addAll(pinMap);
|
||||
new BuilderExpressionCreator(expressionExporter.getBuilder(), ExpressionModifier.IDENTITY).create();
|
||||
expressionExporter.writeTo(out);
|
||||
@ -359,14 +391,8 @@ public class TableDialog extends JDialog {
|
||||
fileChooser.setFileFilter(new FileNameExtensionFilter("PLD", "PLD"));
|
||||
if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
|
||||
try {
|
||||
File f = fileChooser.getSelectedFile();
|
||||
String name = f.getName();
|
||||
if (!name.endsWith(".PLD"))
|
||||
name = name + ".PLD";
|
||||
|
||||
f = new File(f.getParentFile(), name);
|
||||
|
||||
cupl.setProjectName(name);
|
||||
File f = Main.checkSuffix(fileChooser.getSelectedFile(), "PLD");
|
||||
cupl.setProjectName(f.getName());
|
||||
cupl.getPinMapping().addAll(pinMap);
|
||||
new BuilderExpressionCreator(cupl.getBuilder(), ExpressionModifier.IDENTITY).create();
|
||||
try (FileOutputStream out = new FileOutputStream(f)) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user