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.data.Unit;
33 import tim.prune.data.UnitSetLibrary;
34 import tim.prune.gui.GuiGridLayout;
37 * Class to provide the function to paste coordinates
38 * - see wikipedia, opencaching.de, waymarking.com etc
40 public class PasteCoordinates extends GenericFunction
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;
51 * @param inApp application object for callback
53 public PasteCoordinates(App inApp)
58 /** Get the name key */
59 public String getNameKey() {
60 return "function.pastecoordinates";
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());
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);
83 _dialog.setVisible(true);
88 * Create dialog components
89 * @return Panel containing all gui elements in dialog
91 private Component makeDialogComponents()
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() {
102 public void keyReleased(KeyEvent inE) {
104 if (inE.getKeyCode() == KeyEvent.VK_ESCAPE) {
109 MouseAdapter mouseListener = new MouseAdapter() {
110 public void mouseReleased(MouseEvent inE) {
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);
128 JLabel nameLabel = new JLabel(I18nManager.getText("dialog.pointnameedit.name"));
129 nameLabel.setHorizontalAlignment(SwingConstants.RIGHT);
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)
141 if (_okButton.isEnabled()) {finish();}
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)
156 buttonPanel.add(cancelButton);
157 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
158 dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
163 * Enable or disable the OK button based on the contents of the text field
165 private void enableOK()
167 String text = _coordField.getText();
168 _okButton.setEnabled(text != null && text.length() > 6
169 && (text.indexOf(' ') >= 0 || text.indexOf(',') >= 0));
173 * Finish the dialog when OK pressed
175 private void finish()
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);
183 else if (items.length == 3) {
184 point = parseValues(items[0].trim(), items[1].trim(), items[2].trim());
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);
192 else if (items.length == 3 && items[1].length() == 1) {
193 point = parseValues(items[0], items[2], null);
195 else if (items.length == 4) {
196 point = parseValues(items[0] + " " + items[1],
197 items[2] + " " + items[3], null);
199 else if (items.length == 6) {
200 point = parseValues(items[0] + " " + items[1] + " " + items[2],
201 items[3] + " " + items[4] + " " + items[5], null);
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);
210 JOptionPane.showMessageDialog(_parentFrame,
211 I18nManager.getText("dialog.pastecoordinates.nothingfound"),
212 I18nManager.getText(getNameKey()), JOptionPane.ERROR_MESSAGE);
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);
220 // Pass information back to App to complete function
221 _app.createPoint(point);
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
234 private DataPoint parseValues(String inValue1, String inValue2, String inValue3)
236 // Check for parseable altitude
238 if (inValue3 != null)
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;}
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())
252 return new DataPoint(coord1, coord2, alt);
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())
261 return new DataPoint(coord4, coord3, alt);
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);
267 // Or accept other order (if valid)
268 if (coord3.isValid() && coord4.isValid()) {
270 return new DataPoint(coord4, coord3, alt);
272 // Couldn't be parsed either way