mirror of
https://github.com/hneemann/Digital.git
synced 2025-09-17 08:55:05 -04:00
now ist possible to remove columns
This commit is contained in:
parent
07daa504d3
commit
4fee86024b
@ -310,10 +310,19 @@ public class TruthTable {
|
||||
return results.get(result).getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the given result column
|
||||
*
|
||||
* @param i
|
||||
*/
|
||||
public void removeResult(int i) {
|
||||
results.remove(i);
|
||||
}
|
||||
|
||||
/**
|
||||
* A single result column
|
||||
*/
|
||||
public static final class Result {
|
||||
private static final class Result {
|
||||
private String name;
|
||||
private BoolTable values;
|
||||
|
||||
|
@ -0,0 +1,68 @@
|
||||
package de.neemann.digital.gui.components.table;
|
||||
|
||||
import de.neemann.digital.analyse.TruthTable;
|
||||
import de.neemann.digital.analyse.expression.ContextFiller;
|
||||
import de.neemann.digital.analyse.expression.ExpressionException;
|
||||
import de.neemann.digital.analyse.expression.Variable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Used to delete a column from a {@link TruthTable}
|
||||
*
|
||||
* @author hneemann
|
||||
*/
|
||||
public class Delete {
|
||||
|
||||
private final TruthTable table;
|
||||
|
||||
/**
|
||||
* Creates a new instance
|
||||
*
|
||||
* @param table the table to use
|
||||
*/
|
||||
public Delete(TruthTable table) {
|
||||
this.table = table;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a column
|
||||
*
|
||||
* @param i the column to delete
|
||||
* @return the new table
|
||||
* @throws ExpressionException ExpressionException
|
||||
*/
|
||||
public TruthTable delete(int i) throws ExpressionException {
|
||||
|
||||
int vars = table.getVars().size();
|
||||
if (i >= vars) {
|
||||
if (table.getResultCount() > 1)
|
||||
table.removeResult(i - vars);
|
||||
return table;
|
||||
} else {
|
||||
if (table.getVars().size() < 3)
|
||||
return table;
|
||||
|
||||
ArrayList<Variable> newVars = new ArrayList<>();
|
||||
for (int j = 0; j < table.getVars().size(); j++) {
|
||||
if (j != i)
|
||||
newVars.add(table.getVars().get(j));
|
||||
}
|
||||
|
||||
TruthTable newTable = new TruthTable(newVars);
|
||||
for (int j = 0; j < table.getResultCount(); j++)
|
||||
newTable.addResult(table.getResultName(j));
|
||||
|
||||
ContextFiller fc = new ContextFiller(newTable.getVars());
|
||||
fc.set(table.getVars().get(i), false);
|
||||
for (int row = 0; row < newTable.getRows(); row++) {
|
||||
fc.setContextTo(row);
|
||||
for (int t = 0; t < newTable.getResultCount(); t++)
|
||||
newTable.setByContext(t, fc, table.getByContext(t, fc));
|
||||
}
|
||||
|
||||
return newTable;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -54,6 +54,7 @@ public class TableDialog extends JDialog {
|
||||
private final JPopupMenu renamePopup;
|
||||
private final Font font;
|
||||
private final JMenu reorderMenu;
|
||||
private final JMenu deleteMenu;
|
||||
private final ShapeFactory shapeFactory;
|
||||
private final File filename;
|
||||
private TruthTableTableModel model;
|
||||
@ -104,7 +105,7 @@ public class TableDialog extends JDialog {
|
||||
renamePopup.setVisible(false);
|
||||
header.repaint();
|
||||
model.getTable().setColumnName(columnIndex, text.getText());
|
||||
calculateExpressions();
|
||||
updateMenus();
|
||||
}
|
||||
});
|
||||
|
||||
@ -114,17 +115,25 @@ public class TableDialog extends JDialog {
|
||||
|
||||
JMenuBar bar = new JMenuBar();
|
||||
|
||||
JMenu sizeMenu = new JMenu(Lang.get("menu_table_size"));
|
||||
JMenu sizeMenu = new JMenu(Lang.get("menu_table_new"));
|
||||
|
||||
JMenu combinatorial = new JMenu(Lang.get("menu_table_new_combinatorial"));
|
||||
sizeMenu.add(combinatorial);
|
||||
for (int i = 2; i <= 8; i++)
|
||||
sizeMenu.add(new JMenuItem(new SizeAction(i)));
|
||||
combinatorial.add(new JMenuItem(new SizeAction(i)));
|
||||
JMenu sequential = new JMenu(Lang.get("menu_table_new_sequential"));
|
||||
sizeMenu.add(sequential);
|
||||
for (int i = 2; i <= 5; i++)
|
||||
sizeMenu.add(new JMenuItem(new SizeSequentialAction(i)));
|
||||
sequential.add(new JMenuItem(new SizeSequentialAction(i)));
|
||||
bar.add(sizeMenu);
|
||||
|
||||
reorderMenu = new JMenu(Lang.get("menu_table_reorder"));
|
||||
bar.add(reorderMenu);
|
||||
|
||||
JMenu colsMenu = new JMenu(Lang.get("menu_table_columns"));
|
||||
deleteMenu = new JMenu(Lang.get("menu_table_delete"));
|
||||
bar.add(deleteMenu);
|
||||
|
||||
JMenu colsMenu = new JMenu(Lang.get("menu_table_newColumns"));
|
||||
colsMenu.add(new ToolTipAction(Lang.get("menu_table_columnsAdd")) {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent actionEvent) {
|
||||
@ -268,15 +277,27 @@ public class TableDialog extends JDialog {
|
||||
this.model = model;
|
||||
model.addTableModelListener(new CalculationTableModelListener());
|
||||
table.setModel(model);
|
||||
updateMenus();
|
||||
}
|
||||
|
||||
private void updateMenus() {
|
||||
variables = model.getTable().getVars().size();
|
||||
reorderMenu.removeAll();
|
||||
int cols = model.getTable().getVars().size();
|
||||
reorderMenu.add(new JMenuItem(new ReorderAction(cols, -1)));
|
||||
reorderMenu.add(new JMenuItem(new ReorderAction(cols)));
|
||||
for (int i = 0; i < cols - 1; i++) {
|
||||
for (int i = 0; i < cols - 1; i++)
|
||||
reorderMenu.add(new JMenuItem(new ReorderAction(cols, i, i + 1)));
|
||||
}
|
||||
|
||||
reorderMenu.add(new JMenuItem(new ReorderAction(cols, cols - 1, 0)));
|
||||
|
||||
deleteMenu.removeAll();
|
||||
for (int i = 0; i < cols; i++)
|
||||
deleteMenu.add(new JMenuItem(new DeleteAction(i)));
|
||||
int res = model.getTable().getResultCount();
|
||||
for (int i = 0; i < res; i++)
|
||||
deleteMenu.add(new JMenuItem(new DeleteAction(cols + i)));
|
||||
|
||||
calculateExpressions();
|
||||
}
|
||||
|
||||
@ -334,7 +355,7 @@ public class TableDialog extends JDialog {
|
||||
private int n;
|
||||
|
||||
private SizeSequentialAction(int n) {
|
||||
super(Lang.get("menu_table_N_variablesSequential", n));
|
||||
super(Lang.get("menu_table_N_variables", n));
|
||||
this.n = n;
|
||||
}
|
||||
|
||||
@ -403,7 +424,7 @@ public class TableDialog extends JDialog {
|
||||
}
|
||||
|
||||
private ReorderAction(int cols, int swapIndex1, int swapIndex2) {
|
||||
super(Lang.get("menu_table_swap_N1_N2", swapIndex1, swapIndex2));
|
||||
super(Lang.get("menu_table_swap_N1_N2", model.getTable().getColumnName(swapIndex1), model.getTable().getColumnName(swapIndex2)));
|
||||
swap = new int[cols];
|
||||
for (int i = 0; i < cols; i++)
|
||||
swap[i] = i;
|
||||
@ -423,6 +444,24 @@ public class TableDialog extends JDialog {
|
||||
}
|
||||
}
|
||||
|
||||
private final class DeleteAction extends AbstractAction {
|
||||
private final int i;
|
||||
|
||||
DeleteAction(int i) {
|
||||
super(model.getTable().getColumnName(i));
|
||||
this.i = i;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
try {
|
||||
setModel(new TruthTableTableModel(new Delete(model.getTable()).delete(i)));
|
||||
} catch (ExpressionException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class BuiderExpressionCreator extends ExpressionCreator {
|
||||
private final HashSet<String> contained;
|
||||
private final BuilderInterface builder;
|
||||
@ -451,4 +490,5 @@ public class TableDialog extends JDialog {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -275,16 +275,16 @@ menu_view=Ansicht
|
||||
menu_maximize=Maximieren
|
||||
menu_zoomIn=Vergr\u00F6\u00DFern
|
||||
menu_zoomOut=Verkleinern
|
||||
|
||||
|
||||
menu_table_size=Gr\u00F6\u00DFe
|
||||
menu_table_new=Neu
|
||||
menu_table_N_variables={0} Variablen
|
||||
menu_table_N_variablesSequential={0} Variablen Automat
|
||||
menu_table_new_combinatorial=Kombintorisch
|
||||
menu_table_new_sequential=Automat
|
||||
menu_table_reorder=Umsortieren
|
||||
menu_table_delete=L\u00F6schen
|
||||
menu_table_reverse=Reihenfolge invertieren
|
||||
menu_table_rotate=Variablen rotieren
|
||||
menu_table_swap_N1_N2=Tausche Variable {0} und {1}
|
||||
menu_table_columns=Spalten
|
||||
menu_table_swap_N1_N2=Tausche Variablen {0} und {1}
|
||||
menu_table_newColumns=Spalten hinzuf\u00FCgen
|
||||
menu_table_columnsAdd=Ergebnisspalte hinzuf\u00FCgen
|
||||
menu_table_columnsAdd_tt=F\u00FCgt der Tabelle eine Ergebnisspalte hinzu.
|
||||
menu_table_columnsAddVariable=Variable hinzuf\u00FCgen
|
||||
|
@ -253,16 +253,16 @@ menu_view=View
|
||||
menu_maximize=Maximize
|
||||
menu_zoomIn=Zoom In
|
||||
menu_zoomOut=Zoom Out
|
||||
|
||||
|
||||
menu_table_size=Size
|
||||
menu_table_new=New
|
||||
menu_table_N_variables={0} variables
|
||||
menu_table_N_variablesSequential={0} variables sequential
|
||||
menu_table_new_combinatorial=Combinatorial
|
||||
menu_table_new_sequential=Sequential
|
||||
menu_table_reorder=Reorder
|
||||
menu_table_delete=Delete
|
||||
menu_table_reverse=Reverse Variables
|
||||
menu_table_rotate=Rotate Variables
|
||||
menu_table_swap_N1_N2=Swap Variables {0} and {1}
|
||||
menu_table_columns=Columns
|
||||
menu_table_newColumns=Add Columns
|
||||
menu_table_columnsAdd=Add a result Column
|
||||
menu_table_columnsAdd_tt=Adds a new result column.
|
||||
menu_table_columnsAddVariable=Add a variable Column
|
||||
|
@ -0,0 +1,44 @@
|
||||
package de.neemann.digital.gui.components.table;
|
||||
|
||||
import de.neemann.digital.analyse.TruthTable;
|
||||
import de.neemann.digital.analyse.expression.ContextFiller;
|
||||
import de.neemann.digital.analyse.quinemc.BoolTableIntArray;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* @author hneemann
|
||||
*/
|
||||
public class DeleteTest extends TestCase {
|
||||
|
||||
public void testDeleteVar() throws Exception {
|
||||
TruthTable t = new TruthTable(3).addResult();
|
||||
BoolTableIntArray col = (BoolTableIntArray) t.getResult(0);
|
||||
for (int i = 0; i < t.getRows(); i++)
|
||||
col.set(i, i + 1);
|
||||
|
||||
TruthTable newTable = new Delete(t).delete(1);
|
||||
|
||||
assertEquals(2, newTable.getVars().size());
|
||||
assertEquals(1, newTable.getResultCount());
|
||||
|
||||
ContextFiller cf = new ContextFiller(newTable.getVars());
|
||||
cf.set(t.getVars().get(1), false);
|
||||
for (int i = 0; i < newTable.getRows(); i++) {
|
||||
cf.setContextTo(i);
|
||||
assertEquals(newTable.getByContext(0, cf), t.getByContext(0, cf));
|
||||
}
|
||||
}
|
||||
|
||||
public void testDeleteResult() throws Exception {
|
||||
TruthTable t = new TruthTable(3).addResult().addResult();
|
||||
BoolTableIntArray col = (BoolTableIntArray) t.getResult(0);
|
||||
for (int i = 0; i < t.getRows(); i++)
|
||||
col.set(i, i + 1);
|
||||
|
||||
TruthTable newTable = new Delete(t).delete(3);
|
||||
|
||||
assertEquals(3, newTable.getVars().size());
|
||||
assertEquals(1, newTable.getResultCount());
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user