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