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