source: josm/trunk/src/org/openstreetmap/josm/gui/download/DownloadDialog.java@ 1680

Last change on this file since 1680 was 1680, checked in by stoecker, 15 years ago

removed old style world chooser - interactive map is much better

  • Property svn:eol-style set to native
File size: 9.4 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.Color;
7import java.awt.datatransfer.DataFlavor;
8import java.awt.datatransfer.Transferable;
9import java.awt.event.ActionEvent;
10import java.awt.event.InputEvent;
11import java.awt.event.KeyEvent;
12import java.awt.Font;
13import java.awt.GridBagLayout;
14import java.awt.Toolkit;
15import java.util.ArrayList;
16import java.util.List;
17
18import javax.swing.AbstractAction;
19import javax.swing.JCheckBox;
20import javax.swing.JLabel;
21import javax.swing.JOptionPane;
22import javax.swing.JPanel;
23import javax.swing.JTabbedPane;
24import javax.swing.KeyStroke;
25
26import org.openstreetmap.josm.Main;
27import org.openstreetmap.josm.actions.DownloadAction;
28import org.openstreetmap.josm.actions.downloadtasks.DownloadGpsTask;
29import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
30import org.openstreetmap.josm.data.Bounds;
31import org.openstreetmap.josm.gui.MapView;
32import org.openstreetmap.josm.plugins.PluginHandler;
33import org.openstreetmap.josm.tools.GBC;
34import org.openstreetmap.josm.tools.OsmUrlToBounds;
35
36/**
37 * Main download dialog.
38 *
39 * Can be extended by plugins in two ways:
40 * (1) by adding download tasks that are then called with the selected bounding box
41 * (2) by adding "DownloadSelection" objects that implement different ways of selecting a bounding box
42 *
43 * @author Frederik Ramm <frederik@remote.org>
44 *
45 */
46public class DownloadDialog extends JPanel {
47
48 // the JOptionPane that contains this dialog. required for the closeDialog() method.
49 private JOptionPane optionPane;
50
51 public interface DownloadTask {
52 /**
53 * Execute the download using the given bounding box
54 */
55 void download(DownloadAction action, double minlat, double minlon,
56 double maxlat, double maxlon);
57
58 /**
59 * Execute the download using the given bounding box. Set silent to true if no error
60 * messages should be popped up. Message can be used to display an additional text below
61 * the default description.
62 */
63 void download(DownloadAction action, double minlat, double minlon,
64 double maxlat, double maxlon, boolean silent, String message);
65
66 /**
67 * Execute the download using the given URL
68 * @param newLayer
69 * @param url
70 */
71 void loadUrl(boolean newLayer, String url);
72
73 /**
74 * @return The checkbox presented to the user
75 */
76 JCheckBox getCheckBox();
77
78 /**
79 * @return The name of the preferences suffix to use for storing the
80 * selection state.
81 */
82 String getPreferencesSuffix();
83
84 /**
85 * Gets the error message of the task once it executed. If there is no error message, an empty
86 * string is returned.
87 *
88 * WARNING: Never call this in the same thread you requested the download() or it will cause a
89 * dead lock. See actions/downloadTasks/DownloadOsmTaskList.java for a proper implementation.
90 *
91 * @return Error message or empty String
92 */
93 String getErrorMessage();
94 }
95
96 /**
97 * The list of download tasks. First entry should be the osm data entry
98 * and the second the gps entry. After that, plugins can register additional
99 * download possibilities.
100 */
101 public final List<DownloadTask> downloadTasks = new ArrayList<DownloadTask>(5);
102
103 public final List<DownloadSelection> downloadSelections = new ArrayList<DownloadSelection>();
104 public final JTabbedPane tabpane = new JTabbedPane();
105 public final JCheckBox newLayer;
106 public final JLabel sizeCheck = new JLabel();
107
108 public double minlon;
109 public double minlat;
110 public double maxlon;
111 public double maxlat;
112
113
114 public DownloadDialog() {
115 setLayout(new GridBagLayout());
116
117 downloadTasks.add(new DownloadOsmTask());
118 downloadTasks.add(new DownloadGpsTask());
119
120 // adding the download tasks
121 add(new JLabel(tr("Data Sources and Types")), GBC.eol().insets(0,5,0,0));
122 for (DownloadTask task : downloadTasks) {
123 add(task.getCheckBox(), GBC.eol().insets(20,0,0,0));
124 // don't override defaults, if we (initially) don't have any preferences
125 if(Main.pref.hasKey("download."+task.getPreferencesSuffix())) {
126 task.getCheckBox().setSelected(Main.pref.getBoolean("download."+task.getPreferencesSuffix()));
127 }
128 }
129
130 // predefined download selections
131 downloadSelections.add(new SlippyMapChooser());
132 downloadSelections.add(new BookmarkSelection());
133 downloadSelections.add(new BoundingBoxSelection());
134 downloadSelections.add(new PlaceSelection());
135 downloadSelections.add(new TileSelection());
136
137 // add selections from plugins
138 PluginHandler.addDownloadSelection(downloadSelections);
139
140 // now everybody may add their tab to the tabbed pane
141 // (not done right away to allow plugins to remove one of
142 // the default selectors!)
143 for (DownloadSelection s : downloadSelections) {
144 s.addGui(this);
145 }
146
147 if (Main.map != null) {
148 MapView mv = Main.map.mapView;
149 minlon = mv.getLatLon(0, mv.getHeight()).lon();
150 minlat = mv.getLatLon(0, mv.getHeight()).lat();
151 maxlon = mv.getLatLon(mv.getWidth(), 0).lon();
152 maxlat = mv.getLatLon(mv.getWidth(), 0).lat();
153 boundingBoxChanged(null);
154 }
155 else if (Main.pref.hasKey("osm-download.bounds")) {
156 // read the bounding box from the preferences
157 try {
158 String bounds[] = Main.pref.get("osm-download.bounds").split(";");
159 minlat = Double.parseDouble(bounds[0]);
160 minlon = Double.parseDouble(bounds[1]);
161 maxlat = Double.parseDouble(bounds[2]);
162 maxlon = Double.parseDouble(bounds[3]);
163 boundingBoxChanged(null);
164 }
165 catch (Exception e) {
166 e.printStackTrace();
167 }
168 }
169
170 newLayer = new JCheckBox(tr("Download as new layer"), Main.pref.getBoolean("download.newlayer", false));
171 add(newLayer, GBC.eol().insets(0,5,0,0));
172
173 add(new JLabel(tr("Download Area")), GBC.eol().insets(0,5,0,0));
174 add(tabpane, GBC.eol().fill());
175
176 try {
177 tabpane.setSelectedIndex(Main.pref.getInteger("download.tab", 0));
178 } catch (Exception ex) {
179 Main.pref.putInteger("download.tab", 0);
180 }
181
182 Font labelFont = sizeCheck.getFont();
183 sizeCheck.setFont(labelFont.deriveFont(Font.PLAIN, labelFont.getSize()));
184 add(sizeCheck, GBC.eop().insets(0,5,5,10));
185
186 getInputMap(WHEN_IN_FOCUSED_WINDOW).put(
187 KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK), "checkClipboardContents");
188
189 getActionMap().put("checkClipboardContents", new AbstractAction() {
190 public void actionPerformed(ActionEvent e) {
191 checkClipboardContents();
192 }
193 });
194 }
195
196 private void checkClipboardContents() {
197 String result = "";
198 Transferable contents = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
199
200 if(contents == null || !contents.isDataFlavorSupported(DataFlavor.stringFlavor))
201 return;
202
203 try {
204 result = (String)contents.getTransferData(DataFlavor.stringFlavor);
205 }
206 catch(Exception ex) {
207 return;
208 }
209
210 Bounds b = OsmUrlToBounds.parse(result);
211 if (b != null) {
212 minlon = b.min.lon();
213 minlat = b.min.lat();
214 maxlon = b.max.lon();
215 maxlat = b.max.lat();
216 boundingBoxChanged(null);
217 }
218 }
219
220 private void updateSizeCheck() {
221 if ((maxlon-minlon)*(maxlat-minlat) > Main.pref.getDouble("osm-server.max-request-area", 0.25)) {
222 sizeCheck.setText(tr("Download area too large; will probably be rejected by server"));
223 sizeCheck.setForeground(Color.red);
224 } else {
225 sizeCheck.setText(tr("Download area ok, size probably acceptable to server"));
226 sizeCheck.setForeground(Color.darkGray);
227 }
228 }
229
230 /**
231 * Distributes a "bounding box changed" from one DownloadSelection
232 * object to the others, so they may update or clear their input
233 * fields.
234 *
235 * @param eventSource - the DownloadSelection object that fired this notification.
236 */
237 public void boundingBoxChanged(DownloadSelection eventSource) {
238 for (DownloadSelection s : downloadSelections) {
239 if (s != eventSource) s.boundingBoxChanged(this);
240 }
241 updateSizeCheck();
242 }
243
244 /*
245 * Returns currently selected tab.
246 */
247 public int getSelectedTab() {
248 return tabpane.getSelectedIndex();
249 }
250
251 /**
252 * Closes the download dialog. This is intended to be called by one of
253 * the various download area selection "plugins".
254 *
255 * @param download true to download selected data, false to cancel download
256 */
257 public void closeDownloadDialog(boolean download) {
258 optionPane.setValue(download ? JOptionPane.OK_OPTION : JOptionPane.CANCEL_OPTION);
259 }
260
261 /**
262 * Has to be called after this dialog has been added to a JOptionPane.
263 * @param optionPane
264 */
265 public void setOptionPane(JOptionPane optionPane) {
266 this.optionPane = optionPane;
267 }
268}
Note: See TracBrowser for help on using the repository browser.