1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.gui.download;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.GridBagConstraints;
|
---|
7 | import java.awt.GridBagLayout;
|
---|
8 | import java.awt.Insets;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.awt.event.MouseAdapter;
|
---|
11 | import java.awt.event.MouseEvent;
|
---|
12 |
|
---|
13 | import javax.swing.AbstractAction;
|
---|
14 | import javax.swing.DefaultListModel;
|
---|
15 | import javax.swing.JButton;
|
---|
16 | import javax.swing.JOptionPane;
|
---|
17 | import javax.swing.JPanel;
|
---|
18 | import javax.swing.JScrollPane;
|
---|
19 | import javax.swing.JTextArea;
|
---|
20 | import javax.swing.SwingUtilities;
|
---|
21 | import javax.swing.event.ListSelectionEvent;
|
---|
22 | import javax.swing.event.ListSelectionListener;
|
---|
23 |
|
---|
24 | import org.openstreetmap.josm.Main;
|
---|
25 | import org.openstreetmap.josm.data.Bounds;
|
---|
26 | import org.openstreetmap.josm.data.Preferences;
|
---|
27 | import org.openstreetmap.josm.data.coor.CoordinateFormat;
|
---|
28 | import org.openstreetmap.josm.gui.BookmarkList;
|
---|
29 | import org.openstreetmap.josm.gui.BookmarkList.Bookmark;
|
---|
30 | import org.openstreetmap.josm.gui.JMultilineLabel;
|
---|
31 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * DownloadAreaSelector which manages a list of "bookmarks", i.e. a list of
|
---|
35 | * name download areas.
|
---|
36 | *
|
---|
37 | */
|
---|
38 | public class BookmarkSelection implements DownloadSelection {
|
---|
39 |
|
---|
40 | /** the currently selected download area. One can add bookmarks for this
|
---|
41 | * area, if not null
|
---|
42 | */
|
---|
43 | private Bounds currentArea;
|
---|
44 | /** the list of bookmarks */
|
---|
45 | private BookmarkList bookmarks;
|
---|
46 |
|
---|
47 | /** the parent download GUI */
|
---|
48 | private DownloadDialog parent;
|
---|
49 |
|
---|
50 | /** displays information about the current download area */
|
---|
51 | private JMultilineLabel lblCurrentDownloadArea;
|
---|
52 | final private JTextArea bboxDisplay = new JTextArea();
|
---|
53 | /** the add action */
|
---|
54 | private AddAction actAdd;
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Creates the panel with the action buttons on the left
|
---|
58 | *
|
---|
59 | * @return the panel with the action buttons on the left
|
---|
60 | */
|
---|
61 | protected JPanel buildButtonPanel() {
|
---|
62 | JPanel pnl = new JPanel();
|
---|
63 | pnl.setLayout(new GridBagLayout());
|
---|
64 | GridBagConstraints gc = new GridBagConstraints();
|
---|
65 | gc.gridy = 0;
|
---|
66 | RemoveAction removeAction = new RemoveAction();
|
---|
67 | bookmarks.addListSelectionListener(removeAction);
|
---|
68 | pnl.add(new JButton(removeAction), gc);
|
---|
69 |
|
---|
70 | gc.gridy = 1;
|
---|
71 | RenameAction renameAction = new RenameAction();
|
---|
72 | bookmarks.addListSelectionListener(renameAction);
|
---|
73 | pnl.add(new JButton(renameAction), gc);
|
---|
74 |
|
---|
75 | gc.fill = GridBagConstraints.BOTH;
|
---|
76 | gc.weightx = 1.0;
|
---|
77 | gc.weighty = 1.0;
|
---|
78 | gc.gridy = 3;
|
---|
79 | pnl.add(new JPanel(), gc); // just a filler
|
---|
80 | return pnl;
|
---|
81 | }
|
---|
82 |
|
---|
83 | protected JPanel buildDownloadAreaAddPanel() {
|
---|
84 | JPanel pnl = new JPanel();
|
---|
85 | pnl.setLayout(new GridBagLayout());
|
---|
86 |
|
---|
87 | GridBagConstraints gc = new GridBagConstraints();
|
---|
88 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
89 | gc.insets = new Insets(5,5,5,5);
|
---|
90 | pnl.add(lblCurrentDownloadArea = new JMultilineLabel(""), gc);
|
---|
91 |
|
---|
92 | gc.weightx = 1.0;
|
---|
93 | gc.weighty = 1.0;
|
---|
94 | bboxDisplay.setEditable(false);
|
---|
95 | bboxDisplay.setBackground(pnl.getBackground());
|
---|
96 | bboxDisplay.addFocusListener(new BoundingBoxSelection.SelectAllOnFocusHandler(bboxDisplay));
|
---|
97 | pnl.add(bboxDisplay, gc);
|
---|
98 |
|
---|
99 | gc.anchor = GridBagConstraints.NORTHEAST;
|
---|
100 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
101 | gc.weightx = 0.0;
|
---|
102 | gc.weighty = 0.0;
|
---|
103 | gc.insets = new Insets(5,5,5,5);
|
---|
104 | pnl.add(new JButton(actAdd = new AddAction()), gc);
|
---|
105 | return pnl;
|
---|
106 | }
|
---|
107 |
|
---|
108 | public void addGui(final DownloadDialog gui) {
|
---|
109 | JPanel dlg = new JPanel(new GridBagLayout());
|
---|
110 | gui.addDownloadAreaSelector(dlg, tr("Bookmarks"));
|
---|
111 | GridBagConstraints gc = new GridBagConstraints();
|
---|
112 |
|
---|
113 | bookmarks = new BookmarkList();
|
---|
114 | bookmarks.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
|
---|
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(
|
---|
156 | currentArea.getMin().lonToString(CoordinateFormat.DECIMAL_DEGREES)+","+
|
---|
157 | currentArea.getMin().latToString(CoordinateFormat.DECIMAL_DEGREES)+","+
|
---|
158 | currentArea.getMax().lonToString(CoordinateFormat.DECIMAL_DEGREES)+","+
|
---|
159 | currentArea.getMax().latToString(CoordinateFormat.DECIMAL_DEGREES)
|
---|
160 | );
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Sets the current download area
|
---|
166 | *
|
---|
167 | * @param area the download area.
|
---|
168 | */
|
---|
169 | public void setDownloadArea(Bounds area) {
|
---|
170 | if (area == null) return;
|
---|
171 | this.currentArea = area;
|
---|
172 | bookmarks.clearSelection();
|
---|
173 | updateDownloadAreaLabel();
|
---|
174 | actAdd.setEnabled(true);
|
---|
175 | }
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * The action to add a new bookmark for the current download area.
|
---|
179 | *
|
---|
180 | */
|
---|
181 | class AddAction extends AbstractAction {
|
---|
182 | public AddAction() {
|
---|
183 | putValue(NAME, tr("Create bookmark"));
|
---|
184 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "bookmark-new"));
|
---|
185 | putValue(SHORT_DESCRIPTION, tr("Add a bookmark for the currently selected download area"));
|
---|
186 | }
|
---|
187 |
|
---|
188 | public void actionPerformed(ActionEvent e) {
|
---|
189 | if (currentArea == null) {
|
---|
190 | JOptionPane.showMessageDialog(
|
---|
191 | Main.parent,
|
---|
192 | tr("Currently, there is no download area selected. Please select an area first."),
|
---|
193 | tr("Information"),
|
---|
194 | JOptionPane.INFORMATION_MESSAGE
|
---|
195 | );
|
---|
196 | return;
|
---|
197 | }
|
---|
198 | Bookmark b = new Bookmark();
|
---|
199 | b.setName(
|
---|
200 | JOptionPane.showInputDialog(
|
---|
201 | Main.parent,tr("Please enter a name for the bookmarked download area."),
|
---|
202 | tr("Name of location"),
|
---|
203 | JOptionPane.QUESTION_MESSAGE)
|
---|
204 | );
|
---|
205 | b.setArea(currentArea);
|
---|
206 | if (b.getName() != null && !b.getName().equals("")) {
|
---|
207 | ((DefaultListModel)bookmarks.getModel()).addElement(b);
|
---|
208 | bookmarks.save();
|
---|
209 | }
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
213 | class RemoveAction extends AbstractAction implements ListSelectionListener{
|
---|
214 | public RemoveAction() {
|
---|
215 | //putValue(NAME, tr("Remove"));
|
---|
216 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "delete"));
|
---|
217 | putValue(SHORT_DESCRIPTION, tr("Remove the currently selected bookmarks"));
|
---|
218 | updateEnabledState();
|
---|
219 | }
|
---|
220 |
|
---|
221 | public void actionPerformed(ActionEvent e) {
|
---|
222 | Object[] sels = bookmarks.getSelectedValues();
|
---|
223 | if (sels == null || sels.length == 0)
|
---|
224 | return;
|
---|
225 | for (Object sel: sels) {
|
---|
226 | ((DefaultListModel)bookmarks.getModel()).removeElement(sel);
|
---|
227 | }
|
---|
228 | bookmarks.save();
|
---|
229 | }
|
---|
230 | protected void updateEnabledState() {
|
---|
231 | setEnabled(bookmarks.getSelectedIndices().length > 0);
|
---|
232 | }
|
---|
233 | public void valueChanged(ListSelectionEvent e) {
|
---|
234 | updateEnabledState();
|
---|
235 | }
|
---|
236 | }
|
---|
237 |
|
---|
238 | class RenameAction extends AbstractAction implements ListSelectionListener{
|
---|
239 | public RenameAction() {
|
---|
240 | //putValue(NAME, tr("Remove"));
|
---|
241 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "edit"));
|
---|
242 | putValue(SHORT_DESCRIPTION, tr("Rename the currently selected bookmark"));
|
---|
243 | updateEnabledState();
|
---|
244 | }
|
---|
245 |
|
---|
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 | public void valueChanged(ListSelectionEvent e) {
|
---|
270 | updateEnabledState();
|
---|
271 | }
|
---|
272 | }
|
---|
273 |
|
---|
274 | class DoubleClickAdapter extends MouseAdapter {
|
---|
275 | @Override
|
---|
276 | public void mouseClicked(MouseEvent e) {
|
---|
277 | if (!(SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2))
|
---|
278 | return;
|
---|
279 | int idx = bookmarks.locationToIndex(e.getPoint());
|
---|
280 | if (idx < 0 || idx >= bookmarks.getModel().getSize())
|
---|
281 | return;
|
---|
282 | Bookmark b = (Bookmark)bookmarks.getModel().getElementAt(idx);
|
---|
283 | parent.startDownload(b.getArea());
|
---|
284 | }
|
---|
285 | }
|
---|
286 | }
|
---|