source: josm/trunk/src/org/openstreetmap/josm/actions/AutoScaleAction.java@ 2751

Last change on this file since 2751 was 2711, checked in by stoecker, 14 years ago

fix bad line endings

  • Property svn:eol-style set to native
File size: 8.0 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.marktr;
6import static org.openstreetmap.josm.tools.I18n.tr;
7
8import java.awt.event.ActionEvent;
9import java.awt.event.KeyEvent;
10import java.util.Collection;
11import java.util.HashSet;
12import java.util.List;
13
14import javax.swing.JOptionPane;
15
16import org.openstreetmap.josm.Main;
17import org.openstreetmap.josm.data.Bounds;
18import org.openstreetmap.josm.data.osm.OsmPrimitive;
19import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
20import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
21import org.openstreetmap.josm.gui.layer.Layer;
22import org.openstreetmap.josm.tools.Shortcut;
23
24/**
25 * Toggles the autoScale feature of the mapView
26 * @author imi
27 */
28public class AutoScaleAction extends JosmAction {
29
30 public static final String[] MODES = { marktr("data"), marktr("layer"), marktr("selection"), marktr("conflict"), marktr("download") };
31
32 /**
33 * Zooms the current map view to the currently selected primitives.
34 * Does nothing if there either isn't a current map view or if there isn't a current data
35 * layer.
36 *
37 */
38 public static void zoomToSelection() {
39 if (Main.main == null || Main.main.getEditLayer() == null) return;
40 if (Main.map == null || Main.map.mapView == null) return;
41 Collection<OsmPrimitive> sel = new HashSet<OsmPrimitive>();
42 sel = Main.main.getEditLayer().data.getSelected();
43 if (sel.isEmpty()) {
44 JOptionPane.showMessageDialog(
45 Main.parent,
46 tr("Nothing selected to zoom to."),
47 tr("Information"),
48 JOptionPane.INFORMATION_MESSAGE
49 );
50 return;
51 }
52 BoundingXYVisitor bboxCalculator = new BoundingXYVisitor();
53 bboxCalculator.computeBoundingBox(sel);
54 // increase bbox by 0.001 degrees on each side. this is required
55 // especially if the bbox contains one single node, but helpful
56 // in most other cases as well.
57 bboxCalculator.enlargeBoundingBox();
58 if (bboxCalculator.getBounds() != null) {
59 Main.map.mapView.recalculateCenterScale(bboxCalculator);
60 }
61 }
62
63 private final String mode;
64
65 private static int getModeShortcut(String mode) {
66 int shortcut = -1;
67
68 if (mode.equals("data")) {
69 shortcut = KeyEvent.VK_1;
70 }
71 if (mode.equals("layer")) {
72 shortcut = KeyEvent.VK_2;
73 }
74 if (mode.equals("selection")) {
75 shortcut = KeyEvent.VK_3;
76 }
77 if (mode.equals("conflict")) {
78 shortcut = KeyEvent.VK_4;
79 }
80 if (mode.equals("download")) {
81 shortcut = KeyEvent.VK_5;
82 }
83
84 return shortcut;
85 }
86
87 public AutoScaleAction(String mode) {
88 super(tr("Zoom to {0}", tr(mode)), "dialogs/autoscale/" + mode, tr("Zoom the view to {0}.", tr(mode)),
89 Shortcut.registerShortcut("view:zoom"+mode, tr("View: {0}", tr("Zoom to {0}", tr(mode))), getModeShortcut(mode), Shortcut.GROUP_EDIT), true);
90 String modeHelp = Character.toUpperCase(mode.charAt(0)) + mode.substring(1);
91 putValue("help", "Action/AutoScale/" + modeHelp);
92 this.mode = mode;
93 if (mode.equals("data")) {
94 putValue("help", ht("/Action/ZoomToData"));
95 } else if (mode.equals("layer")) {
96 putValue("help", ht("/Action/ZoomToLayer"));
97 } else if (mode.equals("selection")) {
98 putValue("help", ht("/Action/ZoomToSelection"));
99 } else if (mode.equals("conflict")) {
100 putValue("help", ht("/Action/ZoomToConflict"));
101 }else if (mode.equals("download")) {
102 putValue("help", ht("/Action/ZoomToDownload"));
103 }
104 }
105
106 public void autoScale() {
107 if (Main.map != null) {
108 BoundingXYVisitor bbox = getBoundingBox();
109 if (bbox != null && bbox.getBounds() != null) {
110 Main.map.mapView.recalculateCenterScale(bbox);
111 }
112 }
113 putValue("active", true);
114 }
115
116 public void actionPerformed(ActionEvent e) {
117 autoScale();
118 }
119
120 protected Layer getActiveLayer() {
121 try {
122 return Main.map.mapView.getActiveLayer();
123 } catch(NullPointerException e) {
124 return null;
125 }
126 }
127
128 /**
129 * Replies the first selected layer in the layer list dialog. null, if no
130 * such layer exists, either because the layer list dialog is not yet created
131 * or because no layer is selected.
132 *
133 * @return the first selected layer in the layer list dialog
134 */
135 protected Layer getFirstSelectedLayer() {
136 if (LayerListDialog.getInstance() == null) return null;
137 List<Layer> layers = LayerListDialog.getInstance().getModel().getSelectedLayers();
138 if (layers.isEmpty()) return null;
139 return layers.get(0);
140 }
141
142 private BoundingXYVisitor getBoundingBox() {
143 BoundingXYVisitor v = new BoundingXYVisitor();
144 if (mode.equals("data")) {
145 for (Layer l : Main.map.mapView.getAllLayers()) {
146 l.visitBoundingBox(v);
147 }
148 } else if (mode.equals("layer")) {
149 if (getActiveLayer() == null)
150 return null;
151 // try to zoom to the first selected layer
152 //
153 Layer l = getFirstSelectedLayer();
154 if (l == null) return null;
155 l.visitBoundingBox(v);
156 } else if (mode.equals("selection") || mode.equals("conflict")) {
157 Collection<OsmPrimitive> sel = new HashSet<OsmPrimitive>();
158 if (mode.equals("selection")) {
159 sel = getCurrentDataSet().getSelected();
160 } else if (mode.equals("conflict")) {
161 if (Main.map.conflictDialog.getConflicts() != null) {
162 sel = Main.map.conflictDialog.getConflicts().getMyConflictParties();
163 }
164 }
165 if (sel.isEmpty()) {
166 JOptionPane.showMessageDialog(
167 Main.parent,
168 (mode.equals("selection") ? tr("Nothing selected to zoom to.") : tr("No conflicts to zoom to")),
169 tr("Information"),
170 JOptionPane.INFORMATION_MESSAGE
171 );
172 return null;
173 }
174 for (OsmPrimitive osm : sel) {
175 osm.visit(v);
176 }
177 // increase bbox by 0.001 degrees on each side. this is required
178 // especially if the bbox contains one single node, but helpful
179 // in most other cases as well.
180 v.enlargeBoundingBox();
181 }
182 else if (mode.equals("download")) {
183 if (Main.pref.hasKey("osm-download.bounds")) {
184 try {
185 v.visit(new Bounds(Main.pref.get("osm-download.bounds"), ";"));
186 } catch (Exception e) {
187 e.printStackTrace();
188 }
189 }
190 }
191 return v;
192 }
193
194 @Override
195 protected void updateEnabledState() {
196 if ("selection".equals(mode)) {
197 setEnabled(getCurrentDataSet() != null && ! getCurrentDataSet().getSelected().isEmpty());
198 } else if ("layer".equals(mode)) {
199 if (Main.map == null || Main.map.mapView == null || Main.map.mapView.getAllLayersAsList().isEmpty()) {
200 setEnabled(false);
201 } else {
202 // FIXME: should also check for whether a layer is selected in the layer list dialog
203 setEnabled(true);
204 }
205 } else {
206 setEnabled(
207 Main.isDisplayingMapView()
208 && Main.map.mapView.hasLayers()
209 );
210 }
211 }
212
213 @Override
214 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
215 if ("selection".equals(mode)) {
216 setEnabled(selection != null && !selection.isEmpty());
217 }
218 }
219}
Note: See TracBrowser for help on using the repository browser.