Changeset 6056 in josm for trunk


Ignore:
Timestamp:
2013-07-07T00:21:33+02:00 (6 years ago)
Author:
Don-vip
Message:

fix #6515 - Allow switching system of measurement from a single click on status line

Location:
trunk/src/org/openstreetmap/josm/gui
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/MapFrame.java

    r6020 r6056  
    287287        }
    288288
    289         // MapFrame gets destroyed when the last layer is removed, but the status line background
    290         // thread that collects the information doesn't get destroyed automatically.
    291         if(statusLine.thread != null) {
    292             try {
    293                 statusLine.thread.interrupt();
    294             } catch (Exception e) {
    295                 e.printStackTrace();
    296             }
    297         }
     289        statusLine.destroy();
    298290        mapView.destroy();
    299291    }
  • trunk/src/org/openstreetmap/josm/gui/MapStatus.java

    r5991 r6056  
    2828import java.util.ConcurrentModificationException;
    2929import java.util.List;
     30import java.util.TreeSet;
    3031
    3132import javax.swing.AbstractAction;
     
    5051import org.openstreetmap.josm.data.osm.OsmPrimitive;
    5152import org.openstreetmap.josm.data.osm.Way;
     53import org.openstreetmap.josm.gui.NavigatableComponent.SoMChangeListener;
    5254import org.openstreetmap.josm.gui.help.Helpful;
     55import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
    5356import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
    5457import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor.ProgressMonitorDialog;
     
    5659import org.openstreetmap.josm.gui.widgets.ImageLabel;
    5760import org.openstreetmap.josm.gui.widgets.JosmTextField;
     61import org.openstreetmap.josm.tools.Destroyable;
    5862import org.openstreetmap.josm.tools.GBC;
    5963import org.openstreetmap.josm.tools.ImageProvider;
     
    7276 * @author imi
    7377 */
    74 public class MapStatus extends JPanel implements Helpful {
     78public class MapStatus extends JPanel implements Helpful, Destroyable {
    7579
    7680    /**
     
    142146    public final BackgroundProgressMonitor progressMonitor = new BackgroundProgressMonitor();
    143147   
    144     private MouseListener jumpToOnLeftClick;
     148    private final MouseListener jumpToOnLeftClick;
     149    private final SoMChangeListener somListener;
     150   
     151    private double distValue; // Distance value displayed in distText, stored if refresh needed after a change of system of measurement
    145152
    146153    /**
    147154     * This is the thread that runs in the background and collects the information displayed.
    148      * It gets destroyed by MapFrame.java/destroy() when the MapFrame itself is destroyed.
     155     * It gets destroyed by destroy() when the MapFrame itself is destroyed.
    149156     */
    150     public Thread thread;
     157    private Thread thread;
    151158
    152159    private final List<StatusTextHistory> statusText = new ArrayList<StatusTextHistory>();
     
    751758        add(angleText, GBC.std().insets(3,0,0,0));
    752759        add(distText, GBC.std().insets(3,0,0,0));
    753 
     760       
     761        distText.addMouseListener(new MouseAdapter() {
     762            private final List<String> soms = new ArrayList<String>(new TreeSet<String>(NavigatableComponent.SYSTEMS_OF_MEASUREMENT.keySet()));
     763           
     764            @Override
     765            public void mouseClicked(MouseEvent e) {
     766                String som = ProjectionPreference.PROP_SYSTEM_OF_MEASUREMENT.get();
     767                String newsom = soms.get((soms.indexOf(som)+1)%soms.size());
     768                NavigatableComponent.setSystemOfMeasurement(newsom);
     769            }
     770        });
     771       
     772        NavigatableComponent.addSoMChangeListener(somListener = new SoMChangeListener() {
     773            @Override public void systemOfMeasurementChanged(String oldSoM, String newSoM) {
     774                setDist(distValue);
     775            }
     776        });
     777       
    754778        latText.addMouseListener(jumpToOnLeftClick);
    755779        lonText.addMouseListener(jumpToOnLeftClick);
     
    840864     */
    841865    public void setDist(double dist) {
     866        distValue = dist;
    842867        distText.setText(dist < 0 ? "--" : NavigatableComponent.getDistText(dist));
    843868    }
     
    863888        angleText.setBackground(activeFlag ? ImageLabel.backColorActive : ImageLabel.backColor);
    864889    }
     890
     891    @Override
     892    public void destroy() {
     893        NavigatableComponent.removeSoMChangeListener(somListener);
     894       
     895        // MapFrame gets destroyed when the last layer is removed, but the status line background
     896        // thread that collects the information doesn't get destroyed automatically.
     897        if (thread != null) {
     898            try {
     899                thread.interrupt();
     900            } catch (Exception e) {
     901                e.printStackTrace();
     902            }
     903        }
     904    }
    865905}
  • trunk/src/org/openstreetmap/josm/gui/NavigatableComponent.java

    r5952 r6056  
    6464        void zoomChanged();
    6565    }
    66 
     66   
     67    /**
     68     * Interface to notify listeners of the change of the system of measurement.
     69     * @since 6056
     70     */
     71    public interface SoMChangeListener {
     72        /**
     73         * The current SoM has changed.
     74         * @param oldSoM The old system of measurement
     75         * @param newSoM The new (current) system of measurement
     76         */
     77        void systemOfMeasurementChanged(String oldSoM, String newSoM);
     78    }
     79   
    6780    /**
    6881     * Simple data class that keeps map center and scale in one object.
     
    127140        for (ZoomChangeListener l : zoomChangeListeners) {
    128141            l.zoomChanged();
     142        }
     143    }
     144
     145    /**
     146     * the SoM listeners
     147     */
     148    private static final CopyOnWriteArrayList<SoMChangeListener> somChangeListeners = new CopyOnWriteArrayList<SoMChangeListener>();
     149
     150    /**
     151     * Removes a SoM change listener
     152     *
     153     * @param listener the listener. Ignored if null or already absent
     154     * @since 6056
     155     */
     156    public static void removeSoMChangeListener(NavigatableComponent.SoMChangeListener listener) {
     157        somChangeListeners.remove(listener);
     158    }
     159
     160    /**
     161     * Adds a SoM change listener
     162     *
     163     * @param listener the listener. Ignored if null or already registered.
     164     * @since 6056
     165     */
     166    public static void addSoMChangeListener(NavigatableComponent.SoMChangeListener listener) {
     167        if (listener != null) {
     168            somChangeListeners.addIfAbsent(listener);
     169        }
     170    }
     171   
     172    protected static void fireSoMChanged(String oldSoM, String newSoM) {
     173        for (SoMChangeListener l : somChangeListeners) {
     174            l.systemOfMeasurementChanged(oldSoM, newSoM);
    129175        }
    130176    }
     
    12291275            return METRIC_SOM;
    12301276        return som;
     1277    }
     1278
     1279    /**
     1280     * Sets the current system of measurement.
     1281     * @param somKey The system of measurement key. Must be defined in {@link NavigatableComponent#SYSTEMS_OF_MEASUREMENT}.
     1282     * @since 6056
     1283     * @throws IllegalArgumentException if {@code somKey} is not known
     1284     */
     1285    public static void setSystemOfMeasurement(String somKey) {
     1286        if (!SYSTEMS_OF_MEASUREMENT.containsKey(somKey)) {
     1287            throw new IllegalArgumentException("Invalid system of measurement: "+somKey);
     1288        }
     1289        String oldKey = ProjectionPreference.PROP_SYSTEM_OF_MEASUREMENT.get();
     1290        if (ProjectionPreference.PROP_SYSTEM_OF_MEASUREMENT.put(somKey)) {
     1291            fireSoMChanged(oldKey, somKey);
     1292        }
    12311293    }
    12321294
  • trunk/src/org/openstreetmap/josm/gui/preferences/projection/ProjectionPreference.java

    r5891 r6056  
    371371
    372372        int i = unitsCombo.getSelectedIndex();
    373         PROP_SYSTEM_OF_MEASUREMENT.put(unitsValues[i]);
     373        NavigatableComponent.setSystemOfMeasurement(unitsValues[i]);
    374374
    375375        return false;
Note: See TracChangeset for help on using the changeset viewer.