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

Last change on this file since 797 was 797, checked in by stoecker, 16 years ago

corrected autoscale

  • Property svn:eol-style set to native
File size: 3.0 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.tr;
5import static org.openstreetmap.josm.tools.I18n.marktr;
6
7import java.awt.event.ActionEvent;
8import java.awt.event.KeyEvent;
9import java.util.Collection;
10
11import javax.swing.JOptionPane;
12
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.data.coor.EastNorth;
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.layer.Layer;
19
20/**
21 * Toggles the autoScale feature of the mapView
22 * @author imi
23 */
24public class AutoScaleAction extends JosmAction {
25
26 public static final String[] modes = {
27 marktr("data"),
28 marktr("layer"),
29 marktr("selection"),
30 marktr("conflict")
31 };
32 private final String mode;
33
34 private static int getModeShortcut(String mode) {
35 int shortcut = -1;
36
37 if(mode.equals("data")) {
38 shortcut = KeyEvent.VK_1;
39 }
40 if(mode.equals("layer")) {
41 shortcut = KeyEvent.VK_2;
42 }
43 if(mode.equals("selection")) {
44 shortcut = KeyEvent.VK_3;
45 }
46 if(mode.equals("conflict")) {
47 shortcut = KeyEvent.VK_4;
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)), AutoScaleAction.getModeShortcut(mode), 0, true);
55 String modeHelp = Character.toUpperCase(mode.charAt(0))+mode.substring(1);
56 putValue("help", "Action/AutoScale/"+modeHelp);
57 this.mode = mode;
58 }
59
60 public void actionPerformed(ActionEvent e) {
61 if (Main.map != null) {
62 BoundingXYVisitor bbox = getBoundingBox();
63 if (bbox != null) {
64 Main.map.mapView.recalculateCenterScale(bbox);
65 }
66 }
67 putValue("active", true);
68 }
69
70 private BoundingXYVisitor getBoundingBox() {
71 BoundingXYVisitor v = new BoundingXYVisitor();
72 if (mode.equals("data")) {
73 for (Layer l : Main.map.mapView.getAllLayers())
74 l.visitBoundingBox(v);
75 } else if (mode.equals("layer"))
76 Main.map.mapView.getActiveLayer().visitBoundingBox(v);
77 else if (mode.equals("selection") || mode.equals("conflict")) {
78 Collection<OsmPrimitive> sel = mode.equals("selection") ? Main.ds.getSelected() : Main.map.conflictDialog.conflicts.keySet();
79 if (sel.isEmpty()) {
80 JOptionPane.showMessageDialog(Main.parent,
81 mode.equals("selection") ? tr("Nothing selected to zoom to.") : tr("No conflicts to zoom to"));
82 return null;
83 }
84 for (OsmPrimitive osm : sel)
85 osm.visit(v);
86 // increase bbox by 0.001 degrees on each side. this is required
87 // especially if the bbox contains one single node, but helpful
88 // in most other cases as well.
89 if (v.min != null && v.max != null) // && v.min.north() == v.max.north() && v.min.east() == v.max.east()) {
90 {
91 EastNorth en = new EastNorth(0.0001,0.0001);
92 v.min = new EastNorth(v.min.east()-en.east(), v.min.north()-en.north());
93 v.max = new EastNorth(v.max.east()+en.east(), v.max.north()+en.north());
94 }
95 }
96 return v;
97 }
98}
Note: See TracBrowser for help on using the repository browser.