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;
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;
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;
34 * Class to provide the function to paste coordinates
35 * - see wikipedia, opencaching.de, waymarking.com etc
37 public class PasteCoordinates extends GenericFunction
39 private JDialog _dialog = null;
40 private JTextField _nameField = null;
41 private JTextField _coordField = null;
42 private JButton _okButton = null;
43 private JComboBox _altUnitsDropDown;
48 * @param inApp application object for callback
50 public PasteCoordinates(App inApp)
55 /** Get the name key */
56 public String getNameKey() {
57 return "function.pastecoordinates";
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());
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);
80 _dialog.setVisible(true);
85 * Create dialog components
86 * @return Panel containing all gui elements in dialog
88 private Component makeDialogComponents()
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() {
99 public void keyReleased(KeyEvent arg0) {
103 MouseAdapter mouseListener = new MouseAdapter() {
104 public void mouseReleased(java.awt.event.MouseEvent arg0) {
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);
122 JLabel nameLabel = new JLabel(I18nManager.getText("dialog.pointnameedit.name"));
123 nameLabel.setHorizontalAlignment(SwingConstants.RIGHT);
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)
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)
150 buttonPanel.add(cancelButton);
151 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
152 dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
157 * Enable or disable the OK button based on the contents of the text field
159 private void enableOK()
161 String text = _coordField.getText();
162 _okButton.setEnabled(text != null && text.length() > 10
163 && (text.indexOf(' ') >= 0 || text.indexOf(',') >= 0));
167 * Finish the dialog when OK pressed
169 private void finish()
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);
177 else if (items.length == 3) {
178 point = parseValues(items[0].trim(), items[1].trim(), items[2].trim());
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);
186 else if (items.length == 3 && items[1].length() == 1) {
187 point = parseValues(items[0], items[2], null);
189 else if (items.length == 4) {
190 point = parseValues(items[0] + " " + items[1],
191 items[2] + " " + items[3], null);
193 else if (items.length == 6) {
194 point = parseValues(items[0] + " " + items[1] + " " + items[2],
195 items[3] + " " + items[4] + " " + items[5], null);
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);
204 JOptionPane.showMessageDialog(_parentFrame,
205 I18nManager.getText("dialog.pastecoordinates.nothingfound"),
206 I18nManager.getText(getNameKey()), JOptionPane.ERROR_MESSAGE);
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);
214 // Pass information back to App to complete function
215 _app.createPoint(point);
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
228 private DataPoint parseValues(String inValue1, String inValue2, String inValue3)
230 // Check for parseable altitude
232 if (inValue3 != null)
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;}
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())
246 return new DataPoint(coord1, coord2, alt);
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())
255 return new DataPoint(coord4, coord3, alt);
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);
261 // Or accept other order (if valid)
262 if (coord3.isValid() && coord4.isValid()) {
264 return new DataPoint(coord4, coord3, alt);
266 // Couldn't be parsed either way