1 package net.sf.openrocket.gui.dialogs;
3 import java.awt.Window;
4 import java.awt.event.ActionEvent;
5 import java.awt.event.ActionListener;
6 import java.awt.event.MouseAdapter;
7 import java.awt.event.MouseEvent;
8 import java.awt.event.WindowAdapter;
9 import java.awt.event.WindowEvent;
10 import java.util.ArrayList;
11 import java.util.Iterator;
13 import javax.swing.JButton;
14 import javax.swing.JDialog;
15 import javax.swing.JLabel;
16 import javax.swing.JPanel;
17 import javax.swing.JScrollPane;
18 import javax.swing.JTable;
19 import javax.swing.JTextField;
20 import javax.swing.ListSelectionModel;
21 import javax.swing.event.DocumentEvent;
22 import javax.swing.event.DocumentListener;
23 import javax.swing.table.AbstractTableModel;
24 import javax.swing.table.TableColumn;
25 import javax.swing.table.TableColumnModel;
27 import net.miginfocom.swing.MigLayout;
28 import net.sf.openrocket.document.OpenRocketDocument;
29 import net.sf.openrocket.gui.main.BasicFrame;
30 import net.sf.openrocket.rocketcomponent.Motor;
31 import net.sf.openrocket.rocketcomponent.MotorMount;
32 import net.sf.openrocket.rocketcomponent.Rocket;
33 import net.sf.openrocket.rocketcomponent.RocketComponent;
34 import net.sf.openrocket.util.GUIUtil;
36 public class EditMotorConfigurationDialog extends JDialog {
38 private final Rocket rocket;
40 private final MotorMount[] mounts;
42 private final JTable configurationTable;
43 private final MotorConfigurationTableModel configurationTableModel;
46 private final JButton newConfButton, removeConfButton;
47 private final JButton selectMotorButton, removeMotorButton;
48 private final JTextField configurationNameField;
51 private String currentID = null;
52 private MotorMount currentMount = null;
54 // Positive when user is modifying configuration name
55 private int configurationNameModification = 0;
57 public EditMotorConfigurationDialog(final Rocket rocket, Window parent) {
58 super(parent, "Edit motor configurations");
61 this.setModalityType(ModalityType.DOCUMENT_MODAL);
63 this.setModalityType(ModalityType.APPLICATION_MODAL);
67 ArrayList<MotorMount> mountList = new ArrayList<MotorMount>();
68 Iterator<RocketComponent> iterator = rocket.deepIterator();
69 while (iterator.hasNext()) {
70 RocketComponent c = iterator.next();
71 if (c instanceof MotorMount) {
72 mountList.add((MotorMount)c);
75 mounts = mountList.toArray(new MotorMount[0]);
79 JPanel panel = new JPanel(new MigLayout("fill, flowy"));
82 //// Motor mount selection
84 JLabel label = new JLabel("<html><b>Motor mounts:</b>");
85 panel.add(label, "gapbottom para");
87 label = new JLabel("<html>Select which components function as motor mounts:");
88 panel.add(label,"ay 100%, w 1px, growx");
91 JTable table = new JTable(new MotorMountTableModel());
92 table.setTableHeader(null);
93 table.setShowVerticalLines(false);
94 table.setRowSelectionAllowed(false);
95 table.setColumnSelectionAllowed(false);
97 TableColumnModel columnModel = table.getColumnModel();
98 TableColumn col0 = columnModel.getColumn(0);
99 int w = table.getRowHeight() + 2;
101 col0.setPreferredWidth(w);
104 JScrollPane scroll = new JScrollPane(table);
105 panel.add(scroll, "w 200lp, h 150lp, grow, wrap 20lp");
113 label = new JLabel("<html><b>Motor configurations:</b>");
114 panel.add(label, "spanx, gapbottom para");
117 label = new JLabel("Configuration name:");
118 String tip = "Leave name empty for default.";
119 label.setToolTipText(tip);
120 panel.add(label, "");
122 configurationNameField = new JTextField(10);
123 configurationNameField.setToolTipText(tip);
124 configurationNameField.getDocument().addDocumentListener(new DocumentListener() {
126 public void changedUpdate(DocumentEvent e) {
130 public void insertUpdate(DocumentEvent e) {
134 public void removeUpdate(DocumentEvent e) {
137 private void update() {
138 if (configurationNameModification != 0)
141 String text = configurationNameField.getText();
142 if (currentID != null) {
143 configurationNameModification++;
144 rocket.setMotorConfigurationName(currentID, text);
145 int row = configurationTable.getSelectedRow();
146 configurationTableModel.fireTableCellUpdated(row, 0);
148 configurationNameModification--;
152 panel.add(configurationNameField, "cell 2 1, gapright para");
154 newConfButton = new JButton("New configuration");
155 newConfButton.addActionListener(new ActionListener() {
157 public void actionPerformed(ActionEvent e) {
158 String id = rocket.newMotorConfigurationID();
159 rocket.getDefaultConfiguration().setMotorConfigurationID(id);
160 configurationTableModel.fireTableDataChanged();
164 panel.add(newConfButton, "cell 3 1");
166 removeConfButton = new JButton("Remove configuration");
167 removeConfButton.addActionListener(new ActionListener() {
169 public void actionPerformed(ActionEvent e) {
170 if (currentID == null)
172 rocket.removeMotorConfigurationID(currentID);
173 rocket.getDefaultConfiguration().setMotorConfigurationID(null);
174 configurationTableModel.fireTableDataChanged();
178 panel.add(removeConfButton, "cell 4 1");
183 configurationTableModel = new MotorConfigurationTableModel();
184 configurationTable = new JTable(configurationTableModel);
185 configurationTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
186 configurationTable.setCellSelectionEnabled(true);
188 configurationTable.addMouseListener(new MouseAdapter() {
190 public void mouseClicked(MouseEvent e) {
192 if (e.getClickCount() == 1) {
194 // Single click updates selection
197 } else if (e.getClickCount() == 2) {
199 // Double-click edits motor
208 scroll = new JScrollPane(configurationTable);
209 panel.add(scroll, "cell 1 2, spanx, w 500lp, h 150lp, grow");
212 selectMotorButton = new JButton("Select motor");
213 selectMotorButton.addActionListener(new ActionListener() {
215 public void actionPerformed(ActionEvent e) {
219 panel.add(selectMotorButton, "spanx, flowx, split 2, ax 50%");
222 removeMotorButton = new JButton("Remove motor");
223 removeMotorButton.addActionListener(new ActionListener() {
225 public void actionPerformed(ActionEvent e) {
229 panel.add(removeMotorButton, "ax 50%");
235 JButton close = new JButton("Close");
236 close.addActionListener(new ActionListener() {
238 public void actionPerformed(ActionEvent e) {
239 EditMotorConfigurationDialog.this.dispose();
242 panel.add(close, "spanx, right");
250 GUIUtil.installEscapeCloseOperation(this);
251 GUIUtil.setDefaultButton(close);
252 this.setLocationByPlatform(true);
255 final OpenRocketDocument document = BasicFrame.findDocument(rocket);
256 if (document != null) {
257 document.startUndo("Edit motor configurations");
258 this.addWindowListener(new WindowAdapter() {
260 public void windowClosed(WindowEvent e) {
270 private void updateEnabled() {
271 int column = configurationTable.getSelectedColumn();
272 int row = configurationTable.getSelectedRow();
274 if (column < 0 || row < 0) {
279 currentID = findID(row);
283 currentMount = findMount(column);
285 rocket.getDefaultConfiguration().setMotorConfigurationID(currentID);
289 if (configurationNameModification == 0) {
290 // Don't update name field when user is modifying it
291 configurationNameModification++;
293 configurationNameField.setEnabled(currentID != null);
294 if (currentID == null) {
295 configurationNameField.setText("");
297 configurationNameField.setText(rocket.getMotorConfigurationName(currentID));
300 configurationNameModification--;
302 removeConfButton.setEnabled(currentID != null);
303 selectMotorButton.setEnabled(currentMount != null && currentID != null);
304 removeMotorButton.setEnabled(currentMount != null && currentID != null);
310 private void selectMotor() {
311 if (currentID == null || currentMount == null)
314 MotorChooserDialog dialog = new MotorChooserDialog(currentMount.getMotor(currentID),
315 currentMount.getMotorDelay(currentID), currentMount.getMotorMountDiameter(),
317 dialog.setVisible(true);
318 Motor m = dialog.getSelectedMotor();
319 double d = dialog.getSelectedDelay();
322 currentMount.setMotor(currentID, m);
323 currentMount.setMotorDelay(currentID, d);
326 int row = configurationTable.getSelectedRow();
327 configurationTableModel.fireTableRowsUpdated(row, row);
332 private void removeMotor() {
333 if (currentID == null || currentMount == null)
336 currentMount.setMotor(currentID, null);
338 int row = configurationTable.getSelectedRow();
339 configurationTableModel.fireTableRowsUpdated(row, row);
344 private String findID(int row) {
345 return rocket.getMotorConfigurationIDs()[row+1];
349 private MotorMount findMount(int column) {
350 MotorMount mount = null;
353 for (MotorMount m: mounts) {
354 if (m.isMotorMount())
363 throw new IndexOutOfBoundsException("motor mount not found, column="+column);
370 * The table model for selecting whether components are motor mounts or not.
372 private class MotorMountTableModel extends AbstractTableModel {
375 public int getColumnCount() {
380 public int getRowCount() {
381 return mounts.length;
385 public Class<?> getColumnClass(int column) {
388 return Boolean.class;
394 throw new IndexOutOfBoundsException("column="+column);
399 public Object getValueAt(int row, int column) {
402 return new Boolean(mounts[row].isMotorMount());
405 return mounts[row].toString();
408 throw new IndexOutOfBoundsException("column="+column);
413 public boolean isCellEditable(int row, int column) {
418 public void setValueAt(Object value, int row, int column) {
419 if (column != 0 || !(value instanceof Boolean)) {
420 throw new IllegalArgumentException("column="+column+", value="+value);
423 mounts[row].setMotorMount((Boolean)value);
424 configurationTableModel.fireTableStructureChanged();
432 * The table model for selecting and editing the motor configurations.
434 private class MotorConfigurationTableModel extends AbstractTableModel {
437 public int getColumnCount() {
439 for (MotorMount m: mounts) {
440 if (m.isMotorMount())
447 public int getRowCount() {
448 return rocket.getMotorConfigurationIDs().length-1;
452 public Object getValueAt(int row, int column) {
454 String id = findID(row);
457 return rocket.getMotorConfigurationNameOrDescription(id);
460 MotorMount mount = findMount(column);
461 Motor motor = mount.getMotor(id);
465 String str = motor.getDesignation(mount.getMotorDelay(id));
466 int count = mount.getMotorCount();
468 str = "" + count + "\u00d7 " + str;
475 public String getColumnName(int column) {
477 return "Configuration name";
480 MotorMount mount = findMount(column);
481 String name = mount.toString();
482 int count = mount.getMotorCount();
484 name = name + " (\u00d7" + count + ")";