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

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

fix some Sonar issues

  • Property svn:eol-style set to native
File size: 9.7 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;
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.gui.download.BookmarkList.Bookmark;
26import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
27import org.openstreetmap.josm.gui.widgets.JosmTextArea;
28import org.openstreetmap.josm.tools.ImageProvider;
29
30/**
31 * DownloadAreaSelector which manages a list of "bookmarks", i.e. a list of
32 * name download areas.
33 *
34 */
35public class BookmarkSelection implements DownloadSelection {
36
37 /** the currently selected download area. One can add bookmarks for this
38 * area, if not null
39 */
40 private Bounds currentArea;
41 /** the list of bookmarks */
42 private BookmarkList bookmarks;
43
44 /** the parent download GUI */
45 private DownloadDialog parent;
46
47 /** displays information about the current download area */
48 private JMultilineLabel lblCurrentDownloadArea;
49 private final JosmTextArea bboxDisplay = new JosmTextArea();
50 /** the add action */
51 private AddAction actAdd;
52
53 /**
54 * Creates the panel with the action buttons on the left
55 *
56 * @return the panel with the action buttons on the left
57 */
58 protected JPanel buildButtonPanel() {
59 JPanel pnl = new JPanel();
60 pnl.setLayout(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();
82 pnl.setLayout(new GridBagLayout());
83
84 GridBagConstraints gc = new GridBagConstraints();
85 gc.anchor = GridBagConstraints.NORTHWEST;
86 gc.insets = new Insets(5,5,5,5);
87 pnl.add(lblCurrentDownloadArea = new JMultilineLabel(""), gc);
88
89 gc.weightx = 1.0;
90 gc.weighty = 1.0;
91 bboxDisplay.setEditable(false);
92 bboxDisplay.setBackground(pnl.getBackground());
93 bboxDisplay.addFocusListener(new BoundingBoxSelection.SelectAllOnFocusHandler(bboxDisplay));
94 pnl.add(bboxDisplay, gc);
95
96 gc.anchor = GridBagConstraints.NORTHEAST;
97 gc.fill = GridBagConstraints.HORIZONTAL;
98 gc.weightx = 0.0;
99 gc.weighty = 0.0;
100 gc.insets = new Insets(5,5,5,5);
101 pnl.add(new JButton(actAdd = new AddAction()), gc);
102 return pnl;
103 }
104
105 @Override
106 public void addGui(final DownloadDialog gui) {
107 JPanel dlg = new JPanel(new GridBagLayout());
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 = (Bookmark)bookmarks.getSelectedValue();
116 if (b != 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 public 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)bookmarks.getModel()).addElement(b);
204 bookmarks.save();
205 }
206 }
207 }
208
209 class RemoveAction extends AbstractAction implements ListSelectionListener{
210 public RemoveAction() {
211 putValue(SMALL_ICON, ImageProvider.get("dialogs", "delete"));
212 putValue(SHORT_DESCRIPTION, tr("Remove the currently selected bookmarks"));
213 updateEnabledState();
214 }
215
216 @Override
217 public void actionPerformed(ActionEvent e) {
218 Object[] sels = bookmarks.getSelectedValues();
219 if (sels == null || sels.length == 0)
220 return;
221 for (Object sel: sels) {
222 ((DefaultListModel)bookmarks.getModel()).removeElement(sel);
223 }
224 bookmarks.save();
225 }
226 protected void updateEnabledState() {
227 setEnabled(bookmarks.getSelectedIndices().length > 0);
228 }
229 @Override
230 public void valueChanged(ListSelectionEvent e) {
231 updateEnabledState();
232 }
233 }
234
235 class RenameAction extends AbstractAction implements ListSelectionListener{
236 public RenameAction() {
237 putValue(SMALL_ICON, ImageProvider.get("dialogs", "edit"));
238 putValue(SHORT_DESCRIPTION, tr("Rename the currently selected bookmark"));
239 updateEnabledState();
240 }
241
242 @Override
243 public void actionPerformed(ActionEvent e) {
244 Object[] sels = bookmarks.getSelectedValues();
245 if (sels == null || sels.length != 1)
246 return;
247 Bookmark b = (Bookmark)sels[0];
248 Object value =
249 JOptionPane.showInputDialog(
250 Main.parent,tr("Please enter a name for the bookmarked download area."),
251 tr("Name of location"),
252 JOptionPane.QUESTION_MESSAGE,
253 null,
254 null,
255 b.getName()
256 );
257 if (value != null) {
258 b.setName(value.toString());
259 bookmarks.save();
260 bookmarks.repaint();
261 }
262 }
263 protected void updateEnabledState() {
264 setEnabled(bookmarks.getSelectedIndices().length == 1);
265 }
266 @Override
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.