added svn:ignores
[debian/openrocket] / src / net / sf / openrocket / gui / dialogs / motor / MotorChooserDialog.java
1 package net.sf.openrocket.gui.dialogs.motor;
2
3
4 import java.awt.Dialog;
5 import java.awt.Window;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8
9 import javax.swing.JButton;
10 import javax.swing.JComponent;
11 import javax.swing.JDialog;
12 import javax.swing.JPanel;
13
14 import net.miginfocom.swing.MigLayout;
15 import net.sf.openrocket.gui.dialogs.MotorDatabaseLoadingDialog;
16 import net.sf.openrocket.gui.dialogs.motor.thrustcurve.ThrustCurveMotorSelectionPanel;
17 import net.sf.openrocket.motor.Motor;
18 import net.sf.openrocket.motor.ThrustCurveMotor;
19 import net.sf.openrocket.util.GUIUtil;
20
21 public class MotorChooserDialog extends JDialog implements CloseableDialog {
22         
23         private final ThrustCurveMotorSelectionPanel selectionPanel;
24         
25         private boolean okClicked = false;
26         
27         
28         public MotorChooserDialog(Motor current, double delay, double diameter, Window owner) {
29                 super(owner, "Select a rocket motor", Dialog.ModalityType.APPLICATION_MODAL);
30                 
31                 // Check that the motor database has been loaded properly
32                 MotorDatabaseLoadingDialog.check(null);
33                 
34
35                 JPanel panel = new JPanel(new MigLayout("fill"));
36                 
37                 selectionPanel = new ThrustCurveMotorSelectionPanel((ThrustCurveMotor) current, delay, diameter);
38                 
39                 panel.add(selectionPanel, "grow, wrap para");
40                 
41
42                 // OK / Cancel buttons
43                 
44                 JButton okButton = new JButton("OK");
45                 okButton.addActionListener(new ActionListener() {
46                         @Override
47                         public void actionPerformed(ActionEvent e) {
48                                 close(true);
49                         }
50                 });
51                 panel.add(okButton, "tag ok, spanx, split");
52                 
53                 JButton cancelButton = new JButton("Cancel");
54                 cancelButton.addActionListener(new ActionListener() {
55                         @Override
56                         public void actionPerformed(ActionEvent e) {
57                                 close(false);
58                         }
59                 });
60                 panel.add(cancelButton, "tag cancel");
61                 
62                 this.add(panel);
63                 
64                 this.setModal(true);
65                 this.pack();
66                 this.setLocationByPlatform(true);
67                 GUIUtil.setDisposableDialogOptions(this, okButton);
68                 
69                 JComponent focus = selectionPanel.getDefaultFocus();
70                 if (focus != null) {
71                         focus.grabFocus();
72                 }
73                 
74                 // Set the closeable dialog after all initialization
75                 selectionPanel.setCloseableDialog(this);
76         }
77         
78         
79         /**
80          * Return the motor selected by this chooser dialog, or <code>null</code> if the selection has been aborted.
81          * 
82          * @return      the selected motor, or <code>null</code> if no motor has been selected or the selection was canceled.
83          */
84         public Motor getSelectedMotor() {
85                 if (!okClicked)
86                         return null;
87                 return selectionPanel.getSelectedMotor();
88         }
89         
90         /**
91          * Return the selected ejection charge delay.
92          * 
93          * @return      the selected ejection charge delay.
94          */
95         public double getSelectedDelay() {
96                 return selectionPanel.getSelectedDelay();
97         }
98         
99         
100
101         @Override
102         public void close(boolean ok) {
103                 okClicked = ok;
104                 this.setVisible(false);
105                 
106                 Motor selected = getSelectedMotor();
107                 if (okClicked && selected != null) {
108                         selectionPanel.selectedMotor(selected);
109                 }
110         }
111         
112 }