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

Last change on this file since 1938 was 1903, checked in by Gubaer, 15 years ago

fixed #3180: Zoom to layer + GPX = NPE

  • Property svn:eol-style set to native
File size: 5.4 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.tools.I18n.marktr;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.event.ActionEvent;
8import java.awt.event.KeyEvent;
9import java.util.Collection;
10import java.util.HashSet;
11
12import javax.swing.JOptionPane;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.data.coor.LatLon;
16import org.openstreetmap.josm.data.osm.OsmPrimitive;
17import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
18import org.openstreetmap.josm.gui.OptionPaneUtil;
19import org.openstreetmap.josm.gui.layer.Layer;
20import org.openstreetmap.josm.tools.Shortcut;
21
22/**
23 * Toggles the autoScale feature of the mapView
24 * @author imi
25 */
26public class AutoScaleAction extends JosmAction {
27
28 public static final String[] modes = { marktr("data"), marktr("layer"), marktr("selection"), marktr("conflict"), marktr("download") };
29 private final String mode;
30
31 private static int getModeShortcut(String mode) {
32 int shortcut = -1;
33
34 if (mode.equals("data")) {
35 shortcut = KeyEvent.VK_1;
36 }
37 if (mode.equals("layer")) {
38 shortcut = KeyEvent.VK_2;
39 }
40 if (mode.equals("selection")) {
41 shortcut = KeyEvent.VK_3;
42 }
43 if (mode.equals("conflict")) {
44 shortcut = KeyEvent.VK_4;
45 }
46 if (mode.equals("download")) {
47 shortcut = KeyEvent.VK_5;
48 }
49
50 return shortcut;
51 }
52
53 public AutoScaleAction(String mode) {
54 super(tr("Zoom to {0}", tr(mode)), "dialogs/autoscale/" + mode, tr("Zoom the view to {0}.", tr(mode)),
55 Shortcut.registerShortcut("view:zoom"+mode, tr("View: {0}", tr("Zoom to {0}", tr(mode))), getModeShortcut(mode), Shortcut.GROUP_EDIT), true);
56 String modeHelp = Character.toUpperCase(mode.charAt(0)) + mode.substring(1);
57 putValue("help", "Action/AutoScale/" + modeHelp);
58 this.mode = mode;
59 }
60
61 public void autoScale() {
62 if (Main.map != null) {
63 BoundingXYVisitor bbox = getBoundingBox();
64 if (bbox != null && bbox.getBounds() != null) {
65 Main.map.mapView.recalculateCenterScale(bbox);
66 }
67 }
68 putValue("active", true);
69 }
70
71 public void actionPerformed(ActionEvent e) {
72 autoScale();
73 }
74
75 protected Layer getActiveLayer() {
76 try {
77 return Main.map.mapView.getActiveLayer();
78 } catch(NullPointerException e) {
79 return null;
80 }
81 }
82
83 private BoundingXYVisitor getBoundingBox() {
84 BoundingXYVisitor v = new BoundingXYVisitor();
85 if (mode.equals("data")) {
86 for (Layer l : Main.map.mapView.getAllLayers()) {
87 l.visitBoundingBox(v);
88 }
89 } else if (mode.equals("layer")) {
90 if (getActiveLayer() == null)
91 return null;
92 getActiveLayer().visitBoundingBox(v);
93 } else if (mode.equals("selection") || mode.equals("conflict")) {
94 Collection<OsmPrimitive> sel = new HashSet<OsmPrimitive>();
95 if (mode.equals("selection")) {
96 sel = getCurrentDataSet().getSelected();
97 } else if (mode.equals("conflict")) {
98 if (Main.map.conflictDialog.getConflicts() != null) {
99 sel = Main.map.conflictDialog.getConflicts().getMyConflictParties();
100 }
101 }
102 if (sel.isEmpty()) {
103 OptionPaneUtil.showMessageDialog(
104 Main.parent,
105 (mode.equals("selection") ? tr("Nothing selected to zoom to.") : tr("No conflicts to zoom to")),
106 tr("Information"),
107 JOptionPane.INFORMATION_MESSAGE
108 );
109 return null;
110 }
111 for (OsmPrimitive osm : sel) {
112 osm.visit(v);
113 }
114 // increase bbox by 0.001 degrees on each side. this is required
115 // especially if the bbox contains one single node, but helpful
116 // in most other cases as well.
117 v.enlargeBoundingBox();
118 }
119 else if (mode.equals("download")) {
120 if (Main.pref.hasKey("osm-download.bounds")) {
121 try {
122 String bounds[] = Main.pref.get("osm-download.bounds").split(";");
123 double minlat = Double.parseDouble(bounds[0]);
124 double minlon = Double.parseDouble(bounds[1]);
125 double maxlat = Double.parseDouble(bounds[2]);
126 double maxlon = Double.parseDouble(bounds[3]);
127
128 v.visit(Main.proj.latlon2eastNorth(new LatLon(minlat, minlon)));
129 v.visit(Main.proj.latlon2eastNorth(new LatLon(maxlat, maxlon)));
130 }
131 catch (Exception e) {
132 e.printStackTrace();
133 }
134 }
135 }
136 return v;
137 }
138
139 @Override
140 protected void updateEnabledState() {
141 if ("selection".equals(mode)) {
142 setEnabled(getCurrentDataSet() != null && ! getCurrentDataSet().getSelected().isEmpty());
143 } else if ("layer".equals(mode)) {
144 setEnabled(getActiveLayer() != null);
145 } else {
146 setEnabled(
147 Main.map != null
148 && Main.map.mapView != null
149 && Main.map.mapView.hasLayers()
150 );
151 }
152 }
153}
Note: See TracBrowser for help on using the repository browser.