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

Last change on this file since 2512 was 2512, checked in by stoecker, 15 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
RevLine 
[298]1// License: GPL. Copyright 2007 by Immanuel Scholz and others
[403]2package org.openstreetmap.josm.actions;
3
[2477]4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
[948]5import static org.openstreetmap.josm.tools.I18n.marktr;
[403]6import static org.openstreetmap.josm.tools.I18n.tr;
7
8import java.awt.event.ActionEvent;
[458]9import java.awt.event.KeyEvent;
[403]10import java.util.Collection;
[1750]11import java.util.HashSet;
[1953]12import java.util.List;
[403]13
14import javax.swing.JOptionPane;
15
16import org.openstreetmap.josm.Main;
[2477]17import org.openstreetmap.josm.data.Bounds;
[403]18import org.openstreetmap.josm.data.osm.OsmPrimitive;
19import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
[1953]20import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
[403]21import org.openstreetmap.josm.gui.layer.Layer;
[1084]22import org.openstreetmap.josm.tools.Shortcut;
[403]23
24/**
25 * Toggles the autoScale feature of the mapView
26 * @author imi
27 */
28public class AutoScaleAction extends JosmAction {
29
[1302]30 public static final String[] modes = { marktr("data"), marktr("layer"), marktr("selection"), marktr("conflict"), marktr("download") };
[948]31 private final String mode;
[403]32
[948]33 private static int getModeShortcut(String mode) {
34 int shortcut = -1;
[458]35
[948]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 }
[1302]48 if (mode.equals("download")) {
49 shortcut = KeyEvent.VK_5;
50 }
[458]51
[948]52 return shortcut;
53 }
[403]54
[948]55 public AutoScaleAction(String mode) {
56 super(tr("Zoom to {0}", tr(mode)), "dialogs/autoscale/" + mode, tr("Zoom the view to {0}.", tr(mode)),
[1169]57 Shortcut.registerShortcut("view:zoom"+mode, tr("View: {0}", tr("Zoom to {0}", tr(mode))), getModeShortcut(mode), Shortcut.GROUP_EDIT), true);
[948]58 String modeHelp = Character.toUpperCase(mode.charAt(0)) + mode.substring(1);
59 putValue("help", "Action/AutoScale/" + modeHelp);
60 this.mode = mode;
[2323]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"));
[2477]71 }
[948]72 }
[403]73
[1868]74 public void autoScale() {
[948]75 if (Main.map != null) {
76 BoundingXYVisitor bbox = getBoundingBox();
[1797]77 if (bbox != null && bbox.getBounds() != null) {
[948]78 Main.map.mapView.recalculateCenterScale(bbox);
79 }
80 }
81 putValue("active", true);
82 }
83
[1868]84 public void actionPerformed(ActionEvent e) {
85 autoScale();
86 }
87
[1903]88 protected Layer getActiveLayer() {
89 try {
90 return Main.map.mapView.getActiveLayer();
91 } catch(NullPointerException e) {
92 return null;
93 }
94 }
95
[1953]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.
[2512]100 *
[1953]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
[948]110 private BoundingXYVisitor getBoundingBox() {
111 BoundingXYVisitor v = new BoundingXYVisitor();
112 if (mode.equals("data")) {
[1750]113 for (Layer l : Main.map.mapView.getAllLayers()) {
[948]114 l.visitBoundingBox(v);
[1750]115 }
116 } else if (mode.equals("layer")) {
[1903]117 if (getActiveLayer() == null)
118 return null;
[1953]119 // try to zoom to the first selected layer
120 //
121 Layer l = getFirstSelectedLayer();
122 if (l == null) return null;
123 l.visitBoundingBox(v);
[1750]124 } else if (mode.equals("selection") || mode.equals("conflict")) {
125 Collection<OsmPrimitive> sel = new HashSet<OsmPrimitive>();
126 if (mode.equals("selection")) {
[1814]127 sel = getCurrentDataSet().getSelected();
[1750]128 } else if (mode.equals("conflict")) {
129 if (Main.map.conflictDialog.getConflicts() != null) {
130 sel = Main.map.conflictDialog.getConflicts().getMyConflictParties();
131 }
132 }
[948]133 if (sel.isEmpty()) {
[2017]134 JOptionPane.showMessageDialog(
[1847]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 );
[948]140 return null;
141 }
[1750]142 for (OsmPrimitive osm : sel) {
[948]143 osm.visit(v);
[1750]144 }
[1023]145 // increase bbox by 0.001 degrees on each side. this is required
146 // especially if the bbox contains one single node, but helpful
[948]147 // in most other cases as well.
148 v.enlargeBoundingBox();
149 }
[1302]150 else if (mode.equals("download")) {
151 if (Main.pref.hasKey("osm-download.bounds")) {
152 try {
[2477]153 v.visit(new Bounds(Main.pref.get("osm-download.bounds"), ";"));
154 } catch (Exception e) {
[1302]155 e.printStackTrace();
156 }
157 }
158 }
[948]159 return v;
160 }
[1820]161
162 @Override
163 protected void updateEnabledState() {
[1854]164 if ("selection".equals(mode)) {
165 setEnabled(getCurrentDataSet() != null && ! getCurrentDataSet().getSelected().isEmpty());
[1903]166 } else if ("layer".equals(mode)) {
[1953]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 }
[1854]173 } else {
174 setEnabled(
[2343]175 Main.isDisplayingMapView()
[1895]176 && Main.map.mapView.hasLayers()
[1854]177 );
178 }
[1820]179 }
[2256]180
181 @Override
182 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
183 if ("selection".equals(mode)) {
184 setEnabled(selection != null && !selection.isEmpty());
185 }
186 }
[403]187}
Note: See TracBrowser for help on using the repository browser.