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

Last change on this file since 9606 was 9606, checked in by Don-vip, 8 years ago

add more unit tests

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