Refactor MotorMountTable UI currently used in flight configuration

dialog in order to provide reuse.
This commit is contained in:
kruland2607 2013-10-09 12:26:35 -05:00
parent 88ddb86758
commit e091f3341a
3 changed files with 118 additions and 77 deletions

View File

@ -33,63 +33,56 @@ import net.sf.openrocket.rocketcomponent.RocketComponent;
import net.sf.openrocket.startup.Application; import net.sf.openrocket.startup.Application;
public class MotorConfigurationPanel extends JPanel { public class MotorConfigurationPanel extends JPanel {
private static final Translator trans = Application.getTranslator(); private static final Translator trans = Application.getTranslator();
private final FlightConfigurationDialog flightConfigurationDialog; private final FlightConfigurationDialog flightConfigurationDialog;
private final Rocket rocket; private final Rocket rocket;
private final JTable configurationTable; private final JTable configurationTable;
private final MotorConfigurationTableModel configurationTableModel; private final MotorConfigurationTableModel configurationTableModel;
private final JButton selectMotorButton, removeMotorButton, selectIgnitionButton, resetIgnitionButton; private final JButton selectMotorButton, removeMotorButton, selectIgnitionButton, resetIgnitionButton;
private final MotorChooserDialog dialog; private final MotorChooserDialog dialog;
MotorConfigurationPanel(FlightConfigurationDialog flightConfigurationDialog, Rocket rocket) { MotorConfigurationPanel(FlightConfigurationDialog flightConfigurationDialog, Rocket rocket) {
super(new MigLayout("fill")); super(new MigLayout("fill"));
dialog = new MotorChooserDialog(flightConfigurationDialog); dialog = new MotorChooserDialog(flightConfigurationDialog);
this.flightConfigurationDialog = flightConfigurationDialog; this.flightConfigurationDialog = flightConfigurationDialog;
this.rocket = rocket; this.rocket = rocket;
DescriptionArea desc = new DescriptionArea(trans.get("description"), 3, -1); DescriptionArea desc = new DescriptionArea(trans.get("description"), 3, -1);
this.add(desc, "spanx, growx, wrap para"); this.add(desc, "spanx, growx, wrap para");
//// Motor mount selection //// Motor mount selection
JLabel label = new StyledLabel(trans.get("lbl.motorMounts"), Style.BOLD); JLabel label = new StyledLabel(trans.get("lbl.motorMounts"), Style.BOLD);
this.add(label, ""); this.add(label, "");
//// Motor selection //// Motor selection
label = new StyledLabel(trans.get("lbl.motorConfiguration"), Style.BOLD); label = new StyledLabel(trans.get("lbl.motorConfiguration"), Style.BOLD);
this.add(label, "wrap rel"); this.add(label, "wrap rel");
//// Motor Mount selection //// Motor Mount selection
JTable table = new JTable(new MotorMountTableModel(this, rocket)); {
table.setTableHeader(null); MotorMountConfigurationPanel mountConfigPanel = new MotorMountConfigurationPanel(this,rocket) {
table.setShowVerticalLines(false); @Override
table.setRowSelectionAllowed(false); public void onDataChanged() {
table.setColumnSelectionAllowed(false); MotorConfigurationPanel.this.fireTableDataChanged();
TableColumnModel columnModel = table.getColumnModel(); }
TableColumn col0 = columnModel.getColumn(0); };
int w = table.getRowHeight() + 2; this.add(mountConfigPanel, "w 200lp, h 150lp, grow");
col0.setMinWidth(w); }
col0.setPreferredWidth(w);
col0.setMaxWidth(w);
table.addMouseListener(new GUIUtil.BooleanTableClickListener(table));
JScrollPane scroll = new JScrollPane(table);
this.add(scroll, "w 200lp, h 150lp, grow");
//// Motor selection table. //// Motor selection table.
configurationTableModel = new MotorConfigurationTableModel(rocket); configurationTableModel = new MotorConfigurationTableModel(rocket);
configurationTable = new JTable(configurationTableModel); configurationTable = new JTable(configurationTableModel);
configurationTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); configurationTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
configurationTable.setRowSelectionAllowed(true); configurationTable.setRowSelectionAllowed(true);
configurationTable.setDefaultRenderer(Object.class, new MotorTableCellRenderer()); configurationTable.setDefaultRenderer(Object.class, new MotorTableCellRenderer());
configurationTable.addMouseListener(new MouseAdapter() { configurationTable.addMouseListener(new MouseAdapter() {
@Override @Override
public void mouseClicked(MouseEvent e) { public void mouseClicked(MouseEvent e) {
@ -106,10 +99,10 @@ public class MotorConfigurationPanel extends JPanel {
} }
} }
}); });
scroll = new JScrollPane(configurationTable); JScrollPane scroll = new JScrollPane(configurationTable);
this.add(scroll, "w 500lp, h 150lp, grow, wrap"); this.add(scroll, "w 500lp, h 150lp, grow, wrap");
//// Select motor //// Select motor
selectMotorButton = new JButton(trans.get("MotorConfigurationPanel.btn.selectMotor")); selectMotorButton = new JButton(trans.get("MotorConfigurationPanel.btn.selectMotor"));
selectMotorButton.addActionListener(new ActionListener() { selectMotorButton.addActionListener(new ActionListener() {
@ -119,7 +112,7 @@ public class MotorConfigurationPanel extends JPanel {
} }
}); });
this.add(selectMotorButton, "skip, split, sizegroup button"); this.add(selectMotorButton, "skip, split, sizegroup button");
//// Remove motor button //// Remove motor button
removeMotorButton = new JButton(trans.get("MotorConfigurationPanel.btn.removeMotor")); removeMotorButton = new JButton(trans.get("MotorConfigurationPanel.btn.removeMotor"));
removeMotorButton.addActionListener(new ActionListener() { removeMotorButton.addActionListener(new ActionListener() {
@ -129,7 +122,7 @@ public class MotorConfigurationPanel extends JPanel {
} }
}); });
this.add(removeMotorButton, "sizegroup button"); this.add(removeMotorButton, "sizegroup button");
//// Select Ignition button //// Select Ignition button
selectIgnitionButton = new JButton(trans.get("MotorConfigurationPanel.btn.selectIgnition")); selectIgnitionButton = new JButton(trans.get("MotorConfigurationPanel.btn.selectIgnition"));
selectIgnitionButton.addActionListener(new ActionListener() { selectIgnitionButton.addActionListener(new ActionListener() {
@ -139,7 +132,7 @@ public class MotorConfigurationPanel extends JPanel {
} }
}); });
this.add(selectIgnitionButton, "sizegroup button"); this.add(selectIgnitionButton, "sizegroup button");
//// Reset Ignition button //// Reset Ignition button
resetIgnitionButton = new JButton(trans.get("MotorConfigurationPanel.btn.resetIgnition")); resetIgnitionButton = new JButton(trans.get("MotorConfigurationPanel.btn.resetIgnition"));
resetIgnitionButton.addActionListener(new ActionListener() { resetIgnitionButton.addActionListener(new ActionListener() {
@ -149,9 +142,9 @@ public class MotorConfigurationPanel extends JPanel {
} }
}); });
this.add(resetIgnitionButton, "sizegroup button, wrap"); this.add(resetIgnitionButton, "sizegroup button, wrap");
} }
public void fireTableDataChanged() { public void fireTableDataChanged() {
int selected = configurationTable.getSelectedRow(); int selected = configurationTable.getSelectedRow();
configurationTableModel.fireTableDataChanged(); configurationTableModel.fireTableDataChanged();
@ -161,7 +154,7 @@ public class MotorConfigurationPanel extends JPanel {
} }
updateButtonState(); updateButtonState();
} }
private void updateButtonState() { private void updateButtonState() {
String currentID = rocket.getDefaultConfiguration().getFlightConfigurationID(); String currentID = rocket.getDefaultConfiguration().getFlightConfigurationID();
MotorMount currentMount = getCurrentMount(); MotorMount currentMount = getCurrentMount();
@ -170,18 +163,18 @@ public class MotorConfigurationPanel extends JPanel {
selectIgnitionButton.setEnabled(currentMount != null && currentID != null); selectIgnitionButton.setEnabled(currentMount != null && currentID != null);
resetIgnitionButton.setEnabled(currentMount != null && currentID != null); resetIgnitionButton.setEnabled(currentMount != null && currentID != null);
} }
private MotorMount getCurrentMount() { private MotorMount getCurrentMount() {
int row = configurationTable.getSelectedRow(); int row = configurationTable.getSelectedRow();
if (row < 0) { if (row < 0) {
return null; return null;
} }
return getMount(row); return getMount(row);
} }
private MotorMount getMount(int row) { private MotorMount getMount(int row) {
int count = 0; int count = 0;
for (RocketComponent c : rocket) { for (RocketComponent c : rocket) {
@ -195,76 +188,76 @@ public class MotorConfigurationPanel extends JPanel {
} }
} }
} }
throw new IndexOutOfBoundsException("Invalid row, row=" + row + " count=" + count); throw new IndexOutOfBoundsException("Invalid row, row=" + row + " count=" + count);
} }
private void selectMotor() { private void selectMotor() {
String id = rocket.getDefaultConfiguration().getFlightConfigurationID(); String id = rocket.getDefaultConfiguration().getFlightConfigurationID();
MotorMount mount = getCurrentMount(); MotorMount mount = getCurrentMount();
if (id == null || mount == null) if (id == null || mount == null)
return; return;
MotorConfiguration config = mount.getMotorConfiguration().get(id); MotorConfiguration config = mount.getMotorConfiguration().get(id);
dialog.setMotorMountAndConfig(mount, id); dialog.setMotorMountAndConfig(mount, id);
dialog.setVisible(true); dialog.setVisible(true);
Motor m = dialog.getSelectedMotor(); Motor m = dialog.getSelectedMotor();
double d = dialog.getSelectedDelay(); double d = dialog.getSelectedDelay();
if (m != null) { if (m != null) {
config = new MotorConfiguration(); config = new MotorConfiguration();
config.setMotor(m); config.setMotor(m);
config.setEjectionDelay(d); config.setEjectionDelay(d);
mount.getMotorConfiguration().set(id, config); mount.getMotorConfiguration().set(id, config);
} }
fireTableDataChanged(); fireTableDataChanged();
} }
private void removeMotor() { private void removeMotor() {
String id = rocket.getDefaultConfiguration().getFlightConfigurationID(); String id = rocket.getDefaultConfiguration().getFlightConfigurationID();
MotorMount mount = getCurrentMount(); MotorMount mount = getCurrentMount();
if (id == null || mount == null) if (id == null || mount == null)
return; return;
mount.getMotorConfiguration().resetDefault(id); mount.getMotorConfiguration().resetDefault(id);
fireTableDataChanged(); fireTableDataChanged();
} }
private void selectIgnition() { private void selectIgnition() {
String currentID = rocket.getDefaultConfiguration().getFlightConfigurationID(); String currentID = rocket.getDefaultConfiguration().getFlightConfigurationID();
MotorMount currentMount = getCurrentMount(); MotorMount currentMount = getCurrentMount();
if (currentID == null || currentMount == null) if (currentID == null || currentMount == null)
return; return;
IgnitionSelectionDialog dialog = new IgnitionSelectionDialog( IgnitionSelectionDialog dialog = new IgnitionSelectionDialog(
this.flightConfigurationDialog, this.flightConfigurationDialog,
rocket, rocket,
currentMount); currentMount);
dialog.setVisible(true); dialog.setVisible(true);
fireTableDataChanged(); fireTableDataChanged();
} }
private void resetIgnition() { private void resetIgnition() {
String currentID = rocket.getDefaultConfiguration().getFlightConfigurationID(); String currentID = rocket.getDefaultConfiguration().getFlightConfigurationID();
MotorMount currentMount = getCurrentMount(); MotorMount currentMount = getCurrentMount();
if (currentID == null || currentMount == null) if (currentID == null || currentMount == null)
return; return;
currentMount.getIgnitionConfiguration().resetDefault(currentID); currentMount.getIgnitionConfiguration().resetDefault(currentID);
fireTableDataChanged(); fireTableDataChanged();
} }
private class MotorTableCellRenderer extends DefaultTableCellRenderer { private class MotorTableCellRenderer extends DefaultTableCellRenderer {
@Override @Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
@ -272,15 +265,15 @@ public class MotorConfigurationPanel extends JPanel {
return c; return c;
} }
JLabel label = (JLabel) c; JLabel label = (JLabel) c;
MotorMount mount = getMount(row); MotorMount mount = getMount(row);
String id = rocket.getDefaultConfiguration().getFlightConfigurationID(); String id = rocket.getDefaultConfiguration().getFlightConfigurationID();
switch (column) { switch (column) {
case 0: case 0:
regular(label); regular(label);
break; break;
case 1: case 1:
if (mount.getMotorConfiguration().get(id).getMotor() != null) { if (mount.getMotorConfiguration().get(id).getMotor() != null) {
regular(label); regular(label);
@ -288,7 +281,7 @@ public class MotorConfigurationPanel extends JPanel {
shaded(label); shaded(label);
} }
break; break;
case 2: case 2:
if (mount.getIgnitionConfiguration().isDefault(id)) { if (mount.getIgnitionConfiguration().isDefault(id)) {
shaded(label); shaded(label);
@ -297,20 +290,20 @@ public class MotorConfigurationPanel extends JPanel {
} }
break; break;
} }
return label; return label;
} }
private void shaded(JLabel label) { private void shaded(JLabel label) {
GUIUtil.changeFontStyle(label, Font.ITALIC); GUIUtil.changeFontStyle(label, Font.ITALIC);
label.setForeground(Color.GRAY); label.setForeground(Color.GRAY);
} }
private void regular(JLabel label) { private void regular(JLabel label) {
GUIUtil.changeFontStyle(label, Font.PLAIN); GUIUtil.changeFontStyle(label, Font.PLAIN);
label.setForeground(Color.BLACK); label.setForeground(Color.BLACK);
} }
} }
} }

