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

Last change on this file since 2610 was 2512, checked in by stoecker, 14 years ago

i18n updated, fixed files to reduce problems when applying patches, fix #4017

  • Property svn:eol-style set to native
File size: 6.7 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 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 v.visit(new Bounds(Main.pref.get("osm-download.bounds"), ";"));
154 } catch (Exception e) {
155 e.printStackTrace();
156 }
157 }
158 }
159 return v;
160 }
161
162 @Override
163 protected void updateEnabledState() {
164 if ("selection".equals(mode)) {
165 setEnabled(getCurrentDataSet() != null && ! getCurrentDataSet().getSelected().isEmpty());
166 } else if ("layer".equals(mode)) {
167 if (Main.map == null || Main.map.mapView == null || Main.map.mapView.getAllLayersAsList().isEmpty()) {
168 setEnabled(false);
169 } else {
170 // FIXME: should also check for whether a layer is selected in the layer list dialog
171 setEnabled(true);
172 }
173 } else {
174 setEnabled(
175 Main.isDisplayingMapView()
176 && Main.map.mapView.hasLayers()
177 );
178 }
179 }
180
181 @Override
182 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
183 if ("selection".equals(mode)) {
184 setEnabled(selection != null && !selection.isEmpty());
185 }
186 }
187}
Note: See TracBrowser for help on using the repository browser.