[298] | 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
[237] | 2 | package org.openstreetmap.josm.gui.download;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
[2334] | 6 | import java.awt.GridBagConstraints;
|
---|
[237] | 7 | import java.awt.GridBagLayout;
|
---|
[2344] | 8 | import java.awt.Insets;
|
---|
[237] | 9 | import java.awt.event.ActionEvent;
|
---|
[2344] | 10 | import java.awt.event.MouseAdapter;
|
---|
| 11 | import java.awt.event.MouseEvent;
|
---|
[237] | 12 |
|
---|
[2334] | 13 | import javax.swing.AbstractAction;
|
---|
[237] | 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;
|
---|
[2344] | 19 | import javax.swing.SwingUtilities;
|
---|
[237] | 20 | import javax.swing.event.ListSelectionEvent;
|
---|
| 21 | import javax.swing.event.ListSelectionListener;
|
---|
| 22 |
|
---|
| 23 | import org.openstreetmap.josm.Main;
|
---|
[2332] | 24 | import org.openstreetmap.josm.data.Bounds;
|
---|
[237] | 25 | import org.openstreetmap.josm.gui.BookmarkList;
|
---|
[3527] | 26 | import org.openstreetmap.josm.gui.BookmarkList.Bookmark;
|
---|
[2344] | 27 | import org.openstreetmap.josm.gui.JMultilineLabel;
|
---|
[5886] | 28 | import org.openstreetmap.josm.gui.widgets.JosmTextArea;
|
---|
[2334] | 29 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
[237] | 30 |
|
---|
| 31 | /**
|
---|
[2344] | 32 | * DownloadAreaSelector which manages a list of "bookmarks", i.e. a list of
|
---|
| 33 | * name download areas.
|
---|
[1169] | 34 | *
|
---|
[237] | 35 | */
|
---|
| 36 | public class BookmarkSelection implements DownloadSelection {
|
---|
| 37 |
|
---|
[2344] | 38 | /** the currently selected download area. One can add bookmarks for this
|
---|
| 39 | * area, if not null
|
---|
| 40 | */
|
---|
[2334] | 41 | private Bounds currentArea;
|
---|
[2344] | 42 | /** the list of bookmarks */
|
---|
[1169] | 43 | private BookmarkList bookmarks;
|
---|
[2512] | 44 |
|
---|
[2344] | 45 | /** the parent download GUI */
|
---|
| 46 | private DownloadDialog parent;
|
---|
[2512] | 47 |
|
---|
[2344] | 48 | /** displays information about the current download area */
|
---|
| 49 | private JMultilineLabel lblCurrentDownloadArea;
|
---|
[5886] | 50 | final private JosmTextArea bboxDisplay = new JosmTextArea();
|
---|
[2344] | 51 | /** the add action */
|
---|
| 52 | private AddAction actAdd;
|
---|
[2512] | 53 |
|
---|
[2344] | 54 | /**
|
---|
[2512] | 55 | * Creates the panel with the action buttons on the left
|
---|
| 56 | *
|
---|
[2344] | 57 | * @return the panel with the action buttons on the left
|
---|
| 58 | */
|
---|
[2334] | 59 | protected JPanel buildButtonPanel() {
|
---|
| 60 | JPanel pnl = new JPanel();
|
---|
| 61 | pnl.setLayout(new GridBagLayout());
|
---|
| 62 | GridBagConstraints gc = new GridBagConstraints();
|
---|
[2344] | 63 | gc.gridy = 0;
|
---|
[2334] | 64 | RemoveAction removeAction = new RemoveAction();
|
---|
| 65 | bookmarks.addListSelectionListener(removeAction);
|
---|
| 66 | pnl.add(new JButton(removeAction), gc);
|
---|
| 67 |
|
---|
[2344] | 68 | gc.gridy = 1;
|
---|
[2334] | 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 | }
|
---|
[2512] | 80 |
|
---|
[2344] | 81 | protected JPanel buildDownloadAreaAddPanel() {
|
---|
| 82 | JPanel pnl = new JPanel();
|
---|
| 83 | pnl.setLayout(new GridBagLayout());
|
---|
[2512] | 84 |
|
---|
[2344] | 85 | GridBagConstraints gc = new GridBagConstraints();
|
---|
| 86 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
[2891] | 87 | gc.insets = new Insets(5,5,5,5);
|
---|
| 88 | pnl.add(lblCurrentDownloadArea = new JMultilineLabel(""), gc);
|
---|
[3530] | 89 |
|
---|
[2344] | 90 | gc.weightx = 1.0;
|
---|
| 91 | gc.weighty = 1.0;
|
---|
[2891] | 92 | bboxDisplay.setEditable(false);
|
---|
| 93 | bboxDisplay.setBackground(pnl.getBackground());
|
---|
| 94 | bboxDisplay.addFocusListener(new BoundingBoxSelection.SelectAllOnFocusHandler(bboxDisplay));
|
---|
| 95 | pnl.add(bboxDisplay, gc);
|
---|
[2512] | 96 |
|
---|
[2344] | 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 | }
|
---|
[2512] | 105 |
|
---|
[6084] | 106 | @Override
|
---|
[1169] | 107 | public void addGui(final DownloadDialog gui) {
|
---|
| 108 | JPanel dlg = new JPanel(new GridBagLayout());
|
---|
[2327] | 109 | gui.addDownloadAreaSelector(dlg, tr("Bookmarks"));
|
---|
[2334] | 110 | GridBagConstraints gc = new GridBagConstraints();
|
---|
[2512] | 111 |
|
---|
[1169] | 112 | bookmarks = new BookmarkList();
|
---|
| 113 | bookmarks.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
|
---|
[6084] | 114 | @Override
|
---|
[1169] | 115 | public void valueChanged(ListSelectionEvent e) {
|
---|
[3527] | 116 | Bookmark b = (Bookmark)bookmarks.getSelectedValue();
|
---|
[1169] | 117 | if (b != null) {
|
---|
[2334] | 118 | gui.boundingBoxChanged(b.getArea(),BookmarkSelection.this);
|
---|
[1169] | 119 | }
|
---|
| 120 | }
|
---|
| 121 | });
|
---|
[2344] | 122 | bookmarks.addMouseListener(new DoubleClickAdapter());
|
---|
[2512] | 123 |
|
---|
[2344] | 124 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
| 125 | gc.weightx = 1.0;
|
---|
[2512] | 126 | gc.weighty = 0.0;
|
---|
[2344] | 127 | gc.gridwidth = 2;
|
---|
| 128 | dlg.add(buildDownloadAreaAddPanel(),gc);
|
---|
[2512] | 129 |
|
---|
[2344] | 130 | gc.gridwidth = 1;
|
---|
| 131 | gc.gridx = 0;
|
---|
| 132 | gc.gridy = 1;
|
---|
[2334] | 133 | gc.fill = GridBagConstraints.VERTICAL;
|
---|
| 134 | gc.weightx = 0.0;
|
---|
[2512] | 135 | gc.weighty = 1.0;
|
---|
[2334] | 136 | dlg.add(buildButtonPanel(),gc);
|
---|
[2344] | 137 |
|
---|
| 138 | gc.gridwidth = 1;
|
---|
| 139 | gc.gridx = 1;
|
---|
| 140 | gc.gridy = 1;
|
---|
[2334] | 141 | gc.fill = GridBagConstraints.BOTH;
|
---|
| 142 | gc.weightx = 1.0;
|
---|
[2512] | 143 | gc.weighty = 1.0;
|
---|
[2334] | 144 | gc.gridx = 1;
|
---|
[2512] | 145 | dlg.add(new JScrollPane(bookmarks), gc);
|
---|
| 146 |
|
---|
[2344] | 147 | this.parent = gui;
|
---|
[2334] | 148 | }
|
---|
[2512] | 149 |
|
---|
[2344] | 150 | protected void updateDownloadAreaLabel() {
|
---|
| 151 | if (currentArea == null) {
|
---|
| 152 | lblCurrentDownloadArea.setText(tr("<html>There is currently no download area selected.</html>"));
|
---|
| 153 | } else {
|
---|
[2904] | 154 | lblCurrentDownloadArea.setText(tr("<html><strong>Current download area</strong> (minlon, minlat, maxlon, maxlat): </html>"));
|
---|
[6203] | 155 | bboxDisplay.setText(currentArea.toBBox().toStringCSV(","));
|
---|
[2344] | 156 | }
|
---|
| 157 | }
|
---|
[2512] | 158 |
|
---|
[2344] | 159 | /**
|
---|
[2512] | 160 | * Sets the current download area
|
---|
| 161 | *
|
---|
| 162 | * @param area the download area.
|
---|
[2344] | 163 | */
|
---|
[6084] | 164 | @Override
|
---|
[2334] | 165 | public void setDownloadArea(Bounds area) {
|
---|
| 166 | if (area == null) return;
|
---|
| 167 | this.currentArea = area;
|
---|
| 168 | bookmarks.clearSelection();
|
---|
[2344] | 169 | updateDownloadAreaLabel();
|
---|
[2626] | 170 | actAdd.setEnabled(true);
|
---|
[2334] | 171 | }
|
---|
[2512] | 172 |
|
---|
[2344] | 173 | /**
|
---|
| 174 | * The action to add a new bookmark for the current download area.
|
---|
| 175 | *
|
---|
| 176 | */
|
---|
[2334] | 177 | class AddAction extends AbstractAction {
|
---|
| 178 | public AddAction() {
|
---|
[2344] | 179 | putValue(NAME, tr("Create bookmark"));
|
---|
| 180 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "bookmark-new"));
|
---|
[2334] | 181 | putValue(SHORT_DESCRIPTION, tr("Add a bookmark for the currently selected download area"));
|
---|
| 182 | }
|
---|
[2512] | 183 |
|
---|
[6084] | 184 | @Override
|
---|
[2334] | 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
|
---|
[1865] | 192 | );
|
---|
[2334] | 193 | return;
|
---|
[1169] | 194 | }
|
---|
[2334] | 195 | Bookmark b = new Bookmark();
|
---|
| 196 | b.setName(
|
---|
| 197 | JOptionPane.showInputDialog(
|
---|
[2626] | 198 | Main.parent,tr("Please enter a name for the bookmarked download area."),
|
---|
| 199 | tr("Name of location"),
|
---|
| 200 | JOptionPane.QUESTION_MESSAGE)
|
---|
[2334] | 201 | );
|
---|
| 202 | b.setArea(currentArea);
|
---|
[6087] | 203 | if (b.getName() != null && !b.getName().isEmpty()) {
|
---|
[2334] | 204 | ((DefaultListModel)bookmarks.getModel()).addElement(b);
|
---|
[1169] | 205 | bookmarks.save();
|
---|
[2512] | 206 | }
|
---|
[2334] | 207 | }
|
---|
| 208 | }
|
---|
[2512] | 209 |
|
---|
[2334] | 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 | }
|
---|
[2512] | 216 |
|
---|
[6084] | 217 | @Override
|
---|
[2334] | 218 | public void actionPerformed(ActionEvent e) {
|
---|
| 219 | Object[] sels = bookmarks.getSelectedValues();
|
---|
[2626] | 220 | if (sels == null || sels.length == 0)
|
---|
[2334] | 221 | return;
|
---|
| 222 | for (Object sel: sels) {
|
---|
| 223 | ((DefaultListModel)bookmarks.getModel()).removeElement(sel);
|
---|
| 224 | }
|
---|
| 225 | bookmarks.save();
|
---|
| 226 | }
|
---|
| 227 | protected void updateEnabledState() {
|
---|
| 228 | setEnabled(bookmarks.getSelectedIndices().length > 0);
|
---|
| 229 | }
|
---|
[6084] | 230 | @Override
|
---|
[2334] | 231 | public void valueChanged(ListSelectionEvent e) {
|
---|
| 232 | updateEnabledState();
|
---|
[2512] | 233 | }
|
---|
[1169] | 234 | }
|
---|
[2512] | 235 |
|
---|
[2334] | 236 | class RenameAction extends AbstractAction implements ListSelectionListener{
|
---|
| 237 | public RenameAction() {
|
---|
| 238 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "edit"));
|
---|
| 239 | putValue(SHORT_DESCRIPTION, tr("Rename the currently selected bookmark"));
|
---|
| 240 | updateEnabledState();
|
---|
| 241 | }
|
---|
[2512] | 242 |
|
---|
[6084] | 243 | @Override
|
---|
[2334] | 244 | public void actionPerformed(ActionEvent e) {
|
---|
| 245 | Object[] sels = bookmarks.getSelectedValues();
|
---|
[2626] | 246 | if (sels == null || sels.length != 1)
|
---|
[2334] | 247 | return;
|
---|
| 248 | Bookmark b = (Bookmark)sels[0];
|
---|
[2512] | 249 | Object value =
|
---|
[2626] | 250 | JOptionPane.showInputDialog(
|
---|
| 251 | Main.parent,tr("Please enter a name for the bookmarked download area."),
|
---|
| 252 | tr("Name of location"),
|
---|
| 253 | JOptionPane.QUESTION_MESSAGE,
|
---|
| 254 | null,
|
---|
| 255 | null,
|
---|
| 256 | b.getName()
|
---|
| 257 | );
|
---|
[2334] | 258 | if (value != null) {
|
---|
[2512] | 259 | b.setName(value.toString());
|
---|
[2334] | 260 | bookmarks.save();
|
---|
| 261 | bookmarks.repaint();
|
---|
| 262 | }
|
---|
| 263 | }
|
---|
| 264 | protected void updateEnabledState() {
|
---|
| 265 | setEnabled(bookmarks.getSelectedIndices().length == 1);
|
---|
| 266 | }
|
---|
[6084] | 267 | @Override
|
---|
[2334] | 268 | public void valueChanged(ListSelectionEvent e) {
|
---|
| 269 | updateEnabledState();
|
---|
[2512] | 270 | }
|
---|
[1169] | 271 | }
|
---|
[2512] | 272 |
|
---|
[2344] | 273 | class DoubleClickAdapter extends MouseAdapter {
|
---|
| 274 | @Override
|
---|
| 275 | public void mouseClicked(MouseEvent e) {
|
---|
[2512] | 276 | if (!(SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2))
|
---|
[2344] | 277 | return;
|
---|
| 278 | int idx = bookmarks.locationToIndex(e.getPoint());
|
---|
| 279 | if (idx < 0 || idx >= bookmarks.getModel().getSize())
|
---|
| 280 | return;
|
---|
| 281 | Bookmark b = (Bookmark)bookmarks.getModel().getElementAt(idx);
|
---|
[2512] | 282 | parent.startDownload(b.getArea());
|
---|
| 283 | }
|
---|
| 284 | }
|
---|
[237] | 285 | }
|
---|