added ok button to ElementOrderer

This commit is contained in:
hneemann 2016-05-23 14:15:39 +02:00
parent 87eb1728fc
commit 68fdfbc683
3 changed files with 84 additions and 42 deletions

View File

@ -344,7 +344,7 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave, E
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
ElementOrder o = new ElementOrder(circuitComponent.getCircuit(), "In"); ElementOrder o = new ElementOrder(circuitComponent.getCircuit(), "In");
new ElementOrderer<>(Main.this, Lang.get("menu_orderInputs"), o).setVisible(true); new ElementOrderer<>(Main.this, Lang.get("menu_orderInputs"), o).showDialog();
} }
}.setToolTip(Lang.get("menu_orderInputs_tt")); }.setToolTip(Lang.get("menu_orderInputs_tt"));
@ -352,7 +352,7 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave, E
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
ElementOrder o = new ElementOrder(circuitComponent.getCircuit(), "Out"); ElementOrder o = new ElementOrder(circuitComponent.getCircuit(), "Out");
new ElementOrderer<>(Main.this, Lang.get("menu_orderOutputs"), o).setVisible(true); new ElementOrderer<>(Main.this, Lang.get("menu_orderOutputs"), o).showDialog();
} }
}.setToolTip(Lang.get("menu_orderOutputs_tt")); }.setToolTip(Lang.get("menu_orderOutputs_tt"));
@ -513,8 +513,11 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave, E
names.add(s.getName()); names.add(s.getName());
new OrderMerger<String, String>(circuitComponent.getCircuit().getMeasurementOrdering()).order(names); new OrderMerger<String, String>(circuitComponent.getCircuit().getMeasurementOrdering()).order(names);
ElementOrderer.ListOrder<String> o = new ElementOrderer.ListOrder<>(names); ElementOrderer.ListOrder<String> o = new ElementOrderer.ListOrder<>(names);
new ElementOrderer<>(Main.this, Lang.get("menu_orderMeasurements"), o).setVisible(true); if (new ElementOrderer<>(Main.this, Lang.get("menu_orderMeasurements"), o)
.addOkButton()
.showDialog()) {
circuitComponent.getCircuit().setMeasurementOrdering(names); circuitComponent.getCircuit().setMeasurementOrdering(names);
}
} catch (Exception e1) { } catch (Exception e1) {
showErrorAndStopModel(Lang.get("msg_errorCreatingModel"), e1); showErrorAndStopModel(Lang.get("msg_errorCreatingModel"), e1);
} }

View File

