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;
public class MotorConfigurationPanel extends JPanel {
private static final Translator trans = Application.getTranslator();
private final FlightConfigurationDialog flightConfigurationDialog;
private final Rocket rocket;
private final JTable configurationTable;
private final MotorConfigurationTableModel configurationTableModel;
private final JButton selectMotorButton, removeMotorButton, selectIgnitionButton, resetIgnitionButton;
private final MotorChooserDialog dialog;
MotorConfigurationPanel(FlightConfigurationDialog flightConfigurationDialog, Rocket rocket) {
super(new MigLayout("fill"));
dialog = new MotorChooserDialog(flightConfigurationDialog);
this.flightConfigurationDialog = flightConfigurationDialog;
this.rocket = rocket;
DescriptionArea desc = new DescriptionArea(trans.get("description"), 3, -1);
this.add(desc, "spanx, growx, wrap para");
//// Motor mount selection
JLabel label = new StyledLabel(trans.get("lbl.motorMounts"), Style.BOLD);
this.add(label, "");
//// Motor selection
label = new StyledLabel(trans.get("lbl.motorConfiguration"), Style.BOLD);
this.add(label, "wrap rel");
//// 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");
{
MotorMountConfigurationPanel mountConfigPanel = new MotorMountConfigurationPanel(this,rocket) {
@Override
public void onDataChanged() {
MotorConfigurationPanel.this.fireTableDataChanged();
}
};
this.add(mountConfigPanel, "w 200lp, h 150lp, grow");
}
//// Motor selection table.
configurationTableModel = new MotorConfigurationTableModel(rocket);
configurationTable = new JTable(configurationTableModel);
configurationTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
configurationTable.setRowSelectionAllowed(true);
configurationTable.setDefaultRenderer(Object.class, new MotorTableCellRenderer());
configurationTable.addMouseListener(new MouseAdapter() {
@Override
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");
//// Select motor
selectMotorButton = new JButton(trans.get("MotorConfigurationPanel.btn.selectMotor"));
selectMotorButton.addActionListener(new ActionListener() {
@ -119,7 +112,7 @@ public class MotorConfigurationPanel extends JPanel {
}
});
this.add(selectMotorButton, "skip, split, sizegroup button");
//// Remove motor button
removeMotorButton = new JButton(trans.get("MotorConfigurationPanel.btn.removeMotor"));
removeMotorButton.addActionListener(new ActionListener() {
@ -129,7 +122,7 @@ public class MotorConfigurationPanel extends JPanel {
}
});
this.add(removeMotorButton, "sizegroup button");
//// Select Ignition button
selectIgnitionButton = new JButton(trans.get("MotorConfigurationPanel.btn.selectIgnition"));
selectIgnitionButton.addActionListener(new ActionListener() {
@ -139,7 +132,7 @@ public class MotorConfigurationPanel extends JPanel {
}
});
this.add(selectIgnitionButton, "sizegroup button");
//// Reset Ignition button
resetIgnitionButton = new JButton(trans.get("MotorConfigurationPanel.btn.resetIgnition"));
resetIgnitionButton.addActionListener(new ActionListener() {
@ -149,9 +142,9 @@ public class MotorConfigurationPanel extends JPanel {
}
});
this.add(resetIgnitionButton, "sizegroup button, wrap");
}
public void fireTableDataChanged() {
int selected = configurationTable.getSelectedRow();
configurationTableModel.fireTableDataChanged();
@ -161,7 +154,7 @@ public class MotorConfigurationPanel extends JPanel {
}
updateButtonState();
}
private void updateButtonState() {
String currentID = rocket.getDefaultConfiguration().getFlightConfigurationID();
MotorMount currentMount = getCurrentMount();
@ -170,18 +163,18 @@ public class MotorConfigurationPanel extends JPanel {
selectIgnitionButton.setEnabled(currentMount != null && currentID != null);
resetIgnitionButton.setEnabled(currentMount != null && currentID != null);
}
private MotorMount getCurrentMount() {
int row = configurationTable.getSelectedRow();
if (row < 0) {
return null;
}
return getMount(row);
}
private MotorMount getMount(int row) {
int count = 0;
for (RocketComponent c : rocket) {
@ -195,76 +188,76 @@ public class MotorConfigurationPanel extends JPanel {
}
}
}
throw new IndexOutOfBoundsException("Invalid row, row=" + row + " count=" + count);
}
private void selectMotor() {
String id = rocket.getDefaultConfiguration().getFlightConfigurationID();
MotorMount mount = getCurrentMount();
if (id == null || mount == null)
return;
MotorConfiguration config = mount.getMotorConfiguration().get(id);
dialog.setMotorMountAndConfig(mount, id);
dialog.setVisible(true);
Motor m = dialog.getSelectedMotor();
double d = dialog.getSelectedDelay();
if (m != null) {
config = new MotorConfiguration();
config.setMotor(m);
config.setEjectionDelay(d);
mount.getMotorConfiguration().set(id, config);
}
fireTableDataChanged();
}
private void removeMotor() {
String id = rocket.getDefaultConfiguration().getFlightConfigurationID();
MotorMount mount = getCurrentMount();
if (id == null || mount == null)
return;
mount.getMotorConfiguration().resetDefault(id);
fireTableDataChanged();
}
private void selectIgnition() {
String currentID = rocket.getDefaultConfiguration().getFlightConfigurationID();
MotorMount currentMount = getCurrentMount();
if (currentID == null || currentMount == null)
return;
IgnitionSelectionDialog dialog = new IgnitionSelectionDialog(
this.flightConfigurationDialog,
rocket,
currentMount);
dialog.setVisible(true);
fireTableDataChanged();
}
private void resetIgnition() {
String currentID = rocket.getDefaultConfiguration().getFlightConfigurationID();
MotorMount currentMount = getCurrentMount();
if (currentID == null || currentMount == null)
return;
currentMount.getIgnitionConfiguration().resetDefault(currentID);
fireTableDataChanged();
}
private class MotorTableCellRenderer extends DefaultTableCellRenderer {
@Override
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);
@ -272,15 +265,15 @@ public class MotorConfigurationPanel extends JPanel {
return c;
}
JLabel label = (JLabel) c;
MotorMount mount = getMount(row);
String id = rocket.getDefaultConfiguration().getFlightConfigurationID();
switch (column) {
case 0:
regular(label);
break;
case 1:
if (mount.getMotorConfiguration().get(id).getMotor() != null) {
regular(label);
@ -288,7 +281,7 @@ public class MotorConfigurationPanel extends JPanel {
shaded(label);
}
break;
case 2:
if (mount.getIgnitionConfiguration().isDefault(id)) {
shaded(label);
@ -297,20 +290,20 @@ public class MotorConfigurationPanel extends JPanel {
}
break;
}
return label;
}
private void shaded(JLabel label) {
GUIUtil.changeFontStyle(label, Font.ITALIC);
label.setForeground(Color.GRAY);
}
private void regular(JLabel label) {
GUIUtil.changeFontStyle(label, Font.PLAIN);
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 {
private final MotorConfigurationPanel motorConfigurationPanel;
private final MotorMountConfigurationPanel motorConfigurationPanel;
private final List<MotorMount> potentialMounts = new ArrayList<MotorMount>();
/**
* @param motorConfigurationPanel
*/
MotorMountTableModel(MotorConfigurationPanel motorConfigurationPanel, Rocket rocket) {
MotorMountTableModel(MotorMountConfigurationPanel motorConfigurationPanel, Rocket rocket) {
this.motorConfigurationPanel = motorConfigurationPanel;
for (RocketComponent c : rocket) {
@ -82,6 +82,6 @@ class MotorMountTableModel extends AbstractTableModel {
MotorMount mount = potentialMounts.get(row);
mount.setMotorMount((Boolean) value);
this.motorConfigurationPanel.fireTableDataChanged();
this.motorConfigurationPanel.onDataChanged();
}
}