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.Font;
|
---|
8 | import java.awt.GridBagLayout;
|
---|
9 | import java.awt.Toolkit;
|
---|
10 | import java.awt.datatransfer.DataFlavor;
|
---|
11 | import java.awt.datatransfer.Transferable;
|
---|
12 | import java.awt.event.ActionEvent;
|
---|
13 | import java.awt.event.InputEvent;
|
---|
14 | import java.awt.event.KeyEvent;
|
---|
15 | import java.util.ArrayList;
|
---|
16 | import java.util.List;
|
---|
17 | import java.util.logging.Logger;
|
---|
18 |
|
---|
19 | import javax.swing.AbstractAction;
|
---|
20 | import javax.swing.JCheckBox;
|
---|
21 | import javax.swing.JLabel;
|
---|
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.data.Bounds;
|
---|
28 | import org.openstreetmap.josm.gui.MapView;
|
---|
29 | import org.openstreetmap.josm.plugins.PluginHandler;
|
---|
30 | import org.openstreetmap.josm.tools.GBC;
|
---|
31 | import org.openstreetmap.josm.tools.OsmUrlToBounds;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Main download dialog.
|
---|
35 | *
|
---|
36 | * Can be extended by plugins in two ways:
|
---|
37 | * (1) by adding download tasks that are then called with the selected bounding box
|
---|
38 | * (2) by adding "DownloadSelection" objects that implement different ways of selecting a bounding box
|
---|
39 | *
|
---|
40 | * @author Frederik Ramm <frederik@remote.org>
|
---|
41 | *
|
---|
42 | */
|
---|
43 | public class DownloadDialog extends JPanel {
|
---|
44 | static private final Logger logger = Logger.getLogger(DownloadDialog.class.getName());
|
---|
45 |
|
---|
46 | private final List<DownloadSelection> downloadSelections = new ArrayList<DownloadSelection>();
|
---|
47 | private final JTabbedPane tpDownloadAreaSelectors = new JTabbedPane();
|
---|
48 | private final JCheckBox cbNewLayer;
|
---|
49 | private final JLabel sizeCheck = new JLabel();
|
---|
50 |
|
---|
51 | private Bounds currentBounds = null;
|
---|
52 |
|
---|
53 | private JCheckBox cbDownloadOsmData = new JCheckBox(tr("OpenStreetMap data"), true);
|
---|
54 | private JCheckBox cbDownloadGpxData = new JCheckBox(tr("Raw GPS data"));
|
---|
55 |
|
---|
56 |
|
---|
57 | public DownloadDialog() {
|
---|
58 | setLayout(new GridBagLayout());
|
---|
59 |
|
---|
60 | // adding the download tasks
|
---|
61 | add(new JLabel(tr("Data Sources and Types")), GBC.eol().insets(0,5,0,0));
|
---|
62 | add(cbDownloadOsmData, GBC.eol().insets(20,0,0,0));
|
---|
63 | add(cbDownloadGpxData, GBC.eol().insets(20,0,0,0));
|
---|
64 |
|
---|
65 | // predefined download selections
|
---|
66 | downloadSelections.add(new SlippyMapChooser());
|
---|
67 | downloadSelections.add(new BookmarkSelection());
|
---|
68 | downloadSelections.add(new BoundingBoxSelection());
|
---|
69 | downloadSelections.add(new PlaceSelection());
|
---|
70 | downloadSelections.add(new TileSelection());
|
---|
71 |
|
---|
72 | // add selections from plugins
|
---|
73 | PluginHandler.addDownloadSelection(downloadSelections);
|
---|
74 |
|
---|
75 | // now everybody may add their tab to the tabbed pane
|
---|
76 | // (not done right away to allow plugins to remove one of
|
---|
77 | // the default selectors!)
|
---|
78 | for (DownloadSelection s : downloadSelections) {
|
---|
79 | s.addGui(this);
|
---|
80 | }
|
---|
81 |
|
---|
82 | cbNewLayer = new JCheckBox(tr("Download as new layer"));
|
---|
83 | add(cbNewLayer, GBC.eol().insets(0,5,0,0));
|
---|
84 |
|
---|
85 | add(new JLabel(tr("Download Area")), GBC.eol().insets(0,5,0,0));
|
---|
86 | add(tpDownloadAreaSelectors, GBC.eol().fill());
|
---|
87 |
|
---|
88 | try {
|
---|
89 | tpDownloadAreaSelectors.setSelectedIndex(Main.pref.getInteger("download.tab", 0));
|
---|
90 | } catch (Exception ex) {
|
---|
91 | Main.pref.putInteger("download.tab", 0);
|
---|
92 | }
|
---|
93 |
|
---|
94 | Font labelFont = sizeCheck.getFont();
|
---|
95 | sizeCheck.setFont(labelFont.deriveFont(Font.PLAIN, labelFont.getSize()));
|
---|
96 | add(sizeCheck, GBC.eop().insets(0,5,5,10));
|
---|
97 |
|
---|
98 | getInputMap(WHEN_IN_FOCUSED_WINDOW).put(
|
---|
99 | KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK), "checkClipboardContents");
|
---|
100 |
|
---|
101 | getActionMap().put("checkClipboardContents", new AbstractAction() {
|
---|
102 | public void actionPerformed(ActionEvent e) {
|
---|
103 | checkClipboardContents();
|
---|
104 | }
|
---|
105 | });
|
---|
106 |
|
---|
107 | restoreSettings();
|
---|
108 | }
|
---|
109 |
|
---|
110 | private void checkClipboardContents() {
|
---|
111 | String result = "";
|
---|
112 | Transferable contents = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
|
---|
113 |
|
---|
114 | if(contents == null || !contents.isDataFlavorSupported(DataFlavor.stringFlavor))
|
---|
115 | return;
|
---|
116 |
|
---|
117 | try {
|
---|
118 | result = (String)contents.getTransferData(DataFlavor.stringFlavor);
|
---|
119 | }
|
---|
120 | catch(Exception ex) {
|
---|
121 | return;
|
---|
122 | }
|
---|
123 |
|
---|
124 | Bounds b = OsmUrlToBounds.parse(result);
|
---|
125 | if (b != null) {
|
---|
126 | boundingBoxChanged(new Bounds(b),null);
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | private void updateSizeCheck() {
|
---|
131 | if (currentBounds == null) {
|
---|
132 | sizeCheck.setText(tr("No area selected yet"));
|
---|
133 | sizeCheck.setForeground(Color.darkGray);
|
---|
134 | } else if (currentBounds.getArea() > Main.pref.getDouble("osm-server.max-request-area", 0.25)) {
|
---|
135 | sizeCheck.setText(tr("Download area too large; will probably be rejected by server"));
|
---|
136 | sizeCheck.setForeground(Color.red);
|
---|
137 | } else {
|
---|
138 | sizeCheck.setText(tr("Download area ok, size probably acceptable to server"));
|
---|
139 | sizeCheck.setForeground(Color.darkGray);
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Distributes a "bounding box changed" from one DownloadSelection
|
---|
145 | * object to the others, so they may update or clear their input
|
---|
146 | * fields.
|
---|
147 | *
|
---|
148 | * @param eventSource - the DownloadSelection object that fired this notification.
|
---|
149 | */
|
---|
150 | public void boundingBoxChanged(Bounds b, DownloadSelection eventSource) {
|
---|
151 | this.currentBounds = b;
|
---|
152 | for (DownloadSelection s : downloadSelections) {
|
---|
153 | if (s != eventSource) {
|
---|
154 | s.boundingBoxChanged(this);
|
---|
155 | }
|
---|
156 | }
|
---|
157 | updateSizeCheck();
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Replies true if the user selected to download OSM data
|
---|
162 | *
|
---|
163 | * @return true if the user selected to download OSM data
|
---|
164 | */
|
---|
165 | public boolean isDownloadOsmData() {
|
---|
166 | return cbDownloadOsmData.isSelected();
|
---|
167 | }
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * Replies true if the user selected to download GPX data
|
---|
171 | *
|
---|
172 | * @return true if the user selected to download GPX data
|
---|
173 | */
|
---|
174 | public boolean isDownloadGpxData() {
|
---|
175 | return cbDownloadGpxData.isSelected();
|
---|
176 | }
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Replies true if the user requires to download into a new layer
|
---|
180 | *
|
---|
181 | * @return true if the user requires to download into a new layer
|
---|
182 | */
|
---|
183 | public boolean isNewLayerRequired() {
|
---|
184 | return cbNewLayer.isSelected();
|
---|
185 | }
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * Adds a new download area selector to the download dialog
|
---|
189 | *
|
---|
190 | * @param selector the download are selector
|
---|
191 | * @param displayName the display name of the selector
|
---|
192 | */
|
---|
193 | public void addDownloadAreaSelector(JPanel selector, String displayName) {
|
---|
194 | tpDownloadAreaSelectors.add(displayName, selector);
|
---|
195 | }
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * Remembers the current settings in the download dialog
|
---|
199 | *
|
---|
200 | */
|
---|
201 | public void rememberSettings() {
|
---|
202 | Main.pref.put("download.tab", Integer.toString(tpDownloadAreaSelectors.getSelectedIndex()));
|
---|
203 | Main.pref.put("download.osm", cbDownloadOsmData.isSelected());
|
---|
204 | Main.pref.put("download.gps", cbDownloadGpxData.isSelected());
|
---|
205 | Main.pref.put("download.newlayer", cbNewLayer.isSelected());
|
---|
206 | if (currentBounds != null) {
|
---|
207 | Main.pref.put("osm-download.bounds", currentBounds.encodeAsString(";"));
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|
211 | public void restoreSettings() {
|
---|
212 | cbDownloadOsmData.setSelected(Main.pref.getBoolean("download.osm", true));
|
---|
213 | cbDownloadGpxData.setSelected(Main.pref.getBoolean("download.gps", false));
|
---|
214 | cbNewLayer.setSelected(Main.pref.getBoolean("download.newlayer", false));
|
---|
215 | int idx = Main.pref.getInteger("download.tab", 0);
|
---|
216 | if (idx < 0 || idx > tpDownloadAreaSelectors.getTabCount()) {
|
---|
217 | idx = 0;
|
---|
218 | }
|
---|
219 | tpDownloadAreaSelectors.setSelectedIndex(idx);
|
---|
220 |
|
---|
221 | if (Main.map != null) {
|
---|
222 | MapView mv = Main.map.mapView;
|
---|
223 | currentBounds = new Bounds(
|
---|
224 | mv.getLatLon(0, mv.getHeight()),
|
---|
225 | mv.getLatLon(mv.getWidth(), 0)
|
---|
226 | );
|
---|
227 | boundingBoxChanged(currentBounds,null);
|
---|
228 | }
|
---|
229 | else if (Main.pref.hasKey("osm-download.bounds")) {
|
---|
230 | // read the bounding box from the preferences
|
---|
231 | try {
|
---|
232 | currentBounds = new Bounds(Main.pref.get("osm-download.bounds"), ";");
|
---|
233 | boundingBoxChanged(currentBounds,null);
|
---|
234 | }
|
---|
235 | catch (Exception e) {
|
---|
236 | e.printStackTrace();
|
---|
237 | }
|
---|
238 | }
|
---|
239 | }
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * Replies the currently selected download area. May be null, if no download area is selected
|
---|
243 | * yet.
|
---|
244 | */
|
---|
245 | public Bounds getSelectedDownloadArea() {
|
---|
246 | return currentBounds;
|
---|
247 | }
|
---|
248 |
|
---|
249 | }
|
---|