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.util.ArrayList;
|
---|
10 | import java.util.List;
|
---|
11 |
|
---|
12 | import javax.swing.JCheckBox;
|
---|
13 | import javax.swing.JLabel;
|
---|
14 | import javax.swing.JOptionPane;
|
---|
15 | import javax.swing.JPanel;
|
---|
16 | import javax.swing.JTabbedPane;
|
---|
17 |
|
---|
18 | import org.openstreetmap.josm.Main;
|
---|
19 | import org.openstreetmap.josm.actions.DownloadAction;
|
---|
20 | import org.openstreetmap.josm.actions.downloadtasks.DownloadGpsTask;
|
---|
21 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
|
---|
22 | import org.openstreetmap.josm.gui.MapView;
|
---|
23 | import org.openstreetmap.josm.plugins.PluginProxy;
|
---|
24 | import org.openstreetmap.josm.tools.GBC;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Main download dialog.
|
---|
28 | *
|
---|
29 | * Can be extended by plugins in two ways:
|
---|
30 | * (1) by adding download tasks that are then called with the selected bounding box
|
---|
31 | * (2) by adding "DownloadSelection" objects that implement different ways of selecting a bounding box
|
---|
32 | *
|
---|
33 | * @author Frederik Ramm <frederik@remote.org>
|
---|
34 | *
|
---|
35 | */
|
---|
36 | public class DownloadDialog extends JPanel {
|
---|
37 |
|
---|
38 | // the JOptionPane that contains this dialog. required for the closeDialog() method.
|
---|
39 | private JOptionPane optionPane;
|
---|
40 |
|
---|
41 | public interface DownloadTask {
|
---|
42 | /**
|
---|
43 | * Execute the download.
|
---|
44 | */
|
---|
45 | void download(DownloadAction action, double minlat, double minlon, double maxlat, double maxlon);
|
---|
46 | void loadUrl(boolean newLayer, String url);
|
---|
47 | /**
|
---|
48 | * @return The checkbox presented to the user
|
---|
49 | */
|
---|
50 | JCheckBox getCheckBox();
|
---|
51 | /**
|
---|
52 | * @return The name of the preferences suffix to use for storing the
|
---|
53 | * selection state.
|
---|
54 | */
|
---|
55 | String getPreferencesSuffix();
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * The list of download tasks. First entry should be the osm data entry
|
---|
60 | * and the second the gps entry. After that, plugins can register additional
|
---|
61 | * download possibilities.
|
---|
62 | */
|
---|
63 | public final List<DownloadTask> downloadTasks = new ArrayList<DownloadTask>(5);
|
---|
64 |
|
---|
65 | public final List<DownloadSelection> downloadSelections = new ArrayList<DownloadSelection>();
|
---|
66 | public final JTabbedPane tabpane = new JTabbedPane();
|
---|
67 | public final JCheckBox newLayer;
|
---|
68 | public final JLabel sizeCheck = new JLabel();
|
---|
69 |
|
---|
70 | public double minlon;
|
---|
71 | public double minlat;
|
---|
72 | public double maxlon;
|
---|
73 | public double maxlat;
|
---|
74 |
|
---|
75 |
|
---|
76 | public DownloadDialog() {
|
---|
77 | setLayout(new GridBagLayout());
|
---|
78 |
|
---|
79 | downloadTasks.add(new DownloadOsmTask());
|
---|
80 | downloadTasks.add(new DownloadGpsTask());
|
---|
81 |
|
---|
82 | // adding the download tasks
|
---|
83 | add(new JLabel(tr("Data Sources and Types")), GBC.eol().insets(0,5,0,0));
|
---|
84 | for (DownloadTask task : downloadTasks) {
|
---|
85 | add(task.getCheckBox(), GBC.eol().insets(20,0,0,0));
|
---|
86 | // don't override defaults, if we (initially) don't have any preferences
|
---|
87 | if(Main.pref.hasKey("download."+task.getPreferencesSuffix())) {
|
---|
88 | task.getCheckBox().setSelected(Main.pref.getBoolean("download."+task.getPreferencesSuffix()));
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | // predefined download selections
|
---|
93 | downloadSelections.add(new BoundingBoxSelection());
|
---|
94 | downloadSelections.add(new TileSelection());
|
---|
95 | downloadSelections.add(new BookmarkSelection());
|
---|
96 | downloadSelections.add(new WorldChooser());
|
---|
97 |
|
---|
98 | // add selections from plugins
|
---|
99 | for (PluginProxy p : Main.plugins) {
|
---|
100 | p.addDownloadSelection(downloadSelections);
|
---|
101 | }
|
---|
102 |
|
---|
103 | // now everybody may add their tab to the tabbed pane
|
---|
104 | // (not done right away to allow plugins to remove one of
|
---|
105 | // the default selectors!)
|
---|
106 | for (DownloadSelection s : downloadSelections) {
|
---|
107 | s.addGui(this);
|
---|
108 | }
|
---|
109 |
|
---|
110 | if (Main.map != null) {
|
---|
111 | MapView mv = Main.map.mapView;
|
---|
112 | minlon = mv.getLatLon(0, mv.getHeight()).lon();
|
---|
113 | minlat = mv.getLatLon(0, mv.getHeight()).lat();
|
---|
114 | maxlon = mv.getLatLon(mv.getWidth(), 0).lon();
|
---|
115 | maxlat = mv.getLatLon(mv.getWidth(), 0).lat();
|
---|
116 | boundingBoxChanged(null);
|
---|
117 | }
|
---|
118 | else if (Main.pref.hasKey("osm-download.bounds")) {
|
---|
119 | // read the bounding box from the preferences
|
---|
120 | try {
|
---|
121 | String bounds[] = Main.pref.get("osm-download.bounds").split(";");
|
---|
122 | minlat = Double.parseDouble(bounds[0]);
|
---|
123 | minlon = Double.parseDouble(bounds[1]);
|
---|
124 | maxlat = Double.parseDouble(bounds[2]);
|
---|
125 | maxlon = Double.parseDouble(bounds[3]);
|
---|
126 | boundingBoxChanged(null);
|
---|
127 | }
|
---|
128 | catch (Exception e) {
|
---|
129 | e.printStackTrace();
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | newLayer = new JCheckBox(tr("Download as new layer"), Main.pref.getBoolean("download.newlayer", false));
|
---|
134 | add(newLayer, GBC.eol().insets(0,5,0,0));
|
---|
135 |
|
---|
136 | add(new JLabel(tr("Download Area")), GBC.eol().insets(0,5,0,0));
|
---|
137 | add(tabpane, GBC.eol().fill());
|
---|
138 |
|
---|
139 | try {
|
---|
140 | tabpane.setSelectedIndex(Integer.parseInt(Main.pref.get("download.tab", "0")));
|
---|
141 | } catch (Exception ex) {
|
---|
142 | Main.pref.put("download.tab", "0");
|
---|
143 | }
|
---|
144 |
|
---|
145 | Font labelFont = sizeCheck.getFont();
|
---|
146 | sizeCheck.setFont(labelFont.deriveFont(Font.PLAIN, labelFont.getSize()));
|
---|
147 | add(sizeCheck, GBC.eop().insets(0,5,5,10));
|
---|
148 | }
|
---|
149 |
|
---|
150 | private void updateSizeCheck() {
|
---|
151 | if ((maxlon-minlon)*(maxlat-minlat) > Main.pref.getDouble("osm-server.max-request-area", 0.25)) {
|
---|
152 | sizeCheck.setText(tr("Download area too large; will probably be rejected by server"));
|
---|
153 | sizeCheck.setForeground(Color.red);
|
---|
154 | } else {
|
---|
155 | sizeCheck.setText(tr("Download area ok, size probably acceptable to server"));
|
---|
156 | sizeCheck.setForeground(Color.darkGray);
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Distributes a "bounding box changed" from one DownloadSelection
|
---|
162 | * object to the others, so they may update or clear their input
|
---|
163 | * fields.
|
---|
164 | *
|
---|
165 | * @param eventSource - the DownloadSelection object that fired this notification.
|
---|
166 | */
|
---|
167 | public void boundingBoxChanged(DownloadSelection eventSource) {
|
---|
168 | for (DownloadSelection s : downloadSelections) {
|
---|
169 | if (s != eventSource) s.boundingBoxChanged(this);
|
---|
170 | }
|
---|
171 | updateSizeCheck();
|
---|
172 | }
|
---|
173 |
|
---|
174 | /*
|
---|
175 | * Returns currently selected tab.
|
---|
176 | */
|
---|
177 | public int getSelectedTab() {
|
---|
178 | return tabpane.getSelectedIndex();
|
---|
179 | }
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * Closes the download dialog. This is intended to be called by one of
|
---|
183 | * the various download area selection "plugins".
|
---|
184 | *
|
---|
185 | * @param download true to download selected data, false to cancel download
|
---|
186 | */
|
---|
187 | public void closeDownloadDialog(boolean download) {
|
---|
188 | optionPane.setValue(download ? JOptionPane.OK_OPTION : JOptionPane.CANCEL_OPTION);
|
---|
189 | }
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Has to be called after this dialog has been added to a JOptionPane.
|
---|
193 | * @param optionPane
|
---|
194 | */
|
---|
195 | public void setOptionPane(JOptionPane optionPane) {
|
---|
196 | this.optionPane = optionPane;
|
---|
197 | }
|
---|
198 | }
|
---|