Changeset 15500 in josm


Ignore:
Timestamp:
2019-11-03T15:13:20+01:00 (2 years ago)
Author:
Don-vip
Message:

fix #18283 - Remote controle: don't hardcode PermissionPrefWithDefault values for getPermissionPrefs (patch by taylor.smock, modified)

Location:
trunk/src/org/openstreetmap/josm/io/remotecontrol
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/PermissionPrefWithDefault.java

    r12846 r15500  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
    6 import java.util.Arrays;
     6import java.util.ArrayList;
     7import java.util.Collections;
    78import java.util.List;
     9import java.util.stream.Collectors;
    810
    911import org.openstreetmap.josm.spi.preferences.Config;
     
    1719 */
    1820public class PermissionPrefWithDefault {
     21    private static final List<PermissionPrefWithDefault> PREFS = new ArrayList<>();
    1922
    2023    public static final PermissionPrefWithDefault LOAD_DATA =
     
    4750    public final String preferenceText;
    4851
     52    /**
     53     * Create a new {@code PermissionPrefWithDefault}
     54     *
     55     * @param pref           The preference key for the permission
     56     * @param defaultVal     The default value of the preference
     57     * @param preferenceText The text to show in UI objects
     58     */
    4959    public PermissionPrefWithDefault(String pref, boolean defaultVal, String preferenceText) {
    5060        this.pref = pref;
     
    5363    }
    5464
     65    /**
     66     * Determines if the action is allowed.
     67     * @return true if the action is allowed
     68     */
    5569    public boolean isAllowed() {
    5670        return Config.getPref().getBoolean(pref, defaultVal);
    5771    }
    5872
     73    /**
     74     * Returns a non-modifiable list of permission preferences for Remote Control.
     75     * @return A non-modifiable list of permission preferences for Remote Control
     76     */
    5977    public static List<PermissionPrefWithDefault> getPermissionPrefs() {
    60         return Arrays.asList(
    61                 LOAD_DATA, IMPORT_DATA, OPEN_FILES, LOAD_IMAGERY,
    62                 CHANGE_SELECTION, CHANGE_VIEWPORT,
    63                 CREATE_OBJECTS, READ_PROTOCOL_VERSION);
     78        return Collections.unmodifiableList(PREFS);
     79    }
     80
     81    /**
     82     * Adds a permission preference.
     83     * @param pref The preference to add to the list returned by
     84     *             {@link PermissionPrefWithDefault#getPermissionPrefs}
     85     * @since 15500
     86     */
     87    public static void addPermissionPref(PermissionPrefWithDefault pref) {
     88        if (pref.pref != null && PREFS.parallelStream().noneMatch(tPref -> pref.pref.equals(tPref.pref)))
     89            PREFS.add(pref);
     90    }
     91
     92    /**
     93     * Removes a permission preference.
     94     * @param pref The preference to remove from the list returned by
     95     *             {@link PermissionPrefWithDefault#getPermissionPrefs}
     96     *
     97     * @return see {@link List#removeAll}
     98     * @since 15500
     99     */
     100    public static boolean removePermissionPref(PermissionPrefWithDefault pref) {
     101        List<PermissionPrefWithDefault> toRemove = Collections.emptyList();
     102        if (pref.pref != null)
     103            toRemove = PREFS.parallelStream().filter(tPref -> pref.pref.equals(tPref.pref))
     104                    .collect(Collectors.toList());
     105        return PREFS.removeAll(toRemove);
    64106    }
    65107}
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/RequestProcessor.java

    r12620 r15500  
    2020import java.util.Map;
    2121import java.util.Map.Entry;
     22import java.util.Optional;
    2223import java.util.StringTokenizer;
    2324import java.util.TreeMap;
     
    127128            }
    128129            handlers.put(commandWithSlash, handler);
    129         }
    130     }
    131 
    132     /** Add default request handlers */
     130            try {
     131                Optional.ofNullable(handler.getConstructor().newInstance().getPermissionPref())
     132                        .ifPresent(PermissionPrefWithDefault::addPermissionPref);
     133            } catch (ReflectiveOperationException | RuntimeException e) {
     134                Logging.debug(e);
     135            }
     136        }
     137    }
     138
     139    /**
     140     * Add default request handlers and permission preferences (order is important)
     141     */
    133142    static {
    134143        addRequestHandlerClass(LoadAndZoomHandler.command, LoadAndZoomHandler.class, true);
    135144        addRequestHandlerClass(LoadAndZoomHandler.command2, LoadAndZoomHandler.class, true);
     145        addRequestHandlerClass(LoadObjectHandler.command, LoadObjectHandler.class, true);
    136146        addRequestHandlerClass(LoadDataHandler.command, LoadDataHandler.class, true);
     147        addRequestHandlerClass(ImportHandler.command, ImportHandler.class, true);
     148        addRequestHandlerClass(OpenFileHandler.command, OpenFileHandler.class, true);
    137149        addRequestHandlerClass(ImageryHandler.command, ImageryHandler.class, true);
     150        PermissionPrefWithDefault.addPermissionPref(PermissionPrefWithDefault.CHANGE_SELECTION);
     151        PermissionPrefWithDefault.addPermissionPref(PermissionPrefWithDefault.CHANGE_VIEWPORT);
    138152        addRequestHandlerClass(AddNodeHandler.command, AddNodeHandler.class, true);
    139153        addRequestHandlerClass(AddWayHandler.command, AddWayHandler.class, true);
    140         addRequestHandlerClass(ImportHandler.command, ImportHandler.class, true);
    141154        addRequestHandlerClass(VersionHandler.command, VersionHandler.class, true);
    142         addRequestHandlerClass(LoadObjectHandler.command, LoadObjectHandler.class, true);
    143         addRequestHandlerClass(OpenFileHandler.command, OpenFileHandler.class, true);
    144155        addRequestHandlerClass(FeaturesHandler.command, FeaturesHandler.class, true);
    145156    }
Note: See TracChangeset for help on using the changeset viewer.