source: josm/src/org/openstreetmap/josm/gui/download/DownloadDialog.java@ 298

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