1 | package org.openstreetmap.josm.gui.dialogs;
|
---|
2 |
|
---|
3 | import java.awt.BorderLayout;
|
---|
4 | import java.awt.Dimension;
|
---|
5 | import java.awt.GridLayout;
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.awt.event.ActionListener;
|
---|
8 | import java.awt.event.KeyEvent;
|
---|
9 | import java.awt.event.MouseAdapter;
|
---|
10 | import java.awt.event.MouseEvent;
|
---|
11 | import java.util.Collection;
|
---|
12 | import java.util.Map.Entry;
|
---|
13 |
|
---|
14 | import javax.swing.DefaultListModel;
|
---|
15 | import javax.swing.JButton;
|
---|
16 | import javax.swing.JList;
|
---|
17 | import javax.swing.JOptionPane;
|
---|
18 | import javax.swing.JPanel;
|
---|
19 | import javax.swing.JScrollPane;
|
---|
20 | import javax.swing.ListSelectionModel;
|
---|
21 |
|
---|
22 | import org.openstreetmap.josm.Main;
|
---|
23 | import org.openstreetmap.josm.data.SelectionChangedListener;
|
---|
24 | import org.openstreetmap.josm.data.osm.Key;
|
---|
25 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
26 | import org.openstreetmap.josm.gui.ImageProvider;
|
---|
27 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
28 | import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
|
---|
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 | */
|
---|
37 | public 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", KeyEvent.VK_E, "Open a selection list window.");
|
---|
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 | public void actionPerformed(ActionEvent e) {
|
---|
83 | String search = JOptionPane.showInputDialog(Main.main, "Please enter a search string", "Search", JOptionPane.INFORMATION_MESSAGE);
|
---|
84 | if (search == null)
|
---|
85 | return;
|
---|
86 | Main.main.ds.clearSelection();
|
---|
87 | for (OsmPrimitive osm : Main.main.ds.allNonDeletedPrimitives()) {
|
---|
88 | if (osm.keys != null) {
|
---|
89 | for (Entry<Key, String> ent : osm.keys.entrySet()) {
|
---|
90 | if (search.indexOf(ent.getKey().name) != -1 || search.indexOf(ent.getValue()) != -1) {
|
---|
91 | osm.setSelected(true);
|
---|
92 | break;
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|
97 | selectionChanged(Main.main.ds.getSelected());
|
---|
98 | Main.main.getMapFrame().repaint();
|
---|
99 | }
|
---|
100 | });
|
---|
101 | buttonPanel.add(button);
|
---|
102 |
|
---|
103 | add(buttonPanel, BorderLayout.SOUTH);
|
---|
104 | selectionChanged(Main.main.ds.getSelected());
|
---|
105 | }
|
---|
106 |
|
---|
107 | @Override
|
---|
108 | public void setVisible(boolean b) {
|
---|
109 | if (b) {
|
---|
110 | Main.main.ds.addSelectionChangedListener(this);
|
---|
111 | selectionChanged(Main.main.ds.getSelected());
|
---|
112 | } else {
|
---|
113 | Main.main.ds.removeSelectionChangedListener(this);
|
---|
114 | }
|
---|
115 | super.setVisible(b);
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * Called when the selection in the dataset changed.
|
---|
122 | * @param newSelection The new selection array.
|
---|
123 | */
|
---|
124 | public void selectionChanged(Collection<OsmPrimitive> newSelection) {
|
---|
125 | list.removeAllElements();
|
---|
126 | list.setSize(newSelection.size());
|
---|
127 | int i = 0;
|
---|
128 | for (OsmPrimitive osm : newSelection)
|
---|
129 | list.setElementAt(osm, i++);
|
---|
130 | }
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Sets the selection of the map to the current selected items.
|
---|
134 | */
|
---|
135 | public void updateMap() {
|
---|
136 | Main.main.ds.clearSelection();
|
---|
137 | for (int i = 0; i < list.getSize(); ++i)
|
---|
138 | if (displaylist.isSelectedIndex(i))
|
---|
139 | ((OsmPrimitive)list.get(i)).setSelected(true);
|
---|
140 | Main.main.getMapFrame().repaint();
|
---|
141 | }
|
---|
142 | }
|
---|