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