source: josm/src/org/openstreetmap/josm/gui/dialogs/SelectionListDialog.java@ 86

Last change on this file since 86 was 86, checked in by imi, 18 years ago
  • added conflicts and resolve conflict dialog

This is one of those "changed everything" checkpoint.

File size: 6.1 KB
Line 
1package org.openstreetmap.josm.gui.dialogs;
2
3import java.awt.BorderLayout;
4import java.awt.Dimension;
5import java.awt.GridBagLayout;
6import java.awt.GridLayout;
7import java.awt.event.ActionEvent;
8import java.awt.event.ActionListener;
9import java.awt.event.KeyEvent;
10import java.awt.event.MouseAdapter;
11import java.awt.event.MouseEvent;
12import java.util.Collection;
13import java.util.LinkedList;
14
15import javax.swing.ButtonGroup;
16import javax.swing.DefaultListModel;
17import javax.swing.JButton;
18import javax.swing.JLabel;
19import javax.swing.JList;
20import javax.swing.JOptionPane;
21import javax.swing.JPanel;
22import javax.swing.JRadioButton;
23import javax.swing.JScrollPane;
24import javax.swing.JTextField;
25import javax.swing.ListSelectionModel;
26
27import org.openstreetmap.josm.Main;
28import org.openstreetmap.josm.data.SelectionChangedListener;
29import org.openstreetmap.josm.data.osm.OsmPrimitive;
30import org.openstreetmap.josm.gui.MapFrame;
31import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
32import org.openstreetmap.josm.tools.GBC;
33import org.openstreetmap.josm.tools.ImageProvider;
34import 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 */
43public 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}
Note: See TracBrowser for help on using the repository browser.