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

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

new: only one list of layers managed by MapView. LayerListDialog is an adapter to this list.
improved delete confirmation for multi layer delete
various bug fixes in the new LayerListDialog

  • Property svn:eol-style set to native
File size: 5.1 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 private BoundingXYVisitor getBoundingBox() {
76 BoundingXYVisitor v = new BoundingXYVisitor();
77 if (mode.equals("data")) {
78 for (Layer l : Main.map.mapView.getAllLayers()) {
79 l.visitBoundingBox(v);
80 }
81 } else if (mode.equals("layer")) {
82 Main.map.mapView.getActiveLayer().visitBoundingBox(v);
83 } else if (mode.equals("selection") || mode.equals("conflict")) {
84 Collection<OsmPrimitive> sel = new HashSet<OsmPrimitive>();
85 if (mode.equals("selection")) {
86 sel = getCurrentDataSet().getSelected();
87 } else if (mode.equals("conflict")) {
88 if (Main.map.conflictDialog.getConflicts() != null) {
89 sel = Main.map.conflictDialog.getConflicts().getMyConflictParties();
90 }
91 }
92 if (sel.isEmpty()) {
93 OptionPaneUtil.showMessageDialog(
94 Main.parent,
95 (mode.equals("selection") ? tr("Nothing selected to zoom to.") : tr("No conflicts to zoom to")),
96 tr("Information"),
97 JOptionPane.INFORMATION_MESSAGE
98 );
99 return null;
100 }
101 for (OsmPrimitive osm : sel) {
102 osm.visit(v);
103 }
104 // increase bbox by 0.001 degrees on each side. this is required
105 // especially if the bbox contains one single node, but helpful
106 // in most other cases as well.
107 v.enlargeBoundingBox();
108 }
109 else if (mode.equals("download")) {
110 if (Main.pref.hasKey("osm-download.bounds")) {
111 try {
112 String bounds[] = Main.pref.get("osm-download.bounds").split(";");
113 double minlat = Double.parseDouble(bounds[0]);
114 double minlon = Double.parseDouble(bounds[1]);
115 double maxlat = Double.parseDouble(bounds[2]);
116 double maxlon = Double.parseDouble(bounds[3]);
117
118 v.visit(Main.proj.latlon2eastNorth(new LatLon(minlat, minlon)));
119 v.visit(Main.proj.latlon2eastNorth(new LatLon(maxlat, maxlon)));
120 }
121 catch (Exception e) {
122 e.printStackTrace();
123 }
124 }
125 }
126 return v;
127 }
128
129 @Override
130 protected void updateEnabledState() {
131 if ("selection".equals(mode)) {
132 setEnabled(getCurrentDataSet() != null && ! getCurrentDataSet().getSelected().isEmpty());
133 } else {
134 setEnabled(
135 Main.map != null
136 && Main.map.mapView != null
137 && Main.map.mapView.hasLayers()
138 );
139 }
140 }
141}
Note: See TracBrowser for help on using the repository browser.