source: josm/trunk/src/org/openstreetmap/josm/gui/download/BoundingBoxSelection.java@ 2331

Last change on this file since 2331 was 2327, checked in by Gubaer, 15 years ago

Cleanup in download logic (less global, more encapsulation)

  • Property svn:eol-style set to native
File size: 6.0 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.gui.download;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Dimension;
7import java.awt.GridBagLayout;
8import java.awt.event.FocusAdapter;
9import java.awt.event.FocusEvent;
10import java.awt.event.FocusListener;
11
12import javax.swing.JLabel;
13import javax.swing.JPanel;
14import javax.swing.JTextArea;
15import javax.swing.JTextField;
16import javax.swing.SwingUtilities;
17import javax.swing.event.DocumentEvent;
18import javax.swing.event.DocumentListener;
19
20import org.openstreetmap.josm.data.Bounds;
21import org.openstreetmap.josm.data.coor.LatLon;
22import org.openstreetmap.josm.tools.GBC;
23import org.openstreetmap.josm.tools.OsmUrlToBounds;
24
25/**
26 * Bounding box selector.
27 *
28 * Provides max/min lat/lon input fields as well as the "URL from www.openstreetmap.org" text field.
29 *
30 * @author Frederik Ramm <frederik@remote.org>
31 *
32 */
33public class BoundingBoxSelection implements DownloadSelection {
34
35 private JTextField[] latlon = new JTextField[] {
36 new JTextField(11),
37 new JTextField(11),
38 new JTextField(11),
39 new JTextField(11) };
40 final JTextArea osmUrl = new JTextArea();
41 final JTextArea showUrl = new JTextArea();
42
43 public void addGui(final DownloadDialog gui) {
44
45 JPanel dlg = new JPanel(new GridBagLayout());
46
47 final FocusListener dialogUpdater = new FocusAdapter() {
48 @Override public void focusLost(FocusEvent e) {
49 SwingUtilities.invokeLater(new Runnable() {
50 public void run() {
51 try {
52 double minlat = Double.parseDouble(latlon[0].getText());
53 double minlon = Double.parseDouble(latlon[1].getText());
54 double maxlat = Double.parseDouble(latlon[2].getText());
55 double maxlon = Double.parseDouble(latlon[3].getText());
56 Bounds b = new Bounds(minlat,minlon, maxlat,maxlon);
57 if (gui.getSelectedDownloadArea() == null) return;
58 if (gui.getSelectedDownloadArea() == null || !gui.getSelectedDownloadArea().equals(new Bounds(minlat,minlon, maxlat,maxlon))) {
59 gui.boundingBoxChanged(b, BoundingBoxSelection.this);
60 }
61 } catch (NumberFormatException x) {
62 // ignore
63 }
64 updateUrl(gui);
65 }
66 });
67 }
68 };
69
70 for (JTextField f : latlon) {
71 f.setMinimumSize(new Dimension(100,new JTextField().getMinimumSize().height));
72 f.addFocusListener(dialogUpdater);
73 }
74
75 class osmUrlRefresher implements DocumentListener {
76 public void changedUpdate(DocumentEvent e) { parseURL(gui); }
77 public void insertUpdate(DocumentEvent e) { parseURL(gui); }
78 public void removeUpdate(DocumentEvent e) { parseURL(gui); }
79 }
80
81 osmUrl.getDocument().addDocumentListener(new osmUrlRefresher());
82
83 // select content on receiving focus. this seems to be the default in the
84 // windows look+feel but not for others. needs invokeLater to avoid strange
85 // side effects that will cancel out the newly made selection otherwise.
86 osmUrl.addFocusListener(new FocusAdapter() {
87 @Override public void focusGained(FocusEvent e) {
88 SwingUtilities.invokeLater(new Runnable() {
89 public void run() {
90 osmUrl.selectAll();
91 }
92 });
93 }
94 });
95 osmUrl.setLineWrap(true);
96 osmUrl.setBorder(latlon[0].getBorder());
97
98 dlg.add(new JLabel(tr("min lat")), GBC.std().insets(10,20,5,0));
99 dlg.add(latlon[0], GBC.std().insets(0,20,0,0));
100 dlg.add(new JLabel(tr("min lon")), GBC.std().insets(10,20,5,0));
101 dlg.add(latlon[1], GBC.eol().insets(0,20,0,0));
102 dlg.add(new JLabel(tr("max lat")), GBC.std().insets(10,0,5,0));
103 dlg.add(latlon[2], GBC.std());
104 dlg.add(new JLabel(tr("max lon")), GBC.std().insets(10,0,5,0));
105 dlg.add(latlon[3], GBC.eol());
106
107 dlg.add(new JLabel(tr("URL from www.openstreetmap.org (you can paste an URL here to download the area)")), GBC.eol().insets(10,20,5,0));
108 dlg.add(osmUrl, GBC.eop().insets(10,0,5,0).fill());
109 dlg.add(showUrl, GBC.eop().insets(10,0,5,5));
110 showUrl.setEditable(false);
111 showUrl.setBackground(dlg.getBackground());
112 showUrl.addFocusListener(new FocusAdapter(){
113 @Override
114 public void focusGained(FocusEvent e) {
115 showUrl.selectAll();
116 }
117 });
118
119 gui.addDownloadAreaSelector(dlg, tr("Bounding Box"));
120 }
121
122 /**
123 * Called when bounding box is changed by one of the other download dialog tabs.
124 */
125 public void boundingBoxChanged(DownloadDialog gui) {
126 updateBboxFields(gui);
127 updateUrl(gui);
128 }
129
130 private boolean parseURL(DownloadDialog gui) {
131 Bounds b = OsmUrlToBounds.parse(osmUrl.getText());
132 if(b == null) return false;
133 gui.boundingBoxChanged(b,BoundingBoxSelection.this);
134 updateBboxFields(gui);
135 updateUrl(gui);
136 return true;
137 }
138
139 private void updateBboxFields(DownloadDialog gui) {
140 Bounds b = gui.getSelectedDownloadArea();
141 if (b == null) return;
142 latlon[0].setText(Double.toString(b.getMin().lat()));
143 latlon[1].setText(Double.toString(b.getMin().lon()));
144 latlon[2].setText(Double.toString(b.getMax().lat()));
145 latlon[3].setText(Double.toString(b.getMax().lon()));
146 for (JTextField f : latlon) {
147 f.setCaretPosition(0);
148 }
149 }
150
151 private void updateUrl(DownloadDialog gui) {
152 if (gui.getSelectedDownloadArea() == null) return;
153 showUrl.setText(OsmUrlToBounds.getURL(gui.getSelectedDownloadArea()));
154 }
155}
Note: See TracBrowser for help on using the repository browser.