Ignore:
Timestamp:
2013-03-25T20:11:11+01:00 (12 years ago)
Author:
zverik
Message:

small refactoring and tuning of offset watcher

Location:
applications/editors/josm/plugins/imagery_offset_db/src/iodb
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/imagery_offset_db/src/iodb/GetImageryOffsetAction.java

    r29394 r29399  
    2626 * @license WTFPL
    2727 */
    28 public class GetImageryOffsetAction extends JosmAction {
     28public class GetImageryOffsetAction extends JosmAction implements ImageryOffsetWatcher.OffsetStateListener {
    2929    private Icon iconOffsetOk;
    3030    private Icon iconOffsetBad;
     
    3939                KeyEvent.VK_I, Shortcut.ALT_CTRL), true);
    4040        iconOffsetOk = ImageProvider.get("getoffset");
    41         iconOffsetBad = ImageProvider.get("getoffsetnow"); // todo: create icon
     41        iconOffsetBad = ImageProvider.get("getoffsetnow");
    4242        ImageryOffsetWatcher.getInstance().register(this);
    4343    }
  • applications/editors/josm/plugins/imagery_offset_db/src/iodb/ImageryOffsetWatcher.java

    r29394 r29399  
    1212 * for the current layer.
    1313 *
    14  * @author zverik
     14 * @author Zverik
     15 * @license WTFPL
    1516 */
    1617public class ImageryOffsetWatcher implements MapView.ZoomChangeListener, MapView.LayerChangeListener {
    1718    private static final double THRESHOLD = 1e-8;
    18     private static final double MAX_DISTANCE = Main.pref.getDouble("iodb.offset.radius", 15) * 1000;
     19    private static ImageryOffsetWatcher instance;
    1920    private Map<Integer, ImageryLayerData> layers = new TreeMap<Integer, ImageryLayerData>();
    20     private List<GetImageryOffsetAction> listeners = new ArrayList<GetImageryOffsetAction>();
     21    private List<OffsetStateListener> listeners = new ArrayList<OffsetStateListener>();
    2122    private Timer time;
    22     private static ImageryOffsetWatcher instance;
    23     private boolean offsetGood;
     23    private double maxDistance;
     24    private boolean offsetGood = true;
    2425
     26    /**
     27     * Create an instance and register it as a listener to MapView.
     28     * Also starts a timer task.
     29     */
    2530    private ImageryOffsetWatcher() {
    26         if( MAX_DISTANCE <= 0 )
    27             return;
     31        maxDistance = Main.pref.getDouble("iodb.offset.radius", 15);
    2832        MapView.addZoomChangeListener(this);
    2933        MapView.addLayerChangeListener(this);
     
    4246    }
    4347
     48    /**
     49     * This class is a singleton, this method returns the instance,
     50     * creating it if neccessary.
     51     */
    4452    public static ImageryOffsetWatcher getInstance() {
    4553        if( instance == null ) {
     
    4957    }
    5058
    51     public void register( GetImageryOffsetAction listener ) {
     59    /**
     60     * Register an offset state listener.
     61     */
     62    public void register( OffsetStateListener listener ) {
    5263        listeners.add(listener);
    5364        listener.offsetStateChanged(offsetGood);
    5465    }
    5566
    56     public void unregister( GetImageryOffsetAction listener ) {
     67    /**
     68     * Unregister an offset state listener.
     69     */
     70    public void unregister( OffsetStateListener listener ) {
    5771        listeners.remove(listener);
    5872    }
    5973
     74    /**
     75     * Change stored offset state, notify listeners if needed.
     76     */
    6077    private void setOffsetGood( boolean good ) {
    6178        if( good != offsetGood ) {
    62             for( GetImageryOffsetAction listener : listeners )
     79            for( OffsetStateListener listener : listeners )
    6380                listener.offsetStateChanged(good);
    6481        }
     
    6683    }
    6784
     85    /**
     86     * Check if the offset state has been changed.
     87     */
    6888    private void checkOffset() {
     89        if( maxDistance <= 0 ) {
     90            setOffsetGood(true);
     91            return;
     92        }
    6993        ImageryLayer layer = ImageryOffsetTools.getTopImageryLayer();
    7094        if( layer == null ) {
     
    96120                setOffsetGood(true);
    97121            } else {
    98                 setOffsetGood(data.lastChecked != null && center.greatCircleDistance(data.lastChecked) <= MAX_DISTANCE);
     122                setOffsetGood(data.lastChecked != null && center.greatCircleDistance(data.lastChecked) <= maxDistance * 1000);
    99123            }
    100124        }
    101125    }
    102126
     127    /**
     128     * This class stores an offset and last location for a single imagery layer.
     129     * All fields are public, because this is not enterprise.
     130     */
    103131    private static class ImageryLayerData {
    104132        public double lastDx = 0.0;
     
    123151    }
    124152
     153    /**
     154     * This task is run every 1-2 seconds.
     155     */
    125156    private class IntervalOffsetChecker extends TimerTask {
     157        /**
     158         * Reread max radius setting and update offset state.
     159         */
    126160        @Override
    127161        public void run() {
     162            maxDistance = Main.pref.getDouble("iodb.offset.radius", 15);
    128163            checkOffset();
    129164        }
    130165    }
     166
     167    /**
     168     * The interface for offset listeners.
     169     */
     170    public interface OffsetStateListener {
     171        void offsetStateChanged( boolean isOffsetGood );
     172    }
    131173}
Note: See TracChangeset for help on using the changeset viewer.