Ignore:
Timestamp:
2011-02-05T10:40:39+01:00 (13 years ago)
Author:
bastiK
Message:

Extended mappaint style dialog. No longer required to restart JOSM for changes in mappaint preferences.

Location:
trunk/src/org/openstreetmap/josm/gui/mappaint
Files:
5 edited

Legend:

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

    r3854 r3855  
    3535    }
    3636
    37     public void add(StyleSource style) {
    38         styleSources.add(style);
    39     }
    40 
    41     public Collection<StyleSource> getStyleSources() {
    42         return Collections.<StyleSource>unmodifiableCollection(styleSources);
     37    public List<StyleSource> getStyleSources() {
     38        return Collections.<StyleSource>unmodifiableList(styleSources);
    4339    }
    4440
     
    241237        this.drawMultipolygon = drawMultipolygon;
    242238    }
     239
     240    /**
     241     * remove all style sources; only accessed from MapPaintStyles
     242     */
     243    void clear() {
     244        styleSources.clear();
     245    }
     246
     247    /**
     248     * add a style source; only accessed from MapPaintStyles
     249     */
     250    void add(StyleSource style) {
     251        styleSources.add(style);
     252    }
     253
     254    /**
     255     * set the style sources; only accessed from MapPaintStyles
     256     */
     257    void setStyleSources(Collection<StyleSource> sources) {
     258        styleSources.clear();
     259        styleSources.addAll(sources);
     260    }
     261
    243262}
  • trunk/src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java

    r3853 r3855  
    66import java.io.IOException;
    77import java.io.InputStream;
     8import java.util.ArrayList;
     9import java.util.Arrays;
    810import java.util.Collection;
    911import java.util.Collections;
    1012import java.util.LinkedList;
    1113import java.util.List;
     14import java.util.concurrent.CopyOnWriteArrayList;
    1215
    1316import javax.swing.ImageIcon;
     17import javax.swing.SwingUtilities;
    1418
    1519import org.openstreetmap.josm.Main;
     
    2327import org.openstreetmap.josm.tools.ImageProvider;
    2428
     29/**
     30 * This class manages the ElemStyles instance. The object you get with
     31 * getStyles() is read only, any manipulation happens via one of
     32 * the wrapper methods here. (readFromPreferences, moveStyles, ...)
     33 *
     34 * On change, mapPaintSylesUpdated() is fired for all listeners.
     35 */
    2536public class MapPaintStyles {
    2637
     
    7182    }
    7283
    73     @SuppressWarnings("null")
    7484    public static void readFromPreferences() {
     85        styles.clear();
    7586        iconDirs = Main.pref.getCollection("mappaint.icon.sources", Collections.<String>emptySet());
    7687        if(Main.pref.getBoolean("mappaint.icon.enable-defaults", true))
     
    8394        }
    8495
    85         Collection<? extends SourceEntry> sourceEntries = (new MapPaintPrefMigration()).get();
     96        Collection<? extends SourceEntry> sourceEntries = MapPaintPrefMigration.INSTANCE.get();
    8697
    8798        for (SourceEntry entry : sourceEntries) {
     
    120131            s.loadStyleSource();
    121132        }
     133        fireMapPaintSylesUpdated();
     134    }
     135
     136    /**
     137     * reload styles
     138     * preferences are the same, but the file source may have changed
     139     * @param sel the indices of styles to reload
     140     */
     141    public static void reloadStyles(final int... sel) {
     142        List<StyleSource> toReload = new ArrayList<StyleSource>();
     143        List<StyleSource> data = styles.getStyleSources();
     144        for (int i : sel) {
     145            toReload.add(data.get(i));
     146        }
     147        Main.worker.submit(new MapPaintStyleLoader(toReload));
    122148    }
    123149
     
    138164        @Override
    139165        protected void finish() {
     166            SwingUtilities.invokeLater(new Runnable() {
     167                @Override
     168                public void run() {
     169                    fireMapPaintSylesUpdated();
     170                    styles.clearCached();
     171                    Main.map.mapView.preferenceChanged(null);
     172                    Main.map.mapView.repaint();
     173                }
     174            });
    140175        }
    141176
     
    154189    }
    155190
     191    /**
     192     * Move position of entries in the current list of StyleSources
     193     * @param sele The indices of styles to be moved.
     194     * @param delta The number of lines it should move. positive int moves
     195     *      down and negative moves up.
     196     */
     197    public static void moveStyles(int[] sel, int delta) {
     198        if (!canMoveStyles(sel, delta))
     199            return;
     200        int[] selSorted = Arrays.copyOf(sel, sel.length);
     201        Arrays.sort(selSorted);
     202        List<StyleSource> data = new ArrayList<StyleSource>(styles.getStyleSources());
     203        for (int row: selSorted) {
     204            StyleSource t1 = data.get(row);
     205            StyleSource t2 = data.get(row + delta);
     206            data.set(row, t2);
     207            data.set(row + delta, t1);
     208        }
     209        styles.setStyleSources(data);
     210        MapPaintPrefMigration.INSTANCE.put(data);
     211        fireMapPaintSylesUpdated();
     212        styles.clearCached();
     213        Main.map.mapView.repaint();
     214    }
     215
     216    public static boolean canMoveStyles(int[] sel, int i) {
     217        if (sel.length == 0)
     218            return false;
     219        int[] selSorted = Arrays.copyOf(sel, sel.length);
     220        Arrays.sort(selSorted);
     221
     222        if (i < 0) { // Up
     223            return selSorted[0] >= -i;
     224        } else
     225        if (i > 0) { // Down
     226            return selSorted[selSorted.length-1] <= styles.getStyleSources().size() - 1 - i;
     227        } else
     228            return true;
     229    }
     230
     231    public static void toggleStyleActive(int... sel) {
     232        List<StyleSource> data = styles.getStyleSources();
     233        for (int p : sel) {
     234            StyleSource s = data.get(p);
     235            s.active = !s.active;
     236        }
     237        MapPaintPrefMigration.INSTANCE.put(data);
     238        if (sel.length == 1) {
     239            fireMapPaintStyleEntryUpdated(sel[0]);
     240        } else {
     241            fireMapPaintSylesUpdated();
     242        }
     243        styles.clearCached();
     244        Main.map.mapView.repaint();
     245    }
     246
     247    /***********************************
     248     * MapPaintSylesUpdateListener & related code
     249     *  (get informed when the list of MapPaint StyleSources changes)
     250     */
     251
     252    public interface MapPaintSylesUpdateListener {
     253        public void mapPaintStylesUpdated();
     254        public void mapPaintStyleEntryUpdated(int idx);
     255    }
     256
     257    protected static final CopyOnWriteArrayList<MapPaintSylesUpdateListener> listeners
     258            = new CopyOnWriteArrayList<MapPaintSylesUpdateListener>();
     259
     260    public static void addMapPaintSylesUpdateListener(MapPaintSylesUpdateListener listener) {
     261        if (listener != null) {
     262            listeners.addIfAbsent(listener);
     263        }
     264    }
     265
     266    public static void removeMapPaintSylesUpdateListener(MapPaintSylesUpdateListener listener) {
     267        listeners.remove(listener);
     268    }
     269
     270    public static void fireMapPaintSylesUpdated() {
     271        for (MapPaintSylesUpdateListener l : listeners) {
     272            l.mapPaintStylesUpdated();
     273        }
     274    }
     275
     276    public static void fireMapPaintStyleEntryUpdated(int idx) {
     277        for (MapPaintSylesUpdateListener l : listeners) {
     278            l.mapPaintStyleEntryUpdated(idx);
     279        }
     280    }
    156281}
  • trunk/src/org/openstreetmap/josm/gui/mappaint/NodeElemStyle.java

    r3836 r3855  
    9797                }
    9898            }
    99         } else if (primitive instanceof Relation) {
     99        } else if (primitive instanceof Relation && icon != null) {
    100100            painter.drawRestriction((Relation) primitive, this);
    101101        }
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSStyleSource.java

    r3848 r3855  
    4141    public void loadStyleSource() {
    4242        rules.clear();
     43        hasError = false;
    4344        try {
    4445            MirroredInputStream in = new MirroredInputStream(url);
  • trunk/src/org/openstreetmap/josm/gui/mappaint/xml/XmlStyleSource.java

    r3848 r3855  
    5050
    5151    protected void init() {
     52        hasError = false;
    5253        icons.clear();
    5354        lines.clear();
Note: See TracChangeset for help on using the changeset viewer.