Pulled the setup of the Motor Configuration panel out of

FlightConfigurationDialog in to its own class.
This commit is contained in:
kruland2607 2012-10-17 10:50:29 -05:00
parent 1f445b2268
commit 6e66aeac31
4 changed files with 286 additions and 237 deletions

View File

@ -3,56 +3,37 @@ package net.sf.openrocket.gui.dialogs.flightconfiguration;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import net.miginfocom.swing.MigLayout;
import net.sf.openrocket.document.OpenRocketDocument;
import net.sf.openrocket.gui.dialogs.motor.MotorChooserDialog;
import net.sf.openrocket.gui.main.BasicFrame;
import net.sf.openrocket.gui.util.GUIUtil;
import net.sf.openrocket.l10n.Translator;
import net.sf.openrocket.motor.Motor;
import net.sf.openrocket.rocketcomponent.MotorMount;
import net.sf.openrocket.rocketcomponent.Rocket;
import net.sf.openrocket.rocketcomponent.RocketComponent;
import net.sf.openrocket.rocketcomponent.MotorMount.IgnitionEvent;
import net.sf.openrocket.startup.Application;
import net.sf.openrocket.util.Chars;
public class FlightConfigurationDialog extends JDialog {
static final Translator trans = Application.getTranslator();
private final Rocket rocket;
final MotorMount[] mounts;
private MotorConfigurationTableModel configurationTableModel;
private FlightConfigurationModel flightConfigurationModel;
private final JButton renameConfButton, removeConfButton, copyConfButton;
private JButton selectMotorButton, removeMotorButton;
private final MotorConfigurationPanel motorConfigurationPanel;
private String currentID = null;
private MotorMount currentMount = null;
static final Translator trans = Application.getTranslator();
public FlightConfigurationDialog(final Rocket rocket, Window parent) {
//// Edit motor configurations
@ -67,8 +48,6 @@ public class FlightConfigurationDialog extends JDialog {
this.rocket = rocket;
mounts = getPotentialMotorMounts();
JPanel panel = new JPanel(new MigLayout("fill"));
JLabel label = new JLabel("Selected Configuration: ");
@ -125,8 +104,8 @@ public class FlightConfigurationDialog extends JDialog {
panel.add( tabs, "spanx, w 700lp, h 500lp, wrap");
//// Motor tabs
JComponent motorTab = makeMotorTab();
tabs.add(trans.get("edtmotorconfdlg.lbl.Motortab"), motorTab);
motorConfigurationPanel = new MotorConfigurationPanel(this,rocket);
tabs.add(trans.get("edtmotorconfdlg.lbl.Motortab"), motorConfigurationPanel);
//// Recovery tab
tabs.add(trans.get("edtmotorconfdlg.lbl.Recoverytab"), new RecoveryConfigurationPanel(this,rocket));
@ -164,34 +143,27 @@ public class FlightConfigurationDialog extends JDialog {
}
}
private MotorMount[] getPotentialMotorMounts() {
List<MotorMount> list = new ArrayList<MotorMount>();
for (RocketComponent c : rocket) {
if (c instanceof MotorMount) {
list.add((MotorMount) c);
}
}
return list.toArray(new MotorMount[0]);
}
void selectConfiguration( String id ) {
public void selectConfiguration( String id ) {
currentID = id;
rocket.getDefaultConfiguration().setMotorConfigurationID(currentID);
configurationTableModel.fireTableDataChanged();
motorConfigurationPanel.fireTableDataChanged();
// FIXME - update data in recovery configuration panel
updateButtonState();
}
public void addConfiguration() {
currentID = rocket.newMotorConfigurationID();
rocket.getDefaultConfiguration().setMotorConfigurationID(currentID);
configurationTableModel.fireTableDataChanged();
motorConfigurationPanel.fireTableDataChanged();
// FIXME - update data in recovery configuration panel
flightConfigurationModel.fireContentsUpdated();
updateButtonState();
}
public void changeConfigurationName( String newName ) {
rocket.setMotorConfigurationName(currentID, newName);
configurationTableModel.fireTableDataChanged();
motorConfigurationPanel.fireTableDataChanged();
// FIXME - update data in recovery configuration panel
flightConfigurationModel.fireContentsUpdated();
}
@ -200,194 +172,25 @@ public class FlightConfigurationDialog extends JDialog {
return;
rocket.removeMotorConfigurationID(currentID);
rocket.getDefaultConfiguration().setMotorConfigurationID(null);
configurationTableModel.fireTableDataChanged();
motorConfigurationPanel.fireTableDataChanged();
// FIXME - update data in recovery configuration panel
flightConfigurationModel.fireContentsUpdated();
updateButtonState();
}
/**
* Call this from other panels when a change might cause the names of the configurations to change.
*/
public void fireContentsUpdated() {
flightConfigurationModel.fireContentsUpdated();
}
private void updateButtonState() {
removeConfButton.setEnabled(currentID != null);
renameConfButton.setEnabled(currentID != null);
selectMotorButton.setEnabled(currentMount != null && currentID != null);
removeMotorButton.setEnabled(currentMount != null && currentID != null);
}
private void selectMotor() {
if (currentID == null || currentMount == null)
return;
MotorChooserDialog dialog = new MotorChooserDialog(currentMount.getMotor(currentID),
currentMount.getMotorDelay(currentID), currentMount.getMotorMountDiameter(), this);
dialog.setVisible(true);
Motor m = dialog.getSelectedMotor();
double d = dialog.getSelectedDelay();
if (m != null) {
currentMount.setMotor(currentID, m);
currentMount.setMotorDelay(currentID, d);
}
configurationTableModel.fireTableDataChanged();
updateButtonState();
motorConfigurationPanel.updateButtonState();
// FIXME - update button state in recovery configuration panel
}
private void removeMotor() {
if (currentID == null || currentMount == null)
return;
currentMount.setMotor(currentID, null);
configurationTableModel.fireTableDataChanged();
updateButtonState();
}
void makeMotorMount( MotorMount mount, boolean isMotorMount ) {
mount.setMotorMount( isMotorMount );
configurationTableModel.fireTableStructureChanged();
updateButtonState();
}
MotorMount findMount(int column) {
MotorMount mount = null;
int count = column;
for (MotorMount m : mounts) {
if (m.isMotorMount())
count--;
if (count < 0) {
mount = m;
break;
}
}
if (mount == null) {
throw new IndexOutOfBoundsException("motor mount not found, column=" + column);
}
return mount;
}
String findMotorForDisplay( int column ) {
String id = currentID;
MotorMount mount = findMount(column);
Motor motor = mount.getMotor(id);
if (motor == null)
return null;
String str = motor.getDesignation(mount.getMotorDelay(id));
int count = mount.getMotorCount();
if (count > 1) {
str = "" + count + Chars.TIMES + " " + str;
}
return str;
}
String findIgnitionForDisplay( int column ) {
String id = currentID;
MotorMount mount = findMount(column);
Motor motor = mount.getMotor(id);
if (motor == null)
//// None
return null;
IgnitionEvent ignition = mount.getIgnitionEvent();
return ignition.toString();
}
private JComponent makeMotorTab( ) {
JPanel panel = new JPanel(new MigLayout("fill"));
//// Motor mount selection
//// <html><b>Motor mounts:</b>
JLabel label = new JLabel(trans.get("edtmotorconfdlg.lbl.Motormounts"));
panel.add(label, "gapbottom para");
//// Motor selection
//// <html><b>Motor configurations:</b>
label = new JLabel(trans.get("edtmotorconfdlg.lbl.Motorconfig"));
panel.add(label, "gapbottom para, wrap");
//// <html>Select which components function as motor mounts:
label = new JLabel(trans.get("edtmotorconfdlg.selectcomp"));
panel.add(label, "ay 100%, w 1px, growx, wrap");
//// Motor Mount selection
JTable table = new JTable(new MotorMountTableModel(this));
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);
panel.add(scroll, "w 200lp, h 150lp, grow");
//// Motor selection table.
configurationTableModel = new MotorConfigurationTableModel(this, true);
final JTable configurationTable = new JTable(configurationTableModel);
configurationTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
configurationTable.setCellSelectionEnabled(true);
configurationTable.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
JTable table = (JTable) e.getComponent();
int row = configurationTable.getSelectedRow();
int column = configurationTable.getSelectedColumn();
if ( row >= 0 & column == 1) {
currentMount = findMount(row);
} else {
currentMount = null;
}
if (e.getClickCount() == 1) {
// Single click updates selection
updateButtonState();
} else if (e.getClickCount() == 2) {
// Double-click edits motor
selectMotor();
}
}
});
scroll = new JScrollPane(configurationTable);
panel.add(scroll, "w 500lp, h 150lp, grow, wrap");
//// Select motor
selectMotorButton = new JButton(trans.get("edtmotorconfdlg.but.Selectmotor"));
selectMotorButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
selectMotor();
}
});
panel.add(selectMotorButton, "skip, split, sizegroup button");
//// Remove motor button
removeMotorButton = new JButton(trans.get("edtmotorconfdlg.but.removemotor"));
removeMotorButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
removeMotor();
}
});
panel.add(removeMotorButton,"sizegroup button, wrap");
return panel;
}
}

