Changeset 5219 in josm


Ignore:
Timestamp:
May 7, 2012 3:05:27 PM (13 months ago)
Author:
bastiK
Message:

mapstyles: add automatic reloading of local styles when they have been edited in an extenal editor (based on mtime)

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

Legend:

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

    r5054 r5219  
    238238            } 
    239239        }); 
    240          
     240 
    241241        // Add Multipolygon cache to layer listeners 
    242242        addLayerChangeListener(MultipolygonCache.getInstance()); 
     
    730730            AbstractButton button = e.nextElement(); 
    731731            MapMode mode = (MapMode)button.getAction(); 
    732             boolean isLayerSupported = mode.layerIsSupported(layer);  
     732            boolean isLayerSupported = mode.layerIsSupported(layer); 
    733733            button.setEnabled(isLayerSupported); 
    734734            // Also update associated shortcut (fix #6876) 
  • trunk/src/org/openstreetmap/josm/gui/dialogs/MapPaintDialog.java

    r5200 r5219  
    33 
    44import static org.openstreetmap.josm.tools.I18n.tr; 
     5import static org.openstreetmap.josm.tools.I18n.trn; 
    56 
    67import java.awt.Component; 
     
    1112import java.awt.Point; 
    1213import java.awt.Rectangle; 
     14import java.awt.Window; 
    1315import java.awt.event.ActionEvent; 
    1416import java.awt.event.ActionListener; 
    1517import java.awt.event.KeyEvent; 
    1618import java.awt.event.MouseEvent; 
     19import java.awt.event.WindowAdapter; 
     20import java.awt.event.WindowEvent; 
    1721import java.io.BufferedInputStream; 
    1822import java.io.BufferedOutputStream; 
     
    6165import org.openstreetmap.josm.gui.SideButton; 
    6266import org.openstreetmap.josm.gui.mappaint.MapPaintStyles; 
     67import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.MapPaintStyleLoader; 
    6368import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.MapPaintSylesUpdateListener; 
    6469import org.openstreetmap.josm.gui.mappaint.StyleSource; 
     
    143148        selectionModel.addListSelectionListener(upAction); 
    144149        selectionModel.addListSelectionListener(downAction); 
    145          
     150 
    146151        // Toggle style on Enter and Spacebar 
    147152        InputMapUtils.addEnterAction(tblStyles, onoffAction); 
    148153        InputMapUtils.addSpacebarAction(tblStyles, onoffAction); 
    149          
     154 
    150155        createLayout(p, true, Arrays.asList(new SideButton[] { 
    151156                new SideButton(onoffAction, false), 
     
    173178    } 
    174179 
     180    /** 
     181     * Reload local styles when they have been changed in an external editor. 
     182     * 
     183     * Checks file modification time when an WindowEvent is invoked. Because 
     184     * any dialog window can get activated, when switching to another app and back, 
     185     * we have to register listeners to all windows in JOSM. 
     186     */ 
     187    protected static class ReloadWindowListener extends WindowAdapter { 
     188 
     189        private static ReloadWindowListener INSTANCE; 
     190 
     191        public static ReloadWindowListener getInstance() { 
     192            if (INSTANCE == null) { 
     193                INSTANCE = new ReloadWindowListener(); 
     194            } 
     195            return INSTANCE; 
     196        } 
     197 
     198        public static void setup() { 
     199            for (Window w : Window.getWindows()) { 
     200                if (w.isShowing()) { 
     201                    w.addWindowListener(getInstance()); 
     202                } 
     203            } 
     204        } 
     205 
     206        public static void teardown() { 
     207            for (Window w : Window.getWindows()) { 
     208                w.removeWindowListener(getInstance()); 
     209            } 
     210        } 
     211 
     212        @Override 
     213        public void windowActivated(WindowEvent e) { 
     214            if (e.getOppositeWindow() == null) { // we come from a native window, e.g. editor 
     215                // reload local styles, if necessary 
     216                List<StyleSource> toReload = new ArrayList<StyleSource>(); 
     217                for (StyleSource s : MapPaintStyles.getStyles().getStyleSources()) { 
     218                    if (s.isLocal()) { 
     219                        File f = new File(s.url); 
     220                        long mtime = f.lastModified(); 
     221                        if (mtime > s.getLastMTime()) { 
     222                            toReload.add(s); 
     223                            s.setLastMTime(mtime); 
     224                        } 
     225                    } 
     226                } 
     227                if (!toReload.isEmpty()) { 
     228                    System.out.println(trn("Reloading {0} map style.", "Reloading {0} map styles.", toReload.size(), toReload.size())); 
     229                    Main.worker.submit(new MapPaintStyleLoader(toReload)); 
     230                } 
     231            } 
     232        } 
     233 
     234        @Override 
     235        public void windowDeactivated(WindowEvent e) { 
     236            // set up windows that have been created in the meantime 
     237            for (Window w : Window.getWindows()) { 
     238                w.removeWindowListener(getInstance()); 
     239                if (w.isShowing()) { 
     240                    w.addWindowListener(getInstance()); 
     241                } 
     242            } 
     243        } 
     244    } 
     245 
    175246    @Override 
    176247    public void showNotify() { 
    177248        MapPaintStyles.addMapPaintSylesUpdateListener(model); 
    178249        Main.main.menu.wireFrameToggleAction.addButtonModel(cbWireframe.getModel()); 
     250        if (Main.pref.getBoolean("mappaint.auto_reload_local_styles", true)) { 
     251            ReloadWindowListener.setup(); 
     252        } 
    179253    } 
    180254 
     
    183257        Main.main.menu.wireFrameToggleAction.removeButtonModel(cbWireframe.getModel()); 
    184258        MapPaintStyles.removeMapPaintSylesUpdateListener(model); 
     259        if (Main.pref.getBoolean("mappaint.auto_reload_local_styles", true)) { 
     260            ReloadWindowListener.teardown(); 
     261        } 
    185262    } 
    186263 
  • trunk/src/org/openstreetmap/josm/gui/mappaint/LineTextElemStyle.java

    r5217 r5219  
    5050        return text.hashCode(); 
    5151    } 
     52 
     53    @Override 
     54    public String toString() { 
     55        return "LineTextElemStyle{" + super.toString() + "text=" + text + "}"; 
     56    } 
     57 
    5258} 
  • trunk/src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java

    r5054 r5219  
    44import static org.openstreetmap.josm.tools.I18n.tr; 
    55 
     6import java.io.File; 
    67import java.io.IOException; 
    78import java.io.InputStream; 
     
    2122import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource; 
    2223import org.openstreetmap.josm.gui.mappaint.xml.XmlStyleSource; 
     24import org.openstreetmap.josm.gui.preferences.SourceEntry; 
    2325import org.openstreetmap.josm.gui.preferences.map.MapPaintPreference.MapPaintPrefHelper; 
    24 import org.openstreetmap.josm.gui.preferences.SourceEntry; 
    2526import org.openstreetmap.josm.gui.progress.ProgressMonitor; 
    2627import org.openstreetmap.josm.io.MirroredInputStream; 
     
    165166        for (StyleSource source : styles.getStyleSources()) { 
    166167            source.loadStyleSource(); 
    167         } 
    168  
     168            if (Main.pref.getBoolean("mappaint.auto_reload_local_styles", true)) { 
     169                if (source.isLocal()) { 
     170                    File f = new File(source.url); 
     171                    source.setLastMTime(f.lastModified()); 
     172                } 
     173            } 
     174        } 
    169175        fireMapPaintSylesUpdated(); 
    170176    } 
  • trunk/src/org/openstreetmap/josm/gui/mappaint/StyleSource.java

    r4820 r5219  
    2626 
    2727    private ImageIcon imageIcon; 
     28    private long lastMTime = 0l; 
    2829 
    2930    /****** 
     
    3132     * of the source file. 
    3233     */ 
    33      
     34 
    3435    public String icon; 
    3536 
     
    104105        return null; 
    105106    } 
     107 
     108    public long getLastMTime() { 
     109        return lastMTime; 
     110    } 
     111 
     112    public void setLastMTime(long lastMTime) { 
     113        this.lastMTime = lastMTime; 
     114    } 
     115 
     116 
    106117} 
Note: See TracChangeset for help on using the changeset viewer.