Changeset 4668 in josm


Ignore:
Timestamp:
Dec 20, 2011 6:15:44 PM (17 months ago)
Author:
bastiK
Message:

session support (first part, see #4029)

Idea: Save and load the current session, i.e. list of open layers and possibly more.
This change includes only support for reading session files and only for osm-data layers.

session.svg: Public Domain

Location:
trunk
Files:
7 added
4 edited

Legend:

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

    r4593 r4668  
    7070import org.openstreetmap.josm.actions.SaveAsAction; 
    7171import org.openstreetmap.josm.actions.SelectAllAction; 
     72import org.openstreetmap.josm.actions.SessionLoadAction; 
    7273import org.openstreetmap.josm.actions.ShowStatusReportAction; 
    7374import org.openstreetmap.josm.actions.SimplifyWayAction; 
     
    9899import org.openstreetmap.josm.gui.layer.Layer; 
    99100import org.openstreetmap.josm.gui.tagging.TaggingPresetSearchAction; 
     101import org.openstreetmap.josm.tools.ImageProvider; 
    100102import org.openstreetmap.josm.tools.Shortcut; 
    101103 
     
    117119    public final JosmAction save = new SaveAction(); 
    118120    public final JosmAction saveAs = new SaveAsAction(); 
     121    public final JosmAction sessionLoad = new SessionLoadAction(); 
    119122    public final JosmAction gpxExport = new GpxExportAction(); 
    120123    public final DownloadAction download = new DownloadAction(); 
     
    190193 
    191194    public final JMenu fileMenu = addMenu(marktr("File"), KeyEvent.VK_F, 0, ht("/Menu/File")); 
     195    public final JMenu sessionMenu = new JMenu(tr("Session")); // submenu of the file menu 
    192196    public final JMenu editMenu = addMenu(marktr("Edit"), KeyEvent.VK_E, 1, ht("/Menu/Edit")); 
    193197    public final JMenu viewMenu = addMenu(marktr("View"), KeyEvent.VK_V, 2, ht("/Menu/View")); 
     
    206210    public JMenu audioMenu = null; 
    207211    public final JMenu helpMenu = addMenu(marktr("Help"), KeyEvent.VK_H, 7, ht("/Menu/Help")); 
     212 
    208213    public final int defaultMenuPos = 7; 
    209214 
     
    357362        add(fileMenu, save); 
    358363        add(fileMenu, saveAs); 
     364        if (Main.pref.getBoolean("session")) { 
     365            sessionMenu.setToolTipText(tr("Save and load the current session (list of layers, etc.)")); 
     366            sessionMenu.setIcon(ImageProvider.get("session")); 
     367            add(sessionMenu, sessionLoad); 
     368            fileMenu.add(sessionMenu); 
     369        } 
    359370        add(fileMenu, gpxExport); 
    360371        fileMenu.addSeparator(); 
  • trunk/src/org/openstreetmap/josm/io/OsmImporter.java

    r4490 r4668  
    2222public class OsmImporter extends FileImporter { 
    2323 
     24    private OsmDataLayer layer; 
     25    private Runnable postLayerTask; 
     26 
    2427    public OsmImporter() { 
    2528        super(new ExtensionFileFilter("osm,xml", "osm", tr("OSM Server Files") + " (*.osm *.xml)")); 
     
    3033    } 
    3134 
    32     @Override public void importData(File file, ProgressMonitor progressMonitor) throws IOException, IllegalDataException { 
     35    @Override 
     36    public void importData(File file, ProgressMonitor progressMonitor) throws IOException, IllegalDataException { 
    3337        try { 
    3438            FileInputStream in = new FileInputStream(file); 
     
    4145 
    4246    protected void importData(InputStream in, final File associatedFile) throws IllegalDataException { 
    43         final DataSet dataSet = OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE); 
    44         final OsmDataLayer layer = new OsmDataLayer(dataSet, associatedFile.getName(), associatedFile); 
    45         addDataLayer(dataSet, layer, associatedFile.getPath());  
    46     } 
    47          
    48     protected void addDataLayer(final DataSet dataSet, final OsmDataLayer layer, final String filePath) {  
     47        loadLayer(in, associatedFile, associatedFile.getName(), NullProgressMonitor.INSTANCE); 
    4948        // FIXME: remove UI stuff from IO subsystem 
    50         // 
    5149        Runnable uiStuff = new Runnable() { 
    5250            @Override 
    5351            public void run() { 
    54                 if (dataSet.allPrimitives().isEmpty()) { 
    55                     JOptionPane.showMessageDialog( 
    56                             Main.parent, 
    57                             tr("No data found in file {0}.", filePath), 
    58                             tr("Open OSM file"), 
    59                             JOptionPane.INFORMATION_MESSAGE); 
    60                 } 
    6152                Main.main.addLayer(layer); 
     53                postLayerTask.run(); 
    6254                layer.onPostLoadFromFile(); 
    6355            } 
     
    6961        } 
    7062    } 
     63 
     64    /** 
     65     * Load osm data layer from InputStream. 
     66     * associatedFile can be null if the stream does not come from a file. 
     67     */ 
     68    public void loadLayer(InputStream in, final File associatedFile, final String layerName, ProgressMonitor progressMonitor) throws IllegalDataException { 
     69        final DataSet dataSet = OsmReader.parseDataSet(in, progressMonitor); 
     70        String name = associatedFile == null ? OsmDataLayer.createNewName() : associatedFile.getName(); 
     71        layer = new OsmDataLayer(dataSet, layerName, associatedFile); 
     72        postLayerTask = new Runnable() { 
     73            @Override 
     74            public void run() { 
     75                if (dataSet.allPrimitives().isEmpty()) { 
     76                    String msg; 
     77                    if (associatedFile == null) { 
     78                        msg = tr("No data found for layer ''{0}''.", layerName); 
     79                    } else { 
     80                        msg = tr("No data found in file ''{0}''.", associatedFile.getPath()); 
     81                    } 
     82                    JOptionPane.showMessageDialog( 
     83                            Main.parent, 
     84                            msg, 
     85                            tr("Open OSM file"), 
     86                            JOptionPane.INFORMATION_MESSAGE); 
     87                } 
     88                layer.onPostLoadFromFile(); 
     89            } 
     90        }; 
     91    } 
     92 
     93    public OsmDataLayer getLayer() { 
     94        return layer; 
     95    } 
     96 
     97    public Runnable getPostLayerTask() { 
     98        return postLayerTask; 
     99    } 
    71100} 
  • trunk/src/org/openstreetmap/josm/tools/MultiMap.java

    r3674 r4668  
    22package org.openstreetmap.josm.tools; 
    33 
     4import java.util.ArrayList; 
    45import java.util.Collection; 
    56import java.util.HashMap; 
    67import java.util.LinkedHashSet; 
     8import java.util.List; 
    79import java.util.Map; 
    810import java.util.Map.Entry; 
     
    1315 * 
    1416 * Corresponds to Google guava LinkedHashMultimap and Apache Collections MultiValueMap 
    15  * but it is an independent (simplistic) implementation. 
     17 * but it is an independent (simple) implementation. 
    1618 * 
    1719 */ 
     
    116118        return map.values(); 
    117119    } 
     120 
     121    /** 
     122     * Removes a cerain key=value mapping 
     123     * 
     124     * @return true, if something was removed 
     125     */ 
     126    public boolean remove(A key, B value) { 
     127        Set<B> values = get(key); 
     128        if (values != null) { 
     129            return values.remove(value); 
     130        } 
     131        return false; 
     132    } 
     133 
     134    /** 
     135     * Removes all mappings for a certain key 
     136     */ 
     137    public LinkedHashSet<B> remove(A key) { 
     138        return map.remove(key); 
     139    } 
     140 
     141    public String toString() { 
     142        List<String> entries = new ArrayList<String>(map.size()); 
     143        for (A key : map.keySet()) { 
     144            entries.add(key + "->{" + Utils.join(",", map.get(key)) + "}"); 
     145        } 
     146        return "(" + Utils.join(",", entries) + ")"; 
     147    } 
    118148} 
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r4408 r4668  
    1919import java.security.NoSuchAlgorithmException; 
    2020import java.text.MessageFormat; 
     21import java.util.ArrayList; 
    2122import java.util.Collection; 
     23import java.util.List; 
    2224 
    2325/** 
     
    6264                return new FilteredCollection<T>(collection, predicate); 
    6365        } 
    64      
     66 
    6567    /** 
    6668     * Filter a collection by (sub)class. 
     
    233235    /** 
    234236     * <p>Utility method for closing an input stream.</p> 
    235      *  
     237     * 
    236238     * @param is the input stream. May be null. 
    237239     */ 
     
    247249    /** 
    248250     * <p>Utility method for closing an output stream.</p> 
    249      *  
     251     * 
    250252     * @param os the output stream. May be null. 
    251253     */ 
     
    261263    /** 
    262264     * <p>Utility method for closing a reader.</p> 
    263      *  
     265     * 
    264266     * @param reader the reader. May be null. 
    265267     */ 
     
    355357        return new String(hexChars); 
    356358    } 
     359 
     360    /** 
     361     * Topological sort. 
     362     * 
     363     * @param dependencies contains mappings (key -> value). In the final list of sorted objects, the key will come 
     364     * after the value. (In other words, the key depends on the value(s).) 
     365     * There must not be cyclic dependencies. 
     366     * @return the list of sorted objects 
     367     */ 
     368    public static <T> List<T> topologicalSort(final MultiMap<T,T> dependencies) { 
     369        MultiMap<T,T> deps = new MultiMap<T,T>(); 
     370        for (T key : dependencies.keySet()) { 
     371            deps.putVoid(key); 
     372            for (T val : dependencies.get(key)) { 
     373                deps.putVoid(val); 
     374                deps.put(key, val); 
     375            } 
     376        } 
     377 
     378        int size = deps.size(); 
     379        List<T> sorted = new ArrayList<T>(); 
     380        for (int i=0; i<size; ++i) { 
     381            T parentless = null; 
     382            for (T key : deps.keySet()) { 
     383                if (deps.get(key).size() == 0) { 
     384                    parentless = key; 
     385                    break; 
     386                } 
     387            } 
     388            if (parentless == null) throw new RuntimeException(); 
     389            sorted.add(parentless); 
     390            deps.remove(parentless); 
     391            for (T key : deps.keySet()) { 
     392                deps.remove(key, parentless); 
     393            } 
     394        } 
     395        if (sorted.size() != size) throw new RuntimeException(); 
     396        return sorted; 
     397    } 
    357398} 
Note: See TracChangeset for help on using the changeset viewer.