View File

@ -0,0 +1,246 @@
package net.sf.openrocket.gui.dialogs.flightconfiguration;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
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.table.TableColumn;
import javax.swing.table.TableColumnModel;
import net.miginfocom.swing.MigLayout;
import net.sf.openrocket.gui.dialogs.motor.MotorChooserDialog;
import net.sf.openrocket.gui.util.GUIUtil;
import net.sf.openrocket.motor.Motor;
import net.sf.openrocket.rocketcomponent.MotorMount;
import net.sf.openrocket.rocketcomponent.Rocket;
import net.sf.openrocket.rocketcomponent.RocketComponent;
import net.sf.openrocket.rocketcomponent.MotorMount.IgnitionEvent;
import net.sf.openrocket.util.Chars;
public class MotorConfigurationPanel extends JPanel {
private final FlightConfigurationDialog flightConfigurationDialog;
private final Rocket rocket;
private final MotorConfigurationTableModel configurationTableModel;
private final JButton selectMotorButton, removeMotorButton;
private MotorMount currentMount = null;
final MotorMount[] mounts;
MotorConfigurationPanel(FlightConfigurationDialog flightConfigurationDialog, Rocket rocket) {
super(new MigLayout("fill"));
this.flightConfigurationDialog = flightConfigurationDialog;
this.rocket = rocket;
mounts = getPotentialMotorMounts();
//// Motor mount selection
//// <html><b>Motor mounts:</b>
JLabel label = new JLabel(FlightConfigurationDialog.trans.get("edtmotorconfdlg.lbl.Motormounts"));
this.add(label, "gapbottom para");
//// Motor selection
//// <html><b>Motor configurations:</b>
label = new JLabel(FlightConfigurationDialog.trans.get("edtmotorconfdlg.lbl.Motorconfig"));
this.add(label, "gapbottom para, wrap");
//// <html>Select which components function as motor mounts:
label = new JLabel(FlightConfigurationDialog.trans.get("edtmotorconfdlg.selectcomp"));
this.add(label, "ay 100%, w 1px, growx, wrap");
//// Motor Mount selection
JTable table = new JTable(new MotorMountTableModel(this));
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");
//// Motor selection table.
configurationTableModel = new MotorConfigurationTableModel(this, true);
final JTable configurationTable = new JTable(configurationTableModel);
configurationTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
configurationTable.setCellSelectionEnabled(true);
configurationTable.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int row = configurationTable.getSelectedRow();
int column = configurationTable.getSelectedColumn();
if ( row >= 0 & column == 1) {
currentMount = findMount(row);
} else {
currentMount = null;
}
if (e.getClickCount() == 1) {
// Single click updates selection
updateButtonState();
} else if (e.getClickCount() == 2) {
// Double-click edits motor
selectMotor();
}
}
});
scroll = new JScrollPane(configurationTable);
this.add(scroll, "w 500lp, h 150lp, grow, wrap");
//// Select motor
selectMotorButton = new JButton(FlightConfigurationDialog.trans.get("edtmotorconfdlg.but.Selectmotor"));
selectMotorButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
selectMotor();
}
});
this.add(selectMotorButton, "skip, split, sizegroup button");
//// Remove motor button
removeMotorButton = new JButton(FlightConfigurationDialog.trans.get("edtmotorconfdlg.but.removemotor"));
removeMotorButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
removeMotor();
}
});
this.add(removeMotorButton,"sizegroup button, wrap");
}
public void fireTableDataChanged() {
configurationTableModel.fireTableDataChanged();
}
public void updateButtonState() {
String currentID = rocket.getDefaultConfiguration().getMotorConfigurationID();
selectMotorButton.setEnabled(currentMount != null && currentID != null);
removeMotorButton.setEnabled(currentMount != null && currentID != null);
}
private void selectMotor() {
String currentID = rocket.getDefaultConfiguration().getMotorConfigurationID();
if (currentID == null || currentMount == null)
return;
MotorChooserDialog dialog = new MotorChooserDialog(
currentMount.getMotor(currentID),
currentMount.getMotorDelay(currentID),
currentMount.getMotorMountDiameter(),
flightConfigurationDialog);
dialog.setVisible(true);
Motor m = dialog.getSelectedMotor();
double d = dialog.getSelectedDelay();
if (m != null) {
currentMount.setMotor(currentID, m);
currentMount.setMotorDelay(currentID, d);
}
flightConfigurationDialog.fireContentsUpdated();
configurationTableModel.fireTableDataChanged();
updateButtonState();
}
private void removeMotor() {
String currentID = rocket.getDefaultConfiguration().getMotorConfigurationID();
if (currentID == null || currentMount == null)
return;
currentMount.setMotor(currentID, null);
flightConfigurationDialog.fireContentsUpdated();
configurationTableModel.fireTableDataChanged();
updateButtonState();
}
public void makeMotorMount( MotorMount mount, boolean isMotorMount ) {
mount.setMotorMount( isMotorMount );
configurationTableModel.fireTableStructureChanged();
updateButtonState();
}
private MotorMount[] getPotentialMotorMounts() {
List<MotorMount> list = new ArrayList<MotorMount>();
for (RocketComponent c : rocket) {
if (c instanceof MotorMount) {
list.add((MotorMount) c);
}
}
return list.toArray(new MotorMount[0]);
}
public MotorMount findMount(int column) {
MotorMount mount = null;
int count = column;
for (MotorMount m : mounts) {
if (m.isMotorMount())
count--;
if (count < 0) {
mount = m;
break;
}
}
if (mount == null) {
throw new IndexOutOfBoundsException("motor mount not found, column=" + column);
}
return mount;
}
public String findMotorForDisplay( int column ) {
String currentID = rocket.getDefaultConfiguration().getMotorConfigurationID();
MotorMount mount = findMount(column);
Motor motor = mount.getMotor(currentID);
if (motor == null)
return null;
String str = motor.getDesignation(mount.getMotorDelay(currentID));
int count = mount.getMotorCount();
if (count > 1) {
str = "" + count + Chars.TIMES + " " + str;
}
return str;
}
public String findIgnitionForDisplay( int column ) {
String currentID = rocket.getDefaultConfiguration().getMotorConfigurationID();
MotorMount mount = findMount(column);
Motor motor = mount.getMotor(currentID);
if (motor == null)
//// None
return null;
IgnitionEvent ignition = mount.getIgnitionEvent();
return ignition.toString();
}
}

