Changeset 14221 in josm for trunk/src/org/openstreetmap/josm


Ignore:
Timestamp:
2018-09-03T20:15:11+02:00 (6 years ago)
Author:
Don-vip
Message:

see #16706 - use documented enum for AutoScaleMode instead of undocumented string constants

Location:
trunk/src/org/openstreetmap/josm
Files:
10 edited

Legend:

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

    r14153 r14221  
    1515import java.util.HashSet;
    1616import java.util.List;
     17import java.util.Objects;
    1718import java.util.concurrent.TimeUnit;
    1819
     
    4546 * Toggles the autoScale feature of the mapView
    4647 * @author imi
     48 * @since 17
    4749 */
    4850public class AutoScaleAction extends JosmAction {
     
    5052    /**
    5153     * A list of things we can zoom to. The zoom target is given depending on the mode.
    52      */
     54     * @since 14221
     55     */
     56    public enum AutoScaleMode {
     57        /** Zoom the window so that all the data fills the window area */
     58        DATA(marktr(/* ICON(dialogs/autoscale/) */ "data")),
     59        /** Zoom the window so that all the data on the currently selected layer fills the window area */
     60        LAYER(marktr(/* ICON(dialogs/autoscale/) */ "layer")),
     61        /** Zoom the window so that only data which is currently selected fills the window area */
     62        SELECTION(marktr(/* ICON(dialogs/autoscale/) */ "selection")),
     63        /** Zoom to the first selected conflict */
     64        CONFLICT(marktr(/* ICON(dialogs/autoscale/) */ "conflict")),
     65        /** Zoom the view to last downloaded data */
     66        DOWNLOAD(marktr(/* ICON(dialogs/autoscale/) */ "download")),
     67        /** Zoom the view to problem */
     68        PROBLEM(marktr(/* ICON(dialogs/autoscale/) */ "problem")),
     69        /** Zoom to the previous zoomed to scale and location (zoom undo) */
     70        PREVIOUS(marktr(/* ICON(dialogs/autoscale/) */ "previous")),
     71        /** Zoom to the next zoomed to scale and location (zoom redo) */
     72        NEXT(marktr(/* ICON(dialogs/autoscale/) */ "next"));
     73
     74        private final String label;
     75
     76        AutoScaleMode(String label) {
     77            this.label = label;
     78        }
     79
     80        /**
     81         * Returns the English label. Used for retrieving icons.
     82         * @return the English label
     83         */
     84        public String getEnglishLabel() {
     85            return label;
     86        }
     87
     88        /**
     89         * Returns the localized label. Used for display
     90         * @return the localized label
     91         */
     92        public String getLocalizedLabel() {
     93            return tr(label);
     94        }
     95
     96        /**
     97         * Returns {@code AutoScaleMode} for a given English label
     98         * @param englishLabel English label
     99         * @return {@code AutoScaleMode} for given English label
     100         * @throws IllegalArgumentException if Engligh label is unknown
     101         */
     102        public static AutoScaleMode of(String englishLabel) {
     103            for (AutoScaleMode v : values()) {
     104                if (Objects.equals(v.label, englishLabel)) {
     105                    return v;
     106                }
     107            }
     108            throw new IllegalArgumentException(englishLabel);
     109        }
     110    }
     111
     112    /**
     113     * A list of things we can zoom to. The zoom target is given depending on the mode.
     114     * @deprecated Use {@link AutoScaleMode} enum instead
     115     */
     116    @Deprecated
    53117    public static final Collection<String> MODES = Collections.unmodifiableList(Arrays.asList(
    54118        marktr(/* ICON(dialogs/autoscale/) */ "data"),
     
    62126
    63127    /**
    64      * One of {@link #MODES}. Defines what we are zooming to.
    65      */
    66     private final String mode;
     128     * One of {@link AutoScaleMode}. Defines what we are zooming to.
     129     */
     130    private final AutoScaleMode mode;
    67131
    68132    /** Time of last zoom to bounds action */
     
    112176     * Performs the auto scale operation of the given mode without the need to create a new action.
    113177     * @param mode One of {@link #MODES}.
    114      */
     178     * @since 14221
     179     */
     180    public static void autoScale(AutoScaleMode mode) {
     181        new AutoScaleAction(mode, false).autoScale();
     182    }
     183
     184    /**
     185     * Performs the auto scale operation of the given mode without the need to create a new action.
     186     * @param mode One of {@link #MODES}.
     187     * @deprecated Use {@link #autoScale(AutoScaleMode)} instead
     188     */
     189    @Deprecated
    115190    public static void autoScale(String mode) {
    116         new AutoScaleAction(mode, false).autoScale();
     191        autoScale(AutoScaleMode.of(mode));
    117192    }
    118193
     
    140215    /**
    141216     * Constructs a new {@code AutoScaleAction}.
    142      * @param mode The autoscale mode (one of {@link AutoScaleAction#MODES})
     217     * @param mode The autoscale mode (one of {@link AutoScaleMode})
    143218     * @param marker Must be set to false. Used only to differentiate from default constructor
    144219     */
    145     private AutoScaleAction(String mode, boolean marker) {
     220    private AutoScaleAction(AutoScaleMode mode, boolean marker) {
    146221        super(marker);
    147222        this.mode = mode;
     
    151226     * Constructs a new {@code AutoScaleAction}.
    152227     * @param mode The autoscale mode (one of {@link AutoScaleAction#MODES})
    153      */
     228     * @deprecated Use {@link #AutoScaleAction(AutoScaleMode)} instead
     229     */
     230    @Deprecated
    154231    public AutoScaleAction(final String mode) {
    155         super(tr("Zoom to {0}", tr(mode)), "dialogs/autoscale/" + mode, tr("Zoom the view to {0}.", tr(mode)),
    156                 Shortcut.registerShortcut("view:zoom" + mode, tr("View: {0}", tr("Zoom to {0}", tr(mode))),
    157                         getModeShortcut(mode), Shortcut.DIRECT), true, null, false);
    158         String modeHelp = Character.toUpperCase(mode.charAt(0)) + mode.substring(1);
     232        this(AutoScaleMode.of(mode));
     233    }
     234
     235    /**
     236     * Constructs a new {@code AutoScaleAction}.
     237     * @param mode The autoscale mode (one of {@link AutoScaleAction#MODES})
     238     * @since 14221
     239     */
     240    public AutoScaleAction(final AutoScaleMode mode) {
     241        super(tr("Zoom to {0}", mode.getLocalizedLabel()), "dialogs/autoscale/" + mode.getEnglishLabel(),
     242              tr("Zoom the view to {0}.", mode.getLocalizedLabel()),
     243              Shortcut.registerShortcut("view:zoom" + mode.getEnglishLabel(),
     244                        tr("View: {0}", tr("Zoom to {0}", mode.getLocalizedLabel())),
     245                        getModeShortcut(mode.getEnglishLabel()), Shortcut.DIRECT), true, null, false);
     246        String label = mode.getEnglishLabel();
     247        String modeHelp = Character.toUpperCase(label.charAt(0)) + label.substring(1);
    159248        putValue("help", "Action/AutoScale/" + modeHelp);
    160249        this.mode = mode;
    161250        switch (mode) {
    162         case "data":
     251        case DATA:
    163252            putValue("help", ht("/Action/ZoomToData"));
    164253            break;
    165         case "layer":
     254        case LAYER:
    166255            putValue("help", ht("/Action/ZoomToLayer"));
    167256            break;
    168         case "selection":
     257        case SELECTION:
    169258            putValue("help", ht("/Action/ZoomToSelection"));
    170259            break;
    171         case "conflict":
     260        case CONFLICT:
    172261            putValue("help", ht("/Action/ZoomToConflict"));
    173262            break;
    174         case "problem":
     263        case PROBLEM:
    175264            putValue("help", ht("/Action/ZoomToProblem"));
    176265            break;
    177         case "download":
     266        case DOWNLOAD:
    178267            putValue("help", ht("/Action/ZoomToDownload"));
    179268            break;
    180         case "previous":
     269        case PREVIOUS:
    181270            putValue("help", ht("/Action/ZoomToPrevious"));
    182271            break;
    183         case "next":
     272        case NEXT:
    184273            putValue("help", ht("/Action/ZoomToNext"));
    185274            break;
     
    197286            MapView mapView = MainApplication.getMap().mapView;
    198287            switch (mode) {
    199             case "previous":
     288            case PREVIOUS:
    200289                mapView.zoomPrevious();
    201290                break;
    202             case "next":
     291            case NEXT:
    203292                mapView.zoomNext();
    204293                break;
     
    241330    private BoundingXYVisitor getBoundingBox() {
    242331        switch (mode) {
    243         case "problem":
     332        case PROBLEM:
    244333            return modeProblem(new ValidatorBoundingXYVisitor());
    245         case "data":
     334        case DATA:
    246335            return modeData(new BoundingXYVisitor());
    247         case "layer":
     336        case LAYER:
    248337            return modeLayer(new BoundingXYVisitor());
    249         case "selection":
    250         case "conflict":
     338        case SELECTION:
     339        case CONFLICT:
    251340            return modeSelectionOrConflict(new BoundingXYVisitor());
    252         case "download":
     341        case DOWNLOAD:
    253342            return modeDownload(new BoundingXYVisitor());
    254343        default:
     
    286375    private BoundingXYVisitor modeSelectionOrConflict(BoundingXYVisitor v) {
    287376        Collection<IPrimitive> sel = new HashSet<>();
    288         if ("selection".equals(mode)) {
     377        if (AutoScaleMode.SELECTION == mode) {
    289378            OsmData<?, ?, ?, ?> dataSet = getLayerManager().getActiveData();
    290379            if (dataSet != null) {
     
    303392            JOptionPane.showMessageDialog(
    304393                    MainApplication.getMainFrame(),
    305                     "selection".equals(mode) ? tr("Nothing selected to zoom to.") : tr("No conflicts to zoom to"),
     394                    AutoScaleMode.SELECTION == mode ? tr("Nothing selected to zoom to.") : tr("No conflicts to zoom to"),
    306395                    tr("Information"),
    307396                    JOptionPane.INFORMATION_MESSAGE);
     
    357446        MapFrame map = MainApplication.getMap();
    358447        switch (mode) {
    359         case "selection":
     448        case SELECTION:
    360449            setEnabled(ds != null && !ds.selectionEmpty());
    361450            break;
    362         case "layer":
     451        case LAYER:
    363452            setEnabled(getFirstSelectedLayer() != null);
    364453            break;
    365         case "conflict":
     454        case CONFLICT:
    366455            setEnabled(map != null && map.conflictDialog.getSelectedConflict() != null);
    367456            break;
    368         case "download":
     457        case DOWNLOAD:
    369458            setEnabled(ds != null && !ds.getDataSources().isEmpty());
    370459            break;
    371         case "problem":
     460        case PROBLEM:
    372461            setEnabled(map != null && map.validatorDialog.getSelectedError() != null);
    373462            break;
    374         case "previous":
     463        case PREVIOUS:
    375464            setEnabled(MainApplication.isDisplayingMapView() && map.mapView.hasZoomUndoEntries());
    376465            break;
    377         case "next":
     466        case NEXT:
    378467            setEnabled(MainApplication.isDisplayingMapView() && map.mapView.hasZoomRedoEntries());
    379468            break;
     
    385474    @Override
    386475    protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
    387         if ("selection".equals(mode)) {
     476        if (AutoScaleMode.SELECTION == mode) {
    388477            setEnabled(selection != null && !selection.isEmpty());
    389478        }
     
    418507
    419508        MapFrameAdapter() {
    420             if ("conflict".equals(mode)) {
     509            if (AutoScaleMode.CONFLICT == mode) {
    421510                conflictSelectionListener = e -> updateEnabledState();
    422             } else if ("problem".equals(mode)) {
     511            } else if (AutoScaleMode.PROBLEM == mode) {
    423512                validatorSelectionListener = e -> updateEnabledState();
    424513            }
  • trunk/src/org/openstreetmap/josm/actions/ZoomToAction.java

    r14143 r14221  
    1010import javax.swing.event.ListSelectionListener;
    1111
     12import org.openstreetmap.josm.actions.AutoScaleAction.AutoScaleMode;
    1213import org.openstreetmap.josm.data.osm.OsmPrimitive;
    1314import org.openstreetmap.josm.gui.MainApplication;
     
    99100        if (layer != null && primitive != null) {
    100101            layer.data.setSelected(primitive);
    101             AutoScaleAction.autoScale("selection");
     102            AutoScaleAction.autoScale(AutoScaleMode.SELECTION);
    102103        }
    103104    }
  • trunk/src/org/openstreetmap/josm/gui/MainMenu.java

    r14138 r14221  
    3030import org.openstreetmap.josm.actions.AlignInLineAction;
    3131import org.openstreetmap.josm.actions.AutoScaleAction;
     32import org.openstreetmap.josm.actions.AutoScaleAction.AutoScaleMode;
    3233import org.openstreetmap.josm.actions.ChangesetManagerToggleAction;
    3334import org.openstreetmap.josm.actions.CloseChangesetAction;
     
    720721        add(viewMenu, new ZoomOutAction());
    721722        viewMenu.addSeparator();
    722         for (String mode : AutoScaleAction.MODES) {
     723        for (AutoScaleMode mode : AutoScaleMode.values()) {
    723724            AutoScaleAction autoScaleAction = new AutoScaleAction(mode);
    724             autoScaleActions.put(mode, autoScaleAction);
     725            autoScaleActions.put(mode.getEnglishLabel(), autoScaleAction);
    725726            add(viewMenu, autoScaleAction);
    726727        }
  • trunk/src/org/openstreetmap/josm/gui/dialogs/CommandStackDialog.java

    r14134 r14221  
    3838
    3939import org.openstreetmap.josm.actions.AutoScaleAction;
     40import org.openstreetmap.josm.actions.AutoScaleAction.AutoScaleMode;
    4041import org.openstreetmap.josm.command.Command;
    4142import org.openstreetmap.josm.command.PseudoCommand;
     
    453454        public void actionPerformed(ActionEvent e) {
    454455            super.actionPerformed(e);
    455             AutoScaleAction.autoScale("selection");
     456            AutoScaleAction.autoScale(AutoScaleMode.SELECTION);
    456457        }
    457458    }
  • trunk/src/org/openstreetmap/josm/gui/dialogs/SelectionListDialog.java

    r13926 r14221  
    3939import org.openstreetmap.josm.actions.AbstractSelectAction;
    4040import org.openstreetmap.josm.actions.AutoScaleAction;
     41import org.openstreetmap.josm.actions.AutoScaleAction.AutoScaleMode;
    4142import org.openstreetmap.josm.actions.relation.DownloadSelectedIncompleteMembersAction;
    4243import org.openstreetmap.josm.actions.relation.EditRelationAction;
     
    393394        @Override
    394395        public void actionPerformed(ActionEvent e) {
    395             AutoScaleAction.autoScale("selection");
     396            AutoScaleAction.autoScale(AutoScaleMode.SELECTION);
    396397        }
    397398
  • trunk/src/org/openstreetmap/josm/gui/dialogs/relation/MemberTable.java

    r14214 r14221  
    2222
    2323import org.openstreetmap.josm.actions.AutoScaleAction;
     24import org.openstreetmap.josm.actions.AutoScaleAction.AutoScaleMode;
    2425import org.openstreetmap.josm.actions.ZoomToAction;
    2526import org.openstreetmap.josm.data.osm.OsmPrimitive;
     
    239240                getLayer().data.setSelected(WayConnectionType.Direction.FORWARD == connectionType.direction
    240241                        ? way.firstNode() : way.lastNode());
    241                 AutoScaleAction.autoScale("selection");
     242                AutoScaleAction.autoScale(AutoScaleMode.SELECTION);
    242243            } else if (!connectionType.linkNext) {
    243244                getLayer().data.setSelected(WayConnectionType.Direction.FORWARD == connectionType.direction
    244245                        ? way.lastNode() : way.firstNode());
    245                 AutoScaleAction.autoScale("selection");
     246                AutoScaleAction.autoScale(AutoScaleMode.SELECTION);
    246247            }
    247248        }
  • trunk/src/org/openstreetmap/josm/gui/history/NodeListViewer.java

    r13926 r14221  
    2222
    2323import org.openstreetmap.josm.actions.AutoScaleAction;
     24import org.openstreetmap.josm.actions.AutoScaleAction.AutoScaleMode;
    2425import org.openstreetmap.josm.data.osm.IPrimitive;
    2526import org.openstreetmap.josm.data.osm.OsmData;
     
    253254                if (ds != null) {
    254255                    ds.setSelected(p.getPrimitiveId());
    255                     AutoScaleAction.autoScale("selection");
     256                    AutoScaleAction.autoScale(AutoScaleMode.SELECTION);
    256257                }
    257258            }
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/AddNodeHandler.java

    r14134 r14221  
    99
    1010import org.openstreetmap.josm.actions.AutoScaleAction;
     11import org.openstreetmap.josm.actions.AutoScaleAction.AutoScaleMode;
    1112import org.openstreetmap.josm.command.AddCommand;
    1213import org.openstreetmap.josm.data.UndoRedoHandler;
     
    107108        ds.setSelected(node);
    108109        if (PermissionPrefWithDefault.CHANGE_VIEWPORT.isAllowed()) {
    109             AutoScaleAction.autoScale("selection");
     110            AutoScaleAction.autoScale(AutoScaleMode.SELECTION);
    110111        } else {
    111112            MainApplication.getMap().mapView.repaint();
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/AddWayHandler.java

    r14143 r14221  
    1414
    1515import org.openstreetmap.josm.actions.AutoScaleAction;
     16import org.openstreetmap.josm.actions.AutoScaleAction.AutoScaleMode;
    1617import org.openstreetmap.josm.command.AddCommand;
    1718import org.openstreetmap.josm.command.Command;
     
    174175        ds.setSelected(way);
    175176        if (PermissionPrefWithDefault.CHANGE_VIEWPORT.isAllowed()) {
    176             AutoScaleAction.autoScale("selection");
     177            AutoScaleAction.autoScale(AutoScaleMode.SELECTION);
    177178        } else {
    178179            MainApplication.getMap().mapView.repaint();
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/LoadAndZoomHandler.java

    r13973 r14221  
    1717
    1818import org.openstreetmap.josm.actions.AutoScaleAction;
     19import org.openstreetmap.josm.actions.AutoScaleAction.AutoScaleMode;
    1920import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
    2021import org.openstreetmap.josm.actions.downloadtasks.DownloadParams;
     
    264265        // zoom_mode=(download|selection), defaults to selection
    265266        if (!"download".equals(args.get("zoom_mode")) && !primitives.isEmpty()) {
    266             AutoScaleAction.autoScale("selection");
     267            AutoScaleAction.autoScale(AutoScaleMode.SELECTION);
    267268        } else if (MainApplication.isDisplayingMapView()) {
    268269            // make sure this isn't called unless there *is* a MapView
Note: See TracChangeset for help on using the changeset viewer.