1 | package org.openstreetmap.josm.gui.download;
|
---|
2 |
|
---|
3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
4 |
|
---|
5 | import java.awt.GridBagLayout;
|
---|
6 | import java.util.ArrayList;
|
---|
7 | import java.util.List;
|
---|
8 |
|
---|
9 | import javax.swing.JCheckBox;
|
---|
10 | import javax.swing.JLabel;
|
---|
11 | import javax.swing.JPanel;
|
---|
12 | import javax.swing.JTabbedPane;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.Main;
|
---|
15 | import org.openstreetmap.josm.actions.DownloadAction;
|
---|
16 | import org.openstreetmap.josm.actions.downloadtasks.DownloadGpsTask;
|
---|
17 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
|
---|
18 | import org.openstreetmap.josm.gui.MapView;
|
---|
19 | import org.openstreetmap.josm.plugins.PluginProxy;
|
---|
20 | import org.openstreetmap.josm.tools.GBC;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Main download dialog.
|
---|
24 | *
|
---|
25 | * Can be extended by plugins in two ways:
|
---|
26 | * (1) by adding download tasks that are then called with the selected bounding box
|
---|
27 | * (2) by adding "DownloadSelection" objects that implement different ways of selecting a bounding box
|
---|
28 | *
|
---|
29 | * @author Frederik Ramm <frederik@remote.org>
|
---|
30 | *
|
---|
31 | */
|
---|
32 | public class DownloadDialog extends JPanel {
|
---|
33 |
|
---|
34 |
|
---|
35 | public interface DownloadTask {
|
---|
36 | /**
|
---|
37 | * Execute the download.
|
---|
38 | */
|
---|
39 | void download(DownloadAction action, double minlat, double minlon, double maxlat, double maxlon);
|
---|
40 | /**
|
---|
41 | * @return The checkbox presented to the user
|
---|
42 | */
|
---|
43 | JCheckBox getCheckBox();
|
---|
44 | /**
|
---|
45 | * @return The name of the preferences suffix to use for storing the
|
---|
46 | * selection state.
|
---|
47 | */
|
---|
48 | String getPreferencesSuffix();
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * The list of download tasks. First entry should be the osm data entry
|
---|
53 | * and the second the gps entry. After that, plugins can register additional
|
---|
54 | * download possibilities.
|
---|
55 | */
|
---|
56 | public final List<DownloadTask> downloadTasks = new ArrayList<DownloadTask>(5);
|
---|
57 |
|
---|
58 | public final List<DownloadSelection> downloadSelections = new ArrayList<DownloadSelection>();
|
---|
59 | public final JTabbedPane tabpane = new JTabbedPane();
|
---|
60 | public final JCheckBox newLayer;
|
---|
61 |
|
---|
62 | public double minlon;
|
---|
63 | public double minlat;
|
---|
64 | public double maxlon;
|
---|
65 | public double maxlat;
|
---|
66 |
|
---|
67 |
|
---|
68 | public DownloadDialog() {
|
---|
69 | setLayout(new GridBagLayout());
|
---|
70 |
|
---|
71 | downloadTasks.add(new DownloadOsmTask());
|
---|
72 | downloadTasks.add(new DownloadGpsTask());
|
---|
73 |
|
---|
74 | // adding the download tasks
|
---|
75 | add(new JLabel(tr("Data Sources and Types")), GBC.eol().insets(0,5,0,0));
|
---|
76 | for (DownloadTask task : downloadTasks) {
|
---|
77 | add(task.getCheckBox(), GBC.eol().insets(20,0,0,0));
|
---|
78 | task.getCheckBox().setSelected(Main.pref.getBoolean("download."+task.getPreferencesSuffix()));
|
---|
79 | }
|
---|
80 |
|
---|
81 | // predefined download selections
|
---|
82 | downloadSelections.add(new BoundingBoxSelection());
|
---|
83 | downloadSelections.add(new BookmarkSelection());
|
---|
84 | downloadSelections.add(new WorldChooser());
|
---|
85 |
|
---|
86 | // add selections from plugins
|
---|
87 | for (PluginProxy p : Main.plugins) {
|
---|
88 | p.addDownloadSelection(downloadSelections);
|
---|
89 | }
|
---|
90 |
|
---|
91 | // now everybody may add their tab to the tabbed pane
|
---|
92 | // (not done right away to allow plugins to remove one of
|
---|
93 | // the default selectors!)
|
---|
94 | for (DownloadSelection s : downloadSelections) {
|
---|
95 | s.addGui(this);
|
---|
96 | }
|
---|
97 |
|
---|
98 | if (Main.map != null) {
|
---|
99 | MapView mv = Main.map.mapView;
|
---|
100 | minlon = mv.getLatLon(0, mv.getHeight()).lon();
|
---|
101 | minlat = mv.getLatLon(0, mv.getHeight()).lat();
|
---|
102 | maxlon = mv.getLatLon(mv.getWidth(), 0).lon();
|
---|
103 | maxlat = mv.getLatLon(mv.getWidth(), 0).lat();
|
---|
104 | boundingBoxChanged(null);
|
---|
105 | }
|
---|
106 |
|
---|
107 | newLayer = new JCheckBox(tr("Download as new layer"), Main.pref.getBoolean("download.newlayer", true));
|
---|
108 | add(newLayer, GBC.eol().insets(0,5,0,0));
|
---|
109 |
|
---|
110 | add(new JLabel(tr("Download Area")), GBC.eol().insets(0,5,0,0));
|
---|
111 | add(tabpane, GBC.eol().fill());
|
---|
112 |
|
---|
113 | try {
|
---|
114 | tabpane.setSelectedIndex(Integer.parseInt(Main.pref.get("download.tab", "0")));
|
---|
115 | } catch (Exception ex) {
|
---|
116 | Main.pref.put("download.tab", "0");
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * Distributes a "bounding box changed" from ohne DownloadSelection
|
---|
122 | * object to the others, so they may update or clear their input
|
---|
123 | * fields.
|
---|
124 | *
|
---|
125 | * @param eventSource - the DownloadSelection object that fired this notification.
|
---|
126 | */
|
---|
127 | public void boundingBoxChanged(DownloadSelection eventSource) {
|
---|
128 | for (DownloadSelection s : downloadSelections) {
|
---|
129 | if (s != eventSource) s.boundingBoxChanged(this);
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | /*
|
---|
134 | * Returns currently selected tab.
|
---|
135 | */
|
---|
136 | public int getSelectedTab() {
|
---|
137 | return tabpane.getSelectedIndex();
|
---|
138 | }
|
---|
139 | }
|
---|