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