source: josm/trunk/src/org/openstreetmap/josm/gui/download/BookmarkSelection.java@ 2387

Last change on this file since 2387 was 2387, checked in by Gubaer, 14 years ago

More and improved context sensitive help, see #2882

  • Property svn:eol-style set to native
File size: 10.1 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.GridBagConstraints;
7import java.awt.GridBagLayout;
8import java.awt.Insets;
9import java.awt.event.ActionEvent;
10import java.awt.event.MouseAdapter;
11import java.awt.event.MouseEvent;
12
13import javax.swing.AbstractAction;
14import javax.swing.DefaultListModel;
15import javax.swing.JButton;
16import javax.swing.JOptionPane;
17import javax.swing.JPanel;
18import javax.swing.JScrollPane;
19import javax.swing.SwingUtilities;
20import javax.swing.event.ListSelectionEvent;
21import javax.swing.event.ListSelectionListener;
22
23import org.openstreetmap.josm.Main;
24import org.openstreetmap.josm.data.Bounds;
25import org.openstreetmap.josm.data.Preferences;
26import org.openstreetmap.josm.data.Preferences.Bookmark;
27import org.openstreetmap.josm.data.coor.CoordinateFormat;
28import org.openstreetmap.josm.gui.BookmarkList;
29import org.openstreetmap.josm.gui.JMultilineLabel;
30import org.openstreetmap.josm.tools.ImageProvider;
31
32/**
33 * DownloadAreaSelector which manages a list of "bookmarks", i.e. a list of
34 * name download areas.
35 *
36 */
37public class BookmarkSelection implements DownloadSelection {
38
39 /** the currently selected download area. One can add bookmarks for this
40 * area, if not null
41 */
42 private Bounds currentArea;
43 /** the list of bookmarks */
44 private BookmarkList bookmarks;
45
46 /** the parent download GUI */
47 private DownloadDialog parent;
48
49 /** displays information about the current download area */
50 private JMultilineLabel lblCurrentDownloadArea;
51 /** the add action */
52 private AddAction actAdd;
53
54 /**
55 * Creates the panel with the action buttons on the left
56 *
57 * @return the panel with the action buttons on the left
58 */
59 protected JPanel buildButtonPanel() {
60 JPanel pnl = new JPanel();
61 pnl.setLayout(new GridBagLayout());
62 GridBagConstraints gc = new GridBagConstraints();
63 gc.gridy = 0;
64 RemoveAction removeAction = new RemoveAction();
65 bookmarks.addListSelectionListener(removeAction);
66 pnl.add(new JButton(removeAction), gc);
67
68 gc.gridy = 1;
69 RenameAction renameAction = new RenameAction();
70 bookmarks.addListSelectionListener(renameAction);
71 pnl.add(new JButton(renameAction), gc);
72
73 gc.fill = GridBagConstraints.BOTH;
74 gc.weightx = 1.0;
75 gc.weighty = 1.0;
76 gc.gridy = 3;
77 pnl.add(new JPanel(), gc); // just a filler
78 return pnl;
79 }
80
81 protected JPanel buildDownloadAreaAddPanel() {
82 JPanel pnl = new JPanel();
83 pnl.setLayout(new GridBagLayout());
84
85 GridBagConstraints gc = new GridBagConstraints();
86 gc.anchor = GridBagConstraints.NORTHWEST;
87 gc.fill = GridBagConstraints.BOTH;
88 gc.weightx = 1.0;
89 gc.weighty = 1.0;
90 gc.insets = new Insets(5,5,5,5);
91
92 pnl.add(lblCurrentDownloadArea = new JMultilineLabel(""), gc);
93
94 gc.anchor = GridBagConstraints.NORTHEAST;
95 gc.fill = GridBagConstraints.HORIZONTAL;
96 gc.weightx = 0.0;
97 gc.weighty = 0.0;
98 gc.insets = new Insets(5,5,5,5);
99 pnl.add(new JButton(actAdd = new AddAction()), gc);
100 return pnl;
101 }
102
103 public void addGui(final DownloadDialog gui) {
104 JPanel dlg = new JPanel(new GridBagLayout());
105 gui.addDownloadAreaSelector(dlg, tr("Bookmarks"));
106 GridBagConstraints gc = new GridBagConstraints();
107
108
109 bookmarks = new BookmarkList();
110 bookmarks.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
111 public void valueChanged(ListSelectionEvent e) {
112 Preferences.Bookmark b = (Preferences.Bookmark)bookmarks.getSelectedValue();
113 if (b != null) {
114 gui.boundingBoxChanged(b.getArea(),BookmarkSelection.this);
115 }
116 }
117 });
118 bookmarks.addMouseListener(new DoubleClickAdapter());
119
120 gc.fill = GridBagConstraints.HORIZONTAL;
121 gc.weightx = 1.0;
122 gc.weighty = 0.0;
123 gc.gridwidth = 2;
124 dlg.add(buildDownloadAreaAddPanel(),gc);
125
126 gc.gridwidth = 1;
127 gc.gridx = 0;
128 gc.gridy = 1;
129 gc.fill = GridBagConstraints.VERTICAL;
130 gc.weightx = 0.0;
131 gc.weighty = 1.0;
132 dlg.add(buildButtonPanel(),gc);
133
134 gc.gridwidth = 1;
135 gc.gridx = 1;
136 gc.gridy = 1;
137 gc.fill = GridBagConstraints.BOTH;
138 gc.weightx = 1.0;
139 gc.weighty = 1.0;
140 gc.gridx = 1;
141 dlg.add(new JScrollPane(bookmarks), gc);
142
143 this.parent = gui;
144 }
145
146 protected void updateDownloadAreaLabel() {
147 if (currentArea == null) {
148 lblCurrentDownloadArea.setText(tr("<html>There is currently no download area selected.</html>"));
149 } else {
150 lblCurrentDownloadArea.setText(tr("<html><strong>Current download area</strong> (minlat,minlon, maxlat, maxlon): {0}, {1}, {2}, {3}</html>",
151 currentArea.getMin().latToString(CoordinateFormat.DECIMAL_DEGREES),
152 currentArea.getMin().lonToString(CoordinateFormat.DECIMAL_DEGREES),
153 currentArea.getMax().latToString(CoordinateFormat.DECIMAL_DEGREES),
154 currentArea.getMax().lonToString(CoordinateFormat.DECIMAL_DEGREES)
155 )
156 );
157 }
158 }
159
160 /**
161 * Sets the current download area
162 *
163 * @param area the download area.
164 */
165 public void setDownloadArea(Bounds area) {
166 if (area == null) return;
167 this.currentArea = area;
168 bookmarks.clearSelection();
169 updateDownloadAreaLabel();
170 actAdd.setEnabled(area != null);
171 }
172
173 /**
174 * The action to add a new bookmark for the current download area.
175 *
176 */
177 class AddAction extends AbstractAction {
178 public AddAction() {
179 putValue(NAME, tr("Create bookmark"));
180 putValue(SMALL_ICON, ImageProvider.get("dialogs", "bookmark-new"));
181 putValue(SHORT_DESCRIPTION, tr("Add a bookmark for the currently selected download area"));
182 }
183
184 public void actionPerformed(ActionEvent e) {
185 if (currentArea == null) {
186 JOptionPane.showMessageDialog(
187 Main.parent,
188 tr("Currently, there is no download area selected. Please select an area first."),
189 tr("Information"),
190 JOptionPane.INFORMATION_MESSAGE
191 );
192 return;
193 }
194 Bookmark b = new Bookmark();
195 b.setName(
196 JOptionPane.showInputDialog(
197 Main.parent,tr("Please enter a name for the bookmarked download area."),
198 tr("Name of location"),
199 JOptionPane.QUESTION_MESSAGE)
200 );
201 b.setArea(currentArea);
202 if (b.getName() != null && !b.getName().equals("")) {
203 ((DefaultListModel)bookmarks.getModel()).addElement(b);
204 bookmarks.save();
205 }
206 }
207 }
208
209 class RemoveAction extends AbstractAction implements ListSelectionListener{
210 public RemoveAction() {
211 //putValue(NAME, tr("Remove"));
212 putValue(SMALL_ICON, ImageProvider.get("dialogs", "delete"));
213 putValue(SHORT_DESCRIPTION, tr("Remove the currently selected bookmarks"));
214 updateEnabledState();
215 }
216
217 public void actionPerformed(ActionEvent e) {
218 Object[] sels = bookmarks.getSelectedValues();
219 if (sels == null || sels.length == 0) {
220 return;
221 }
222 for (Object sel: sels) {
223 ((DefaultListModel)bookmarks.getModel()).removeElement(sel);
224 }
225 bookmarks.save();
226 }
227 protected void updateEnabledState() {
228 setEnabled(bookmarks.getSelectedIndices().length > 0);
229 }
230 public void valueChanged(ListSelectionEvent e) {
231 updateEnabledState();
232 }
233 }
234
235 class RenameAction extends AbstractAction implements ListSelectionListener{
236 public RenameAction() {
237 //putValue(NAME, tr("Remove"));
238 putValue(SMALL_ICON, ImageProvider.get("dialogs", "edit"));
239 putValue(SHORT_DESCRIPTION, tr("Rename the currently selected bookmark"));
240 updateEnabledState();
241 }
242
243 public void actionPerformed(ActionEvent e) {
244 Object[] sels = bookmarks.getSelectedValues();
245 if (sels == null || sels.length != 1) {
246 return;
247 }
248 Bookmark b = (Bookmark)sels[0];
249 Object value =
250 JOptionPane.showInputDialog(
251 Main.parent,tr("Please enter a name for the bookmarked download area."),
252 tr("Name of location"),
253 JOptionPane.QUESTION_MESSAGE,
254 null,
255 null,
256 b.getName()
257 );
258 if (value != null) {
259 b.setName(value.toString());
260 bookmarks.save();
261 bookmarks.repaint();
262 }
263 }
264 protected void updateEnabledState() {
265 setEnabled(bookmarks.getSelectedIndices().length == 1);
266 }
267 public void valueChanged(ListSelectionEvent e) {
268 updateEnabledState();
269 }
270 }
271
272 class DoubleClickAdapter extends MouseAdapter {
273 @Override
274 public void mouseClicked(MouseEvent e) {
275 if (!(SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2))
276 return;
277 int idx = bookmarks.locationToIndex(e.getPoint());
278 if (idx < 0 || idx >= bookmarks.getModel().getSize())
279 return;
280 Bookmark b = (Bookmark)bookmarks.getModel().getElementAt(idx);
281 parent.startDownload(b.getArea());
282 }
283 }
284}
Note: See TracBrowser for help on using the repository browser.