source: josm/branch/0.5/src/org/openstreetmap/josm/gui/dialogs/SelectionListDialog.java@ 329

Last change on this file since 329 was 329, checked in by framm, 17 years ago

This commit is a manual merge of all changes that have been made to
the intermediate "core_0.5" branch on the main OSM repository,
bevore JOSM was moved to openstreetmap.de.

Changes incorporated here:

r4464@svn.openstreetmap.org
r4466@svn.openstreetmap.org
r4468@svn.openstreetmap.org
r4469@svn.openstreetmap.org
r4479@svn.openstreetmap.org

File size: 4.2 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.gui.dialogs;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.BorderLayout;
7import java.awt.GridLayout;
8import java.awt.event.ActionEvent;
9import java.awt.event.ActionListener;
10import java.awt.event.KeyEvent;
11import java.awt.event.MouseAdapter;
12import java.awt.event.MouseEvent;
13import java.util.Arrays;
14import java.util.Collection;
15import java.util.LinkedList;
16
17import javax.swing.DefaultListModel;
18import javax.swing.JButton;
19import javax.swing.JList;
20import javax.swing.JPanel;
21import javax.swing.JScrollPane;
22import javax.swing.ListSelectionModel;
23
24import org.openstreetmap.josm.Main;
25import org.openstreetmap.josm.data.SelectionChangedListener;
26import org.openstreetmap.josm.data.osm.DataSet;
27import org.openstreetmap.josm.data.osm.OsmPrimitive;
28import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
29import 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 */
38public 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}
Note: See TracBrowser for help on using the repository browser.