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

Last change on this file since 1811 was 1811, checked in by jttt, 15 years ago

PleaseWait refactoring. Progress is now reported using ProgressMonitor interface, that is available through PleaseWaitRunnable.

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