001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.download;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.GridBagConstraints;
007import java.awt.GridBagLayout;
008import java.awt.Insets;
009import java.awt.event.ActionEvent;
010import java.awt.event.MouseAdapter;
011import java.awt.event.MouseEvent;
012import java.util.List;
013
014import javax.swing.AbstractAction;
015import javax.swing.DefaultListModel;
016import javax.swing.JButton;
017import javax.swing.JOptionPane;
018import javax.swing.JPanel;
019import javax.swing.JScrollPane;
020import javax.swing.SwingUtilities;
021import javax.swing.event.ListSelectionEvent;
022import javax.swing.event.ListSelectionListener;
023
024import org.openstreetmap.josm.Main;
025import org.openstreetmap.josm.data.Bounds;
026import org.openstreetmap.josm.gui.download.BookmarkList.Bookmark;
027import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
028import org.openstreetmap.josm.gui.widgets.JosmTextArea;
029import org.openstreetmap.josm.tools.ImageProvider;
030
031/**
032 * DownloadAreaSelector which manages a list of "bookmarks", i.e. a list of
033 * name download areas.
034 *
035 */
036public class BookmarkSelection implements DownloadSelection {
037
038    /** the currently selected download area. One can add bookmarks for this
039     * area, if not null
040     */
041    private Bounds currentArea;
042    /** the list of bookmarks */
043    private BookmarkList bookmarks;
044
045    /** the parent download GUI */
046    private DownloadDialog parent;
047
048    /** displays information about the current download area */
049    private final JMultilineLabel lblCurrentDownloadArea = new JMultilineLabel("");
050    private final JosmTextArea bboxDisplay = new JosmTextArea();
051    /** the add action */
052    private final AddAction actAdd = new AddAction();
053
054    /**
055     * Creates the panel with the action buttons on the left
056     *
057     * @return the panel with the action buttons on the left
058     */
059    protected JPanel buildButtonPanel() {
060        JPanel pnl = new JPanel(new GridBagLayout());
061        GridBagConstraints gc = new GridBagConstraints();
062        gc.gridy = 0;
063        RemoveAction removeAction = new RemoveAction();
064        bookmarks.addListSelectionListener(removeAction);
065        pnl.add(new JButton(removeAction), gc);
066
067        gc.gridy = 1;
068        RenameAction renameAction = new RenameAction();
069        bookmarks.addListSelectionListener(renameAction);
070        pnl.add(new JButton(renameAction), gc);
071
072        gc.fill = GridBagConstraints.BOTH;
073        gc.weightx = 1.0;
074        gc.weighty = 1.0;
075        gc.gridy = 3;
076        pnl.add(new JPanel(), gc); // just a filler
077        return pnl;
078    }
079
080    protected JPanel buildDownloadAreaAddPanel() {
081        JPanel pnl = new JPanel(new GridBagLayout());
082
083        GridBagConstraints  gc = new GridBagConstraints();
084        gc.anchor = GridBagConstraints.NORTHWEST;
085        gc.insets = new Insets(5, 5, 5, 5);
086        pnl.add(lblCurrentDownloadArea, gc);
087
088        gc.weightx = 1.0;
089        gc.weighty = 1.0;
090        bboxDisplay.setEditable(false);
091        bboxDisplay.setBackground(pnl.getBackground());
092        bboxDisplay.addFocusListener(new BoundingBoxSelection.SelectAllOnFocusHandler(bboxDisplay));
093        pnl.add(bboxDisplay, gc);
094
095        gc.anchor = GridBagConstraints.NORTHEAST;
096        gc.fill = GridBagConstraints.HORIZONTAL;
097        gc.weightx = 0.0;
098        gc.weighty = 0.0;
099        gc.insets = new Insets(5, 5, 5, 5);
100        pnl.add(new JButton(actAdd), gc);
101        return pnl;
102    }
103
104    @Override
105    public void addGui(final DownloadDialog gui) {
106        JPanel dlg = new JPanel(new GridBagLayout());
107        if (gui != null)
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 = bookmarks.getSelectedValue();
116                if (b != null && gui != 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)
166            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        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        /**
212         * Constructs a new {@code RemoveAction}.
213         */
214        RemoveAction() {
215            putValue(SMALL_ICON, ImageProvider.get("dialogs", "delete"));
216            putValue(SHORT_DESCRIPTION, tr("Remove the currently selected bookmarks"));
217            updateEnabledState();
218        }
219
220        @Override
221        public void actionPerformed(ActionEvent e) {
222            List<Bookmark> sels = bookmarks.getSelectedValuesList();
223            if (sels == null || sels.isEmpty())
224                return;
225            for (Object sel: sels) {
226                ((DefaultListModel<Bookmark>) bookmarks.getModel()).removeElement(sel);
227            }
228            bookmarks.save();
229        }
230
231        protected final void updateEnabledState() {
232            setEnabled(bookmarks.getSelectedIndices().length > 0);
233        }
234
235        @Override
236        public void valueChanged(ListSelectionEvent e) {
237            updateEnabledState();
238        }
239    }
240
241    class RenameAction extends AbstractAction implements ListSelectionListener {
242        /**
243         * Constructs a new {@code RenameAction}.
244         */
245        RenameAction() {
246            putValue(SMALL_ICON, ImageProvider.get("dialogs", "edit"));
247            putValue(SHORT_DESCRIPTION, tr("Rename the currently selected bookmark"));
248            updateEnabledState();
249        }
250
251        @Override
252        public void actionPerformed(ActionEvent e) {
253            List<Bookmark> sels = bookmarks.getSelectedValuesList();
254            if (sels == null || sels.size() != 1)
255                return;
256            Bookmark b = sels.get(0);
257            Object value =
258                JOptionPane.showInputDialog(
259                        Main.parent, tr("Please enter a name for the bookmarked download area."),
260                        tr("Name of location"),
261                        JOptionPane.QUESTION_MESSAGE,
262                        null,
263                        null,
264                        b.getName()
265                );
266            if (value != null) {
267                b.setName(value.toString());
268                bookmarks.save();
269                bookmarks.repaint();
270            }
271        }
272
273        protected final void updateEnabledState() {
274            setEnabled(bookmarks.getSelectedIndices().length == 1);
275        }
276
277        @Override
278        public void valueChanged(ListSelectionEvent e) {
279            updateEnabledState();
280        }
281    }
282
283    class DoubleClickAdapter extends MouseAdapter {
284        @Override
285        public void mouseClicked(MouseEvent e) {
286            if (!(SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2))
287                return;
288            int idx = bookmarks.locationToIndex(e.getPoint());
289            if (idx < 0 || idx >= bookmarks.getModel().getSize())
290                return;
291            Bookmark b = bookmarks.getModel().getElementAt(idx);
292            parent.startDownload(b.getArea());
293        }
294    }
295}