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 |
|
---|
18 | import javax.swing.AbstractAction;
|
---|
19 | import javax.swing.JCheckBox;
|
---|
20 | import javax.swing.JLabel;
|
---|
21 | import javax.swing.JPanel;
|
---|
22 | import javax.swing.JTabbedPane;
|
---|
23 | import javax.swing.KeyStroke;
|
---|
24 |
|
---|
25 | import org.openstreetmap.josm.Main;
|
---|
26 | import org.openstreetmap.josm.actions.DownloadAction;
|
---|
27 | import org.openstreetmap.josm.actions.downloadtasks.DownloadGpsTask;
|
---|
28 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
|
---|
29 | import org.openstreetmap.josm.actions.downloadtasks.DownloadTask;
|
---|
30 | import org.openstreetmap.josm.data.Bounds;
|
---|
31 | import org.openstreetmap.josm.gui.MapView;
|
---|
32 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
33 | import org.openstreetmap.josm.plugins.PluginHandler;
|
---|
34 | import org.openstreetmap.josm.tools.GBC;
|
---|
35 | import 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 | */
|
---|
47 | public class DownloadDialog extends JPanel {
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * The list of download tasks. First entry should be the osm data entry
|
---|
51 | * and the second the gps entry. After that, plugins can register additional
|
---|
52 | * download possibilities.
|
---|
53 | */
|
---|
54 | public final List<DownloadTask> downloadTasks = new ArrayList<DownloadTask>(5);
|
---|
55 |
|
---|
56 | public final List<DownloadSelection> downloadSelections = new ArrayList<DownloadSelection>();
|
---|
57 | public final JTabbedPane tabpane = new JTabbedPane();
|
---|
58 | public final JCheckBox newLayer;
|
---|
59 | public final JLabel sizeCheck = new JLabel();
|
---|
60 |
|
---|
61 | public double minlon;
|
---|
62 | public double minlat;
|
---|
63 | public double maxlon;
|
---|
64 | public double maxlat;
|
---|
65 |
|
---|
66 |
|
---|
67 | public DownloadDialog() {
|
---|
68 | setLayout(new GridBagLayout());
|
---|
69 |
|
---|
70 | downloadTasks.add(new DownloadOsmTask());
|
---|
71 | downloadTasks.add(new DownloadGpsTask());
|
---|
72 |
|
---|
73 | // adding the download tasks
|
---|
74 | add(new JLabel(tr("Data Sources and Types")), GBC.eol().insets(0,5,0,0));
|
---|
75 | for (DownloadTask task : downloadTasks) {
|
---|
76 | add(task.getCheckBox(), GBC.eol().insets(20,0,0,0));
|
---|
77 | // don't override defaults, if we (initially) don't have any preferences
|
---|
78 | if(Main.pref.hasKey("download."+task.getPreferencesSuffix())) {
|
---|
79 | task.getCheckBox().setSelected(Main.pref.getBoolean("download."+task.getPreferencesSuffix()));
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | // predefined download selections
|
---|
84 | downloadSelections.add(new SlippyMapChooser());
|
---|
85 | downloadSelections.add(new BookmarkSelection());
|
---|
86 | downloadSelections.add(new BoundingBoxSelection());
|
---|
87 | downloadSelections.add(new PlaceSelection());
|
---|
88 | downloadSelections.add(new TileSelection());
|
---|
89 |
|
---|
90 | // add selections from plugins
|
---|
91 | PluginHandler.addDownloadSelection(downloadSelections);
|
---|
92 |
|
---|
93 | // now everybody may add their tab to the tabbed pane
|
---|
94 | // (not done right away to allow plugins to remove one of
|
---|
95 | // the default selectors!)
|
---|
96 | for (DownloadSelection s : downloadSelections) {
|
---|
97 | s.addGui(this);
|
---|
98 | }
|
---|
99 |
|
---|
100 | if (Main.map != null) {
|
---|
101 | MapView mv = Main.map.mapView;
|
---|
102 | minlon = mv.getLatLon(0, mv.getHeight()).lon();
|
---|
103 | minlat = mv.getLatLon(0, mv.getHeight()).lat();
|
---|
104 | maxlon = mv.getLatLon(mv.getWidth(), 0).lon();
|
---|
105 | maxlat = mv.getLatLon(mv.getWidth(), 0).lat();
|
---|
106 | boundingBoxChanged(null);
|
---|
107 | }
|
---|
108 | else if (Main.pref.hasKey("osm-download.bounds")) {
|
---|
109 | // read the bounding box from the preferences
|
---|
110 | try {
|
---|
111 | String bounds[] = Main.pref.get("osm-download.bounds").split(";");
|
---|
112 | minlat = Double.parseDouble(bounds[0]);
|
---|
113 | minlon = Double.parseDouble(bounds[1]);
|
---|
114 | maxlat = Double.parseDouble(bounds[2]);
|
---|
115 | maxlon = Double.parseDouble(bounds[3]);
|
---|
116 | boundingBoxChanged(null);
|
---|
117 | }
|
---|
118 | catch (Exception e) {
|
---|
119 | e.printStackTrace();
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | newLayer = new JCheckBox(tr("Download as new layer"), Main.pref.getBoolean("download.newlayer", false));
|
---|
124 | add(newLayer, GBC.eol().insets(0,5,0,0));
|
---|
125 |
|
---|
126 | add(new JLabel(tr("Download Area")), GBC.eol().insets(0,5,0,0));
|
---|
127 | add(tabpane, GBC.eol().fill());
|
---|
128 |
|
---|
129 | try {
|
---|
130 | tabpane.setSelectedIndex(Main.pref.getInteger("download.tab", 0));
|
---|
131 | } catch (Exception ex) {
|
---|
132 | Main.pref.putInteger("download.tab", 0);
|
---|
133 | }
|
---|
134 |
|
---|
135 | Font labelFont = sizeCheck.getFont();
|
---|
136 | sizeCheck.setFont(labelFont.deriveFont(Font.PLAIN, labelFont.getSize()));
|
---|
137 | add(sizeCheck, GBC.eop().insets(0,5,5,10));
|
---|
138 |
|
---|
139 | getInputMap(WHEN_IN_FOCUSED_WINDOW).put(
|
---|
140 | KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK), "checkClipboardContents");
|
---|
141 |
|
---|
142 | getActionMap().put("checkClipboardContents", new AbstractAction() {
|
---|
143 | public void actionPerformed(ActionEvent e) {
|
---|
144 | checkClipboardContents();
|
---|
145 | }
|
---|
146 | });
|
---|
147 | }
|
---|
148 |
|
---|
149 | private void checkClipboardContents() {
|
---|
150 | String result = "";
|
---|
151 | Transferable contents = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
|
---|
152 |
|
---|
153 | if(contents == null || !contents.isDataFlavorSupported(DataFlavor.stringFlavor))
|
---|
154 | return;
|
---|
155 |
|
---|
156 | try {
|
---|
157 | result = (String)contents.getTransferData(DataFlavor.stringFlavor);
|
---|
158 | }
|
---|
159 | catch(Exception ex) {
|
---|
160 | return;
|
---|
161 | }
|
---|
162 |
|
---|
163 | Bounds b = OsmUrlToBounds.parse(result);
|
---|
164 | if (b != null) {
|
---|
165 | minlon = b.min.lon();
|
---|
166 | minlat = b.min.lat();
|
---|
167 | maxlon = b.max.lon();
|
---|
168 | maxlat = b.max.lat();
|
---|
169 | boundingBoxChanged(null);
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | private void updateSizeCheck() {
|
---|
174 | if ((maxlon-minlon)*(maxlat-minlat) > Main.pref.getDouble("osm-server.max-request-area", 0.25)) {
|
---|
175 | sizeCheck.setText(tr("Download area too large; will probably be rejected by server"));
|
---|
176 | sizeCheck.setForeground(Color.red);
|
---|
177 | } else {
|
---|
178 | sizeCheck.setText(tr("Download area ok, size probably acceptable to server"));
|
---|
179 | sizeCheck.setForeground(Color.darkGray);
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * Distributes a "bounding box changed" from one DownloadSelection
|
---|
185 | * object to the others, so they may update or clear their input
|
---|
186 | * fields.
|
---|
187 | *
|
---|
188 | * @param eventSource - the DownloadSelection object that fired this notification.
|
---|
189 | */
|
---|
190 | public void boundingBoxChanged(DownloadSelection eventSource) {
|
---|
191 | for (DownloadSelection s : downloadSelections) {
|
---|
192 | if (s != eventSource) {
|
---|
193 | s.boundingBoxChanged(this);
|
---|
194 | }
|
---|
195 | }
|
---|
196 | updateSizeCheck();
|
---|
197 | }
|
---|
198 |
|
---|
199 | /*
|
---|
200 | * Returns currently selected tab.
|
---|
201 | */
|
---|
202 | public int getSelectedTab() {
|
---|
203 | return tabpane.getSelectedIndex();
|
---|
204 | }
|
---|
205 | }
|
---|