1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.gui.dialogs;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.BorderLayout;
|
---|
7 | import java.awt.GridLayout;
|
---|
8 | import java.awt.event.ActionEvent;
|
---|
9 | import java.awt.event.ActionListener;
|
---|
10 | import java.awt.event.KeyEvent;
|
---|
11 | import java.awt.event.MouseAdapter;
|
---|
12 | import java.awt.event.MouseEvent;
|
---|
13 | import java.util.Arrays;
|
---|
14 | import java.util.Collection;
|
---|
15 | import java.util.LinkedList;
|
---|
16 |
|
---|
17 | import javax.swing.DefaultListModel;
|
---|
18 | import javax.swing.JButton;
|
---|
19 | import javax.swing.JList;
|
---|
20 | import javax.swing.JPanel;
|
---|
21 | import javax.swing.JScrollPane;
|
---|
22 | import javax.swing.ListSelectionModel;
|
---|
23 |
|
---|
24 | import org.openstreetmap.josm.Main;
|
---|
25 | import org.openstreetmap.josm.data.SelectionChangedListener;
|
---|
26 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
27 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
28 | import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
|
---|
29 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * A small tool dialog for displaying the current selection. The selection manager
|
---|
33 | * respects clicks into the selection list. Ctrl-click will remove entries from
|
---|
34 | * the list while single click will make the clicked entry the only selection.
|
---|
35 | *
|
---|
36 | * @author imi
|
---|
37 | */
|
---|
38 | public class SelectionListDialog extends ToggleDialog implements SelectionChangedListener {
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * The selection's list data.
|
---|
42 | */
|
---|
43 | private final DefaultListModel list = new DefaultListModel();
|
---|
44 | /**
|
---|
45 | * The display list.
|
---|
46 | */
|
---|
47 | private JList displaylist = new JList(list);
|
---|
48 |
|
---|
49 | public SelectionListDialog() {
|
---|
50 | super(tr("Current Selection"), "selectionlist", tr("Open a selection list window."), KeyEvent.VK_E, 150);
|
---|
51 | displaylist.setCellRenderer(new OsmPrimitivRenderer());
|
---|
52 | displaylist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
|
---|
53 | displaylist.addMouseListener(new MouseAdapter(){
|
---|
54 | @Override public void mouseClicked(MouseEvent e) {
|
---|
55 | if (e.getClickCount() < 2)
|
---|
56 | return;
|
---|
57 | updateMap();
|
---|
58 | }
|
---|
59 | });
|
---|
60 |
|
---|
61 | add(new JScrollPane(displaylist), BorderLayout.CENTER);
|
---|
62 |
|
---|
63 | JPanel buttonPanel = new JPanel(new GridLayout(1,2));
|
---|
64 |
|
---|
65 | buttonPanel.add(createButton("Select", "mapmode/selection/select", "Set the selected elements on the map to the selected items in the list above.", new ActionListener(){
|
---|
66 | public void actionPerformed(ActionEvent e) {
|
---|
67 | updateMap();
|
---|
68 | }
|
---|
69 | }));
|
---|
70 |
|
---|
71 | buttonPanel.add(createButton("Reload", "dialogs/refresh", "Refresh the selection list.", new ActionListener(){
|
---|
72 | public void actionPerformed(ActionEvent e) {
|
---|
73 | selectionChanged(Main.ds.getSelected());
|
---|
74 | }
|
---|
75 | }));
|
---|
76 |
|
---|
77 | buttonPanel.add(createButton("Search", "dialogs/search", "Search for objects.", Main.main.menu.search));
|
---|
78 |
|
---|
79 | add(buttonPanel, BorderLayout.SOUTH);
|
---|
80 | selectionChanged(Main.ds.getSelected());
|
---|
81 |
|
---|
82 | DataSet.selListeners.add(this);
|
---|
83 | }
|
---|
84 |
|
---|
85 | private JButton createButton(String name, String icon, String tooltip, ActionListener action) {
|
---|
86 | JButton button = new JButton(tr(name), ImageProvider.get(icon));
|
---|
87 | button.setToolTipText(tr(tooltip));
|
---|
88 | button.addActionListener(action);
|
---|
89 | button.putClientProperty("help", "Dialog/SelectionList/"+name);
|
---|
90 | return button;
|
---|
91 | }
|
---|
92 |
|
---|
93 | @Override public void setVisible(boolean b) {
|
---|
94 | super.setVisible(b);
|
---|
95 | if (b)
|
---|
96 | selectionChanged(Main.ds.getSelected());
|
---|
97 | }
|
---|
98 |
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Called when the selection in the dataset changed.
|
---|
102 | * @param newSelection The new selection array.
|
---|
103 | */
|
---|
104 | public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
|
---|
105 | if (list == null)
|
---|
106 | return; // selection changed may be received in base class constructor before init
|
---|
107 | if (!isVisible())
|
---|
108 | return;
|
---|
109 | OsmPrimitive[] selArr = new OsmPrimitive[newSelection.size()];
|
---|
110 | selArr = newSelection.toArray(selArr);
|
---|
111 | Arrays.sort(selArr);
|
---|
112 | list.setSize(selArr.length);
|
---|
113 | int i = 0;
|
---|
114 | for (OsmPrimitive osm : selArr)
|
---|
115 | list.setElementAt(osm, i++);
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Sets the selection of the map to the current selected items.
|
---|
120 | */
|
---|
121 | public void updateMap() {
|
---|
122 | Collection<OsmPrimitive> sel = new LinkedList<OsmPrimitive>();
|
---|
123 | for (int i = 0; i < list.getSize(); ++i)
|
---|
124 | if (displaylist.isSelectedIndex(i))
|
---|
125 | sel.add((OsmPrimitive)list.get(i));
|
---|
126 | Main.ds.setSelected(sel);
|
---|
127 | }
|
---|
128 | }
|
---|