Ignore:
Timestamp:
2017-08-25T23:56:58+02:00 (7 years ago)
Author:
Don-vip
Message:

see #15182 - code refactoring to avoid dependence on GUI packages from MapPaintStyles

File:
1 edited

Legend:

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

    r12649 r12651  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.gui.mappaint;
    3 
    4 import static org.openstreetmap.josm.tools.I18n.tr;
    53
    64import java.io.File;
     
    2422import org.openstreetmap.josm.data.preferences.sources.MapPaintPrefHelper;
    2523import org.openstreetmap.josm.data.preferences.sources.SourceEntry;
    26 import org.openstreetmap.josm.gui.MainApplication;
    27 import org.openstreetmap.josm.gui.PleaseWaitRunnable;
    28 import org.openstreetmap.josm.gui.layer.Layer;
    29 import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    3024import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
    3125import org.openstreetmap.josm.gui.mappaint.styleelement.MapImage;
    3226import org.openstreetmap.josm.gui.mappaint.styleelement.NodeElement;
    3327import org.openstreetmap.josm.gui.mappaint.styleelement.StyleElement;
    34 import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    3528import org.openstreetmap.josm.io.CachedFile;
    3629import org.openstreetmap.josm.tools.ImageProvider;
     
    340333
    341334    /**
    342      * reload styles
    343      * preferences are the same, but the file source may have changed
    344      * @param sel the indices of styles to reload
    345      */
    346     public static void reloadStyles(final int... sel) {
    347         List<StyleSource> toReload = new ArrayList<>();
    348         List<StyleSource> data = styles.getStyleSources();
    349         for (int i : sel) {
    350             toReload.add(data.get(i));
    351         }
    352         MainApplication.worker.submit(new MapPaintStyleLoader(toReload));
    353     }
    354 
    355     /**
    356      * This class loads the map paint styles
    357      */
    358     public static class MapPaintStyleLoader extends PleaseWaitRunnable {
    359         private boolean canceled;
    360         private final Collection<StyleSource> sources;
    361 
    362         /**
    363          * Create a new {@link MapPaintStyleLoader}
    364          * @param sources The styles to load
    365          */
    366         public MapPaintStyleLoader(Collection<StyleSource> sources) {
    367             super(tr("Reloading style sources"));
    368             this.sources = sources;
    369         }
    370 
    371         @Override
    372         protected void cancel() {
    373             canceled = true;
    374         }
    375 
    376         @Override
    377         protected void finish() {
    378             fireMapPaintSylesUpdated();
    379             afterStyleUpdate();
    380         }
    381 
    382         @Override
    383         protected void realRun() {
    384             ProgressMonitor monitor = getProgressMonitor();
    385             monitor.setTicksCount(sources.size());
    386             for (StyleSource s : sources) {
    387                 if (canceled)
    388                     return;
    389                 monitor.subTask(tr("loading style ''{0}''...", s.getDisplayString()));
    390                 s.loadStyleSource();
    391                 monitor.worked(1);
    392             }
    393         }
    394     }
    395 
    396     /**
    397335     * Move position of entries in the current list of StyleSources
    398336     * @param sel The indices of styles to be moved.
     
    415353        MapPaintPrefHelper.INSTANCE.put(data);
    416354        fireMapPaintSylesUpdated();
    417         afterStyleUpdate();
    418     }
    419 
    420     /**
    421      * Manually trigger for now. TODO: Move this to a listener
    422      */
    423     private static void afterStyleUpdate() {
    424         SwingUtilities.invokeLater(() -> {
    425             styles.clearCached();
    426 
    427             // Trigger a repaint of all data layers
    428             MainApplication.getLayerManager().getLayers()
    429                 .stream()
    430                 .filter(layer -> layer instanceof OsmDataLayer)
    431                 .forEach(Layer::invalidate);
    432         });
    433355    }
    434356
     
    469391            fireMapPaintSylesUpdated();
    470392        }
    471         afterStyleUpdate();
    472393    }
    473394
     
    500421        MapPaintPrefHelper.INSTANCE.put(styles.getStyleSources());
    501422        fireMapPaintSylesUpdated();
    502         afterStyleUpdate();
    503423    }
    504424
     
    522442    private static final ListenerList<MapPaintSylesUpdateListener> listeners = ListenerList.createUnchecked();
    523443
     444    static {
     445        listeners.addListener(new MapPaintSylesUpdateListener() {
     446            @Override
     447            public void mapPaintStylesUpdated() {
     448                SwingUtilities.invokeLater(styles::clearCached);
     449            }
     450
     451            @Override
     452            public void mapPaintStyleEntryUpdated(int index) {
     453                mapPaintStylesUpdated();
     454            }
     455        });
     456    }
     457
    524458    /**
    525459     * Add a listener that listens to global style changes.
Note: See TracChangeset for help on using the changeset viewer.