Changeset 8554 in josm for trunk/src/org


Ignore:
Timestamp:
2015-07-01T21:33:04+02:00 (9 years ago)
Author:
bastiK
Message:

applied #11634 - Moved global SOM management out of NavigationComponent to SystemOfMeasurement class (patch by michael2402)

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

Legend:

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

    r8540 r8554  
    3232import org.openstreetmap.josm.gui.MapFrame;
    3333import org.openstreetmap.josm.gui.MapView;
    34 import org.openstreetmap.josm.gui.NavigatableComponent;
    3534import org.openstreetmap.josm.gui.layer.Layer;
    3635import org.openstreetmap.josm.gui.layer.MapViewPaintable;
     
    424423            // - Snap steps relative to the distance?
    425424            double snapDistance;
    426             SystemOfMeasurement som = NavigatableComponent.getSystemOfMeasurement();
     425            SystemOfMeasurement som = SystemOfMeasurement.getSystemOfMeasurement();
    427426            if (som.equals(SystemOfMeasurement.CHINESE)) {
    428427                snapDistance = snapDistanceChinese * SystemOfMeasurement.CHINESE.aValue;
  • trunk/src/org/openstreetmap/josm/data/SystemOfMeasurement.java

    r8540 r8554  
    88import java.util.Locale;
    99import java.util.Map;
     10import java.util.concurrent.CopyOnWriteArrayList;
    1011
    1112import org.openstreetmap.josm.Main;
     13import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
    1214
    1315/**
    1416 * A system of units used to express length and area measurements.
     17 * <p>
     18 * This class also manages one globally set system of measurement stored in the {@link ProjectionPreference}
    1519 * @since 3406 (creation)
    1620 * @since 6992 (extraction in this package)
    1721 */
    1822public class SystemOfMeasurement {
     23
     24    /**
     25     * Interface to notify listeners of the change of the system of measurement.
     26     */
     27    public interface SoMChangeListener {
     28        /**
     29         * The current SoM has changed.
     30         * @param oldSoM The old system of measurement
     31         * @param newSoM The new (current) system of measurement
     32         */
     33        void systemOfMeasurementChanged(String oldSoM, String newSoM);
     34    }
    1935
    2036    /**
     
    5369        ALL_SYSTEMS.put(marktr("Imperial"), IMPERIAL);
    5470        ALL_SYSTEMS.put(marktr("Nautical Mile"), NAUTICAL_MILE);
     71    }
     72
     73    private static final CopyOnWriteArrayList<SoMChangeListener> somChangeListeners = new CopyOnWriteArrayList<>();
     74
     75    /**
     76     * Removes a global SoM change listener
     77     *
     78     * @param listener the listener. Ignored if null or already absent
     79     */
     80    public static void removeSoMChangeListener(SoMChangeListener listener) {
     81        somChangeListeners.remove(listener);
     82    }
     83
     84    /**
     85     * Adds a SoM change listener
     86     *
     87     * @param listener the listener. Ignored if null or already registered.
     88     */
     89    public static void addSoMChangeListener(SoMChangeListener listener) {
     90        if (listener != null) {
     91            somChangeListeners.addIfAbsent(listener);
     92        }
     93    }
     94
     95    protected static void fireSoMChanged(String oldSoM, String newSoM) {
     96        for (SoMChangeListener l : somChangeListeners) {
     97            l.systemOfMeasurementChanged(oldSoM, newSoM);
     98        }
     99    }
     100
     101    /**
     102     * Returns the current global system of measurement.
     103     * @return The current system of measurement (metric system by default).
     104     */
     105    public static SystemOfMeasurement getSystemOfMeasurement() {
     106        SystemOfMeasurement som = SystemOfMeasurement.ALL_SYSTEMS.get(ProjectionPreference.PROP_SYSTEM_OF_MEASUREMENT.get());
     107        if (som == null)
     108            return SystemOfMeasurement.METRIC;
     109        return som;
     110    }
     111
     112    /**
     113     * Sets the current global system of measurement.
     114     * @param somKey The system of measurement key. Must be defined in {@link SystemOfMeasurement#ALL_SYSTEMS}.
     115     * @throws IllegalArgumentException if {@code somKey} is not known
     116     */
     117    public static void setSystemOfMeasurement(String somKey) {
     118        if (!SystemOfMeasurement.ALL_SYSTEMS.containsKey(somKey)) {
     119            throw new IllegalArgumentException("Invalid system of measurement: "+somKey);
     120        }
     121        String oldKey = ProjectionPreference.PROP_SYSTEM_OF_MEASUREMENT.get();
     122        if (ProjectionPreference.PROP_SYSTEM_OF_MEASUREMENT.put(somKey)) {
     123            fireSoMChanged(oldKey, somKey);
     124        }
    55125    }
    56126
  • trunk/src/org/openstreetmap/josm/gui/MapStatus.java

    r8513 r8554  
    5454import org.openstreetmap.josm.data.Preferences.PreferenceChangedListener;
    5555import org.openstreetmap.josm.data.SystemOfMeasurement;
     56import org.openstreetmap.josm.data.SystemOfMeasurement.SoMChangeListener;
    5657import org.openstreetmap.josm.data.coor.CoordinateFormat;
    5758import org.openstreetmap.josm.data.coor.LatLon;
     
    6061import org.openstreetmap.josm.data.osm.Way;
    6162import org.openstreetmap.josm.data.preferences.ColorProperty;
    62 import org.openstreetmap.josm.gui.NavigatableComponent.SoMChangeListener;
    6363import org.openstreetmap.josm.gui.help.Helpful;
    6464import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
     
    865865        }
    866866
    867         NavigatableComponent.addSoMChangeListener(somListener = new SoMChangeListener() {
     867        SystemOfMeasurement.addSoMChangeListener(somListener = new SoMChangeListener() {
    868868            @Override
    869869            public void systemOfMeasurementChanged(String oldSoM, String newSoM) {
     
    908908     */
    909909    public void updateSystemOfMeasurement(String newsom) {
    910         NavigatableComponent.setSystemOfMeasurement(newsom);
     910        SystemOfMeasurement.setSystemOfMeasurement(newsom);
    911911        if (Main.pref.getBoolean("statusbar.notify.change-system-of-measurement", true)) {
    912912            new Notification(tr("System of measurement changed to {0}", newsom))
     
    10201020    @Override
    10211021    public void destroy() {
    1022         NavigatableComponent.removeSoMChangeListener(somListener);
     1022        SystemOfMeasurement.removeSoMChangeListener(somListener);
    10231023        Main.pref.removePreferenceChangeListener(this);
    10241024
  • trunk/src/org/openstreetmap/josm/gui/NavigatableComponent.java

    r8510 r8554  
    5252import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
    5353import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
    54 import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
    5554import org.openstreetmap.josm.tools.Predicate;
    5655import org.openstreetmap.josm.tools.Utils;
     
    7978     * @since 6056
    8079     */
    81     public interface SoMChangeListener {
    82         /**
    83          * The current SoM has changed.
    84          * @param oldSoM The old system of measurement
    85          * @param newSoM The new (current) system of measurement
    86          */
    87         void systemOfMeasurementChanged(String oldSoM, String newSoM);
     80    @Deprecated
     81    public interface SoMChangeListener extends SystemOfMeasurement.SoMChangeListener {
    8882    }
    8983
     
    138132    }
    139133
    140     private static final CopyOnWriteArrayList<SoMChangeListener> somChangeListeners = new CopyOnWriteArrayList<>();
    141134
    142135    /**
     
    146139     * @since 6056
    147140     */
     141    @Deprecated
    148142    public static void removeSoMChangeListener(NavigatableComponent.SoMChangeListener listener) {
    149         somChangeListeners.remove(listener);
     143        SystemOfMeasurement.removeSoMChangeListener(listener);
    150144    }
    151145
     
    156150     * @since 6056
    157151     */
     152    @Deprecated
    158153    public static void addSoMChangeListener(NavigatableComponent.SoMChangeListener listener) {
    159         if (listener != null) {
    160             somChangeListeners.addIfAbsent(listener);
    161         }
    162     }
    163 
    164     protected static void fireSoMChanged(String oldSoM, String newSoM) {
    165         for (SoMChangeListener l : somChangeListeners) {
    166             l.systemOfMeasurementChanged(oldSoM, newSoM);
    167         }
    168     }
    169 
    170     /**
    171      * The scale factor in x or y-units per pixel. This means, if scale = 10,
    172      * every physical pixel on screen are 10 x or 10 y units in the
    173      * northing/easting space of the projection.
    174      */
     154        SystemOfMeasurement.addSoMChangeListener(listener);
     155    }
     156
     157    /**
     158     * Returns the current system of measurement.
     159     * @return The current system of measurement (metric system by default).
     160     * @since 3490
     161     */
     162    @Deprecated
     163    public static SystemOfMeasurement getSystemOfMeasurement() {
     164        return SystemOfMeasurement.getSystemOfMeasurement();
     165    }
     166
     167    /**
     168     * Sets the current system of measurement.
     169     * @param somKey The system of measurement key. Must be defined in {@link SystemOfMeasurement#ALL_SYSTEMS}.
     170     * @throws IllegalArgumentException if {@code somKey} is not known
     171     * @since 6056
     172     */
     173    @Deprecated
     174    public static void setSystemOfMeasurement(String somKey) {
     175        SystemOfMeasurement.setSystemOfMeasurement(somKey);
     176    }
     177
    175178    private double scale = Main.getProjection().getDefaultZoomInPPD();
    176 
    177179    /**
    178180     * Center n/e coordinate of the desired screen center.
     
    212214     */
    213215    public static String getDistText(double dist) {
    214         return getSystemOfMeasurement().getDistText(dist);
     216        return SystemOfMeasurement.getSystemOfMeasurement().getDistText(dist);
    215217    }
    216218
     
    224226     */
    225227    public static String getDistText(final double dist, final NumberFormat format, final double threshold) {
    226         return getSystemOfMeasurement().getDistText(dist, format, threshold);
     228        return SystemOfMeasurement.getSystemOfMeasurement().getDistText(dist, format, threshold);
    227229    }
    228230
     
    234236     */
    235237    public static String getAreaText(double area) {
    236         return getSystemOfMeasurement().getAreaText(area);
     238        return SystemOfMeasurement.getSystemOfMeasurement().getAreaText(area);
    237239    }
    238240
     
    246248     */
    247249    public static String getAreaText(final double area, final NumberFormat format, final double threshold) {
    248         return getSystemOfMeasurement().getAreaText(area, format, threshold);
     250        return SystemOfMeasurement.getSystemOfMeasurement().getAreaText(area, format, threshold);
    249251    }
    250252
     
    14351437    }
    14361438
    1437     /**
    1438      * Returns the current system of measurement.
    1439      * @return The current system of measurement (metric system by default).
    1440      * @since 3490
    1441      */
    1442     public static SystemOfMeasurement getSystemOfMeasurement() {
    1443         SystemOfMeasurement som = SystemOfMeasurement.ALL_SYSTEMS.get(ProjectionPreference.PROP_SYSTEM_OF_MEASUREMENT.get());
    1444         if (som == null)
    1445             return SystemOfMeasurement.METRIC;
    1446         return som;
    1447     }
    1448 
    1449     /**
    1450      * Sets the current system of measurement.
    1451      * @param somKey The system of measurement key. Must be defined in {@link SystemOfMeasurement#ALL_SYSTEMS}.
    1452      * @throws IllegalArgumentException if {@code somKey} is not known
    1453      * @since 6056
    1454      */
    1455     public static void setSystemOfMeasurement(String somKey) {
    1456         if (!SystemOfMeasurement.ALL_SYSTEMS.containsKey(somKey)) {
    1457             throw new IllegalArgumentException("Invalid system of measurement: "+somKey);
    1458         }
    1459         String oldKey = ProjectionPreference.PROP_SYSTEM_OF_MEASUREMENT.get();
    1460         if (ProjectionPreference.PROP_SYSTEM_OF_MEASUREMENT.put(somKey)) {
    1461             fireSoMChanged(oldKey, somKey);
    1462         }
    1463     }
    1464 
    14651439    private static class CursorInfo {
    14661440        private final Cursor cursor;
  • trunk/src/org/openstreetmap/josm/gui/layer/GpxLayer.java

    r8510 r8554  
    2626import org.openstreetmap.josm.actions.SaveActionBase;
    2727import org.openstreetmap.josm.data.Bounds;
     28import org.openstreetmap.josm.data.SystemOfMeasurement;
    2829import org.openstreetmap.josm.data.gpx.GpxConstants;
    2930import org.openstreetmap.josm.data.gpx.GpxData;
     
    150151                info.append(getTimespanForTrack(trk));
    151152                info.append("</td><td>");
    152                 info.append(NavigatableComponent.getSystemOfMeasurement().getDistText(trk.length()));
     153                info.append(SystemOfMeasurement.getSystemOfMeasurement().getDistText(trk.length()));
    153154                info.append("</td><td>");
    154155                if (trk.getAttributes().containsKey("url")) {
     
    160161        }
    161162
    162         info.append(tr("Length: {0}", NavigatableComponent.getSystemOfMeasurement().getDistText(data.length()))).append("<br>")
     163        info.append(tr("Length: {0}", SystemOfMeasurement.getSystemOfMeasurement().getDistText(data.length()))).append("<br>")
    163164            .append(trn("{0} route, ", "{0} routes, ", data.routes.size(), data.routes.size())).append(
    164165                trn("{0} waypoint", "{0} waypoints", data.waypoints.size(), data.waypoints.size())).append("<br>");
     
    222223            .append(trn("{0} route, ", "{0} routes, ", data.routes.size(), data.routes.size()))
    223224            .append(trn("{0} waypoint", "{0} waypoints", data.waypoints.size(), data.waypoints.size())).append("<br>")
    224             .append(tr("Length: {0}", NavigatableComponent.getSystemOfMeasurement().getDistText(data.length())))
     225            .append(tr("Length: {0}", SystemOfMeasurement.getSystemOfMeasurement().getDistText(data.length())))
    225226            .append("<br></html>");
    226227        return info.toString();
  • trunk/src/org/openstreetmap/josm/gui/layer/gpx/ChooseTrackVisibilityAction.java

    r8510 r8554  
    3333
    3434import org.openstreetmap.josm.Main;
     35import org.openstreetmap.josm.data.SystemOfMeasurement;
    3536import org.openstreetmap.josm.data.gpx.GpxConstants;
    3637import org.openstreetmap.josm.data.gpx.GpxTrack;
    3738import org.openstreetmap.josm.gui.ExtendedDialog;
    38 import org.openstreetmap.josm.gui.NavigatableComponent;
    3939import org.openstreetmap.josm.gui.layer.GpxLayer;
    4040import org.openstreetmap.josm.tools.GBC;
     
    8383        @Override
    8484        public String toString() {
    85             return NavigatableComponent.getSystemOfMeasurement().getDistText(value);
     85            return SystemOfMeasurement.getSystemOfMeasurement().getDistText(value);
    8686        }
    8787    }
  • trunk/src/org/openstreetmap/josm/gui/preferences/projection/ProjectionPreference.java

    r8540 r8554  
    3030import org.openstreetmap.josm.data.projection.CustomProjection;
    3131import org.openstreetmap.josm.data.projection.Projection;
    32 import org.openstreetmap.josm.gui.NavigatableComponent;
    3332import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
    3433import org.openstreetmap.josm.gui.preferences.PreferenceSettingFactory;
     
    384383
    385384        int i = unitsCombo.getSelectedIndex();
    386         NavigatableComponent.setSystemOfMeasurement(unitsValues[i]);
     385        SystemOfMeasurement.setSystemOfMeasurement(unitsValues[i]);
    387386
    388387        return false;
Note: See TracChangeset for help on using the changeset viewer.