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.gui.main.MotorChooserDialog;
31 import net.sf.openrocket.rocketcomponent.Motor;
32 import net.sf.openrocket.rocketcomponent.MotorMount;
33 import net.sf.openrocket.rocketcomponent.Rocket;
34 import net.sf.openrocket.rocketcomponent.RocketComponent;
35 import net.sf.openrocket.util.GUIUtil;
37 public class EditMotorConfigurationDialog extends JDialog {
39 private final Rocket rocket;
41 private final MotorMount[] mounts;
43 private final JTable configurationTable;
44 private final MotorConfigurationTableModel configurationTableModel;
47 private final JButton newConfButton, removeConfButton;
48 private final JButton selectMotorButton, removeMotorButton;
49 private final JTextField configurationNameField;
52 private String currentID = null;
53 private MotorMount currentMount = null;
55 // Positive when user is modifying configuration name
56 private int configurationNameModification = 0;
58 public EditMotorConfigurationDialog(final Rocket rocket, Window parent) {
59 super(parent, "Edit motor configurations");
62 this.setModalityType(ModalityType.DOCUMENT_MODAL);
64 this.setModalityType(ModalityType.APPLICATION_MODAL);
68 ArrayList<MotorMount> mountList = new ArrayList<MotorMount>();
69 Iterator<RocketComponent> iterator = rocket.deepIterator();
70 while (iterator.hasNext()) {
71 RocketComponent c = iterator.next();
72 if (c instanceof MotorMount) {
73 mountList.add((MotorMount)c);
76 mounts = mountList.toArray(new MotorMount[0]);
80 JPanel panel = new JPanel(new MigLayout("fill, flowy"));
83 //// Motor mount selection
85 JLabel label = new JLabel("<html><b>Motor mounts:</b>");
86 panel.add(label, "gapbottom para");
88 label = new JLabel("<html>Select which components function as motor mounts:");
89 panel.add(label,"ay 100%, w 1px, growx");
92 JTable table = new JTable(new MotorMountTableModel());
93 table.setTableHeader(null);
94 table.setShowVerticalLines(false);
95 table.setRowSelectionAllowed(false);
96 table.setColumnSelectionAllowed(false);
98 TableColumnModel columnModel = table.getColumnModel();
99 TableColumn col0 = columnModel.getColumn(0);
100 int w = table.getRowHeight() + 2;
102 col0.setPreferredWidth(w);
105 JScrollPane scroll = new JScrollPane(table);
106 panel.add(scroll, "w 200lp, h 150lp, grow, wrap 20lp");
114 label = new JLabel("<html><b>Motor configurations:</b>");
115 panel.add(label, "spanx, gapbottom para");
118 label = new JLabel("Configuration name:");
119 String tip = "Leave name empty for default.";
120 label.setToolTipText(tip);
121 panel.add(label, "");
123 configurationNameField = new JTextField(10);
124 configurationNameField.setToolTipText(tip);
125 configurationNameField.getDocument().addDocumentListener(new DocumentListener() {
127 public void changedUpdate(DocumentEvent e) {
131 public void insertUpdate(DocumentEvent e) {
135 public void removeUpdate(DocumentEvent e) {
138 private void update() {
139 if (configurationNameModification != 0)
142 String text = configurationNameField.getText();
143 if (currentID != null) {
144 configurationNameModification++;
145 rocket.setMotorConfigurationName(currentID, text);
146 int row = configurationTable.getSelectedRow();
147 configurationTableModel.fireTableCellUpdated(row, 0);
149 configurationNameModification--;
153 panel.add(configurationNameField, "cell 2 1, gapright para");
155 newConfButton = new JButton("New configuration");
156 newConfButton.addActionListener(new ActionListener() {
158 public void actionPerformed(ActionEvent e) {
159 String id = rocket.newMotorConfigurationID();
160 rocket.getDefaultConfiguration().setMotorConfigurationID(id);
161 configurationTableModel.fireTableDataChanged();
165 panel.add(newConfButton, "cell 3 1");
167 removeConfButton = new JButton("Remove configuration");
168 removeConfButton.addActionListener(new ActionListener() {
170 public void actionPerformed(ActionEvent e) {
171 if (currentID == null)
173 rocket.removeMotorConfigurationID(currentID);
174 rocket.getDefaultConfiguration().setMotorConfigurationID(null);
175 configurationTableModel.fireTableDataChanged();
179 panel.add(removeConfButton, "cell 4 1");
184 configurationTableModel = new MotorConfigurationTableModel();
185 configurationTable = new JTable(configurationTableModel);
186 configurationTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
187 configurationTable.setCellSelectionEnabled(true);
189 configurationTable.addMouseListener(new MouseAdapter() {
191 public void mouseClicked(MouseEvent e) {
193 if (e.getClickCount() == 1) {
195 // Single click updates selection
198 } else if (e.getClickCount() == 2) {
200 // Double-click edits motor
209 scroll = new JScrollPane(configurationTable);
210 panel.add(scroll, "cell 1 2, spanx, w 500lp, h 150lp, grow");
213 selectMotorButton = new JButton("Select motor");
214 selectMotorButton.addActionListener(new ActionListener() {
216 public void actionPerformed(ActionEvent e) {
220 panel.add(selectMotorButton, "spanx, flowx, split 2, ax 50%");
223 removeMotorButton = new JButton("Remove motor");
224 removeMotorButton.addActionListener(new ActionListener() {
226 public void actionPerformed(ActionEvent e) {
230 panel.add(removeMotorButton, "ax 50%");
236 JButton close = new JButton("Close");
237 close.addActionListener(new ActionListener() {
239 public void actionPerformed(ActionEvent e) {
240 EditMotorConfigurationDialog.this.dispose();
243 panel.add(close, "spanx, right");
251 GUIUtil.installEscapeCloseOperation(this);
252 GUIUtil.setDefaultButton(close);
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());
316 dialog.setVisible(true);
317 Motor m = dialog.getSelectedMotor();
318 double d = dialog.getSelectedDelay();
321 currentMount.setMotor(currentID, m);
322 currentMount.setMotorDelay(currentID, d);
325 int row = configurationTable.getSelectedRow();
326 configurationTableModel.fireTableRowsUpdated(row, row);
331 private void removeMotor() {
332 if (currentID == null || currentMount == null)
335 currentMount.setMotor(currentID, null);
337 int row = configurationTable.getSelectedRow();
338 configurationTableModel.fireTableRowsUpdated(row, row);
343 private String findID(int row) {
344 return rocket.getMotorConfigurationIDs()[row+1];
348 private MotorMount findMount(int column) {
349 MotorMount mount = null;
352 for (MotorMount m: mounts) {
353 if (m.isMotorMount())
362 throw new IndexOutOfBoundsException("motor mount not found, column="+column);
369 * The table model for selecting whether components are motor mounts or not.
371 private class MotorMountTableModel extends AbstractTableModel {
374 public int getColumnCount() {
379 public int getRowCount() {
380 return mounts.length;
384 public Class<?> getColumnClass(int column) {
387 return Boolean.class;
393 throw new IndexOutOfBoundsException("column="+column);
398 public Object getValueAt(int row, int column) {
401 return new Boolean(mounts[row].isMotorMount());
404 return mounts[row].toString();
407 throw new IndexOutOfBoundsException("column="+column);
412 public boolean isCellEditable(int row, int column) {
417 public void setValueAt(Object value, int row, int column) {
418 if (column != 0 || !(value instanceof Boolean)) {
419 throw new IllegalArgumentException("column="+column+", value="+value);
422 mounts[row].setMotorMount((Boolean)value);
423 configurationTableModel.fireTableStructureChanged();
431 * The table model for selecting and editing the motor configurations.
433 private class MotorConfigurationTableModel extends AbstractTableModel {
436 public int getColumnCount() {
438 for (MotorMount m: mounts) {
439 if (m.isMotorMount())
446 public int getRowCount() {
447 return rocket.getMotorConfigurationIDs().length-1;
451 public Object getValueAt(int row, int column) {
453 String id = findID(row);
456 return rocket.getMotorConfigurationNameOrDescription(id);
459 MotorMount mount = findMount(column);
460 Motor motor = mount.getMotor(id);
464 String str = motor.getDesignation(mount.getMotorDelay(id));
465 int count = mount.getMotorCount();
467 str = "" + count + "\u00d7 " + str;
474 public String getColumnName(int column) {
476 return "Configuration name";
479 MotorMount mount = findMount(column);
480 String name = mount.toString();
481 int count = mount.getMotorCount();
483 name = name + " (\u00d7" + count + ")";