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

Last change on this file since 22 was 22, checked in by imi, 19 years ago

starting restructure of dataset. Checkpoint is broken!

File size: 3.6 KB
Line 
1package org.openstreetmap.josm.gui.dialogs;
2
3import java.awt.BorderLayout;
4import java.awt.Component;
5import java.awt.event.ActionEvent;
6import java.awt.event.ActionListener;
7import java.awt.event.KeyEvent;
8import java.util.Collection;
9
10import javax.swing.DefaultListCellRenderer;
11import javax.swing.DefaultListModel;
12import javax.swing.JButton;
13import javax.swing.JLabel;
14import javax.swing.JList;
15import javax.swing.JScrollPane;
16import javax.swing.ListSelectionModel;
17
18import org.openstreetmap.josm.Main;
19import org.openstreetmap.josm.data.SelectionChangedListener;
20import org.openstreetmap.josm.data.osm.OsmPrimitive;
21import org.openstreetmap.josm.data.osm.visitor.SelectionComponentVisitor;
22import org.openstreetmap.josm.gui.ImageProvider;
23import org.openstreetmap.josm.gui.MapFrame;
24
25/**
26 * A small tool dialog for displaying the current selection. The selection manager
27 * respects clicks into the selection list. Ctrl-click will remove entries from
28 * the list while single click will make the clicked entry the only selection.
29 *
30 * @author imi
31 */
32public class SelectionListDialog extends ToggleDialog implements SelectionChangedListener {
33
34 /**
35 * The selection's list data.
36 */
37 private final DefaultListModel list = new DefaultListModel();
38 /**
39 * The display list.
40 */
41 private JList displaylist = new JList(list);
42
43 /**
44 * Create a SelectionList dialog.
45 * @param mapView The mapView to get the dataset from.
46 */
47 public SelectionListDialog(MapFrame mapFrame) {
48 super(mapFrame, "Current Selection", "Selection List", "selectionlist", KeyEvent.VK_E, "Open a selection list window.");
49 setLayout(new BorderLayout());
50 setSize(300,400);
51 displaylist.setCellRenderer(new DefaultListCellRenderer(){
52 private SelectionComponentVisitor visitor = new SelectionComponentVisitor();
53 @Override
54 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
55 Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
56 if (c instanceof JLabel && value != null) {
57 ((OsmPrimitive)value).visit(visitor);
58 ((JLabel)c).setText(visitor.name);
59 ((JLabel)c).setIcon(visitor.icon);
60 }
61 return c;
62 }
63 });
64 displaylist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
65
66 getContentPane().add(new JScrollPane(displaylist), BorderLayout.CENTER);
67
68 JButton button = new JButton("Select", ImageProvider.get("mapmode", "selection"));
69 button.setToolTipText("Set the selected elements on the map to the selected items in the list above.");
70 button.addActionListener(new ActionListener(){
71 public void actionPerformed(ActionEvent e) {
72 updateMap();
73 }
74 });
75 getContentPane().add(button, BorderLayout.SOUTH);
76
77 selectionChanged(Main.main.ds.getSelected());
78 }
79
80 @Override
81 public void setVisible(boolean b) {
82 if (b) {
83 Main.main.ds.addSelectionChangedListener(this);
84 selectionChanged(Main.main.ds.getSelected());
85 } else {
86 Main.main.ds.removeSelectionChangedListener(this);
87 }
88 super.setVisible(b);
89 }
90
91
92
93 /**
94 * Called when the selection in the dataset changed.
95 * @param newSelection The new selection array.
96 */
97 public void selectionChanged(Collection<OsmPrimitive> newSelection) {
98 list.removeAllElements();
99 list.setSize(newSelection.size());
100 int i = 0;
101 for (OsmPrimitive osm : newSelection)
102 list.setElementAt(osm, i++);
103 }
104
105 /**
106 * Sets the selection of the map to the current selected items.
107 */
108 public void updateMap() {
109 Main.main.ds.clearSelection();
110 for (int i = 0; i < list.getSize(); ++i)
111 if (displaylist.isSelectedIndex(i))
112 ((OsmPrimitive)list.get(i)).setSelected(true);
113 Main.main.getMapFrame().repaint();
114 }
115}
Note: See TracBrowser for help on using the repository browser.