]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/PasteCoordinates.java
23fe8d285afd8af965a47e62f3fcdc0827a12603
[GpsPrune.git] / tim / prune / function / PasteCoordinates.java
1 package tim.prune.function;
2
3 import java.awt.BorderLayout;
4 import java.awt.Component;
5 import java.awt.FlowLayout;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8 import java.awt.event.KeyAdapter;
9 import java.awt.event.KeyEvent;
10 import java.awt.event.MouseAdapter;
11 import java.awt.event.MouseEvent;
12
13 import javax.swing.BorderFactory;
14 import javax.swing.JButton;
15 import javax.swing.JComboBox;
16 import javax.swing.JDialog;
17 import javax.swing.JLabel;
18 import javax.swing.JOptionPane;
19 import javax.swing.JPanel;
20 import javax.swing.JTextField;
21 import javax.swing.SwingConstants;
22
23 import tim.prune.App;
24 import tim.prune.GenericFunction;
25 import tim.prune.I18nManager;
26 import tim.prune.config.Config;
27 import tim.prune.data.Altitude;
28 import tim.prune.data.DataPoint;
29 import tim.prune.data.Field;
30 import tim.prune.data.Latitude;
31 import tim.prune.data.Longitude;
32 import tim.prune.gui.GuiGridLayout;
33
34 /**
35  * Class to provide the function to paste coordinates
36  * - see wikipedia, opencaching.de, waymarking.com etc
37  */
38 public class PasteCoordinates extends GenericFunction
39 {
40         private JDialog _dialog = null;
41         private JTextField _nameField = null;
42         private JTextField _coordField = null;
43         private JButton _okButton = null;
44         private JComboBox _altUnitsDropDown;
45
46
47         /**
48          * Constructor
49          * @param inApp application object for callback
50          */
51         public PasteCoordinates(App inApp)
52         {
53                 super(inApp);
54         }
55
56         /** Get the name key */
57         public String getNameKey() {
58                 return "function.pastecoordinates";
59         }
60
61         /**
62          * Begin the function
63          */
64         public void begin()
65         {
66                 // Make dialog window
67                 if (_dialog == null)
68                 {
69                         _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()), true);
70                         _dialog.setLocationRelativeTo(_parentFrame);
71                         _dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
72                         _dialog.getContentPane().add(makeDialogComponents());
73                         _dialog.pack();
74                 }
75                 // MAYBE: Paste clipboard into the edit field
76                 _coordField.setText("");
77                 _nameField.setText("");
78                 boolean metric = Config.getConfigBoolean(Config.KEY_METRIC_UNITS);
79                 _altUnitsDropDown.setSelectedIndex(metric?0:1);
80                 enableOK();
81                 _dialog.setVisible(true);
82         }
83
84
85         /**
86          * Create dialog components
87          * @return Panel containing all gui elements in dialog
88          */
89         private Component makeDialogComponents()
90         {
91                 JPanel dialogPanel = new JPanel();
92                 dialogPanel.setLayout(new BorderLayout(0, 10));
93                 dialogPanel.add(new JLabel(I18nManager.getText("dialog.pastecoordinates.desc")), BorderLayout.NORTH);
94                 JPanel mainPanel = new JPanel();
95                 GuiGridLayout grid = new GuiGridLayout(mainPanel);
96                 _coordField = new JTextField("", 25);
97                 // Listeners to enable/disable ok button
98                 KeyAdapter keyListener = new KeyAdapter() {
99                         /** Key released */
100                         public void keyReleased(KeyEvent inE) {
101                                 enableOK();
102                                 if (inE.getKeyCode() == KeyEvent.VK_ESCAPE) {
103                                         _dialog.dispose();
104                                 }
105                         }
106                 };
107                 MouseAdapter mouseListener = new MouseAdapter() {
108                         public void mouseReleased(MouseEvent inE) {
109                                 enableOK();
110                         };
111                 };
112                 _coordField.addKeyListener(keyListener);
113                 _coordField.addMouseListener(mouseListener);
114                 JLabel coordLabel = new JLabel(I18nManager.getText("dialog.pastecoordinates.coords"));
115                 coordLabel.setHorizontalAlignment(SwingConstants.RIGHT);
116                 grid.add(coordLabel);
117                 grid.add(_coordField);
118                 // Altitude format (if any)
119                 JLabel formatLabel = new JLabel(I18nManager.getText("dialog.openoptions.altitudeunits"));
120                 formatLabel.setHorizontalAlignment(SwingConstants.RIGHT);
121                 grid.add(formatLabel);
122                 final String[] altunits = {I18nManager.getText("units.metres"), I18nManager.getText("units.feet")};
123                 _altUnitsDropDown = new JComboBox(altunits);
124                 grid.add(_altUnitsDropDown);
125                 // Waypoint name
126                 JLabel nameLabel = new JLabel(I18nManager.getText("dialog.pointnameedit.name"));
127                 nameLabel.setHorizontalAlignment(SwingConstants.RIGHT);
128                 grid.add(nameLabel);
129                 _nameField = new JTextField("", 12);
130                 grid.add(_nameField);
131                 dialogPanel.add(mainPanel, BorderLayout.CENTER);
132                 // button panel at bottom
133                 JPanel buttonPanel = new JPanel();
134                 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
135                 _okButton = new JButton(I18nManager.getText("button.ok"));
136                 ActionListener okListener = new ActionListener() {
137                         public void actionPerformed(ActionEvent e)
138                         {
139                                 if (_okButton.isEnabled()) {finish();}
140                         }
141                 };
142                 _okButton.addActionListener(okListener);
143                 _okButton.setEnabled(false);
144                 _coordField.addActionListener(okListener);
145                 _nameField.addActionListener(okListener);
146                 buttonPanel.add(_okButton);
147                 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
148                 cancelButton.addActionListener(new ActionListener() {
149                         public void actionPerformed(ActionEvent e)
150                         {
151                                 _dialog.dispose();
152                         }
153                 });
154                 buttonPanel.add(cancelButton);
155                 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
156                 dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
157                 return dialogPanel;
158         }
159
160         /**
161          * Enable or disable the OK button based on the contents of the text field
162          */
163         private void enableOK()
164         {
165                 String text = _coordField.getText();
166                 _okButton.setEnabled(text != null && text.length() > 6
167                         && (text.indexOf(' ') >= 0 || text.indexOf(',') >= 0));
168         }
169
170         /**
171          * Finish the dialog when OK pressed
172          */
173         private void finish()
174         {
175                 DataPoint point = null;
176                 // Try to split using commas
177                 String[] items = _coordField.getText().split(",");
178                 if (items.length == 2) {
179                         point = parseValues(items[0].trim(), items[1].trim(), null);
180                 }
181                 else if (items.length == 3) {
182                         point = parseValues(items[0].trim(), items[1].trim(), items[2].trim());
183                 }
184                 else {
185                         // Splitting with commas didn't work, so try spaces
186                         items = _coordField.getText().split(" ");
187                         if (items.length == 2) {
188                                 point = parseValues(items[0], items[1], null);
189                         }
190                         else if (items.length == 3 && items[1].length() == 1) {
191                                 point = parseValues(items[0], items[2], null);
192                         }
193                         else if (items.length == 4) {
194                                 point = parseValues(items[0] + " " + items[1],
195                                         items[2] + " " + items[3], null);
196                         }
197                         else if (items.length == 6) {
198                                 point = parseValues(items[0] + " " + items[1] + " " + items[2],
199                                         items[3] + " " + items[4] + " " + items[5], null);
200                         }
201                         else if (items.length == 8) {
202                                 point = parseValues(items[0] + " " + items[1] + " " + items[2] + " " + items[3],
203                                         items[4] + " " + items[5] + " " + items[6] + " " + items[7], null);
204                         }
205                 }
206
207                 if (point == null) {
208                         JOptionPane.showMessageDialog(_parentFrame,
209                                 I18nManager.getText("dialog.pastecoordinates.nothingfound"),
210                                 I18nManager.getText(getNameKey()), JOptionPane.ERROR_MESSAGE);
211                 }
212                 else {
213                         // See if name was entered
214                         String name = _nameField.getText();
215                         if (name != null && name.length() > 0) {
216                                 point.setFieldValue(Field.WAYPT_NAME, name, false);
217                         }
218                         // Pass information back to App to complete function
219                         _app.createPoint(point);
220                         _dialog.dispose();
221                 }
222         }
223
224
225         /**
226          * Try to parse the three given Strings into lat, lon and alt
227          * @param inValue1 first value (either lat/lon)
228          * @param inValue2 second value (either lon/lat)
229          * @param inValue3 altitude value or null if absent
230          * @return DataPoint object or null if failed
231          */
232         private DataPoint parseValues(String inValue1, String inValue2, String inValue3)
233         {
234                 // Check for parseable altitude
235                 Altitude alt = null;
236                 if (inValue3 != null)
237                 {
238                         // Look at altitude units dropdown
239                         final Altitude.Format altFormat = (_altUnitsDropDown.getSelectedIndex()==0?
240                                 Altitude.Format.METRES:Altitude.Format.FEET);
241                         alt = new Altitude(inValue3, altFormat);
242                         if (!alt.isValid()) {alt = null;}
243                 }
244                 // See if value1 can be lat and value2 lon:
245                 Latitude coord1 = new Latitude(inValue1);
246                 Longitude coord2 = new Longitude(inValue2);
247                 if (coord1.isValid() && !coord1.getCardinalGuessed()
248                         && coord2.isValid() && !coord2.getCardinalGuessed())
249                 {
250                         return new DataPoint(coord1, coord2, alt);
251                 }
252                 // Now see if lat/lon are reversed
253                 Longitude coord3 = new Longitude(inValue1);
254                 Latitude coord4 = new Latitude(inValue2);
255                 if (coord3.isValid() && !coord3.getCardinalGuessed()
256                         && coord4.isValid() && !coord4.getCardinalGuessed())
257                 {
258                         // reversed order
259                         return new DataPoint(coord4, coord3, alt);
260                 }
261                 // Didn't work without guessing cardinals, so accept latitude, longitude order (if valid)
262                 if (coord1.isValid() && coord2.isValid()) {
263                         return new DataPoint(coord1, coord2, alt);
264                 }
265                 // Or accept other order (if valid)
266                 if (coord3.isValid() && coord4.isValid()) {
267                         // reversed order
268                         return new DataPoint(coord4, coord3, alt);
269                 }
270                 // Couldn't be parsed either way
271                 return null;
272         }
273 }