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 | import java.awt.event.MouseListener;
|
---|
11 | import java.awt.event.MouseAdapter;
|
---|
12 | import java.awt.event.MouseEvent;
|
---|
13 |
|
---|
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;
|
---|
19 | import javax.swing.event.ListSelectionEvent;
|
---|
20 | import javax.swing.event.ListSelectionListener;
|
---|
21 |
|
---|
22 | import org.openstreetmap.josm.Main;
|
---|
23 | import org.openstreetmap.josm.data.Preferences;
|
---|
24 | import org.openstreetmap.josm.gui.BookmarkList;
|
---|
25 | import org.openstreetmap.josm.gui.OptionPaneUtil;
|
---|
26 | import org.openstreetmap.josm.tools.GBC;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Bookmark selector.
|
---|
30 | *
|
---|
31 | * Provides selection, creation and deletion of bookmarks.
|
---|
32 | * Extracted from old DownloadAction.
|
---|
33 | *
|
---|
34 | * @author Frederik Ramm <frederik@remote.org>
|
---|
35 | *
|
---|
36 | */
|
---|
37 | public class BookmarkSelection implements DownloadSelection {
|
---|
38 |
|
---|
39 | private Preferences.Bookmark tempBookmark = null;
|
---|
40 | private BookmarkList bookmarks;
|
---|
41 |
|
---|
42 | public void addGui(final DownloadDialog gui) {
|
---|
43 |
|
---|
44 | JPanel dlg = new JPanel(new GridBagLayout());
|
---|
45 | gui.tabpane.addTab(tr("Bookmarks"), dlg);
|
---|
46 |
|
---|
47 | bookmarks = new BookmarkList();
|
---|
48 |
|
---|
49 | /* add a handler for "double click" mouse events */
|
---|
50 | MouseListener mouseListener = new MouseAdapter() {
|
---|
51 | @Override public void mouseClicked(MouseEvent e) {
|
---|
52 | if (e.getClickCount() == 2) {
|
---|
53 | //int index = bookmarks.locationToIndex(e.getPoint());
|
---|
54 | gui.closeDownloadDialog(true);
|
---|
55 | }
|
---|
56 | }
|
---|
57 | };
|
---|
58 | bookmarks.addMouseListener(mouseListener);
|
---|
59 |
|
---|
60 | bookmarks.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
|
---|
61 | public void valueChanged(ListSelectionEvent e) {
|
---|
62 | Preferences.Bookmark b = (Preferences.Bookmark)bookmarks.getSelectedValue();
|
---|
63 | if (b != null) {
|
---|
64 | gui.minlat = b.latlon[0];
|
---|
65 | gui.minlon = b.latlon[1];
|
---|
66 | gui.maxlat = b.latlon[2];
|
---|
67 | gui.maxlon = b.latlon[3];
|
---|
68 | gui.boundingBoxChanged(BookmarkSelection.this);
|
---|
69 | }
|
---|
70 | }
|
---|
71 | });
|
---|
72 | //wc.addListMarker(bookmarks);
|
---|
73 | dlg.add(new JScrollPane(bookmarks), GBC.eol().fill());
|
---|
74 |
|
---|
75 | JPanel buttons = new JPanel(new GridLayout(1,2));
|
---|
76 | JButton add = new JButton(tr("Add"));
|
---|
77 | add.addActionListener(new ActionListener(){
|
---|
78 | public void actionPerformed(ActionEvent e) {
|
---|
79 |
|
---|
80 | if (tempBookmark == null) {
|
---|
81 | OptionPaneUtil.showMessageDialog(
|
---|
82 | Main.parent,
|
---|
83 | tr("Please enter the desired coordinates first."),
|
---|
84 | tr("Information"),
|
---|
85 | JOptionPane.INFORMATION_MESSAGE
|
---|
86 | );
|
---|
87 | return;
|
---|
88 | }
|
---|
89 | tempBookmark.name = OptionPaneUtil.showInputDialog(
|
---|
90 | Main.parent,tr("Please enter a name for the location."),
|
---|
91 | tr("Name of location"),
|
---|
92 | JOptionPane.QUESTION_MESSAGE
|
---|
93 | );
|
---|
94 | if (tempBookmark.name != null && !tempBookmark.name.equals("")) {
|
---|
95 | ((DefaultListModel)bookmarks.getModel()).addElement(tempBookmark);
|
---|
96 | bookmarks.save();
|
---|
97 | }
|
---|
98 | }
|
---|
99 | });
|
---|
100 | buttons.add(add);
|
---|
101 | JButton remove = new JButton(tr("Remove"));
|
---|
102 | remove.addActionListener(new ActionListener(){
|
---|
103 | public void actionPerformed(ActionEvent e) {
|
---|
104 | Object sel = bookmarks.getSelectedValue();
|
---|
105 | if (sel == null) {
|
---|
106 | OptionPaneUtil.showMessageDialog(
|
---|
107 | Main.parent,
|
---|
108 | tr("Select a bookmark first."),
|
---|
109 | tr("Information"),
|
---|
110 | JOptionPane.INFORMATION_MESSAGE
|
---|
111 | );
|
---|
112 | return;
|
---|
113 | }
|
---|
114 | ((DefaultListModel)bookmarks.getModel()).removeElement(sel);
|
---|
115 | bookmarks.save();
|
---|
116 | }
|
---|
117 | });
|
---|
118 | buttons.add(remove);
|
---|
119 | dlg.add(buttons, GBC.eop().fill(GBC.HORIZONTAL));
|
---|
120 | }
|
---|
121 |
|
---|
122 | public void boundingBoxChanged(DownloadDialog gui) {
|
---|
123 | tempBookmark = new Preferences.Bookmark();
|
---|
124 | tempBookmark.latlon[0] = gui.minlat;
|
---|
125 | tempBookmark.latlon[1] = gui.minlon;
|
---|
126 | tempBookmark.latlon[2] = gui.maxlat;
|
---|
127 | tempBookmark.latlon[3] = gui.maxlon;
|
---|
128 | bookmarks.clearSelection();
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | }
|
---|