1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.gui.download;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Color;
|
---|
7 | import java.awt.datatransfer.DataFlavor;
|
---|
8 | import java.awt.datatransfer.Transferable;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.awt.event.InputEvent;
|
---|
11 | import java.awt.event.KeyEvent;
|
---|
12 | import java.awt.Font;
|
---|
13 | import java.awt.GridBagLayout;
|
---|
14 | import java.awt.Toolkit;
|
---|
15 | import java.util.ArrayList;
|
---|
16 | import java.util.List;
|
---|
17 |
|
---|
18 | import javax.swing.AbstractAction;
|
---|
19 | import javax.swing.JCheckBox;
|
---|
20 | import javax.swing.JLabel;
|
---|
21 | import javax.swing.JOptionPane;
|
---|
22 | import javax.swing.JPanel;
|
---|
23 | import javax.swing.JTabbedPane;
|
---|
24 | import javax.swing.KeyStroke;
|
---|
25 |
|
---|
26 | import org.openstreetmap.josm.Main;
|
---|
27 | import org.openstreetmap.josm.actions.DownloadAction;
|
---|
28 | import org.openstreetmap.josm.actions.downloadtasks.DownloadGpsTask;
|
---|
29 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
|
---|
30 | import org.openstreetmap.josm.data.Bounds;
|
---|
31 | import org.openstreetmap.josm.gui.MapView;
|
---|
32 | import org.openstreetmap.josm.plugins.PluginHandler;
|
---|
33 | import org.openstreetmap.josm.tools.GBC;
|
---|
34 | import 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 | */
|
---|
46 | public 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.
|
---|
54 | */
|
---|
55 | void download(DownloadAction action, double minlat, double minlon, double maxlat, double maxlon);
|
---|
56 | void loadUrl(boolean newLayer, String url);
|
---|
57 | /**
|
---|
58 | * @return The checkbox presented to the user
|
---|
59 | */
|
---|
60 | JCheckBox getCheckBox();
|
---|
61 | /**
|
---|
62 | * @return The name of the preferences suffix to use for storing the
|
---|
63 | * selection state.
|
---|
64 | */
|
---|
65 | String getPreferencesSuffix();
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * The list of download tasks. First entry should be the osm data entry
|
---|
70 | * and the second the gps entry. After that, plugins can register additional
|
---|
71 | * download possibilities.
|
---|
72 | */
|
---|
73 | public final List<DownloadTask> downloadTasks = new ArrayList<DownloadTask>(5);
|
---|
74 |
|
---|
75 | public final List<DownloadSelection> downloadSelections = new ArrayList<DownloadSelection>();
|
---|
76 | public final JTabbedPane tabpane = new JTabbedPane();
|
---|
77 | public final JCheckBox newLayer;
|
---|
78 | public final JLabel sizeCheck = new JLabel();
|
---|
79 |
|
---|
80 | public double minlon;
|
---|
81 | public double minlat;
|
---|
82 | public double maxlon;
|
---|
83 | public double maxlat;
|
---|
84 |
|
---|
85 |
|
---|
86 | public DownloadDialog() {
|
---|
87 | setLayout(new GridBagLayout());
|
---|
88 |
|
---|
89 | downloadTasks.add(new DownloadOsmTask());
|
---|
90 | downloadTasks.add(new DownloadGpsTask());
|
---|
91 |
|
---|
92 | // adding the download tasks
|
---|
93 | add(new JLabel(tr("Data Sources and Types")), GBC.eol().insets(0,5,0,0));
|
---|
94 | for (DownloadTask task : downloadTasks) {
|
---|
95 | add(task.getCheckBox(), GBC.eol().insets(20,0,0,0));
|
---|
96 | // don't override defaults, if we (initially) don't have any preferences
|
---|
97 | if(Main.pref.hasKey("download."+task.getPreferencesSuffix())) {
|
---|
98 | task.getCheckBox().setSelected(Main.pref.getBoolean("download."+task.getPreferencesSuffix()));
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | // predefined download selections
|
---|
103 | downloadSelections.add(new SlippyMapChooser());
|
---|
104 | downloadSelections.add(new BookmarkSelection());
|
---|
105 | downloadSelections.add(new BoundingBoxSelection());
|
---|
106 | downloadSelections.add(new PlaceSelection());
|
---|
107 | downloadSelections.add(new TileSelection());
|
---|
108 | downloadSelections.add(new WorldChooser());
|
---|
109 |
|
---|
110 | // add selections from plugins
|
---|
111 | PluginHandler.addDownloadSelection(downloadSelections);
|
---|
112 |
|
---|
113 | // now everybody may add their tab to the tabbed pane
|
---|
114 | // (not done right away to allow plugins to remove one of
|
---|
115 | // the default selectors!)
|
---|
116 | for (DownloadSelection s : downloadSelections) {
|
---|
117 | s.addGui(this);
|
---|
118 | }
|
---|
119 |
|
---|
120 | if (Main.map != null) {
|
---|
121 | MapView mv = Main.map.mapView;
|
---|
122 | minlon = mv.getLatLon(0, mv.getHeight()).lon();
|
---|
123 | minlat = mv.getLatLon(0, mv.getHeight()).lat();
|
---|
124 | maxlon = mv.getLatLon(mv.getWidth(), 0).lon();
|
---|
125 | maxlat = mv.getLatLon(mv.getWidth(), 0).lat();
|
---|
126 | boundingBoxChanged(null);
|
---|
127 | }
|
---|
128 | else if (Main.pref.hasKey("osm-download.bounds")) {
|
---|
129 | // read the bounding box from the preferences
|
---|
130 | try {
|
---|
131 | String bounds[] = Main.pref.get("osm-download.bounds").split(";");
|
---|
132 | minlat = Double.parseDouble(bounds[0]);
|
---|
133 | minlon = Double.parseDouble(bounds[1]);
|
---|
134 | maxlat = Double.parseDouble(bounds[2]);
|
---|
135 | maxlon = Double.parseDouble(bounds[3]);
|
---|
136 | boundingBoxChanged(null);
|
---|
137 | }
|
---|
138 | catch (Exception e) {
|
---|
139 | e.printStackTrace();
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | newLayer = new JCheckBox(tr("Download as new layer"), Main.pref.getBoolean("download.newlayer", false));
|
---|
144 | add(newLayer, GBC.eol().insets(0,5,0,0));
|
---|
145 |
|
---|
146 | add(new JLabel(tr("Download Area")), GBC.eol().insets(0,5,0,0));
|
---|
147 | add(tabpane, GBC.eol().fill());
|
---|
148 |
|
---|
149 | try {
|
---|
150 | tabpane.setSelectedIndex(Main.pref.getInteger("download.tab", 0));
|
---|
151 | } catch (Exception ex) {
|
---|
152 | Main.pref.putInteger("download.tab", 0);
|
---|
153 | }
|
---|
154 |
|
---|
155 | Font labelFont = sizeCheck.getFont();
|
---|
156 | sizeCheck.setFont(labelFont.deriveFont(Font.PLAIN, labelFont.getSize()));
|
---|
157 | add(sizeCheck, GBC.eop().insets(0,5,5,10));
|
---|
158 |
|
---|
159 | getInputMap(WHEN_IN_FOCUSED_WINDOW).put(
|
---|
160 | KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK), "checkClipboardContents");
|
---|
161 |
|
---|
162 | getActionMap().put("checkClipboardContents", new AbstractAction() {
|
---|
163 | public void actionPerformed(ActionEvent e) {
|
---|
164 | checkClipboardContents();
|
---|
165 | }
|
---|
166 | });
|
---|
167 | }
|
---|
168 |
|
---|
169 | private void checkClipboardContents() {
|
---|
170 | String result = "";
|
---|
171 | Transferable contents = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
|
---|
172 |
|
---|
173 | if(contents == null || !contents.isDataFlavorSupported(DataFlavor.stringFlavor))
|
---|
174 | return;
|
---|
175 |
|
---|
176 | try {
|
---|
177 | result = (String)contents.getTransferData(DataFlavor.stringFlavor);
|
---|
178 | }
|
---|
179 | catch(Exception ex) {
|
---|
180 | return;
|
---|
181 | }
|
---|
182 |
|
---|
183 | Bounds b = OsmUrlToBounds.parse(result);
|
---|
184 | if (b != null) {
|
---|
185 | minlon = b.min.lon();
|
---|
186 | minlat = b.min.lat();
|
---|
187 | maxlon = b.max.lon();
|
---|
188 | maxlat = b.max.lat();
|
---|
189 | boundingBoxChanged(null);
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | private void updateSizeCheck() {
|
---|
194 | if ((maxlon-minlon)*(maxlat-minlat) > Main.pref.getDouble("osm-server.max-request-area", 0.25)) {
|
---|
195 | sizeCheck.setText(tr("Download area too large; will probably be rejected by server"));
|
---|
196 | sizeCheck.setForeground(Color.red);
|
---|
197 | } else {
|
---|
198 | sizeCheck.setText(tr("Download area ok, size probably acceptable to server"));
|
---|
199 | sizeCheck.setForeground(Color.darkGray);
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | /**
|
---|
204 | * Distributes a "bounding box changed" from one DownloadSelection
|
---|
205 | * object to the others, so they may update or clear their input
|
---|
206 | * fields.
|
---|
207 | *
|
---|
208 | * @param eventSource - the DownloadSelection object that fired this notification.
|
---|
209 | */
|
---|
210 | public void boundingBoxChanged(DownloadSelection eventSource) {
|
---|
211 | for (DownloadSelection s : downloadSelections) {
|
---|
212 | if (s != eventSource) s.boundingBoxChanged(this);
|
---|
213 | }
|
---|
214 | updateSizeCheck();
|
---|
215 | }
|
---|
216 |
|
---|
217 | /*
|
---|
218 | * Returns currently selected tab.
|
---|
219 | */
|
---|
220 | public int getSelectedTab() {
|
---|
221 | return tabpane.getSelectedIndex();
|
---|
222 | }
|
---|
223 |
|
---|
224 | /**
|
---|
225 | * Closes the download dialog. This is intended to be called by one of
|
---|
226 | * the various download area selection "plugins".
|
---|
227 | *
|
---|
228 | * @param download true to download selected data, false to cancel download
|
---|
229 | */
|
---|
230 | public void closeDownloadDialog(boolean download) {
|
---|
231 | optionPane.setValue(download ? JOptionPane.OK_OPTION : JOptionPane.CANCEL_OPTION);
|
---|
232 | }
|
---|
233 |
|
---|
234 | /**
|
---|
235 | * Has to be called after this dialog has been added to a JOptionPane.
|
---|
236 | * @param optionPane
|
---|
237 | */
|
---|
238 | public void setOptionPane(JOptionPane optionPane) {
|
---|
239 | this.optionPane = optionPane;
|
---|
240 | }
|
---|
241 | }
|
---|