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.GridBagLayout;
|
---|
7 | import java.awt.GridLayout;
|
---|
8 | import java.awt.event.ActionEvent;
|
---|
9 | import java.awt.event.ActionListener;
|
---|
10 |
|
---|
11 | import javax.swing.DefaultListModel;
|
---|
12 | import javax.swing.JButton;
|
---|
13 | import javax.swing.JOptionPane;
|
---|
14 | import javax.swing.JPanel;
|
---|
15 | import javax.swing.JScrollPane;
|
---|
16 | import javax.swing.event.ListSelectionEvent;
|
---|
17 | import javax.swing.event.ListSelectionListener;
|
---|
18 |
|
---|
19 | import org.openstreetmap.josm.Main;
|
---|
20 | import org.openstreetmap.josm.data.Bounds;
|
---|
21 | import org.openstreetmap.josm.data.Preferences;
|
---|
22 | import org.openstreetmap.josm.gui.BookmarkList;
|
---|
23 | import org.openstreetmap.josm.tools.GBC;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Bookmark selector.
|
---|
27 | *
|
---|
28 | * Provides selection, creation and deletion of bookmarks.
|
---|
29 | * Extracted from old DownloadAction.
|
---|
30 | *
|
---|
31 | * @author Frederik Ramm <frederik@remote.org>
|
---|
32 | *
|
---|
33 | */
|
---|
34 | public class BookmarkSelection implements DownloadSelection {
|
---|
35 |
|
---|
36 | private Preferences.Bookmark tempBookmark = null;
|
---|
37 | private BookmarkList bookmarks;
|
---|
38 |
|
---|
39 | public void addGui(final DownloadDialog gui) {
|
---|
40 |
|
---|
41 | JPanel dlg = new JPanel(new GridBagLayout());
|
---|
42 | gui.addDownloadAreaSelector(dlg, tr("Bookmarks"));
|
---|
43 |
|
---|
44 | bookmarks = new BookmarkList();
|
---|
45 | bookmarks.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
|
---|
46 | public void valueChanged(ListSelectionEvent e) {
|
---|
47 | Preferences.Bookmark b = (Preferences.Bookmark)bookmarks.getSelectedValue();
|
---|
48 | if (b != null) {
|
---|
49 | gui.boundingBoxChanged(b.asBounds(),BookmarkSelection.this);
|
---|
50 | }
|
---|
51 | }
|
---|
52 | });
|
---|
53 | //wc.addListMarker(bookmarks);
|
---|
54 | dlg.add(new JScrollPane(bookmarks), GBC.eol().fill());
|
---|
55 |
|
---|
56 | JPanel buttons = new JPanel(new GridLayout(1,2));
|
---|
57 | JButton add = new JButton(tr("Add"));
|
---|
58 | add.addActionListener(new ActionListener(){
|
---|
59 | public void actionPerformed(ActionEvent e) {
|
---|
60 |
|
---|
61 | if (tempBookmark == null) {
|
---|
62 | JOptionPane.showMessageDialog(
|
---|
63 | Main.parent,
|
---|
64 | tr("Please enter the desired coordinates first."),
|
---|
65 | tr("Information"),
|
---|
66 | JOptionPane.INFORMATION_MESSAGE
|
---|
67 | );
|
---|
68 | return;
|
---|
69 | }
|
---|
70 | tempBookmark.name = JOptionPane.showInputDialog(
|
---|
71 | Main.parent,tr("Please enter a name for the location."),
|
---|
72 | tr("Name of location"),
|
---|
73 | JOptionPane.QUESTION_MESSAGE
|
---|
74 | );
|
---|
75 | if (tempBookmark.name != null && !tempBookmark.name.equals("")) {
|
---|
76 | ((DefaultListModel)bookmarks.getModel()).addElement(tempBookmark);
|
---|
77 | bookmarks.save();
|
---|
78 | }
|
---|
79 | }
|
---|
80 | });
|
---|
81 | buttons.add(add);
|
---|
82 | JButton remove = new JButton(tr("Remove"));
|
---|
83 | remove.addActionListener(new ActionListener(){
|
---|
84 | public void actionPerformed(ActionEvent e) {
|
---|
85 | Object sel = bookmarks.getSelectedValue();
|
---|
86 | if (sel == null) {
|
---|
87 | JOptionPane.showMessageDialog(
|
---|
88 | Main.parent,
|
---|
89 | tr("Select a bookmark first."),
|
---|
90 | tr("Information"),
|
---|
91 | JOptionPane.INFORMATION_MESSAGE
|
---|
92 | );
|
---|
93 | return;
|
---|
94 | }
|
---|
95 | ((DefaultListModel)bookmarks.getModel()).removeElement(sel);
|
---|
96 | bookmarks.save();
|
---|
97 | }
|
---|
98 | });
|
---|
99 | buttons.add(remove);
|
---|
100 | dlg.add(buttons, GBC.eop().fill(GBC.HORIZONTAL));
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 | public void setDownloadArea(Bounds area) {
|
---|
105 | if (area == null) return;
|
---|
106 | tempBookmark = new Preferences.Bookmark(area);
|
---|
107 | bookmarks.clearSelection();
|
---|
108 | }
|
---|
109 | }
|
---|