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.util.Collection;
|
---|
13 | import java.util.LinkedList;
|
---|
14 |
|
---|
15 | import javax.swing.ButtonGroup;
|
---|
16 | import javax.swing.DefaultListModel;
|
---|
17 | import javax.swing.JButton;
|
---|
18 | import javax.swing.JLabel;
|
---|
19 | import javax.swing.JList;
|
---|
20 | import javax.swing.JOptionPane;
|
---|
21 | import javax.swing.JPanel;
|
---|
22 | import javax.swing.JRadioButton;
|
---|
23 | import javax.swing.JScrollPane;
|
---|
24 | import javax.swing.JTextField;
|
---|
25 | import javax.swing.ListSelectionModel;
|
---|
26 |
|
---|
27 | import org.openstreetmap.josm.Main;
|
---|
28 | import org.openstreetmap.josm.data.SelectionChangedListener;
|
---|
29 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
30 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
31 | import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
|
---|
32 | import org.openstreetmap.josm.tools.GBC;
|
---|
33 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
34 | import org.openstreetmap.josm.tools.SearchCompiler;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * A small tool dialog for displaying the current selection. The selection manager
|
---|
38 | * respects clicks into the selection list. Ctrl-click will remove entries from
|
---|
39 | * the list while single click will make the clicked entry the only selection.
|
---|
40 | *
|
---|
41 | * @author imi
|
---|
42 | */
|
---|
43 | public class SelectionListDialog extends ToggleDialog implements SelectionChangedListener {
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * The selection's list data.
|
---|
47 | */
|
---|
48 | private final DefaultListModel list = new DefaultListModel();
|
---|
49 | /**
|
---|
50 | * The display list.
|
---|
51 | */
|
---|
52 | private JList displaylist = new JList(list);
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Create a SelectionList dialog.
|
---|
56 | * @param mapView The mapView to get the dataset from.
|
---|
57 | */
|
---|
58 | public SelectionListDialog(MapFrame mapFrame) {
|
---|
59 | super("Current Selection", "Selection List", "selectionlist", "Open a selection list window.", "E", KeyEvent.VK_E, "selectionlist");
|
---|
60 | setPreferredSize(new Dimension(320,150));
|
---|
61 | displaylist.setCellRenderer(new OsmPrimitivRenderer());
|
---|
62 | displaylist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
|
---|
63 | displaylist.addMouseListener(new MouseAdapter(){
|
---|
64 | @Override public void mouseClicked(MouseEvent e) {
|
---|
65 | if (e.getClickCount() < 2)
|
---|
66 | return;
|
---|
67 | updateMap();
|
---|
68 | }
|
---|
69 | });
|
---|
70 |
|
---|
71 | add(new JScrollPane(displaylist), BorderLayout.CENTER);
|
---|
72 |
|
---|
73 | JPanel buttonPanel = new JPanel(new GridLayout(1,2));
|
---|
74 |
|
---|
75 | JButton button = new JButton("Select", ImageProvider.get("mapmode", "selection"));
|
---|
76 | button.setToolTipText("Set the selected elements on the map to the selected items in the list above.");
|
---|
77 | button.addActionListener(new ActionListener(){
|
---|
78 | public void actionPerformed(ActionEvent e) {
|
---|
79 | updateMap();
|
---|
80 | }
|
---|
81 | });
|
---|
82 | buttonPanel.add(button);
|
---|
83 |
|
---|
84 | button = new JButton("Search", ImageProvider.get("dialogs", "search"));
|
---|
85 | button.setToolTipText("Search for objects.");
|
---|
86 | button.addActionListener(new ActionListener(){
|
---|
87 | private String lastSearch = "";
|
---|
88 | public void actionPerformed(ActionEvent e) {
|
---|
89 | JLabel label = new JLabel("Please enter a search string.");
|
---|
90 | final JTextField input = new JTextField(lastSearch);
|
---|
91 | input.setToolTipText("<html>Fulltext search.<ul>" +
|
---|
92 | "<li><code>Baker Street</code> - 'Baker' and 'Street' in any key or name.</li>" +
|
---|
93 | "<li><code>\"Baker Street\"</code> - 'Baker Street' in any key or name.</li>" +
|
---|
94 | "<li><code>name:Bak</code> - 'Bak' anywhere in the name.</li>" +
|
---|
95 | "<li><code>-name:Bak</code> - not 'Bak' in the name.</li>" +
|
---|
96 | "<li><code>foot:</code> - key=foot set to any value." +
|
---|
97 | "</ul></html>");
|
---|
98 |
|
---|
99 | JRadioButton replace = new JRadioButton("replace selection", true);
|
---|
100 | JRadioButton add = new JRadioButton("add to selection", false);
|
---|
101 | JRadioButton remove = new JRadioButton("remove from selection", false);
|
---|
102 | ButtonGroup bg = new ButtonGroup();
|
---|
103 | bg.add(replace);
|
---|
104 | bg.add(add);
|
---|
105 | bg.add(remove);
|
---|
106 |
|
---|
107 | JPanel p = new JPanel(new GridBagLayout());
|
---|
108 | p.add(label, GBC.eop());
|
---|
109 | p.add(input, GBC.eop().fill(GBC.HORIZONTAL));
|
---|
110 | p.add(replace, GBC.eol());
|
---|
111 | p.add(add, GBC.eol());
|
---|
112 | p.add(remove, GBC.eol());
|
---|
113 | JOptionPane pane = new JOptionPane(p, JOptionPane.INFORMATION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null){
|
---|
114 | @Override public void selectInitialValue() {
|
---|
115 | input.requestFocusInWindow();
|
---|
116 | }
|
---|
117 | };
|
---|
118 | pane.createDialog(Main.main, "Search").setVisible(true);
|
---|
119 | System.out.println(pane.getValue());
|
---|
120 | if (!Integer.valueOf(JOptionPane.OK_OPTION).equals(pane.getValue()))
|
---|
121 | return;
|
---|
122 | lastSearch = input.getText();
|
---|
123 | SearchCompiler.Match matcher = SearchCompiler.compile(lastSearch);
|
---|
124 | Collection<OsmPrimitive> sel = Main.ds.getSelected();
|
---|
125 | for (OsmPrimitive osm : Main.ds.allNonDeletedPrimitives()) {
|
---|
126 | if (replace.isSelected()) {
|
---|
127 | if (matcher.match(osm))
|
---|
128 | sel.add(osm);
|
---|
129 | else
|
---|
130 | sel.remove(osm);
|
---|
131 | } else if (add.isSelected() && !osm.selected && matcher.match(osm))
|
---|
132 | sel.add(osm);
|
---|
133 | else if (remove.isSelected() && osm.selected && matcher.match(osm))
|
---|
134 | sel.remove(osm);
|
---|
135 | }
|
---|
136 | Main.ds.setSelected(sel);
|
---|
137 | }
|
---|
138 | });
|
---|
139 | buttonPanel.add(button);
|
---|
140 |
|
---|
141 | add(buttonPanel, BorderLayout.SOUTH);
|
---|
142 | selectionChanged(Main.ds.getSelected());
|
---|
143 | }
|
---|
144 |
|
---|
145 | @Override public void setVisible(boolean b) {
|
---|
146 | if (b) {
|
---|
147 | Main.ds.addSelectionChangedListener(this);
|
---|
148 | selectionChanged(Main.ds.getSelected());
|
---|
149 | } else {
|
---|
150 | Main.ds.removeSelectionChangedListener(this);
|
---|
151 | }
|
---|
152 | super.setVisible(b);
|
---|
153 | }
|
---|
154 |
|
---|
155 |
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Called when the selection in the dataset changed.
|
---|
159 | * @param newSelection The new selection array.
|
---|
160 | */
|
---|
161 | public void selectionChanged(Collection<OsmPrimitive> newSelection) {
|
---|
162 | if (list == null)
|
---|
163 | return; // selection changed may be received in base class constructor before init
|
---|
164 | list.removeAllElements();
|
---|
165 | list.setSize(newSelection.size());
|
---|
166 | int i = 0;
|
---|
167 | for (OsmPrimitive osm : newSelection)
|
---|
168 | list.setElementAt(osm, i++);
|
---|
169 | }
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Sets the selection of the map to the current selected items.
|
---|
173 | */
|
---|
174 | public void updateMap() {
|
---|
175 | Collection<OsmPrimitive> sel = new LinkedList<OsmPrimitive>();
|
---|
176 | for (int i = 0; i < list.getSize(); ++i)
|
---|
177 | if (displaylist.isSelectedIndex(i))
|
---|
178 | sel.add((OsmPrimitive)list.get(i));
|
---|
179 | Main.ds.setSelected(sel);
|
---|
180 | }
|
---|
181 | }
|
---|