- Timestamp:
- 2008-09-13T13:37:31+02:00 (16 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/AutoScaleAction.java
r797 r948 2 2 package org.openstreetmap.josm.actions; 3 3 4 import static org.openstreetmap.josm.tools.I18n.marktr; 4 5 import static org.openstreetmap.josm.tools.I18n.tr; 5 import static org.openstreetmap.josm.tools.I18n.marktr;6 6 7 7 import java.awt.event.ActionEvent; … … 12 12 13 13 import org.openstreetmap.josm.Main; 14 import org.openstreetmap.josm.data.coor.EastNorth;15 import org.openstreetmap.josm.data.coor.LatLon;16 14 import org.openstreetmap.josm.data.osm.OsmPrimitive; 17 15 import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor; … … 24 22 public class AutoScaleAction extends JosmAction { 25 23 26 public static final String[] modes = { 27 marktr("data"), 28 marktr("layer"), 29 marktr("selection"), 30 marktr("conflict") 31 }; 32 private final String mode; 24 public static final String[] modes = { marktr("data"), marktr("layer"), marktr("selection"), marktr("conflict") }; 25 private final String mode; 33 26 34 35 27 private static int getModeShortcut(String mode) { 28 int shortcut = -1; 36 29 37 if(mode.equals("data")) {38 39 } 40 if(mode.equals("layer")) {41 42 43 if(mode.equals("selection")) {44 45 46 if(mode.equals("conflict")) {47 48 30 if (mode.equals("data")) { 31 shortcut = KeyEvent.VK_1; 32 } 33 if (mode.equals("layer")) { 34 shortcut = KeyEvent.VK_2; 35 } 36 if (mode.equals("selection")) { 37 shortcut = KeyEvent.VK_3; 38 } 39 if (mode.equals("conflict")) { 40 shortcut = KeyEvent.VK_4; 41 } 49 42 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 } 43 return shortcut; 44 } 59 45 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 } 46 public AutoScaleAction(String mode) { 47 super(tr("Zoom to {0}", tr(mode)), "dialogs/autoscale/" + mode, tr("Zoom the view to {0}.", tr(mode)), 48 AutoScaleAction.getModeShortcut(mode), 0, true); 49 String modeHelp = Character.toUpperCase(mode.charAt(0)) + mode.substring(1); 50 putValue("help", "Action/AutoScale/" + modeHelp); 51 this.mode = mode; 52 } 69 53 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 } 54 public void actionPerformed(ActionEvent e) { 55 if (Main.map != null) { 56 BoundingXYVisitor bbox = getBoundingBox(); 57 if (bbox != null) { 58 Main.map.mapView.recalculateCenterScale(bbox); 59 } 60 } 61 putValue("active", true); 62 } 63 64 private BoundingXYVisitor getBoundingBox() { 65 BoundingXYVisitor v = new BoundingXYVisitor(); 66 if (mode.equals("data")) { 67 for (Layer l : Main.map.mapView.getAllLayers()) 68 l.visitBoundingBox(v); 69 } else if (mode.equals("layer")) 70 Main.map.mapView.getActiveLayer().visitBoundingBox(v); 71 else if (mode.equals("selection") || mode.equals("conflict")) { 72 Collection<OsmPrimitive> sel = mode.equals("selection") ? Main.ds.getSelected() 73 : Main.map.conflictDialog.conflicts.keySet(); 74 if (sel.isEmpty()) { 75 JOptionPane.showMessageDialog(Main.parent, 76 mode.equals("selection") ? tr("Nothing selected to zoom to.") : tr("No conflicts to zoom to")); 77 return null; 78 } 79 for (OsmPrimitive osm : sel) 80 osm.visit(v); 81 // increase bbox by 0.001 degrees on each side. this is required 82 // especially if the bbox contains one single node, but helpful 83 // in most other cases as well. 84 v.enlargeBoundingBox(); 85 } 86 return v; 87 } 98 88 } -
trunk/src/org/openstreetmap/josm/data/osm/visitor/BoundingXYVisitor.java
r760 r948 5 5 import org.openstreetmap.josm.data.Bounds; 6 6 import org.openstreetmap.josm.data.coor.EastNorth; 7 import org.openstreetmap.josm.data.osm.Node; 8 import org.openstreetmap.josm.data.osm.OsmPrimitive; 7 9 import org.openstreetmap.josm.data.osm.Relation; 8 10 import org.openstreetmap.josm.data.osm.RelationMember; 9 import org.openstreetmap.josm.data.osm.Node;10 11 import org.openstreetmap.josm.data.osm.Way; 11 12 12 13 /** 13 * Calculates the total bounding rectangle of a series of OsmPrimitives, using the14 * Calculates the total bounding rectangle of a series of {@link OsmPrimitive} objects, using the 14 15 * EastNorth values as reference. 15 16 * @author imi … … 17 18 public class BoundingXYVisitor implements Visitor { 18 19 19 20 public EastNorth min, max; 20 21 21 22 23 22 public void visit(Node n) { 23 visit(n.eastNorth); 24 } 24 25 25 26 27 26 public void visit(Way w) { 27 w.visitNodes(this); 28 } 28 29 29 30 31 32 33 34 35 36 30 public void visit(Relation e) { 31 // only use direct members 32 for (RelationMember m : e.members) { 33 if (!(m.member instanceof Relation)) { 34 m.member.visit(this); 35 } 36 } 37 } 37 38 38 public void visit(EastNorth eastNorth) { 39 if (eastNorth != null) { 40 if (min == null) 41 min = eastNorth; 42 else if (eastNorth.east() < min.east() || eastNorth.north() < min.north()) 43 min = new EastNorth(Math.min(min.east(), eastNorth.east()), Math.min(min.north(), eastNorth.north())); 44 45 if (max == null) 46 max = eastNorth; 47 else if (eastNorth.east() > max.east() || eastNorth.north() > max.north()) 48 max = new EastNorth(Math.max(max.east(), eastNorth.east()), Math.max(max.north(), eastNorth.north())); 49 } 50 } 39 public void visit(EastNorth eastNorth) { 40 if (eastNorth != null) { 41 if (min == null) 42 min = eastNorth; 43 else if (eastNorth.east() < min.east() || eastNorth.north() < min.north()) 44 min = new EastNorth(Math.min(min.east(), eastNorth.east()), Math.min(min.north(), eastNorth.north())); 51 45 52 /** 53 * @return The bounding box or <code>null</code> if no coordinates have passed 54 */ 55 public Bounds getBounds() { 56 if (min == null || max == null) 57 return null; 58 return new Bounds(Main.proj.eastNorth2latlon(min), Main.proj.eastNorth2latlon(max)); 59 } 46 if (max == null) 47 max = eastNorth; 48 else if (eastNorth.east() > max.east() || eastNorth.north() > max.north()) 49 max = new EastNorth(Math.max(max.east(), eastNorth.east()), Math.max(max.north(), eastNorth.north())); 50 } 51 } 52 53 /** 54 * @return The bounding box or <code>null</code> if no coordinates have passed 55 */ 56 public Bounds getBounds() { 57 if (min == null || max == null) 58 return null; 59 return new Bounds(Main.proj.eastNorth2latlon(min), Main.proj.eastNorth2latlon(max)); 60 } 61 62 /** 63 * Enlarges the calculated bounding box by 0.0001 degrees. 64 * If the bounding box has not been set (<code>min</code> or <code>max</code> 65 * equal <code>null</code>) this method does not do anything. 66 * 67 * @param enlargeDegree 68 */ 69 public void enlargeBoundingBox() { 70 enlargeBoundingBox(0.0001); 71 } 72 73 /** 74 * Enlarges the calculated bounding box by the specified number of degrees. 75 * If the bounding box has not been set (<code>min</code> or <code>max</code> 76 * equal <code>null</code>) this method does not do anything. 77 * 78 * @param enlargeDegree 79 */ 80 public void enlargeBoundingBox(double enlargeDegree) { 81 if (min == null || max == null) 82 return; 83 EastNorth en = new EastNorth(enlargeDegree, enlargeDegree); 84 min = new EastNorth(min.east() - en.east(), min.north() - en.north()); 85 max = new EastNorth(max.east() + en.east(), max.north() + en.north()); 86 } 60 87 }
Note:
See TracChangeset
for help on using the changeset viewer.