Changeset 7185 in josm


Ignore:
Timestamp:
2014-05-29T00:58:29+02:00 (10 years ago)
Author:
Don-vip
Message:

see #9518 - Automatically reload local styles using new Java 7 WatchService API

Location:
trunk/src/org/openstreetmap/josm
Files:
1 added
4 edited

Legend:

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

    r7100 r7185  
    8888import org.openstreetmap.josm.gui.util.RedirectInputMap;
    8989import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
     90import org.openstreetmap.josm.io.FileWatcher;
    9091import org.openstreetmap.josm.io.OsmApi;
    9192import org.openstreetmap.josm.tools.CheckParameterUtil;
     
    198199     */
    199200    public OsmValidator validator;
     201
     202    /**
     203     * The file watcher service.
     204     */
     205    public static final FileWatcher fileWatcher = new FileWatcher();
    200206
    201207    /**
  • trunk/src/org/openstreetmap/josm/gui/dialogs/MapPaintDialog.java

    r7082 r7185  
    33
    44import static org.openstreetmap.josm.tools.I18n.tr;
    5 import static org.openstreetmap.josm.tools.I18n.trn;
    65
    76import java.awt.Component;
     
    6665import org.openstreetmap.josm.gui.SideButton;
    6766import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
    68 import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.MapPaintStyleLoader;
    6967import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.MapPaintSylesUpdateListener;
    7068import org.openstreetmap.josm.gui.mappaint.StyleSource;
     
    8381import org.openstreetmap.josm.tools.Utils;
    8482
    85 public class MapPaintDialog extends ToggleDialog implements Main.WindowSwitchListener {
     83public class MapPaintDialog extends ToggleDialog {
    8684
    8785    protected StylesTable tblStyles;
     
    191189
    192190    @Override
    193     public void toOtherApplication() {
    194         // nothing
    195     }
    196 
    197     @Override
    198     public void fromOtherApplication() {
    199         // Reload local styles when they have been changed in an external editor.
    200         // Checks file modification time.
    201         List<StyleSource> toReload = new ArrayList<>();
    202         for (StyleSource s : MapPaintStyles.getStyles().getStyleSources()) {
    203             if (s.isLocal()) {
    204                 File f = new File(s.url);
    205                 long mtime = f.lastModified();
    206                 if (mtime > s.getLastMTime()) {
    207                     toReload.add(s);
    208                     s.setLastMTime(mtime);
    209                 }
    210             }
    211         }
    212         if (!toReload.isEmpty()) {
    213             Main.info(trn("Reloading {0} map style.", "Reloading {0} map styles.", toReload.size(), toReload.size()));
    214             Main.worker.submit(new MapPaintStyleLoader(toReload));
    215         }
    216     }
    217 
    218     @Override
    219191    public void showNotify() {
    220192        MapPaintStyles.addMapPaintSylesUpdateListener(model);
    221193        Main.main.menu.wireFrameToggleAction.addButtonModel(cbWireframe.getModel());
    222         if (Main.pref.getBoolean("mappaint.auto_reload_local_styles", true)) {
    223             Main.addWindowSwitchListener(this);
    224         }
    225194    }
    226195
     
    229198        Main.main.menu.wireFrameToggleAction.removeButtonModel(cbWireframe.getModel());
    230199        MapPaintStyles.removeMapPaintSylesUpdateListener(model);
    231         if (Main.pref.getBoolean("mappaint.auto_reload_local_styles", true)) {
    232             Main.removeWindowSwitchListener(this);
    233         }
    234200    }
    235201
     
    296262            tblStyles.repaint();
    297263        }
    298 
    299         /**
    300          * MapPaintSylesUpdateListener interface
    301          */
    302264
    303265        @Override
  • trunk/src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java

    r7143 r7185  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
    6 import java.io.File;
    76import java.io.IOException;
    87import java.io.InputStreamReader;
     
    206205        }
    207206        for (StyleSource source : styles.getStyleSources()) {
    208             final long startTime = System.currentTimeMillis();
    209             source.loadStyleSource();
    210             if (Main.pref.getBoolean("mappaint.auto_reload_local_styles", true) && source.isLocal()) {
    211                 File f = new File(source.url);
    212                 source.setLastMTime(f.lastModified());
    213             }
    214             if (Main.isDebugEnabled()) {
    215                 final long elapsedTime = System.currentTimeMillis() - startTime;
    216                 Main.debug("Initializing map style " + source.url + " completed in " + Utils.getDurationString(elapsedTime));
    217             }
     207            loadStyleForFirstTime(source);
    218208        }
    219209        fireMapPaintSylesUpdated();
     210    }
     211
     212    private static void loadStyleForFirstTime(StyleSource source) {
     213        final long startTime = System.currentTimeMillis();
     214        source.loadStyleSource();
     215        if (Main.pref.getBoolean("mappaint.auto_reload_local_styles", true) && source.isLocal()) {
     216            try {
     217                Main.fileWatcher.registerStyleSource(source);
     218            } catch (IOException e) {
     219                Main.error(e);
     220            }
     221        }
     222        if (Main.isDebugEnabled()) {
     223            final long elapsedTime = System.currentTimeMillis() - startTime;
     224            Main.debug("Initializing map style " + source.url + " completed in " + Utils.getDurationString(elapsedTime));
     225        }
    220226    }
    221227
     
    287293    public static class MapPaintStyleLoader extends PleaseWaitRunnable {
    288294        private boolean canceled;
    289         private List<StyleSource> sources;
    290 
    291         public MapPaintStyleLoader(List<StyleSource> sources) {
     295        private Collection<StyleSource> sources;
     296
     297        public MapPaintStyleLoader(Collection<StyleSource> sources) {
    292298            super(tr("Reloading style sources"));
    293299            this.sources = sources;
     
    385391        if (source != null) {
    386392            styles.add(source);
    387             source.loadStyleSource();
     393            loadStyleForFirstTime(source);
    388394            MapPaintPrefHelper.INSTANCE.put(styles.getStyleSources());
    389395            fireMapPaintSylesUpdated();
  • trunk/src/org/openstreetmap/josm/gui/mappaint/StyleSource.java

    r7081 r7185  
    3434
    3535    private ImageIcon imageIcon;
    36     private long lastMTime = 0L;
    3736
    3837    /******
     
    8180     */
    8281    public abstract InputStream getSourceInputStream() throws IOException;
    83    
     82
    8483    /**
    8584     * Returns a new {@code MirroredInputStream} to the local file containing style source (can be a text file or an archive).
     
    156155        return null;
    157156    }
    158 
    159     public long getLastMTime() {
    160         return lastMTime;
    161     }
    162 
    163     public void setLastMTime(long lastMTime) {
    164         this.lastMTime = lastMTime;
    165     }
    166157}
Note: See TracChangeset for help on using the changeset viewer.