@ -19,6 +19,13 @@ import java.util.ArrayList;
*/ */
public class ElementOrderer<T> extends JDialog { public class ElementOrderer<T> extends JDialog {
private final JPanel buttons;
private final JList<T> list;
private final MyListModel<T> listModel;
private final OrderInterface<T> data;
private boolean okPressed = true;
/** /**
* Creates a new instance * Creates a new instance
* *
@ -27,27 +34,16 @@ public class ElementOrderer<T> extends JDialog {
* @param data the data to order * @param data the data to order
*/ */
public ElementOrderer(Frame owner, String title, OrderInterface<T> data) { public ElementOrderer(Frame owner, String title, OrderInterface<T> data) {
this(owner, title, data, false);
}
/**
* Creates a new instance
*
* @param owner the owner of this dialog
* @param title the dialogs title
* @param data the data to order
* @param deleteAllowed its allowed to delete items
*/
public ElementOrderer(Frame owner, String title, OrderInterface<T> data, boolean deleteAllowed) {
super(owner, title, true); super(owner, title, true);
this.data = data;
setDefaultCloseOperation(DISPOSE_ON_CLOSE); setDefaultCloseOperation(DISPOSE_ON_CLOSE);
MyListModel<T> listModel = new MyListModel<T>(data); listModel = new MyListModel<T>(data);
JList list = new JList<T>(listModel); list = new JList<T>(listModel);
list.setPreferredSize(new Dimension(100, 150)); list.setPreferredSize(new Dimension(100, 150));
getContentPane().add(new JScrollPane(list)); getContentPane().add(new JScrollPane(list));
JPanel buttons = new JPanel(); buttons = new JPanel();
buttons.setLayout(new BoxLayout(buttons, BoxLayout.Y_AXIS)); buttons.setLayout(new BoxLayout(buttons, BoxLayout.Y_AXIS));
buttons.add(new ToolTipAction("\u2191") { buttons.add(new ToolTipAction("\u2191") {
@Override @Override
@ -70,7 +66,35 @@ public class ElementOrderer<T> extends JDialog {
} }
}.setToolTip(Lang.get("tt_moveItemDown")).createJButton()); }.setToolTip(Lang.get("tt_moveItemDown")).createJButton());
if (deleteAllowed) { getContentPane().add(buttons, BorderLayout.EAST);
setLocationRelativeTo(owner);
}
/**
* Called to add a ok button
*
* @return this for chained calls
*/
public ElementOrderer<T> addOkButton() {
JButton okButton = new JButton(new AbstractAction(Lang.get("ok")) {
@Override
public void actionPerformed(ActionEvent e) {
okPressed = true;
dispose();
}
});
getContentPane().add(okButton, BorderLayout.SOUTH);
okPressed = false;
return this;
}
/**
* Called to add a delete button
*
* @return this for chained calls
*/
public ElementOrderer<T> addDeleteButton() {
buttons.add(new ToolTipAction("\u274C") { buttons.add(new ToolTipAction("\u274C") {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
@ -81,11 +105,18 @@ public class ElementOrderer<T> extends JDialog {
} }
}.setToolTip(Lang.get("tt_deleteItem")).createJButton()); }.setToolTip(Lang.get("tt_deleteItem")).createJButton());
return this;
} }
getContentPane().add(buttons, BorderLayout.EAST);
/**
* Shows the dialog
*
* @return true if ok was pressed
*/
public boolean showDialog() {
pack(); pack();
setLocationRelativeTo(owner); setVisible(true);
return okPressed;
} }
/** /**

View File

@ -131,25 +131,33 @@ public class TableDialog extends JDialog {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
ReorderInputs ri = new ReorderInputs(model.getTable()); ReorderInputs ri = new ReorderInputs(model.getTable());
new ElementOrderer<>(parent, Lang.get("menu_table_inputs"), ri.getItems(), true).setVisible(true); if (new ElementOrderer<>(parent, Lang.get("menu_table_inputs"), ri.getItems())
.addDeleteButton()
.addOkButton()
.showDialog()) {
try { try {
setModel(new TruthTableTableModel(ri.reorder())); setModel(new TruthTableTableModel(ri.reorder()));
} catch (ExpressionException e1) { } catch (ExpressionException e1) {
new ErrorMessage().addCause(e1).show(TableDialog.this); new ErrorMessage().addCause(e1).show(TableDialog.this);
} }
} }
}
}.createJMenuItem()); }.createJMenuItem());
reorderMenu.add(new ToolTipAction(Lang.get("menu_table_outputs")) { reorderMenu.add(new ToolTipAction(Lang.get("menu_table_outputs")) {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
ReorderOutputs ro = new ReorderOutputs(model.getTable()); ReorderOutputs ro = new ReorderOutputs(model.getTable());
new ElementOrderer<>(parent, Lang.get("menu_table_inputs"), ro.getItems(), true).setVisible(true); if (new ElementOrderer<>(parent, Lang.get("menu_table_outputs"), ro.getItems())
.addDeleteButton()
.addOkButton()
.showDialog()) {
try { try {
setModel(new TruthTableTableModel(ro.reorder())); setModel(new TruthTableTableModel(ro.reorder()));
} catch (ExpressionException e1) { } catch (ExpressionException e1) {
new ErrorMessage().addCause(e1).show(TableDialog.this); new ErrorMessage().addCause(e1).show(TableDialog.this);
} }
} }
}
}.createJMenuItem()); }.createJMenuItem());
JMenu colsMenu = new JMenu(Lang.get("menu_table_newColumns")); JMenu colsMenu = new JMenu(Lang.get("menu_table_newColumns"));