View File

@ -0,0 +1,48 @@
package net.sf.openrocket.gui.dialogs.flightconfiguration;
import java.awt.Component;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import net.miginfocom.swing.MigLayout;
import net.sf.openrocket.gui.util.GUIUtil;
import net.sf.openrocket.rocketcomponent.Rocket;
public abstract class MotorMountConfigurationPanel extends JPanel {
private final Rocket rocket;
private final Component parent;
public MotorMountConfigurationPanel( Component parent, Rocket rocket ) {
super(new MigLayout("") );
this.parent = parent;
this.rocket = rocket;
//// Motor Mount selection
JTable table = new JTable(new MotorMountTableModel(this, rocket));
table.setTableHeader(null);
table.setShowVerticalLines(false);
table.setRowSelectionAllowed(false);
table.setColumnSelectionAllowed(false);
TableColumnModel columnModel = table.getColumnModel();
TableColumn col0 = columnModel.getColumn(0);
int w = table.getRowHeight() + 2;
col0.setMinWidth(w);
col0.setPreferredWidth(w);
col0.setMaxWidth(w);
table.addMouseListener(new GUIUtil.BooleanTableClickListener(table));
JScrollPane scroll = new JScrollPane(table);
this.add(scroll, "w 200lp, h 150lp, grow");
}
public abstract void onDataChanged();
}

