Add motor mount selection table to the Flight Configuration tab.

This commit is contained in:
kruland2607 2013-10-09 13:53:05 -05:00
parent e091f3341a
commit d6b19eb4b6

View File

@ -1,6 +1,5 @@
package net.sf.openrocket.gui.main.flightconfigpanel;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
@ -8,13 +7,17 @@ import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;
import net.miginfocom.swing.MigLayout;
import net.sf.openrocket.gui.components.StyledLabel;
import net.sf.openrocket.gui.components.StyledLabel.Style;
import net.sf.openrocket.gui.dialogs.flightconfiguration.IgnitionSelectionDialog;
import net.sf.openrocket.gui.dialogs.flightconfiguration.MotorMountConfigurationPanel;
import net.sf.openrocket.gui.dialogs.motor.MotorChooserDialog;
import net.sf.openrocket.motor.Motor;
import net.sf.openrocket.rocketcomponent.IgnitionConfiguration;
@ -25,22 +28,38 @@ import net.sf.openrocket.rocketcomponent.RocketComponent;
import net.sf.openrocket.unit.UnitGroup;
import net.sf.openrocket.util.Chars;
import net.sf.openrocket.util.Coordinate;
import net.sf.openrocket.util.Pair;
public class MotorConfigurationPanel extends FlightConfigurablePanel<MotorMount> {
private static final String NONE = trans.get("edtmotorconfdlg.tbl.None");
private final JButton selectMotorButton, removeMotorButton, selectIgnitionButton, resetIgnitionButton;
protected FlightConfigurableTableModel<MotorMount> configurationTableModel;
MotorConfigurationPanel(final FlightConfigurationPanel flightConfigurationPanel, Rocket rocket) {
super(flightConfigurationPanel,rocket);
{
//// Select motor mounts
JPanel subpanel = new JPanel(new MigLayout(""));
JLabel label = new StyledLabel(trans.get("lbl.motorMounts"), Style.BOLD);
subpanel.add(label, "wrap");
MotorMountConfigurationPanel mountConfigPanel = new MotorMountConfigurationPanel(this,rocket) {
@Override
public void onDataChanged() {
MotorConfigurationPanel.this.fireTableDataChanged();
}
};
subpanel.add(mountConfigPanel, "grow");
this.add(subpanel, "split, w 200lp, growy");
}
JScrollPane scroll = new JScrollPane(table);
this.add(scroll, "grow, wrap");
//// Select motor
selectMotorButton = new JButton(trans.get("MotorConfigurationPanel.btn.selectMotor"));
selectMotorButton.addActionListener(new ActionListener() {
@ -50,7 +69,7 @@ public class MotorConfigurationPanel extends FlightConfigurablePanel<MotorMount>
}
});
this.add(selectMotorButton, "split, sizegroup button");
//// Remove motor button
removeMotorButton = new JButton(trans.get("MotorConfigurationPanel.btn.removeMotor"));
removeMotorButton.addActionListener(new ActionListener() {
@ -60,7 +79,7 @@ public class MotorConfigurationPanel extends FlightConfigurablePanel<MotorMount>
}
});
this.add(removeMotorButton, "sizegroup button");
//// Select Ignition button
selectIgnitionButton = new JButton(trans.get("MotorConfigurationPanel.btn.selectIgnition"));
selectIgnitionButton.addActionListener(new ActionListener() {
@ -70,7 +89,7 @@ public class MotorConfigurationPanel extends FlightConfigurablePanel<MotorMount>
}
});
this.add(selectIgnitionButton, "sizegroup button");
//// Reset Ignition button
resetIgnitionButton = new JButton(trans.get("MotorConfigurationPanel.btn.resetIgnition"));
resetIgnitionButton.addActionListener(new ActionListener() {
@ -80,11 +99,11 @@ public class MotorConfigurationPanel extends FlightConfigurablePanel<MotorMount>
}
});
this.add(resetIgnitionButton, "sizegroup button, wrap");
updateButtonState();
}
@Override
protected JTable initializeTable() {
//// Motor selection table.
@ -94,13 +113,13 @@ public class MotorConfigurationPanel extends FlightConfigurablePanel<MotorMount>
protected boolean includeComponent(MotorMount component) {
return component.isMotorMount();
}
};
JTable configurationTable = new JTable(configurationTableModel);
configurationTable.setCellSelectionEnabled(true);
configurationTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
configurationTable.setDefaultRenderer(Object.class, new MotorTableCellRenderer());
configurationTable.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
@ -125,7 +144,7 @@ public class MotorConfigurationPanel extends FlightConfigurablePanel<MotorMount>
}
updateButtonState();
}
private void updateButtonState() {
String currentID = rocket.getDefaultConfiguration().getFlightConfigurationID();
MotorMount currentMount = getSelectedComponent();
@ -134,16 +153,16 @@ public class MotorConfigurationPanel extends FlightConfigurablePanel<MotorMount>
selectIgnitionButton.setEnabled(currentMount != null && currentID != null);
resetIgnitionButton.setEnabled(currentMount != null && currentID != null);
}
private void selectMotor() {
String id = rocket.getDefaultConfiguration().getFlightConfigurationID();
MotorMount mount = getSelectedComponent();
if (id == null || mount == null)
return;
MotorConfiguration config = mount.getMotorConfiguration().get(id);
MotorChooserDialog dialog = new MotorChooserDialog(
mount,
id,
@ -151,59 +170,59 @@ public class MotorConfigurationPanel extends FlightConfigurablePanel<MotorMount>
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 = getSelectedComponent();
if (id == null || mount == null)
return;
mount.getMotorConfiguration().resetDefault(id);
fireTableDataChanged();
}
private void selectIgnition() {
String currentID = rocket.getDefaultConfiguration().getFlightConfigurationID();
MotorMount currentMount = getSelectedComponent();
if (currentID == null || currentMount == null)
return;
IgnitionSelectionDialog dialog = new IgnitionSelectionDialog(
SwingUtilities.getWindowAncestor(this.flightConfigurationPanel),
rocket,
currentMount);
dialog.setVisible(true);
fireTableDataChanged();
}
private void resetIgnition() {
String currentID = rocket.getDefaultConfiguration().getFlightConfigurationID();
MotorMount currentMount = getSelectedComponent();
if (currentID == null || currentMount == null)
return;
currentMount.getIgnitionConfiguration().resetDefault(currentID);
fireTableDataChanged();
}
private class MotorTableCellRenderer extends FlightConfigurablePanel<MotorMount>.FlightConfigurableCellRenderer {
@Override
protected void format(MotorMount mount, String configId, JLabel label) {
MotorConfiguration motorConfig = mount.getMotorConfiguration().get(configId);
@ -214,10 +233,10 @@ public class MotorConfigurationPanel extends FlightConfigurablePanel<MotorMount>
private String getMotorSpecification(MotorMount mount, MotorConfiguration motorConfig) {
Motor motor = motorConfig.getMotor();
if (motor == null)
return NONE;
String str = motor.getDesignation(motorConfig.getEjectionDelay());
int count = getMountMultiplicity(mount);
if (count > 1) {
@ -225,21 +244,21 @@ public class MotorConfigurationPanel extends FlightConfigurablePanel<MotorMount>
}
return str;
}
private int getMountMultiplicity(MotorMount mount) {
RocketComponent c = (RocketComponent) mount;
return c.toAbsolute(Coordinate.NUL).length;
}
private String getIgnitionEventString(String id, MotorMount mount) {
IgnitionConfiguration ignitionConfig = mount.getIgnitionConfiguration().get(id);
IgnitionConfiguration.IgnitionEvent ignitionEvent = ignitionConfig.getIgnitionEvent();
Double ignitionDelay = ignitionConfig.getIgnitionDelay();
boolean isDefault = mount.getIgnitionConfiguration().isDefault(id);
String str = trans.get("MotorMount.IgnitionEvent.short." + ignitionEvent.name());
if (ignitionDelay > 0.001) {
str = str + " + " + UnitGroup.UNITS_SHORT_TIME.toStringUnit(ignitionDelay);
@ -250,7 +269,7 @@ public class MotorConfigurationPanel extends FlightConfigurablePanel<MotorMount>
}
return str;
}
}
}