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

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

fix #9024 - bbox/bounds memory optimizations (modified patch by shinigami) + javadoc

  • Property svn:eol-style set to native
File size: 9.8 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.gui.BookmarkList;
26import org.openstreetmap.josm.gui.BookmarkList.Bookmark;
27import org.openstreetmap.josm.gui.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 final private 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 = (Bookmark)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)bookmarks.getModel()).addElement(b);
205 bookmarks.save();
206 }
207 }
208 }
209
210 class RemoveAction extends AbstractAction implements ListSelectionListener{
211 public RemoveAction() {
212 //putValue(NAME, tr("Remove"));
213 putValue(SMALL_ICON, ImageProvider.get("dialogs", "delete"));
214 putValue(SHORT_DESCRIPTION, tr("Remove the currently selected bookmarks"));
215 updateEnabledState();
216 }
217
218 @Override
219 public void actionPerformed(ActionEvent e) {
220 Object[] sels = bookmarks.getSelectedValues();
221 if (sels == null || sels.length == 0)
222 return;
223 for (Object sel: sels) {
224 ((DefaultListModel)bookmarks.getModel()).removeElement(sel);
225 }
226 bookmarks.save();
227 }
228 protected 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(NAME, tr("Remove"));
240 putValue(SMALL_ICON, ImageProvider.get("dialogs", "edit"));
241 putValue(SHORT_DESCRIPTION, tr("Rename the currently selected bookmark"));
242 updateEnabledState();
243 }
244
245 @Override
246 public void actionPerformed(ActionEvent e) {
247 Object[] sels = bookmarks.getSelectedValues();
248 if (sels == null || sels.length != 1)
249 return;
250 Bookmark b = (Bookmark)sels[0];
251 Object value =
252 JOptionPane.showInputDialog(
253 Main.parent,tr("Please enter a name for the bookmarked download area."),
254 tr("Name of location"),
255 JOptionPane.QUESTION_MESSAGE,
256 null,
257 null,
258 b.getName()
259 );
260 if (value != null) {
261 b.setName(value.toString());
262 bookmarks.save();
263 bookmarks.repaint();
264 }
265 }
266 protected void updateEnabledState() {
267 setEnabled(bookmarks.getSelectedIndices().length == 1);
268 }
269 @Override
270 public void valueChanged(ListSelectionEvent e) {
271 updateEnabledState();
272 }
273 }
274
275 class DoubleClickAdapter extends MouseAdapter {
276 @Override
277 public void mouseClicked(MouseEvent e) {
278 if (!(SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2))
279 return;
280 int idx = bookmarks.locationToIndex(e.getPoint());
281 if (idx < 0 || idx >= bookmarks.getModel().getSize())
282 return;
283 Bookmark b = (Bookmark)bookmarks.getModel().getElementAt(idx);
284 parent.startDownload(b.getArea());
285 }
286 }
287}
Note: See TracBrowser for help on using the repository browser.