Ticket #1515: ZoomToSelection patch.txt

File ZoomToSelection patch.txt, 9.1 KB (added by anonymous, 18 years ago)
Line 
1### Eclipse Workspace Patch 1.0
2#P josm
3Index: src/org/openstreetmap/josm/gui/dialogs/SelectionListDialog.java
4===================================================================
5--- src/org/openstreetmap/josm/gui/dialogs/SelectionListDialog.java (revision 931)
6+++ src/org/openstreetmap/josm/gui/dialogs/SelectionListDialog.java (working copy)
7@@ -1,8 +1,8 @@
8 // License: GPL. Copyright 2007 by Immanuel Scholz and others
9 package org.openstreetmap.josm.gui.dialogs;
10
11+import static org.openstreetmap.josm.tools.I18n.marktr;
12 import static org.openstreetmap.josm.tools.I18n.tr;
13-import static org.openstreetmap.josm.tools.I18n.marktr;
14
15 import java.awt.BorderLayout;
16 import java.awt.GridLayout;
17@@ -11,13 +11,14 @@
18 import java.awt.event.KeyEvent;
19 import java.awt.event.MouseAdapter;
20 import java.awt.event.MouseEvent;
21-import java.util.Arrays;
22 import java.util.Collection;
23 import java.util.LinkedList;
24
25 import javax.swing.DefaultListModel;
26 import javax.swing.JList;
27+import javax.swing.JMenuItem;
28 import javax.swing.JPanel;
29+import javax.swing.JPopupMenu;
30 import javax.swing.JScrollPane;
31 import javax.swing.ListSelectionModel;
32
33@@ -25,6 +26,7 @@
34 import org.openstreetmap.josm.data.SelectionChangedListener;
35 import org.openstreetmap.josm.data.osm.DataSet;
36 import org.openstreetmap.josm.data.osm.OsmPrimitive;
37+import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
38 import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
39 import org.openstreetmap.josm.gui.SideButton;
40
41@@ -37,81 +39,139 @@
42 */
43 public class SelectionListDialog extends ToggleDialog implements SelectionChangedListener {
44
45- /**
46- * The selection's list data.
47- */
48- private final DefaultListModel list = new DefaultListModel();
49- /**
50- * The display list.
51- */
52- private JList displaylist = new JList(list);
53+ private static final double ZOOMTO_MIN_SCALE = 0.00001;
54
55- public SelectionListDialog() {
56- super(tr("Current Selection"), "selectionlist", tr("Open a selection list window."), KeyEvent.VK_T, 150);
57- displaylist.setCellRenderer(new OsmPrimitivRenderer());
58- displaylist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
59- displaylist.addMouseListener(new MouseAdapter(){
60- @Override public void mouseClicked(MouseEvent e) {
61- if (e.getClickCount() < 2)
62- return;
63- updateMap();
64- }
65- });
66+ /**
67+ * The selection's list data.
68+ */
69+ private final DefaultListModel list = new DefaultListModel();
70+ /**
71+ * The display list.
72+ */
73+ private JList displaylist = new JList(list);
74
75- add(new JScrollPane(displaylist), BorderLayout.CENTER);
76+ public SelectionListDialog() {
77+ super(tr("Current Selection"), "selectionlist", tr("Open a selection list window."), KeyEvent.VK_T, 150);
78
79- JPanel buttonPanel = new JPanel(new GridLayout(1,2));
80+ final JPopupMenu popupMenu = new JPopupMenu();
81+ displaylist.setCellRenderer(new OsmPrimitivRenderer());
82+ displaylist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
83+ displaylist.addMouseListener(new MouseAdapter() {
84+ @Override
85+ public void mouseClicked(MouseEvent e) {
86+ if (e.getClickCount() == 2 && e.getButton() == MouseEvent.BUTTON1)
87+ updateMap();
88+ }
89
90- buttonPanel.add(new SideButton(marktr("Select"), "select", "SelectionList",
91- tr("Set the selected elements on the map to the selected items in the list above."), new ActionListener(){
92- public void actionPerformed(ActionEvent e) {
93- updateMap();
94- }
95- }));
96+ @Override
97+ public void mousePressed(MouseEvent e) {
98+ if (e.isPopupTrigger())
99+ popupMenu.show(e.getComponent(), e.getX(), e.getY());
100+ }
101
102- buttonPanel.add(new SideButton(marktr("Reload"), "refresh", "SelectionList", tr("Refresh the selection list."), new ActionListener(){
103- public void actionPerformed(ActionEvent e) {
104- selectionChanged(Main.ds.getSelected());
105+ @Override
106+ public void mouseReleased(MouseEvent e) {
107+ if (e.isPopupTrigger())
108+ popupMenu.show(e.getComponent(), e.getX(), e.getY());
109 }
110- }));
111
112- buttonPanel.add(new SideButton(marktr("Search"), "search", "SelectionList", tr("Search for objects."), Main.main.menu.search));
113+ });
114
115- add(buttonPanel, BorderLayout.SOUTH);
116- selectionChanged(Main.ds.getSelected());
117+ add(new JScrollPane(displaylist), BorderLayout.CENTER);
118
119- DataSet.selListeners.add(this);
120- }
121+ JPanel buttonPanel = new JPanel(new GridLayout(1, 2));
122
123- @Override public void setVisible(boolean b) {
124- super.setVisible(b);
125- if (b)
126- selectionChanged(Main.ds.getSelected());
127- }
128+ buttonPanel.add(new SideButton(marktr("Select"), "select", "SelectionList",
129+ tr("Set the selected elements on the map to the selected items in the list above."),
130+ new ActionListener() {
131+ public void actionPerformed(ActionEvent e) {
132+ updateMap();
133+ }
134+ }));
135
136+ buttonPanel.add(new SideButton(marktr("Reload"), "refresh", "SelectionList", tr("Refresh the selection list."),
137+ new ActionListener() {
138+ public void actionPerformed(ActionEvent e) {
139+ selectionChanged(Main.ds.getSelected());
140+ }
141+ }));
142
143- /**
144- * Called when the selection in the dataset changed.
145- * @param newSelection The new selection array.
146- */
147- public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
148- if (list == null || !isVisible())
149- return; // selection changed may be received in base class constructor before init
150- OsmPrimitive selArr[] = Main.ds.sort(newSelection);
151- list.setSize(selArr.length);
152- int i = 0;
153- for (OsmPrimitive osm : selArr)
154- list.setElementAt(osm, i++);
155- }
156+ buttonPanel.add(new SideButton(marktr("Search"), "search", "SelectionList", tr("Search for objects."),
157+ Main.main.menu.search));
158
159- /**
160- * Sets the selection of the map to the current selected items.
161- */
162- public void updateMap() {
163- Collection<OsmPrimitive> sel = new LinkedList<OsmPrimitive>();
164- for (int i = 0; i < list.getSize(); ++i)
165- if (displaylist.isSelectedIndex(i))
166- sel.add((OsmPrimitive)list.get(i));
167- Main.ds.setSelected(sel);
168- }
169+ add(buttonPanel, BorderLayout.SOUTH);
170+
171+ JMenuItem zoomTo = new JMenuItem("Zoom to selection");
172+ zoomTo.addActionListener(new ActionListener() {
173+ public void actionPerformed(ActionEvent e) {
174+ zoomToSelection();
175+ }
176+ });
177+ popupMenu.add(zoomTo);
178+
179+ selectionChanged(Main.ds.getSelected());
180+
181+ DataSet.selListeners.add(this);
182+ }
183+
184+ @Override
185+ public void setVisible(boolean b) {
186+ super.setVisible(b);
187+ if (b)
188+ selectionChanged(Main.ds.getSelected());
189+ }
190+
191+ public void zoomToSelection() {
192+ BoundingXYVisitor box = new BoundingXYVisitor();
193+ int[] selected = displaylist.getSelectedIndices();
194+ if (selected.length > 0) {
195+ // One or more elements are selected in the list -> we zoom only to them
196+ for (int i = 0; i < selected.length; i++) {
197+ Object o = list.get(selected[i]);
198+ if (o instanceof OsmPrimitive)
199+ ((OsmPrimitive) o).visit(box);
200+ }
201+ } else {
202+ // No element is selected in the list -> we zoom to the whole selection
203+ for (int i = 0; i < list.getSize(); i++) {
204+ Object o = list.get(i);
205+ if (o instanceof OsmPrimitive)
206+ ((OsmPrimitive) o).visit(box);
207+ }
208+ }
209+ if (box.max == null || box.min == null)
210+ return;
211+ if (box.max.equals(box.min))
212+ Main.map.mapView.zoomTo(box.max, ZOOMTO_MIN_SCALE);
213+ else {
214+ Main.map.mapView.recalculateCenterScale(box);
215+ if (Main.map.mapView.getScale() < ZOOMTO_MIN_SCALE)
216+ Main.map.mapView.zoomTo(Main.map.mapView.getCenter(), ZOOMTO_MIN_SCALE);
217+ }
218+ }
219+
220+ /**
221+ * Called when the selection in the dataset changed.
222+ * @param newSelection The new selection array.
223+ */
224+ public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
225+ if (list == null || !isVisible())
226+ return; // selection changed may be received in base class constructor before init
227+ OsmPrimitive selArr[] = DataSet.sort(newSelection);
228+ list.setSize(selArr.length);
229+ int i = 0;
230+ for (OsmPrimitive osm : selArr)
231+ list.setElementAt(osm, i++);
232+ }
233+
234+ /**
235+ * Sets the selection of the map to the current selected items.
236+ */
237+ public void updateMap() {
238+ Collection<OsmPrimitive> sel = new LinkedList<OsmPrimitive>();
239+ for (int i = 0; i < list.getSize(); ++i)
240+ if (displaylist.isSelectedIndex(i))
241+ sel.add((OsmPrimitive) list.get(i));
242+ Main.ds.setSelected(sel);
243+ }
244 }