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.tabpane.addTab("Bookmarks", dlg);
|
---|
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.minlat = b.latlon[0];
|
---|
49 | gui.minlon = b.latlon[1];
|
---|
50 | gui.maxlat = b.latlon[2];
|
---|
51 | gui.maxlon = b.latlon[3];
|
---|
52 | gui.boundingBoxChanged(BookmarkSelection.this);
|
---|
53 | }
|
---|
54 | }
|
---|
55 | });
|
---|
56 | //wc.addListMarker(bookmarks);
|
---|
57 | dlg.add(new JScrollPane(bookmarks), GBC.eol().fill());
|
---|
58 |
|
---|
59 | JPanel buttons = new JPanel(new GridLayout(1,2));
|
---|
60 | JButton add = new JButton(tr("Add"));
|
---|
61 | add.addActionListener(new ActionListener(){
|
---|
62 | public void actionPerformed(ActionEvent e) {
|
---|
63 |
|
---|
64 | if (tempBookmark == null) {
|
---|
65 | JOptionPane.showMessageDialog(Main.parent, tr("Please enter the desired coordinates first."));
|
---|
66 | return;
|
---|
67 | }
|
---|
68 | tempBookmark.name = JOptionPane.showInputDialog(Main.parent,tr("Please enter a name for the location."));
|
---|
69 | if (tempBookmark.name != null && !tempBookmark.name.equals("")) {
|
---|
70 | ((DefaultListModel)bookmarks.getModel()).addElement(tempBookmark);
|
---|
71 | bookmarks.save();
|
---|
72 | }
|
---|
73 | }
|
---|
74 | });
|
---|
75 | buttons.add(add);
|
---|
76 | JButton remove = new JButton(tr("Remove"));
|
---|
77 | remove.addActionListener(new ActionListener(){
|
---|
78 | public void actionPerformed(ActionEvent e) {
|
---|
79 | Object sel = bookmarks.getSelectedValue();
|
---|
80 | if (sel == null) {
|
---|
81 | JOptionPane.showMessageDialog(Main.parent,tr("Select a bookmark first."));
|
---|
82 | return;
|
---|
83 | }
|
---|
84 | ((DefaultListModel)bookmarks.getModel()).removeElement(sel);
|
---|
85 | bookmarks.save();
|
---|
86 | }
|
---|
87 | });
|
---|
88 | buttons.add(remove);
|
---|
89 | dlg.add(buttons, GBC.eop().fill(GBC.HORIZONTAL));
|
---|
90 | }
|
---|
91 |
|
---|
92 | public void boundingBoxChanged(DownloadDialog gui) {
|
---|
93 | tempBookmark = new Preferences.Bookmark();
|
---|
94 | tempBookmark.latlon[0] = gui.minlat;
|
---|
95 | tempBookmark.latlon[1] = gui.minlon;
|
---|
96 | tempBookmark.latlon[2] = gui.maxlat;
|
---|
97 | tempBookmark.latlon[3] = gui.maxlon;
|
---|
98 | bookmarks.clearSelection();
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | }
|
---|