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