Changeset 1646 in josm


Ignore:
Timestamp:
Jun 6, 2009 9:39:12 PM (4 years ago)
Author:
stoecker
Message:

cleanup in file access

Location:
trunk/src/org/openstreetmap/josm
Files:
9 edited

Legend:

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

    r1637 r1646  
    66import java.io.File; 
    77import javax.swing.JFileChooser; 
     8import javax.swing.filechooser.FileFilter; 
    89import org.openstreetmap.josm.Main; 
    910import org.openstreetmap.josm.gui.ExtendedDialog; 
     
    2021    } 
    2122 
    22     protected static JFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title) { 
     23    public static JFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title) { 
    2324        String curDir = Main.pref.get("lastDirectory"); 
    2425        if (curDir.equals("")) 
     
    5556        return fc; 
    5657    } 
     58 
     59    public static File createAndOpenSaveFileChooser(String title, 
     60    String extension) 
     61    { 
     62        String curDir = Main.pref.get("lastDirectory"); 
     63        if (curDir.equals("")) 
     64            curDir = "."; 
     65        JFileChooser fc = new JFileChooser(new File(curDir)); 
     66        if (title != null) 
     67            fc.setDialogTitle(title); 
     68 
     69        fc.setMultiSelectionEnabled(false); 
     70        for (FileImporter imExporter: ExtensionFileFilter.importers) { 
     71            fc.addChoosableFileFilter(imExporter.filter); 
     72        } 
     73 
     74        fc.setAcceptAllFileFilterUsed(true); 
     75 
     76        int answer = fc.showSaveDialog(Main.parent); 
     77        if (answer != JFileChooser.APPROVE_OPTION) 
     78            return null; 
     79 
     80        if (!fc.getCurrentDirectory().getAbsolutePath().equals(curDir)) 
     81            Main.pref.put("lastDirectory", fc.getCurrentDirectory().getAbsolutePath()); 
     82 
     83        File file = fc.getSelectedFile(); 
     84        if(extension != null) 
     85        { 
     86            String fn = file.getPath(); 
     87            if(fn.indexOf('.') == -1) 
     88            { 
     89                FileFilter ff = fc.getFileFilter(); 
     90                if (ff instanceof ExtensionFileFilter) 
     91                    fn += "." + ((ExtensionFileFilter)ff).defaultExtension; 
     92                else 
     93                    fn += extension; 
     94                file = new File(fn); 
     95            } 
     96        } 
     97        if(file == null || (file.exists() && 1 != new ExtendedDialog(Main.parent, 
     98        tr("Overwrite"), tr("File exists. Overwrite?"), 
     99        new String[] {tr("Overwrite"), tr("Cancel")}, 
     100        new String[] {"save_as.png", "cancel.png"}).getValue())) 
     101            return null; 
     102        return file; 
     103    } 
     104 
    57105} 
  • trunk/src/org/openstreetmap/josm/actions/RenameLayerAction.java

    r1169 r1646  
    8282                File newFile = new File(newname); 
    8383                if (file.renameTo(newFile)) { 
    84                     layer.associatedFile = newFile; 
     84                    layer.setAssociatedFile(newFile); 
    8585                    nameText = newFile.getName(); 
    8686                } else { 
  • trunk/src/org/openstreetmap/josm/actions/SaveAction.java

    r1169 r1646  
    88 
    99import org.openstreetmap.josm.gui.layer.Layer; 
    10 import org.openstreetmap.josm.gui.layer.GpxLayer; 
    11 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 
    1210import org.openstreetmap.josm.tools.Shortcut; 
    1311 
     
    2927 
    3028    @Override public File getFile(Layer layer) { 
    31         if (layer instanceof OsmDataLayer) { 
    32             File f = ((OsmDataLayer)layer).associatedFile; 
    33             if (f != null) { 
    34                 return f; 
    35             } 
    36         } 
    37         if (layer instanceof GpxLayer) { 
    38             File f = ((GpxLayer)layer).data.storageFile; 
    39             if (f != null) { 
    40                 return f; 
    41             } 
    42         } 
    43         return openFileDialog(layer); 
     29        File f = layer.getAssociatedFile(); 
     30        return f == null ? openFileDialog(layer) : f; 
    4431    } 
    4532} 
  • trunk/src/org/openstreetmap/josm/actions/SaveActionBase.java

    r1637 r1646  
    1414import javax.swing.JFileChooser; 
    1515import javax.swing.JOptionPane; 
    16 import javax.swing.filechooser.FileFilter; 
    1716 
    1817import org.openstreetmap.josm.Main; 
     
    6059 
    6160        layer.name = file.getName(); 
    62         layer.associatedFile = file; 
     61        layer.setAssociatedFile(file); 
    6362        Main.parent.repaint(); 
    6463        return true; 
     
    101100 
    102101    public static File openFileDialog(Layer layer) { 
    103         JFileChooser fc = createAndOpenFileChooser(false, false, layer instanceof GpxLayer ? tr("Save GPX file") : tr("Save OSM file")); 
    104         if (fc == null) 
    105             return null; 
    106  
    107         File file = fc.getSelectedFile(); 
    108  
    109         String fn = file.getPath(); 
    110         if (fn.indexOf('.') == -1) { 
    111             FileFilter ff = fc.getFileFilter(); 
    112             if (ff instanceof ExtensionFileFilter) 
    113                 fn += "." + ((ExtensionFileFilter)ff).defaultExtension; 
    114             else if (layer instanceof GpxLayer) 
    115                 fn += ".gpx"; 
    116             else 
    117                 fn += ".osm"; 
    118             file = new File(fn); 
    119         } 
    120         return file; 
     102        if (layer instanceof OsmDataLayer) 
     103            return createAndOpenSaveFileChooser(tr("Save OSM file"), ".osm"); 
     104        else if (layer instanceof GpxLayer) 
     105            return createAndOpenSaveFileChooser(tr("Save GPX file"), ".gpx"); 
     106        return createAndOpenSaveFileChooser(tr("Save Layer"), ".lay"); 
    121107    } 
    122108 
  • trunk/src/org/openstreetmap/josm/gui/layer/GpxLayer.java

    r1638 r1646  
    175175                                namedTrackPoints.waypoints.add(point); 
    176176 
    177                 MarkerLayer ml = new MarkerLayer(namedTrackPoints, tr("Named Trackpoints from {0}", name), associatedFile, me); 
     177                MarkerLayer ml = new MarkerLayer(namedTrackPoints, tr("Named Trackpoints from {0}", name), getAssociatedFile(), me); 
    178178                if (ml.data.size() > 0) { 
    179179                    Main.main.addLayer(ml); 
     
    203203                        Main.pref.put("markers.lastaudiodirectory", fc.getCurrentDirectory().getAbsolutePath()); 
    204204 
    205                     MarkerLayer ml = new MarkerLayer(new GpxData(), tr("Audio markers from {0}", name), associatedFile, me); 
     205                    MarkerLayer ml = new MarkerLayer(new GpxData(), tr("Audio markers from {0}", name), getAssociatedFile(), me); 
    206206                    File sel[] = fc.getSelectedFiles(); 
    207207                    if(sel != null) { 
     
    270270                new JMenuItem(new ConvertToDataLayerAction()), 
    271271                new JSeparator(), 
    272                 new JMenuItem(new RenameLayerAction(associatedFile, this)), 
     272                new JMenuItem(new RenameLayerAction(getAssociatedFile(), this)), 
    273273                new JSeparator(), 
    274274                new JMenuItem(new LayerListPopup.InfoAction(this))}; 
     
    287287            new JMenuItem(new DownloadAlongTrackAction()), 
    288288            new JSeparator(), 
    289             new JMenuItem(new RenameLayerAction(associatedFile, this)), 
     289            new JMenuItem(new RenameLayerAction(getAssociatedFile(), this)), 
    290290            new JSeparator(), 
    291291            new JMenuItem(new LayerListPopup.InfoAction(this))}; 
     
    680680            } 
    681681            Main.main.addLayer(new OsmDataLayer(ds, tr("Converted from: {0}", GpxLayer.this.name), 
    682                         data.storageFile)); 
     682                        getAssociatedFile())); 
    683683            Main.main.removeLayer(GpxLayer.this); 
    684684        } 
    685685    } 
     686 
     687    public File getAssociatedFile() { return data.storageFile; } 
     688    public void setAssociatedFile(File file) { data.storageFile = file; } 
    686689 
    687690    /** 
     
    932935            double duration = AudioUtil.getCalibratedDuration(wavFile); 
    933936            double startTime = lastModified - duration; 
    934             startTime = firstStartTime + (startTime - firstStartTime) /   
     937            startTime = firstStartTime + (startTime - firstStartTime) / 
    935938                Main.pref.getDouble("audio.calibration", "1.0" /* default, ratio */); 
    936939            WayPoint w1 = null; 
     
    949952                    if (w2 != null) break; 
    950953                } 
    951             }     
     954            } 
    952955 
    953956            if (w1 == null || w2 == null) { 
     
    964967                wayPointFromTimeStamp.attr.put("name", name); 
    965968                waypoints.add(wayPointFromTimeStamp); 
    966             }             
    967         } 
    968          
     969            } 
     970        } 
     971 
    969972        // (e) analyse audio for spoken markers here, in due course 
    970973 
     
    10131016                    name, uri, ml, w.time, offset); 
    10141017            /* timeFromAudio intended for future use to shift markers of this type on synchronization */ 
    1015             if (w == wayPointFromTimeStamp) {  
    1016                 am.timeFromAudio = true;  
     1018            if (w == wayPointFromTimeStamp) { 
     1019                am.timeFromAudio = true; 
    10171020            } 
    10181021            ml.data.add(am); 
  • trunk/src/org/openstreetmap/josm/gui/layer/Layer.java

    r1508 r1646  
    6565     * If a file is associated with this layer, this variable should be set to it. 
    6666     */ 
    67     public File associatedFile; 
     67    private File associatedFile; 
    6868 
    6969    /** 
     
    120120     */ 
    121121    public void destroy() {} 
     122 
     123    public File getAssociatedFile() { return associatedFile; } 
     124    public void setAssociatedFile(File file) { associatedFile = file; } 
    122125} 
  • trunk/src/org/openstreetmap/josm/gui/layer/OsmDataLayer.java

    r1640 r1646  
    152152        super(name); 
    153153        this.data = data; 
    154         this.associatedFile = associatedFile; 
     154        this.setAssociatedFile(associatedFile); 
    155155    } 
    156156 
     
    217217        tool += undeletedSize(data.ways)+" "+trn("way", "ways", undeletedSize(data.ways)); 
    218218        if (data.version != null) tool += ", " + tr("version {0}", data.version); 
    219         if (associatedFile != null) 
    220             tool = "<html>"+tool+"<br>"+associatedFile.getPath()+"</html>"; 
     219        File f = getAssociatedFile(); 
     220        if (f != null) 
     221            tool = "<html>"+tool+"<br>"+f.getPath()+"</html>"; 
    221222        return tool; 
    222223    } 
     
    307308 
    308309        // update the modified flag 
    309         if (associatedFile != null && processed != null && !dataAdded) 
     310        boolean b = (getAssociatedFile() != null && processed != null); 
     311        if (b && !dataAdded) 
    310312            return; // do nothing when uploading non-harmful changes. 
    311313 
    312314        // modified if server changed the data (esp. the id). 
    313         uploadedModified = associatedFile != null && processed != null && dataAdded; 
     315        uploadedModified = b && dataAdded; 
    314316        setModified(uploadedModified); 
    315317    } 
     
    378380                    new JMenuItem(new LayerListDialog.DeleteLayerAction(this)), 
    379381                    new JSeparator(), 
    380                     new JMenuItem(new RenameLayerAction(associatedFile, this)), 
     382                    new JMenuItem(new RenameLayerAction(getAssociatedFile(), this)), 
    381383                    new JSeparator(), 
    382384                    new JMenuItem(new LayerListPopup.InfoAction(this))}; 
     
    391393                new JMenuItem(new ConvertToGpxLayerAction()), 
    392394                new JSeparator(), 
    393                 new JMenuItem(new RenameLayerAction(associatedFile, this)), 
     395                new JMenuItem(new RenameLayerAction(getAssociatedFile(), this)), 
    394396                new JSeparator(), 
    395397                new JMenuItem(new LayerListPopup.InfoAction(this))}; 
     
    454456 
    455457    public GpxData toGpxData() { 
    456         return toGpxData(data, associatedFile); 
     458        return toGpxData(data, getAssociatedFile()); 
    457459    } 
    458460 
  • trunk/src/org/openstreetmap/josm/gui/layer/RawGpsLayer.java

    r1589 r1646  
    107107        super(name); 
    108108        this.fromServer = fromServer; 
    109         this.associatedFile = associatedFile; 
     109        setAssociatedFile(associatedFile); 
    110110        this.data = data; 
    111111        Main.pref.listener.add(this); 
     
    151151        String tool = data.size()+" "+trn("track", "tracks", data.size()) 
    152152        +" "+points+" "+trn("point", "points", points); 
    153         if (associatedFile != null) 
    154             tool = "<html>"+tool+"<br>"+associatedFile.getPath()+"</html>"; 
     153        File f = getAssociatedFile(); 
     154        if (f != null) 
     155            tool = "<html>"+tool+"<br>"+f.getPath()+"</html>"; 
    155156        return tool; 
    156157    } 
     
    242243                new JMenuItem(new ConvertToDataLayerAction()), 
    243244                new JSeparator(), 
    244                 new JMenuItem(new RenameLayerAction(associatedFile, this)), 
     245                new JMenuItem(new RenameLayerAction(getAssociatedFile(), this)), 
    245246                new JSeparator(), 
    246247                new JMenuItem(new LayerListPopup.InfoAction(this))}; 
     
    254255                new JMenuItem(new ConvertToDataLayerAction()), 
    255256                new JSeparator(), 
    256                 new JMenuItem(new RenameLayerAction(associatedFile, this)), 
     257                new JMenuItem(new RenameLayerAction(getAssociatedFile(), this)), 
    257258                new JSeparator(), 
    258259                new JMenuItem(new LayerListPopup.InfoAction(this))}; 
  • trunk/src/org/openstreetmap/josm/gui/layer/markerlayer/MarkerLayer.java

    r1601 r1646  
    7171 
    7272        super(name); 
    73         this.associatedFile = associatedFile; 
     73        this.setAssociatedFile(associatedFile); 
    7474        this.data = new ArrayList<Marker>(); 
    7575        this.fromLayer = fromLayer; 
     
    258258            components.add (moveaudio); 
    259259        } 
    260         components.add(new JMenuItem(new RenameLayerAction(associatedFile, this))); 
     260        components.add(new JMenuItem(new RenameLayerAction(getAssociatedFile(), this))); 
    261261        components.add(new JSeparator()); 
    262262        components.add(new JMenuItem(new LayerListPopup.InfoAction(this))); 
Note: See TracChangeset for help on using the changeset viewer.