]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/PasteCoordinates.java
c9b2ad1c7ff3f74da6e8162fbb84d8fa886c1e42
[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                         // Splitting with commas didn't work, so try spaces
188                         items = _coordField.getText().split(" ");
189                         if (items.length == 2) {
190                                 point = parseValues(items[0], items[1], null);
191                         }
192                         else if (items.length == 3 && items[1].length() == 1) {
193                                 point = parseValues(items[0], items[2], null);
194                         }
195                         else if (items.length == 4) {
196                                 point = parseValues(items[0] + " " + items[1],
197                                         items[2] + " " + items[3], null);
198                         }
199                         else if (items.length == 6) {
200                                 point = parseValues(items[0] + " " + items[1] + " " + items[2],
201                                         items[3] + " " + items[4] + " " + items[5], null);
202                         }
203                         else if (items.length == 8) {
204                                 point = parseValues(items[0] + " " + items[1] + " " + items[2] + " " + items[3],
205                                         items[4] + " " + items[5] + " " + items[6] + " " + items[7], null);
206                         }
207                 }
208
209                 if (point == null) {
210                         JOptionPane.showMessageDialog(_parentFrame,
211                                 I18nManager.getText("dialog.pastecoordinates.nothingfound"),
212                                 I18nManager.getText(getNameKey()), JOptionPane.ERROR_MESSAGE);
213                 }
214                 else {
215                         // See if name was entered
216                         String name = _nameField.getText();
217                         if (name != null && name.length() > 0) {
218                                 point.setFieldValue(Field.WAYPT_NAME, name, false);
219                         }
220                         // Pass information back to App to complete function
221                         _app.createPoint(point);
222                         _dialog.dispose();
223                 }
224         }
225
226
227         /**
228          * Try to parse the three given Strings into lat, lon and alt
229          * @param inValue1 first value (either lat/lon)
230          * @param inValue2 second value (either lon/lat)
231          * @param inValue3 altitude value or null if absent
232          * @return DataPoint object or null if failed
233          */
234         private DataPoint parseValues(String inValue1, String inValue2, String inValue3)
235         {
236                 // Check for parseable altitude
237                 Altitude alt = null;
238                 if (inValue3 != null)
239                 {
240                         // Look at altitude units dropdown
241                         final Unit altUnit = (_altUnitsDropDown.getSelectedIndex()==0?
242                                 UnitSetLibrary.UNITS_METRES : UnitSetLibrary.UNITS_FEET);
243                         alt = new Altitude(inValue3, altUnit);
244                         if (!alt.isValid()) {alt = null;}
245                 }
246                 // See if value1 can be lat and value2 lon:
247                 Latitude coord1 = new Latitude(inValue1);
248                 Longitude coord2 = new Longitude(inValue2);
249                 if (coord1.isValid() && !coord1.getCardinalGuessed()
250                         && coord2.isValid() && !coord2.getCardinalGuessed())
251                 {
252                         return new DataPoint(coord1, coord2, alt);
253                 }
254                 // Now see if lat/lon are reversed
255                 Longitude coord3 = new Longitude(inValue1);
256                 Latitude coord4 = new Latitude(inValue2);
257                 if (coord3.isValid() && !coord3.getCardinalGuessed()
258                         && coord4.isValid() && !coord4.getCardinalGuessed())
259                 {
260                         // reversed order
261                         return new DataPoint(coord4, coord3, alt);
262                 }
263                 // Didn't work without guessing cardinals, so accept latitude, longitude order (if valid)
264                 if (coord1.isValid() && coord2.isValid()) {
265                         return new DataPoint(coord1, coord2, alt);
266                 }
267                 // Or accept other order (if valid)
268                 if (coord3.isValid() && coord4.isValid()) {
269                         // reversed order
270                         return new DataPoint(coord4, coord3, alt);
271                 }
272                 // Couldn't be parsed either way
273                 return null;
274         }
275 }