1 package tim.prune.function;
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;
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;
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;
35 * Class to provide the function to paste coordinates
36 * - see wikipedia, opencaching.de, waymarking.com etc
38 public class PasteCoordinates extends GenericFunction
40 private JDialog _dialog = null;
41 private JTextField _nameField = null;
42 private JTextField _coordField = null;
43 private JButton _okButton = null;
44 private JComboBox _altUnitsDropDown;
49 * @param inApp application object for callback
51 public PasteCoordinates(App inApp)
56 /** Get the name key */
57 public String getNameKey() {
58 return "function.pastecoordinates";
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());
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);
81 _dialog.setVisible(true);
86 * Create dialog components
87 * @return Panel containing all gui elements in dialog
89 private Component makeDialogComponents()
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() {
100 public void keyReleased(KeyEvent inE) {
102 if (inE.getKeyCode() == KeyEvent.VK_ESCAPE) {
107 MouseAdapter mouseListener = new MouseAdapter() {
108 public void mouseReleased(MouseEvent inE) {
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);
126 JLabel nameLabel = new JLabel(I18nManager.getText("dialog.pointnameedit.name"));
127 nameLabel.setHorizontalAlignment(SwingConstants.RIGHT);
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)
139 if (_okButton.isEnabled()) {finish();}
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)
154 buttonPanel.add(cancelButton);
155 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
156 dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
161 * Enable or disable the OK button based on the contents of the text field
163 private void enableOK()
165 String text = _coordField.getText();
166 _okButton.setEnabled(text != null && text.length() > 6
167 && (text.indexOf(' ') >= 0 || text.indexOf(',') >= 0));
171 * Finish the dialog when OK pressed
173 private void finish()
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);
181 else if (items.length == 3) {
182 point = parseValues(items[0].trim(), items[1].trim(), items[2].trim());
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);
190 else if (items.length == 3 && items[1].length() == 1) {
191 point = parseValues(items[0], items[2], null);
193 else if (items.length == 4) {
194 point = parseValues(items[0] + " " + items[1],
195 items[2] + " " + items[3], null);
197 else if (items.length == 6) {
198 point = parseValues(items[0] + " " + items[1] + " " + items[2],
199 items[3] + " " + items[4] + " " + items[5], null);
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);
208 JOptionPane.showMessageDialog(_parentFrame,
209 I18nManager.getText("dialog.pastecoordinates.nothingfound"),
210 I18nManager.getText(getNameKey()), JOptionPane.ERROR_MESSAGE);
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);
218 // Pass information back to App to complete function
219 _app.createPoint(point);
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
232 private DataPoint parseValues(String inValue1, String inValue2, String inValue3)
234 // Check for parseable altitude
236 if (inValue3 != null)
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;}
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())
250 return new DataPoint(coord1, coord2, alt);
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())
259 return new DataPoint(coord4, coord3, alt);
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);
265 // Or accept other order (if valid)
266 if (coord3.isValid() && coord4.isValid()) {
268 return new DataPoint(coord4, coord3, alt);
270 // Couldn't be parsed either way