diff --git a/core/src/net/sf/openrocket/gui/dialogs/flightconfiguration/FlightConfigurationDialog.java b/core/src/net/sf/openrocket/gui/dialogs/flightconfiguration/FlightConfigurationDialog.java index 1192ca0f5..355705b21 100644 --- a/core/src/net/sf/openrocket/gui/dialogs/flightconfiguration/FlightConfigurationDialog.java +++ b/core/src/net/sf/openrocket/gui/dialogs/flightconfiguration/FlightConfigurationDialog.java @@ -11,17 +11,14 @@ import java.util.ArrayList; import java.util.List; import javax.swing.JButton; +import javax.swing.JComboBox; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; -import javax.swing.JTextField; import javax.swing.ListSelectionModel; -import javax.swing.event.DocumentEvent; -import javax.swing.event.DocumentListener; -import javax.swing.event.ListSelectionEvent; -import javax.swing.event.ListSelectionListener; +import javax.swing.event.ListDataEvent; import javax.swing.table.TableColumn; import javax.swing.table.TableColumnModel; @@ -45,12 +42,12 @@ public class FlightConfigurationDialog extends JDialog { private final JTable configurationTable; final MotorConfigurationTableModel configurationTableModel; + final FlightConfigurationModel flightConfigurationModel; - - private final JButton newConfButton, removeConfButton; + private final JButton renameConfButton, removeConfButton; private final JButton selectMotorButton, removeMotorButton; - private String currentID = null; + String currentID = null; private MotorMount currentMount = null; static final Translator trans = Application.getTranslator(); @@ -59,6 +56,8 @@ public class FlightConfigurationDialog extends JDialog { //// Edit motor configurations super(parent, trans.get("edtmotorconfdlg.title.Editmotorconf")); + currentID = rocket.getDefaultConfiguration().getMotorConfigurationID(); + if (parent != null) this.setModalityType(ModalityType.DOCUMENT_MODAL); else @@ -70,9 +69,47 @@ public class FlightConfigurationDialog extends JDialog { JPanel panel = new JPanel(new MigLayout("fill, wrap 5")); + JLabel label = new JLabel("Selected Configuration: "); + panel.add(label,"gapbottom para"); + + flightConfigurationModel = new FlightConfigurationModel(rocket.getDefaultConfiguration()); + JComboBox configSelector = new JComboBox(flightConfigurationModel); + + panel.add(configSelector,"gapright para"); + + JButton newConfButton = new JButton(trans.get("edtmotorconfdlg.but.Newconfiguration")); + newConfButton.addActionListener( new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + FlightConfigurationDialog.this.addConfiguration(); + } + + }); + + panel.add(newConfButton); + + renameConfButton = new JButton("Rename Configuration"); + renameConfButton.addActionListener( new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + new RenameConfigDialog( rocket, FlightConfigurationDialog.this).setVisible(true); + } + }); + panel.add(renameConfButton); + + //// Remove configuration + removeConfButton = new JButton(trans.get("edtmotorconfdlg.but.Removeconfiguration")); + removeConfButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + removeConfiguration(); + } + }); + panel.add(removeConfButton,"wrap"); + //// Motor mount selection //// Motor mounts: - JLabel label = new JLabel(trans.get("edtmotorconfdlg.lbl.Motormounts")); + label = new JLabel(trans.get("edtmotorconfdlg.lbl.Motormounts")); panel.add(label, "gapbottom para"); //// Motor selection @@ -115,7 +152,7 @@ public class FlightConfigurationDialog extends JDialog { if (e.getClickCount() == 1) { // Single click updates selection - updateEnabled(); + updateButtonState(); } else if (e.getClickCount() == 2) { @@ -130,34 +167,6 @@ public class FlightConfigurationDialog extends JDialog { scroll = new JScrollPane(configurationTable); panel.add(scroll, "span 4, w 500lp, h 150lp, grow"); - //// New configuration - newConfButton = new JButton(trans.get("edtmotorconfdlg.but.Newconfiguration")); - newConfButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - String id = rocket.newMotorConfigurationID(); - rocket.getDefaultConfiguration().setMotorConfigurationID(id); - configurationTableModel.fireTableDataChanged(); - updateEnabled(); - } - }); - panel.add(newConfButton, "skip, sizegroup button"); - - //// Remove configuration - removeConfButton = new JButton(trans.get("edtmotorconfdlg.but.Removeconfiguration")); - removeConfButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - if (currentID == null) - return; - rocket.removeMotorConfigurationID(currentID); - rocket.getDefaultConfiguration().setMotorConfigurationID(null); - configurationTableModel.fireTableDataChanged(); - updateEnabled(); - } - }); - panel.add(removeConfButton, "sizegroup button"); - //// Select motor selectMotorButton = new JButton(trans.get("edtmotorconfdlg.but.Selectmotor")); selectMotorButton.addActionListener(new ActionListener() { @@ -192,7 +201,7 @@ public class FlightConfigurationDialog extends JDialog { this.validate(); this.pack(); - updateEnabled(); + updateButtonState(); this.setLocationByPlatform(true); GUIUtil.setDisposableDialogOptions(this, close); @@ -221,43 +230,45 @@ public class FlightConfigurationDialog extends JDialog { } return list.toArray(new MotorMount[0]); } - - - void updateConfigurationName( String newName ) { - - int row = configurationTable.getSelectedRow(); - String currentID = findID( row ); - rocket.setMotorConfigurationName(currentID, newName); + void selectConfiguration( String id ) { + currentID = id; + rocket.getDefaultConfiguration().setMotorConfigurationID(currentID); + configurationTableModel.fireTableDataChanged(); + updateButtonState(); } - void updateEnabled() { - int column = configurationTable.getSelectedColumn(); - int row = configurationTable.getSelectedRow(); - - if (column < 0 || row < 0) { - currentID = null; - currentMount = null; - } else { - - currentID = findID(row); - if (column == 0) { - currentMount = null; - } else { - currentMount = findMount(column); - } - rocket.getDefaultConfiguration().setMotorConfigurationID(currentID); - - } - + public void addConfiguration() { + currentID = rocket.newMotorConfigurationID(); + rocket.getDefaultConfiguration().setMotorConfigurationID(currentID); + configurationTableModel.fireTableDataChanged(); + flightConfigurationModel.fireContentsUpdated(); + updateButtonState(); + } + + public void changeConfigurationName( String newName ) { + rocket.setMotorConfigurationName(currentID, newName); + configurationTableModel.fireTableDataChanged(); + flightConfigurationModel.fireContentsUpdated(); + } + + public void removeConfiguration() { + if (currentID == null) + return; + rocket.removeMotorConfigurationID(currentID); + rocket.getDefaultConfiguration().setMotorConfigurationID(null); + configurationTableModel.fireTableDataChanged(); + flightConfigurationModel.fireContentsUpdated(); + updateButtonState(); + } + + 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; @@ -275,7 +286,7 @@ public class FlightConfigurationDialog extends JDialog { int row = configurationTable.getSelectedRow(); configurationTableModel.fireTableRowsUpdated(row, row); - updateEnabled(); + updateButtonState(); } @@ -287,7 +298,7 @@ public class FlightConfigurationDialog extends JDialog { int row = configurationTable.getSelectedRow(); configurationTableModel.fireTableRowsUpdated(row, row); - updateEnabled(); + updateButtonState(); } @@ -303,7 +314,7 @@ public class FlightConfigurationDialog extends JDialog { for (MotorMount m : mounts) { if (m.isMotorMount()) count--; - if (count <= 0) { + if (count < 0) { mount = m; break; } diff --git a/core/src/net/sf/openrocket/gui/dialogs/flightconfiguration/FlightConfigurationModel.java b/core/src/net/sf/openrocket/gui/dialogs/flightconfiguration/FlightConfigurationModel.java new file mode 100644 index 000000000..c2a00fbb6 --- /dev/null +++ b/core/src/net/sf/openrocket/gui/dialogs/flightconfiguration/FlightConfigurationModel.java @@ -0,0 +1,95 @@ +package net.sf.openrocket.gui.dialogs.flightconfiguration; + +import java.util.HashMap; +import java.util.Map; + +import javax.swing.DefaultComboBoxModel; + +import net.sf.openrocket.rocketcomponent.Configuration; +import net.sf.openrocket.rocketcomponent.Rocket; + +public class FlightConfigurationModel extends DefaultComboBoxModel { + + private final Configuration config; + private final Rocket rocket; + + private Map map = new HashMap(); + + public FlightConfigurationModel(Configuration config) { + this.config = config; + this.rocket = config.getRocket(); + } + + void fireContentsUpdated() { + fireContentsChanged(this, 0, rocket.getMotorConfigurationIDs().length); + } + + @Override + public Object getElementAt(int index) { + String[] ids = rocket.getMotorConfigurationIDs(); + if (index < 0 || index >= ids.length) + return null; + + return get(ids[index]); + } + + @Override + public int getSize() { + return rocket.getMotorConfigurationIDs().length; + } + + @Override + public Object getSelectedItem() { + return get(config.getMotorConfigurationID()); + } + + @Override + public void setSelectedItem(Object item) { + if (item == null) { + // Clear selection - huh? + return; + } + if (!(item instanceof ID)) { + throw new IllegalArgumentException("MotorConfigurationModel item="+item); + } + + ID idObject = (ID) item; + config.setMotorConfigurationID(idObject.getID()); + } + + /* + * The ID class is an adapter, that contains the actual configuration ID, + * but gives the configuration description as its String representation. + * The get(id) method retrieves ID objects and caches them for reuse. + */ + + private ID get(String id) { + ID idObject = map.get(id); + if (idObject != null) + return idObject; + + idObject = new ID(id); + map.put(id, idObject); + return idObject; + } + + + private class ID { + private final String id; + + public ID(String id) { + this.id = id; + } + + public String getID() { + return id; + } + + @Override + public String toString() { + return rocket.getMotorConfigurationNameOrDescription(id); + } + } + +} + diff --git a/core/src/net/sf/openrocket/gui/dialogs/flightconfiguration/MotorConfigurationTableModel.java b/core/src/net/sf/openrocket/gui/dialogs/flightconfiguration/MotorConfigurationTableModel.java index bb46a7eb7..1f38d6093 100644 --- a/core/src/net/sf/openrocket/gui/dialogs/flightconfiguration/MotorConfigurationTableModel.java +++ b/core/src/net/sf/openrocket/gui/dialogs/flightconfiguration/MotorConfigurationTableModel.java @@ -24,22 +24,15 @@ class MotorConfigurationTableModel extends AbstractTableModel { this.flightConfigurationDialog = flightConfigurationDialog; } - @Override - public boolean isCellEditable(int rowIndex, int columnIndex) { - return columnIndex == 0; - } - - @Override - public void setValueAt(Object aValue, int rowIndex, int columnIndex) { - if ( columnIndex == 0 ) { - // Set description: - flightConfigurationDialog.updateConfigurationName( (String) aValue ); - } - } - @Override public int getColumnCount() { - int count = 1; + int count = 2; + return count; + } + + @Override + public int getRowCount() { + int count = 0; for (MotorMount m : this.flightConfigurationDialog.mounts) { if (m.isMotorMount()) count++; @@ -47,21 +40,21 @@ class MotorConfigurationTableModel extends AbstractTableModel { return count; } - @Override - public int getRowCount() { - return this.flightConfigurationDialog.rocket.getMotorConfigurationIDs().length - 1; - } - @Override public Object getValueAt(int row, int column) { - String id = this.flightConfigurationDialog.findID(row); + String id = this.flightConfigurationDialog.currentID; if (column == 0) { - return this.flightConfigurationDialog.rocket.getMotorConfigurationNameOrDescription(id); - } - - MotorMount mount = this.flightConfigurationDialog.findMount(column); + MotorMount mount = this.flightConfigurationDialog.findMount(row); + String name = mount.toString(); + int count = mount.getMotorCount(); + if (count > 1) { + name = name + " (" + Chars.TIMES + count + ")"; + } + return name; + } else { + MotorMount mount = this.flightConfigurationDialog.findMount(row); Motor motor = mount.getMotor(id); if (motor == null) //// None @@ -73,23 +66,18 @@ class MotorConfigurationTableModel extends AbstractTableModel { str = "" + count + Chars.TIMES + " " + str; } return str; + } } @Override public String getColumnName(int column) { if (column == 0) { - //// Configuration name - return FlightConfigurationDialog.trans.get("edtmotorconfdlg.lbl.Configname"); + return "Motor Mount"; + } else { + return "Motor"; } - MotorMount mount = this.flightConfigurationDialog.findMount(column); - String name = mount.toString(); - int count = mount.getMotorCount(); - if (count > 1) { - name = name + " (" + Chars.TIMES + count + ")"; - } - return name; } } \ No newline at end of file diff --git a/core/src/net/sf/openrocket/gui/dialogs/flightconfiguration/MotorMountTableModel.java b/core/src/net/sf/openrocket/gui/dialogs/flightconfiguration/MotorMountTableModel.java index bbc861350..b89878bd6 100644 --- a/core/src/net/sf/openrocket/gui/dialogs/flightconfiguration/MotorMountTableModel.java +++ b/core/src/net/sf/openrocket/gui/dialogs/flightconfiguration/MotorMountTableModel.java @@ -70,6 +70,6 @@ class MotorMountTableModel extends AbstractTableModel { this.flightConfigurationDialog.mounts[row].setMotorMount((Boolean) value); this.flightConfigurationDialog.configurationTableModel.fireTableStructureChanged(); - this.flightConfigurationDialog.updateEnabled(); + this.flightConfigurationDialog.updateButtonState(); } } \ No newline at end of file diff --git a/core/src/net/sf/openrocket/gui/dialogs/flightconfiguration/RenameConfigDialog.java b/core/src/net/sf/openrocket/gui/dialogs/flightconfiguration/RenameConfigDialog.java new file mode 100644 index 000000000..1d08652bf --- /dev/null +++ b/core/src/net/sf/openrocket/gui/dialogs/flightconfiguration/RenameConfigDialog.java @@ -0,0 +1,68 @@ +package net.sf.openrocket.gui.dialogs.flightconfiguration; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JButton; +import javax.swing.JDialog; +import javax.swing.JPanel; +import javax.swing.JTextArea; + +import net.miginfocom.swing.MigLayout; +import net.sf.openrocket.rocketcomponent.Configuration; +import net.sf.openrocket.rocketcomponent.Rocket; + +public class RenameConfigDialog extends JDialog { + + RenameConfigDialog( final Rocket rocket, final FlightConfigurationDialog parent ) { + super(parent); + super.setModal(true); + final Configuration config = rocket.getDefaultConfiguration(); + + JPanel panel = new JPanel(new MigLayout("fill")); + + final JTextArea textbox = new JTextArea( config.getMotorConfigurationDescription() ); + panel.add(textbox, "span, w 200lp, wrap"); + + JButton okButton = new JButton("Ok"); + okButton.addActionListener( new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + parent.changeConfigurationName(textbox.getText()); + RenameConfigDialog.this.setVisible(false); + } + + }); + + panel.add( okButton ); + + JButton defaultButton = new JButton("Reset to default"); + defaultButton.addActionListener( new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + parent.changeConfigurationName(null); + RenameConfigDialog.this.setVisible(false); + } + + }); + + panel.add( defaultButton ); + + JButton cancel = new JButton("Cancel"); + cancel.addActionListener( new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + RenameConfigDialog.this.setVisible(false); + } + + }); + + panel.add( cancel ); + + this.setContentPane(panel); + } + +}