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

Last change on this file since 2323 was 2323, checked in by Gubaer, 16 years ago

Added explicit help topics
See also current list of help topics with links to source files and to help pages

  • Property svn:eol-style set to native
File size: 7.2 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.gui.help.HelpUtil.ht;
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.coor.LatLon;
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 private final String mode;
32
33 private static int getModeShortcut(String mode) {
34 int shortcut = -1;
35
36 if (mode.equals("data")) {
37 shortcut = KeyEvent.VK_1;
38 }
39 if (mode.equals("layer")) {
40 shortcut = KeyEvent.VK_2;
41 }
42 if (mode.equals("selection")) {
43 shortcut = KeyEvent.VK_3;
44 }
45 if (mode.equals("conflict")) {
46 shortcut = KeyEvent.VK_4;
47 }
48 if (mode.equals("download")) {
49 shortcut = KeyEvent.VK_5;
50 }
51
52 return shortcut;
53 }
54
55 public AutoScaleAction(String mode) {
56 super(tr("Zoom to {0}", tr(mode)), "dialogs/autoscale/" + mode, tr("Zoom the view to {0}.", tr(mode)),
57 Shortcut.registerShortcut("view:zoom"+mode, tr("View: {0}", tr("Zoom to {0}", tr(mode))), getModeShortcut(mode), Shortcut.GROUP_EDIT), true);
58 String modeHelp = Character.toUpperCase(mode.charAt(0)) + mode.substring(1);
59 putValue("help", "Action/AutoScale/" + modeHelp);
60 this.mode = mode;
61 if (mode.equals("data")) {
62 putValue("help", ht("/Action/ZoomToData"));
63 } else if (mode.equals("layer")) {
64 putValue("help", ht("/Action/ZoomToLayer"));
65 } else if (mode.equals("selection")) {
66 putValue("help", ht("/Action/ZoomToSelection"));
67 } else if (mode.equals("conflict")) {
68 putValue("help", ht("/Action/ZoomToConflict"));
69 }else if (mode.equals("download")) {
70 putValue("help", ht("/Action/ZoomToDownload"));
71 }
72 }
73
74 public void autoScale() {
75 if (Main.map != null) {
76 BoundingXYVisitor bbox = getBoundingBox();
77 if (bbox != null && bbox.getBounds() != null) {
78 Main.map.mapView.recalculateCenterScale(bbox);
79 }
80 }
81 putValue("active", true);
82 }
83
84 public void actionPerformed(ActionEvent e) {
85 autoScale();
86 }
87
88 protected Layer getActiveLayer() {
89 try {
90 return Main.map.mapView.getActiveLayer();
91 } catch(NullPointerException e) {
92 return null;
93 }
94 }
95
96 /**
97 * Replies the first selected layer in the layer list dialog. null, if no
98 * such layer exists, either because the layer list dialog is not yet created
99 * or because no layer is selected.
100 *
101 * @return the first selected layer in the layer list dialog
102 */
103 protected Layer getFirstSelectedLayer() {
104 if (LayerListDialog.getInstance() == null) return null;
105 List<Layer> layers = LayerListDialog.getInstance().getModel().getSelectedLayers();
106 if (layers.isEmpty()) return null;
107 return layers.get(0);
108 }
109
110 private BoundingXYVisitor getBoundingBox() {
111 BoundingXYVisitor v = new BoundingXYVisitor();
112 if (mode.equals("data")) {
113 for (Layer l : Main.map.mapView.getAllLayers()) {
114 l.visitBoundingBox(v);
115 }
116 } else if (mode.equals("layer")) {
117 if (getActiveLayer() == null)
118 return null;
119 // try to zoom to the first selected layer
120 //
121 Layer l = getFirstSelectedLayer();
122 if (l == null) return null;
123 l.visitBoundingBox(v);
124 } else if (mode.equals("selection") || mode.equals("conflict")) {
125 Collection<OsmPrimitive> sel = new HashSet<OsmPrimitive>();
126 if (mode.equals("selection")) {
127 sel = getCurrentDataSet().getSelected();
128 } else if (mode.equals("conflict")) {
129 if (Main.map.conflictDialog.getConflicts() != null) {
130 sel = Main.map.conflictDialog.getConflicts().getMyConflictParties();
131 }
132 }
133 if (sel.isEmpty()) {
134 JOptionPane.showMessageDialog(
135 Main.parent,
136 (mode.equals("selection") ? tr("Nothing selected to zoom to.") : tr("No conflicts to zoom to")),
137 tr("Information"),
138 JOptionPane.INFORMATION_MESSAGE
139 );
140 return null;
141 }
142 for (OsmPrimitive osm : sel) {
143 osm.visit(v);
144 }
145 // increase bbox by 0.001 degrees on each side. this is required
146 // especially if the bbox contains one single node, but helpful
147 // in most other cases as well.
148 v.enlargeBoundingBox();
149 }
150 else if (mode.equals("download")) {
151 if (Main.pref.hasKey("osm-download.bounds")) {
152 try {
153 String bounds[] = Main.pref.get("osm-download.bounds").split(";");
154 double minlat = Double.parseDouble(bounds[0]);
155 double minlon = Double.parseDouble(bounds[1]);
156 double maxlat = Double.parseDouble(bounds[2]);
157 double maxlon = Double.parseDouble(bounds[3]);
158
159 v.visit(Main.proj.latlon2eastNorth(new LatLon(minlat, minlon)));
160 v.visit(Main.proj.latlon2eastNorth(new LatLon(maxlat, maxlon)));
161 }
162 catch (Exception e) {
163 e.printStackTrace();
164 }
165 }
166 }
167 return v;
168 }
169
170 @Override
171 protected void updateEnabledState() {
172 if ("selection".equals(mode)) {
173 setEnabled(getCurrentDataSet() != null && ! getCurrentDataSet().getSelected().isEmpty());
174 } else if ("layer".equals(mode)) {
175 if (Main.map == null || Main.map.mapView == null || Main.map.mapView.getAllLayersAsList().isEmpty()) {
176 setEnabled(false);
177 } else {
178 // FIXME: should also check for whether a layer is selected in the layer list dialog
179 setEnabled(true);
180 }
181 } else {
182 setEnabled(
183 Main.map != null
184 && Main.map.mapView != null
185 && Main.map.mapView.hasLayers()
186 );
187 }
188 }
189
190 @Override
191 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
192 if ("selection".equals(mode)) {
193 setEnabled(selection != null && !selection.isEmpty());
194 }
195 }
196}
Note: See TracBrowser for help on using the repository browser.