adds a plain text export in the table dialog, closes #475

This commit is contained in:
hneemann 2020-06-09 08:12:59 +02:00
parent bb5e9241f9
commit 49e5e39795
4 changed files with 78 additions and 23 deletions

View File

@ -0,0 +1,34 @@
/*
* Copyright (c) 2020 Helmut Neemann.
* Use of this source code is governed by the GPL v3 license
* that can be found in the LICENSE file.
*/
package de.neemann.digital.gui.components.table;
import de.neemann.digital.analyse.expression.Expression;
import de.neemann.digital.analyse.expression.format.FormatToExpression;
/**
* Formats expressions to plain text
*/
public class PlainTextExpressionListener implements ExpressionListener {
private final StringBuilder sb = new StringBuilder();
@Override
public void resultFound(String name, Expression expression) {
String exp = FormatToExpression.defaultFormat(expression);
sb.append(name);
sb.append(" = ");
sb.append(exp);
sb.append('\n');
}
@Override
public void close() {
}
@Override
public String toString() {
return sb.toString();
}
}

View File

@ -107,15 +107,15 @@ public class TableDialog extends JDialog {
private final HashMap<String, HardwareDescriptionGenerator> availGenerators = new HashMap<>();
private final JMenu hardwareMenu;
private final TruthTableTableModel model;
private final AllSolutionsDialog allSolutionsDialog;
private final KarnaughMapDialog kvMap;
private final Mouse mouse = Mouse.getMouse();
private final UndoManager<TruthTable> undoManager;
private JCheckBoxMenuItem createJK;
private File filename;
private int columnIndex;
private AllSolutionsDialog allSolutionsDialog;
private ExpressionListenerStore lastGeneratedExpressions;
private KarnaughMapDialog kvMap;
private JMenuItem lastUsedGenratorMenuItem;
private Mouse mouse = Mouse.getMouse();
private UndoManager<TruthTable> undoManager;
/**
* Creates a new instance
@ -136,7 +136,7 @@ public class TableDialog extends JDialog {
model = new TruthTableTableModel(undoManager);
model.addTableModelListener(new CalculationTableModelListener());
kvMap = new KarnaughMapDialog(this, (boolTable, row) -> model.incValue(boolTable, row));
kvMap = new KarnaughMapDialog(this, model::incValue);
statusBar = new ExpressionComponent();
font = Screen.getInstance().getFont(1.66f);
@ -403,23 +403,17 @@ public class TableDialog extends JDialog {
}
});
fileMenu.add(new ToolTipAction(Lang.get("menu_table_exportTableLaTeX")) {
fileMenu.add(new TextExportAction(Lang.get("menu_table_exportTablePlainText")) {
@Override
public void actionPerformed(ActionEvent e) {
try {
final LaTeXExpressionListener laTeXExpressionListener = new LaTeXExpressionListener(undoManager.getActual());
ExpressionListener expressionListener = laTeXExpressionListener;
if (createJK.isSelected())
expressionListener = new ExpressionListenerJK(expressionListener);
lastGeneratedExpressions.replayTo(expressionListener);
expressionListener.close();
ExpressionListener createExpressionListener() {
return new PlainTextExpressionListener();
}
});
new ShowStringDialog(TableDialog.this, Lang.get("win_table_exportDialog"),
laTeXExpressionListener.toString()).setVisible(true);
} catch (ExpressionException | FormatterException e1) {
new ErrorMessage(Lang.get("msg_errorDuringCalculation")).addCause(e1).show(TableDialog.this);
}
fileMenu.add(new TextExportAction(Lang.get("menu_table_exportTableLaTeX")) {
@Override
ExpressionListener createExpressionListener() throws ExpressionException {
return new LaTeXExpressionListener(undoManager.getActual());
}
});
@ -770,7 +764,7 @@ public class TableDialog extends JDialog {
}
private final class SizeAction extends AbstractAction {
private int n;
private final int n;
private SizeAction(int n) {
super(Lang.get("menu_table_N_variables", n));
@ -785,7 +779,7 @@ public class TableDialog extends JDialog {
}
private final class SizeSequentialAction extends AbstractAction {
private int n;
private final int n;
private SizeSequentialAction(int n) {
super(Lang.get("menu_table_N_variables", n));
@ -814,7 +808,7 @@ public class TableDialog extends JDialog {
}
private final class SizeSequentialBidirectionalAction extends AbstractAction {
private int n;
private final int n;
private SizeSequentialBidirectionalAction(int n) {
super(Lang.get("menu_table_N_variables", n));
@ -974,4 +968,29 @@ public class TableDialog extends JDialog {
}
}
}
private abstract class TextExportAction extends ToolTipAction {
private TextExportAction(String name) {
super(name);
}
@Override
public void actionPerformed(ActionEvent e) {
try {
final ExpressionListener laTeXExpressionListener = createExpressionListener();
ExpressionListener expressionListener = laTeXExpressionListener;
if (createJK.isSelected())
expressionListener = new ExpressionListenerJK(expressionListener);
lastGeneratedExpressions.replayTo(expressionListener);
expressionListener.close();
new ShowStringDialog(TableDialog.this, Lang.get("win_table_exportDialog"),
laTeXExpressionListener.toString()).setVisible(true);
} catch (ExpressionException | FormatterException e1) {
new ErrorMessage(Lang.get("msg_errorDuringCalculation")).addCause(e1).show(TableDialog.this);
}
}
abstract ExpressionListener createExpressionListener() throws ExpressionException;
}
}

View File

@ -1639,6 +1639,7 @@ Sind evtl. die Namen der Variablen nicht eindeutig?</string>
<string name="menu_table_create_hardware">Bausteine</string>
<string name="menu_table_create_jedec_tt">Erzeugt eine JEDEC Datei für den Baustein</string>
<string name="menu_table_exportTableLaTeX">Erzeuge LaTeX</string>
<string name="menu_table_exportTablePlainText">Erzeuge einfachen Text</string>
<string name="menu_table_createFunctionFixture">Erzeuge Testfall</string>
<string name="menu_table_createFunctionFixture_tt">Erzeugt eine Testfallbeschreibung, welche in einen Testfall
übernommen werden kann.

View File

@ -1602,6 +1602,7 @@
<string name="menu_table_create_hardware">Device</string>
<string name="menu_table_create_jedec_tt">Creates a JEDEC file for the device</string>
<string name="menu_table_exportTableLaTeX">Export LaTeX</string>
<string name="menu_table_exportTablePlainText">Export Plain Text</string>
<string name="menu_table_createFunctionFixture">Export Test Case</string>
<string name="menu_table_createFunctionFixture_tt">Creates a test case description that can be used in a test
case.