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

Last change on this file since 69 was 69, checked in by imi, 18 years ago
  • improved search (now like google)
  • link in About dialog work with windows
File size: 4.7 KB
Line 
1package org.openstreetmap.josm.gui.dialogs;
2
3import java.awt.BorderLayout;
4import java.awt.Dimension;
5import java.awt.GridLayout;
6import java.awt.event.ActionEvent;
7import java.awt.event.ActionListener;
8import java.awt.event.KeyEvent;
9import java.awt.event.MouseAdapter;
10import java.awt.event.MouseEvent;
11import java.util.Collection;
12
13import javax.swing.DefaultListModel;
14import javax.swing.JButton;
15import javax.swing.JLabel;
16import javax.swing.JList;
17import javax.swing.JOptionPane;
18import javax.swing.JPanel;
19import javax.swing.JScrollPane;
20import javax.swing.ListSelectionModel;
21
22import org.openstreetmap.josm.Main;
23import org.openstreetmap.josm.data.SelectionChangedListener;
24import org.openstreetmap.josm.data.osm.OsmPrimitive;
25import org.openstreetmap.josm.gui.ImageProvider;
26import org.openstreetmap.josm.gui.MapFrame;
27import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
28import org.openstreetmap.josm.tools.SearchCompiler;
29
30/**
31 * A small tool dialog for displaying the current selection. The selection manager
32 * respects clicks into the selection list. Ctrl-click will remove entries from
33 * the list while single click will make the clicked entry the only selection.
34 *
35 * @author imi
36 */
37public class SelectionListDialog extends ToggleDialog implements SelectionChangedListener {
38
39 /**
40 * The selection's list data.
41 */
42 private final DefaultListModel list = new DefaultListModel();
43 /**
44 * The display list.
45 */
46 private JList displaylist = new JList(list);
47
48 /**
49 * Create a SelectionList dialog.
50 * @param mapView The mapView to get the dataset from.
51 */
52 public SelectionListDialog(MapFrame mapFrame) {
53 super("Current Selection", "Selection List", "selectionlist", "Open a selection list window.", "E", KeyEvent.VK_E);
54 setPreferredSize(new Dimension(320,150));
55 displaylist.setCellRenderer(new OsmPrimitivRenderer());
56 displaylist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
57 displaylist.addMouseListener(new MouseAdapter(){
58 @Override
59 public void mouseClicked(MouseEvent e) {
60 if (e.getClickCount() < 2)
61 return;
62 updateMap();
63 }
64 });
65
66 add(new JScrollPane(displaylist), BorderLayout.CENTER);
67
68 JPanel buttonPanel = new JPanel(new GridLayout(1,2));
69
70 JButton button = new JButton("Select", ImageProvider.get("mapmode", "selection"));
71 button.setToolTipText("Set the selected elements on the map to the selected items in the list above.");
72 button.addActionListener(new ActionListener(){
73 public void actionPerformed(ActionEvent e) {
74 updateMap();
75 }
76 });
77 buttonPanel.add(button);
78
79 button = new JButton("Search", ImageProvider.get("dialogs", "search"));
80 button.setToolTipText("Search for objects.");
81 button.addActionListener(new ActionListener(){
82 private String lastSearch = "";
83 public void actionPerformed(ActionEvent e) {
84 JLabel l = new JLabel("Please enter a search string.");
85 l.setToolTipText("<html>Fulltext search.<ul>" +
86 "<li><code>Baker Street</code> - 'Baker' and 'Street' in any key or name.</li>" +
87 "<li><code>\"Baker Street\"</code> - 'Baker Street' in any key or name.</li>" +
88 "<li><code>name:Bak</code> - 'Bak' anywhere in the name.</li>" +
89 "<li><code>-name:Bak</code> - not 'Bak' in the name.</li>" +
90 "<li><code>foot:</code> - key=foot set to any value." +
91 "</ul></html>");
92 lastSearch = (String)JOptionPane.showInputDialog(Main.main,l,"Search",JOptionPane.INFORMATION_MESSAGE,null,null,lastSearch);
93 if (lastSearch == null)
94 return;
95 SearchCompiler.Match matcher = SearchCompiler.compile(lastSearch);
96 for (OsmPrimitive osm : Main.main.ds.allNonDeletedPrimitives())
97 osm.setSelected(matcher.match(osm));
98 selectionChanged(Main.main.ds.getSelected());
99 Main.main.getMapFrame().repaint();
100 }
101 });
102 buttonPanel.add(button);
103
104 add(buttonPanel, BorderLayout.SOUTH);
105 selectionChanged(Main.main.ds.getSelected());
106 }
107
108 @Override
109 public void setVisible(boolean b) {
110 if (b) {
111 Main.main.ds.addSelectionChangedListener(this);
112 selectionChanged(Main.main.ds.getSelected());
113 } else {
114 Main.main.ds.removeSelectionChangedListener(this);
115 }
116 super.setVisible(b);
117 }
118
119
120
121 /**
122 * Called when the selection in the dataset changed.
123 * @param newSelection The new selection array.
124 */
125 public void selectionChanged(Collection<OsmPrimitive> newSelection) {
126 list.removeAllElements();
127 list.setSize(newSelection.size());
128 int i = 0;
129 for (OsmPrimitive osm : newSelection)
130 list.setElementAt(osm, i++);
131 }
132
133 /**
134 * Sets the selection of the map to the current selected items.
135 */
136 public void updateMap() {
137 Main.main.ds.clearSelection();
138 for (int i = 0; i < list.getSize(); ++i)
139 if (displaylist.isSelectedIndex(i))
140 ((OsmPrimitive)list.get(i)).setSelected(true);
141 Main.main.getMapFrame().repaint();
142 }
143}
Note: See TracBrowser for help on using the repository browser.