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