]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/AddAltitudeOffset.java
Version 14, October 2012
[GpsPrune.git] / tim / prune / function / AddAltitudeOffset.java
1 package tim.prune.function;
2
3 import java.awt.BorderLayout;
4 import java.awt.FlowLayout;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
7 import java.awt.event.KeyAdapter;
8 import java.awt.event.KeyEvent;
9 import java.awt.event.MouseAdapter;
10
11 import javax.swing.BorderFactory;
12 import javax.swing.JButton;
13 import javax.swing.JDialog;
14 import javax.swing.JLabel;
15 import javax.swing.JPanel;
16 import javax.swing.JTextField;
17
18 import tim.prune.App;
19 import tim.prune.GenericFunction;
20 import tim.prune.I18nManager;
21 import tim.prune.config.Config;
22 import tim.prune.data.Altitude;
23 import tim.prune.data.Field;
24
25 /**
26  * Class to provide the function to add an altitude offset to a track range
27  */
28 public class AddAltitudeOffset extends GenericFunction
29 {
30         private JDialog _dialog = null;
31         private JLabel _descLabel = null;
32         private JTextField _editField = null;
33         private JButton _okButton = null;
34         private Altitude.Format _altFormat = Altitude.Format.NO_FORMAT;
35
36
37         /**
38          * Constructor
39          * @param inApp application object for callback
40          */
41         public AddAltitudeOffset(App inApp)
42         {
43                 super(inApp);
44         }
45
46         /** Get the name key */
47         public String getNameKey() {
48                 return "function.addaltitudeoffset";
49         }
50
51         /**
52          * Begin the function
53          */
54         public void begin()
55         {
56                 int selStart = _app.getTrackInfo().getSelection().getStart();
57                 int selEnd = _app.getTrackInfo().getSelection().getEnd();
58                 if (!_app.getTrackInfo().getTrack().hasData(Field.ALTITUDE, selStart, selEnd))
59                 {
60                         _app.showErrorMessage(getNameKey(), "dialog.addaltitude.noaltitudes");
61                         return;
62                 }
63                 // Make dialog window
64                 if (_dialog == null)
65                 {
66                         _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()), true);
67                         _dialog.setLocationRelativeTo(_parentFrame);
68                         _dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
69                         _dialog.getContentPane().add(makeDialogComponents());
70                         _dialog.pack();
71                 }
72                 // Set label according to altitude units
73                 setLabelText();
74                 // Select the contents of the edit field
75                 _editField.selectAll();
76                 _dialog.setVisible(true);
77         }
78
79
80         /**
81          * Create dialog components
82          * @return Panel containing all gui elements in dialog
83          */
84         private JPanel makeDialogComponents()
85         {
86                 JPanel dialogPanel = new JPanel();
87                 dialogPanel.setLayout(new BorderLayout());
88                 JPanel mainPanel = new JPanel();
89                 _descLabel = new JLabel(I18nManager.getText("dialog.addaltitude.desc") + " (ft)");
90                 // Note, this label will be reset at each run
91                 mainPanel.add(_descLabel);
92                 _editField = new JTextField("0", 6);
93                 // Listeners to enable/disable ok button
94                 KeyAdapter keyListener = new KeyAdapter() {
95                         /** Key released */
96                         public void keyReleased(KeyEvent arg0) {
97                                 _okButton.setEnabled(Math.abs(getOffset()) > 0.001);
98                         }
99                 };
100                 MouseAdapter mouseListener = new MouseAdapter() {
101                         public void mouseReleased(java.awt.event.MouseEvent arg0) {
102                                 _okButton.setEnabled(Math.abs(getOffset()) > 0.001);
103                         };
104                 };
105                 _editField.addKeyListener(keyListener);
106                 _editField.addMouseListener(mouseListener);
107                 mainPanel.add(_editField);
108                 dialogPanel.add(mainPanel, BorderLayout.CENTER);
109                 // button panel at bottom
110                 JPanel buttonPanel = new JPanel();
111                 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
112                 _okButton = new JButton(I18nManager.getText("button.ok"));
113                 ActionListener okListener = new ActionListener() {
114                         public void actionPerformed(ActionEvent e)
115                         {
116                                 finish();
117                         }
118                 };
119                 _okButton.addActionListener(okListener);
120                 _okButton.setEnabled(false);
121                 _editField.addActionListener(okListener);
122                 buttonPanel.add(_okButton);
123                 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
124                 cancelButton.addActionListener(new ActionListener() {
125                         public void actionPerformed(ActionEvent e)
126                         {
127                                 _dialog.dispose();
128                         }
129                 });
130                 buttonPanel.add(cancelButton);
131                 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
132                 dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
133                 return dialogPanel;
134         }
135
136         /**
137          * Get the value of the altitude to add
138          * @return entered altitude offset as numeric value
139          */
140         private double getOffset()
141         {
142                 double ans = 0.0;
143                 try {
144                         ans = Double.parseDouble(_editField.getText());
145                 }
146                 catch (Exception e) {}
147                 return ans;
148         }
149
150         /**
151          * Set the label text according to the current units
152          */
153         private void setLabelText()
154         {
155                 _altFormat = Altitude.Format.FEET;
156                 if (Config.getUnitSet().getAltitudeUnit().isStandard()) {
157                         _altFormat = Altitude.Format.METRES;
158                 }
159                 final String unitKey = (_altFormat==Altitude.Format.METRES?"units.metres.short":"units.feet.short");
160                 _descLabel.setText(I18nManager.getText("dialog.addaltitude.desc") + " (" + I18nManager.getText(unitKey) + ")");
161         }
162
163         /**
164          * Finish the dialog when OK pressed
165          */
166         private void finish()
167         {
168                 // Pass information back to App to complete function
169                 _app.finishAddAltitudeOffset(_editField.getText(), _altFormat);
170                 _dialog.dispose();
171         }
172 }