View File

@ -18,15 +18,15 @@ class MotorConfigurationTableModel extends AbstractTableModel {
/**
*
*/
private final FlightConfigurationDialog flightConfigurationDialog;
private final MotorConfigurationPanel motorConfigurationPanel;
private final boolean advanced;
/**
* @param flightConfigurationDialog
* @param motorConfigurationPanel
*/
MotorConfigurationTableModel( FlightConfigurationDialog flightConfigurationDialog, boolean advanced) {
this.flightConfigurationDialog = flightConfigurationDialog;
MotorConfigurationTableModel( MotorConfigurationPanel motorConfigurationPanel, boolean advanced) {
this.motorConfigurationPanel = motorConfigurationPanel;
this.advanced = advanced;
}
@ -39,7 +39,7 @@ class MotorConfigurationTableModel extends AbstractTableModel {
@Override
public int getRowCount() {
int count = 0;
for (MotorMount m : this.flightConfigurationDialog.mounts) {
for (MotorMount m : this.motorConfigurationPanel.mounts) {
if (m.isMotorMount())
count++;
}
@ -52,7 +52,7 @@ class MotorConfigurationTableModel extends AbstractTableModel {
switch( column ) {
case 0:
{
MotorMount mount = this.flightConfigurationDialog.findMount(row);
MotorMount mount = this.motorConfigurationPanel.findMount(row);
String name = mount.toString();
int count = mount.getMotorCount();
if (count > 1) {
@ -62,7 +62,7 @@ class MotorConfigurationTableModel extends AbstractTableModel {
}
case 1:
{
String str = this.flightConfigurationDialog.findMotorForDisplay(row);
String str = this.motorConfigurationPanel.findMotorForDisplay(row);
if (str == null)
//// None
return NONE;
@ -71,7 +71,7 @@ class MotorConfigurationTableModel extends AbstractTableModel {
}
case 2:
{
String str = this.flightConfigurationDialog.findIgnitionForDisplay(row);
String str = this.motorConfigurationPanel.findIgnitionForDisplay(row);
if (str == null)
//// None
return NONE;

View File

@ -10,13 +10,13 @@ class MotorMountTableModel extends AbstractTableModel {
/**
*
*/
private final FlightConfigurationDialog flightConfigurationDialog;
private final MotorConfigurationPanel motorConfigurationPanel;
/**
* @param flightConfigurationDialog
* @param motorConfigurationPanel
*/
MotorMountTableModel(FlightConfigurationDialog flightConfigurationDialog) {
this.flightConfigurationDialog = flightConfigurationDialog;
MotorMountTableModel(MotorConfigurationPanel motorConfigurationPanel) {
this.motorConfigurationPanel = motorConfigurationPanel;
}
@Override
@ -26,7 +26,7 @@ class MotorMountTableModel extends AbstractTableModel {
@Override
public int getRowCount() {
return this.flightConfigurationDialog.mounts.length;
return this.motorConfigurationPanel.mounts.length;
}
@Override
@ -47,10 +47,10 @@ class MotorMountTableModel extends AbstractTableModel {
public Object getValueAt(int row, int column) {
switch (column) {
case 0:
return new Boolean(this.flightConfigurationDialog.mounts[row].isMotorMount());
return new Boolean(this.motorConfigurationPanel.mounts[row].isMotorMount());
case 1:
return this.flightConfigurationDialog.mounts[row].toString();
return this.motorConfigurationPanel.mounts[row].toString();
default:
throw new IndexOutOfBoundsException("column=" + column);
@ -68,6 +68,6 @@ class MotorMountTableModel extends AbstractTableModel {
throw new IllegalArgumentException("column=" + column + ", value=" + value);
}
this.flightConfigurationDialog.makeMotorMount( this.flightConfigurationDialog.mounts[row], (Boolean) value);
this.motorConfigurationPanel.makeMotorMount( this.motorConfigurationPanel.mounts[row], (Boolean) value);
}
}