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

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

fix more warnings, remove unused code

  • Property svn:eol-style set to native
File size: 9.8 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();
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.insets = new Insets(5,5,5,5);
88 pnl.add(lblCurrentDownloadArea = new JMultilineLabel(""), gc);
89
90 gc.weightx = 1.0;
91 gc.weighty = 1.0;
92 bboxDisplay.setEditable(false);
93 bboxDisplay.setBackground(pnl.getBackground());
94 bboxDisplay.addFocusListener(new BoundingBoxSelection.SelectAllOnFocusHandler(bboxDisplay));
95 pnl.add(bboxDisplay, gc);
96
97 gc.anchor = GridBagConstraints.NORTHEAST;
98 gc.fill = GridBagConstraints.HORIZONTAL;
99 gc.weightx = 0.0;
100 gc.weighty = 0.0;
101 gc.insets = new Insets(5,5,5,5);
102 pnl.add(new JButton(actAdd = new AddAction()), gc);
103 return pnl;
104 }
105
106 @Override
107 public void addGui(final DownloadDialog gui) {
108 JPanel dlg = new JPanel(new GridBagLayout());
109 gui.addDownloadAreaSelector(dlg, tr("Bookmarks"));
110 GridBagConstraints gc = new GridBagConstraints();
111
112 bookmarks = new BookmarkList();
113 bookmarks.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
114 @Override
115 public void valueChanged(ListSelectionEvent e) {
116 Bookmark b = bookmarks.getSelectedValue();
117 if (b != null) {
118 gui.boundingBoxChanged(b.getArea(),BookmarkSelection.this);
119 }
120 }
121 });
122 bookmarks.addMouseListener(new DoubleClickAdapter());
123
124 gc.fill = GridBagConstraints.HORIZONTAL;
125 gc.weightx = 1.0;
126 gc.weighty = 0.0;
127 gc.gridwidth = 2;
128 dlg.add(buildDownloadAreaAddPanel(),gc);
129
130 gc.gridwidth = 1;
131 gc.gridx = 0;
132 gc.gridy = 1;
133 gc.fill = GridBagConstraints.VERTICAL;
134 gc.weightx = 0.0;
135 gc.weighty = 1.0;
136 dlg.add(buildButtonPanel(),gc);
137
138 gc.gridwidth = 1;
139 gc.gridx = 1;
140 gc.gridy = 1;
141 gc.fill = GridBagConstraints.BOTH;
142 gc.weightx = 1.0;
143 gc.weighty = 1.0;
144 gc.gridx = 1;
145 dlg.add(new JScrollPane(bookmarks), gc);
146
147 this.parent = gui;
148 }
149
150 protected void updateDownloadAreaLabel() {
151 if (currentArea == null) {
152 lblCurrentDownloadArea.setText(tr("<html>There is currently no download area selected.</html>"));
153 } else {
154 lblCurrentDownloadArea.setText(tr("<html><strong>Current download area</strong> (minlon, minlat, maxlon, maxlat): </html>"));
155 bboxDisplay.setText(currentArea.toBBox().toStringCSV(","));
156 }
157 }
158
159 /**
160 * Sets the current download area
161 *
162 * @param area the download area.
163 */
164 @Override
165 public void setDownloadArea(Bounds area) {
166 if (area == null) return;
167 this.currentArea = area;
168 bookmarks.clearSelection();
169 updateDownloadAreaLabel();
170 actAdd.setEnabled(true);
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 @Override
185 public void actionPerformed(ActionEvent e) {
186 if (currentArea == null) {
187 JOptionPane.showMessageDialog(
188 Main.parent,
189 tr("Currently, there is no download area selected. Please select an area first."),
190 tr("Information"),
191 JOptionPane.INFORMATION_MESSAGE
192 );
193 return;
194 }
195 Bookmark b = new Bookmark();
196 b.setName(
197 JOptionPane.showInputDialog(
198 Main.parent,tr("Please enter a name for the bookmarked download area."),
199 tr("Name of location"),
200 JOptionPane.QUESTION_MESSAGE)
201 );
202 b.setArea(currentArea);
203 if (b.getName() != null && !b.getName().isEmpty()) {
204 ((DefaultListModel<BookmarkList.Bookmark>)bookmarks.getModel()).addElement(b);
205 bookmarks.save();
206 }
207 }
208 }
209
210 class RemoveAction extends AbstractAction implements ListSelectionListener{
211 public RemoveAction() {
212 putValue(SMALL_ICON, ImageProvider.get("dialogs", "delete"));
213 putValue(SHORT_DESCRIPTION, tr("Remove the currently selected bookmarks"));
214 updateEnabledState();
215 }
216
217 @Override
218 public void actionPerformed(ActionEvent e) {
219 List<Bookmark> sels = bookmarks.getSelectedValuesList();
220 if (sels == null || sels.isEmpty())
221 return;
222 for (Object sel: sels) {
223 ((DefaultListModel<Bookmark>)bookmarks.getModel()).removeElement(sel);
224 }
225 bookmarks.save();
226 }
227
228 protected final void updateEnabledState() {
229 setEnabled(bookmarks.getSelectedIndices().length > 0);
230 }
231 @Override
232 public void valueChanged(ListSelectionEvent e) {
233 updateEnabledState();
234 }
235 }
236
237 class RenameAction extends AbstractAction implements ListSelectionListener{
238 public RenameAction() {
239 putValue(SMALL_ICON, ImageProvider.get("dialogs", "edit"));
240 putValue(SHORT_DESCRIPTION, tr("Rename the currently selected bookmark"));
241 updateEnabledState();
242 }
243
244 @Override
245 public void actionPerformed(ActionEvent e) {
246 List<Bookmark> sels = bookmarks.getSelectedValuesList();
247 if (sels == null || sels.size() != 1)
248 return;
249 Bookmark b = sels.get(0);
250 Object value =
251 JOptionPane.showInputDialog(
252 Main.parent,tr("Please enter a name for the bookmarked download area."),
253 tr("Name of location"),
254 JOptionPane.QUESTION_MESSAGE,
255 null,
256 null,
257 b.getName()
258 );
259 if (value != null) {
260 b.setName(value.toString());
261 bookmarks.save();
262 bookmarks.repaint();
263 }
264 }
265
266 protected final void updateEnabledState() {
267 setEnabled(bookmarks.getSelectedIndices().length == 1);
268 }
269
270 @Override
271 public void valueChanged(ListSelectionEvent e) {
272 updateEnabledState();
273 }
274 }
275
276 class DoubleClickAdapter extends MouseAdapter {
277 @Override
278 public void mouseClicked(MouseEvent e) {
279 if (!(SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2))
280 return;
281 int idx = bookmarks.locationToIndex(e.getPoint());
282 if (idx < 0 || idx >= bookmarks.getModel().getSize())
283 return;
284 Bookmark b = bookmarks.getModel().getElementAt(idx);
285 parent.startDownload(b.getArea());
286 }
287 }
288}
Note: See TracBrowser for help on using the repository browser.