1 package tim.prune.function;
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;
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;
19 import tim.prune.GenericFunction;
20 import tim.prune.I18nManager;
21 import tim.prune.data.Altitude;
22 import tim.prune.data.Field;
25 * Class to provide the function to add an altitude offset to a track range
27 public class AddAltitudeOffset extends GenericFunction
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;
38 * @param inApp application object for callback
40 public AddAltitudeOffset(App inApp)
45 /** Get the name key */
46 public String getNameKey() {
47 return "function.addaltitudeoffset";
55 int selStart = _app.getTrackInfo().getSelection().getStart();
56 int selEnd = _app.getTrackInfo().getSelection().getEnd();
57 if (!_app.getTrackInfo().getTrack().hasData(Field.ALTITUDE, selStart, selEnd))
59 _app.showErrorMessage(getNameKey(), "dialog.addaltitude.noaltitudes");
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());
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);
80 * Create dialog components
81 * @return Panel containing all gui elements in dialog
83 private JPanel makeDialogComponents()
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() {
95 public void keyReleased(KeyEvent arg0) {
96 _okButton.setEnabled(Math.abs(getOffset()) > 0.001);
99 MouseAdapter mouseListener = new MouseAdapter() {
100 public void mouseReleased(java.awt.event.MouseEvent arg0) {
101 _okButton.setEnabled(Math.abs(getOffset()) > 0.001);
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)
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)
129 buttonPanel.add(cancelButton);
130 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
131 dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
136 * Get the value of the altitude to add
137 * @return entered altitude offset as numeric value
139 private double getOffset()
143 ans = Double.parseDouble(_editField.getText());
145 catch (Exception e) {}
150 * Set the label text according to the current units
151 * @param inStart start index of selection
152 * @param inEnd end index of selection
154 private void setLabelText(int inStart, int inEnd)
156 _altFormat = Altitude.Format.NO_FORMAT;
157 for (int i=inStart; i<=inEnd && _altFormat==Altitude.Format.NO_FORMAT; i++)
159 Altitude alt = _app.getTrackInfo().getTrack().getPoint(i).getAltitude();
161 _altFormat = alt.getFormat();
164 if (_altFormat==Altitude.Format.NO_FORMAT) {
165 _altFormat = Altitude.Format.METRES;
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) + ")");
172 * Finish the dialog when OK pressed
174 private void finish()
176 // Pass information back to App to complete function
177 _app.finishAddAltitudeOffset(_editField.getText(), _altFormat);