View File

@ -14,14 +14,14 @@ import net.sf.openrocket.util.ArrayList;
*/ */
class MotorMountTableModel extends AbstractTableModel { class MotorMountTableModel extends AbstractTableModel {
private final MotorConfigurationPanel motorConfigurationPanel; private final MotorMountConfigurationPanel motorConfigurationPanel;
private final List<MotorMount> potentialMounts = new ArrayList<MotorMount>(); private final List<MotorMount> potentialMounts = new ArrayList<MotorMount>();
/** /**
* @param motorConfigurationPanel * @param motorConfigurationPanel
*/ */
MotorMountTableModel(MotorConfigurationPanel motorConfigurationPanel, Rocket rocket) { MotorMountTableModel(MotorMountConfigurationPanel motorConfigurationPanel, Rocket rocket) {
this.motorConfigurationPanel = motorConfigurationPanel; this.motorConfigurationPanel = motorConfigurationPanel;
for (RocketComponent c : rocket) { for (RocketComponent c : rocket) {
@ -82,6 +82,6 @@ class MotorMountTableModel extends AbstractTableModel {
MotorMount mount = potentialMounts.get(row); MotorMount mount = potentialMounts.get(row);
mount.setMotorMount((Boolean) value); mount.setMotorMount((Boolean) value);
this.motorConfigurationPanel.fireTableDataChanged(); this.motorConfigurationPanel.onDataChanged();
} }
} }