1 | package org.openstreetmap.josm.gui.dialogs;
|
---|
2 |
|
---|
3 | import java.awt.BorderLayout;
|
---|
4 | import java.awt.Dimension;
|
---|
5 | import java.awt.GridBagLayout;
|
---|
6 | import java.awt.GridLayout;
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.ActionListener;
|
---|
9 | import java.awt.event.KeyEvent;
|
---|
10 | import java.awt.event.MouseAdapter;
|
---|
11 | import java.awt.event.MouseEvent;
|
---|
12 | import java.io.IOException;
|
---|
13 | import java.io.Reader;
|
---|
14 | import java.net.MalformedURLException;
|
---|
15 | import java.net.URL;
|
---|
16 | import java.net.URLConnection;
|
---|
17 | import java.util.Collection;
|
---|
18 | import java.util.LinkedList;
|
---|
19 | import java.util.Map;
|
---|
20 | import java.util.TreeSet;
|
---|
21 |
|
---|
22 | import javax.swing.ButtonGroup;
|
---|
23 | import javax.swing.DefaultListModel;
|
---|
24 | import javax.swing.JButton;
|
---|
25 | import javax.swing.JLabel;
|
---|
26 | import javax.swing.JList;
|
---|
27 | import javax.swing.JOptionPane;
|
---|
28 | import javax.swing.JPanel;
|
---|
29 | import javax.swing.JRadioButton;
|
---|
30 | import javax.swing.JScrollPane;
|
---|
31 | import javax.swing.JTextField;
|
---|
32 | import javax.swing.ListSelectionModel;
|
---|
33 |
|
---|
34 | import org.jdom.JDOMException;
|
---|
35 | import org.openstreetmap.josm.Main;
|
---|
36 | import org.openstreetmap.josm.data.SelectionChangedListener;
|
---|
37 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
38 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
39 | import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
|
---|
40 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
41 | import org.openstreetmap.josm.io.OsmIdReader;
|
---|
42 | import org.openstreetmap.josm.io.ProgressReader;
|
---|
43 | import org.openstreetmap.josm.tools.GBC;
|
---|
44 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
45 | import org.openstreetmap.josm.tools.SearchCompiler;
|
---|
46 | import org.xml.sax.SAXException;
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * A small tool dialog for displaying the current selection. The selection manager
|
---|
50 | * respects clicks into the selection list. Ctrl-click will remove entries from
|
---|
51 | * the list while single click will make the clicked entry the only selection.
|
---|
52 | *
|
---|
53 | * @author imi
|
---|
54 | */
|
---|
55 | public class SelectionListDialog extends ToggleDialog implements SelectionChangedListener {
|
---|
56 |
|
---|
57 | public static enum SearchMode {replace, add, remove}
|
---|
58 |
|
---|
59 | private static class SelectionWebsiteLoader extends PleaseWaitRunnable {
|
---|
60 | public final URL url;
|
---|
61 | public Collection<OsmPrimitive> sel;
|
---|
62 | private final SearchMode mode;
|
---|
63 | private OsmIdReader idReader = new OsmIdReader();
|
---|
64 | public SelectionWebsiteLoader(String urlStr, SearchMode mode) {
|
---|
65 | super("Load Selection");
|
---|
66 | this.mode = mode;
|
---|
67 | URL u = null;
|
---|
68 | try {u = new URL(urlStr);} catch (MalformedURLException e) {}
|
---|
69 | this.url = u;
|
---|
70 | }
|
---|
71 | @Override protected void realRun() throws SAXException, JDOMException, IOException {
|
---|
72 | currentAction.setText("Contact "+url.getHost()+"...");
|
---|
73 | sel = mode != SearchMode.remove ? new LinkedList<OsmPrimitive>() : Main.ds.allNonDeletedPrimitives();
|
---|
74 | try {
|
---|
75 | URLConnection con = url.openConnection();
|
---|
76 | Reader in = new ProgressReader(con, progress);
|
---|
77 | currentAction.setText("Downloading...");
|
---|
78 | Map<Long, String> ids = idReader.parseIds(in);
|
---|
79 | for (OsmPrimitive osm : Main.ds.allNonDeletedPrimitives()) {
|
---|
80 | if (ids.containsKey(osm.id) && osm.getClass().getName().toLowerCase().endsWith(ids.get(osm.id))) {
|
---|
81 | if (mode == SearchMode.remove)
|
---|
82 | sel.remove(osm);
|
---|
83 | else
|
---|
84 | sel.add(osm);
|
---|
85 | }
|
---|
86 | }
|
---|
87 | } catch (IOException e) {
|
---|
88 | e.printStackTrace();
|
---|
89 | JOptionPane.showMessageDialog(Main.parent, "Could not read from url: '"+url+"'");
|
---|
90 | } catch (SAXException e) {
|
---|
91 | e.printStackTrace();
|
---|
92 | JOptionPane.showMessageDialog(Main.parent, "Parsing error in url: '"+url+"'");
|
---|
93 | }
|
---|
94 | }
|
---|
95 | @Override protected void cancel() {
|
---|
96 | sel = null;
|
---|
97 | idReader.cancel();
|
---|
98 | }
|
---|
99 | @Override protected void finish() {
|
---|
100 | if (sel != null)
|
---|
101 | Main.ds.setSelected(sel);
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * The selection's list data.
|
---|
107 | */
|
---|
108 | private final DefaultListModel list = new DefaultListModel();
|
---|
109 | /**
|
---|
110 | * The display list.
|
---|
111 | */
|
---|
112 | private JList displaylist = new JList(list);
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Create a SelectionList dialog.
|
---|
116 | * @param mapView The mapView to get the dataset from.
|
---|
117 | */
|
---|
118 | public SelectionListDialog(MapFrame mapFrame) {
|
---|
119 | super("Current Selection", "selectionlist", "Open a selection list window.", KeyEvent.VK_E);
|
---|
120 | setPreferredSize(new Dimension(320,150));
|
---|
121 | displaylist.setCellRenderer(new OsmPrimitivRenderer());
|
---|
122 | displaylist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
|
---|
123 | displaylist.addMouseListener(new MouseAdapter(){
|
---|
124 | @Override public void mouseClicked(MouseEvent e) {
|
---|
125 | if (e.getClickCount() < 2)
|
---|
126 | return;
|
---|
127 | updateMap();
|
---|
128 | }
|
---|
129 | });
|
---|
130 |
|
---|
131 | add(new JScrollPane(displaylist), BorderLayout.CENTER);
|
---|
132 |
|
---|
133 | JPanel buttonPanel = new JPanel(new GridLayout(1,2));
|
---|
134 |
|
---|
135 | JButton button = new JButton("Select", ImageProvider.get("mapmode", "selection"));
|
---|
136 | button.setToolTipText("Set the selected elements on the map to the selected items in the list above.");
|
---|
137 | button.addActionListener(new ActionListener(){
|
---|
138 | public void actionPerformed(ActionEvent e) {
|
---|
139 | updateMap();
|
---|
140 | }
|
---|
141 | });
|
---|
142 | buttonPanel.add(button);
|
---|
143 |
|
---|
144 | button = new JButton("Reload", ImageProvider.get("dialogs", "refresh"));
|
---|
145 | button.setToolTipText("Refresh the selection list.");
|
---|
146 | button.addActionListener(new ActionListener(){
|
---|
147 | public void actionPerformed(ActionEvent e) {
|
---|
148 | selectionChanged(Main.ds.getSelected());
|
---|
149 | }
|
---|
150 | });
|
---|
151 | buttonPanel.add(button);
|
---|
152 |
|
---|
153 | button = new JButton("Search", ImageProvider.get("dialogs", "search"));
|
---|
154 | button.setToolTipText("Search for objects.");
|
---|
155 | button.addActionListener(new ActionListener(){
|
---|
156 | private String lastSearch = "";
|
---|
157 | public void actionPerformed(ActionEvent e) {
|
---|
158 | JLabel label = new JLabel("Please enter a search string.");
|
---|
159 | final JTextField input = new JTextField(lastSearch);
|
---|
160 | input.setToolTipText("<html>Fulltext search.<ul>" +
|
---|
161 | "<li><code>Baker Street</code> - 'Baker' and 'Street' in any key or name.</li>" +
|
---|
162 | "<li><code>\"Baker Street\"</code> - 'Baker Street' in any key or name.</li>" +
|
---|
163 | "<li><code>name:Bak</code> - 'Bak' anywhere in the name.</li>" +
|
---|
164 | "<li><code>-name:Bak</code> - not 'Bak' in the name.</li>" +
|
---|
165 | "<li><code>foot:</code> - key=foot set to any value." +
|
---|
166 | "</ul></html>");
|
---|
167 |
|
---|
168 | JRadioButton replace = new JRadioButton("replace selection", true);
|
---|
169 | JRadioButton add = new JRadioButton("add to selection", false);
|
---|
170 | JRadioButton remove = new JRadioButton("remove from selection", false);
|
---|
171 | ButtonGroup bg = new ButtonGroup();
|
---|
172 | bg.add(replace);
|
---|
173 | bg.add(add);
|
---|
174 | bg.add(remove);
|
---|
175 |
|
---|
176 | JPanel p = new JPanel(new GridBagLayout());
|
---|
177 | p.add(label, GBC.eop());
|
---|
178 | p.add(input, GBC.eop().fill(GBC.HORIZONTAL));
|
---|
179 | p.add(replace, GBC.eol());
|
---|
180 | p.add(add, GBC.eol());
|
---|
181 | p.add(remove, GBC.eol());
|
---|
182 | JOptionPane pane = new JOptionPane(p, JOptionPane.INFORMATION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null){
|
---|
183 | @Override public void selectInitialValue() {
|
---|
184 | input.requestFocusInWindow();
|
---|
185 | }
|
---|
186 | };
|
---|
187 | pane.createDialog(Main.parent, "Search").setVisible(true);
|
---|
188 | if (!Integer.valueOf(JOptionPane.OK_OPTION).equals(pane.getValue()))
|
---|
189 | return;
|
---|
190 | lastSearch = input.getText();
|
---|
191 | SearchMode mode = replace.isSelected() ? SearchMode.replace : (add.isSelected() ? SearchMode.add : SearchMode.remove);
|
---|
192 | search(lastSearch, mode);
|
---|
193 | }
|
---|
194 | });
|
---|
195 | buttonPanel.add(button);
|
---|
196 |
|
---|
197 | add(buttonPanel, BorderLayout.SOUTH);
|
---|
198 | selectionChanged(Main.ds.getSelected());
|
---|
199 | }
|
---|
200 |
|
---|
201 | @Override public void setVisible(boolean b) {
|
---|
202 | if (b) {
|
---|
203 | Main.ds.addSelectionChangedListener(this);
|
---|
204 | selectionChanged(Main.ds.getSelected());
|
---|
205 | } else {
|
---|
206 | Main.ds.removeSelectionChangedListener(this);
|
---|
207 | }
|
---|
208 | super.setVisible(b);
|
---|
209 | }
|
---|
210 |
|
---|
211 |
|
---|
212 |
|
---|
213 | /**
|
---|
214 | * Called when the selection in the dataset changed.
|
---|
215 | * @param newSelection The new selection array.
|
---|
216 | */
|
---|
217 | public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
|
---|
218 | TreeSet<OsmPrimitive> sortedSel = new TreeSet<OsmPrimitive>(newSelection);
|
---|
219 | if (list == null)
|
---|
220 | return; // selection changed may be received in base class constructor before init
|
---|
221 | list.setSize(sortedSel.size());
|
---|
222 | int i = 0;
|
---|
223 | for (OsmPrimitive osm : sortedSel)
|
---|
224 | list.setElementAt(osm, i++);
|
---|
225 | }
|
---|
226 |
|
---|
227 | /**
|
---|
228 | * Sets the selection of the map to the current selected items.
|
---|
229 | */
|
---|
230 | public void updateMap() {
|
---|
231 | Collection<OsmPrimitive> sel = new LinkedList<OsmPrimitive>();
|
---|
232 | for (int i = 0; i < list.getSize(); ++i)
|
---|
233 | if (displaylist.isSelectedIndex(i))
|
---|
234 | sel.add((OsmPrimitive)list.get(i));
|
---|
235 | Main.ds.setSelected(sel);
|
---|
236 | }
|
---|
237 |
|
---|
238 | public static void search(String search, SearchMode mode) {
|
---|
239 | if (search.startsWith("http://") || search.startsWith("ftp://") || search.startsWith("https://") || search.startsWith("file:/")) {
|
---|
240 | SelectionWebsiteLoader loader = new SelectionWebsiteLoader(search, mode);
|
---|
241 | if (loader.url != null) {
|
---|
242 | Main.worker.execute(loader);
|
---|
243 | loader.pleaseWaitDlg.setVisible(true);
|
---|
244 | return;
|
---|
245 | }
|
---|
246 | }
|
---|
247 | Collection<OsmPrimitive> sel = Main.ds.getSelected();
|
---|
248 | SearchCompiler.Match matcher = SearchCompiler.compile(search);
|
---|
249 | for (OsmPrimitive osm : Main.ds.allNonDeletedPrimitives()) {
|
---|
250 | if (mode == SearchMode.replace) {
|
---|
251 | if (matcher.match(osm))
|
---|
252 | sel.add(osm);
|
---|
253 | else
|
---|
254 | sel.remove(osm);
|
---|
255 | } else if (mode == SearchMode.add && !osm.selected && matcher.match(osm))
|
---|
256 | sel.add(osm);
|
---|
257 | else if (mode == SearchMode.remove && osm.selected && matcher.match(osm))
|
---|
258 | sel.remove(osm);
|
---|
259 | }
|
---|
260 | Main.ds.setSelected(sel);
|
---|
261 | }
|
---|
262 | }
|
---|