Ignore:
Timestamp:
2008-09-13T13:37:31+02:00 (18 years ago)
Author:
stoecker
Message:

allow to use zoom from plugins

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/AutoScaleAction.java

    r797 r948  
    22package org.openstreetmap.josm.actions;
    33
     4import static org.openstreetmap.josm.tools.I18n.marktr;
    45import static org.openstreetmap.josm.tools.I18n.tr;
    5 import static org.openstreetmap.josm.tools.I18n.marktr;
    66
    77import java.awt.event.ActionEvent;
     
    1212
    1313import org.openstreetmap.josm.Main;
    14 import org.openstreetmap.josm.data.coor.EastNorth;
    15 import org.openstreetmap.josm.data.coor.LatLon;
    1614import org.openstreetmap.josm.data.osm.OsmPrimitive;
    1715import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
     
    2422public class AutoScaleAction extends JosmAction {
    2523
    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;
    3326
    34         private static int getModeShortcut(String mode) {
    35                 int shortcut = -1;
     27    private static int getModeShortcut(String mode) {
     28        int shortcut = -1;
    3629
    37                 if(mode.equals("data")) {
    38                         shortcut = KeyEvent.VK_1;
    39                 }       
    40                 if(mode.equals("layer")) {
    41                         shortcut = KeyEvent.VK_2;
    42                 }
    43                 if(mode.equals("selection")) {
    44                         shortcut = KeyEvent.VK_3;
    45                 }
    46                 if(mode.equals("conflict")) {
    47                         shortcut = KeyEvent.VK_4;
    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        }
    4942
    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    }
    5945
    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    }
    6953
    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    }
    9888}
Note: See TracChangeset for help on using the changeset viewer.