Ignore:
Timestamp:
2015-04-17T20:55:39+02:00 (5 years ago)
Author:
simon04
Message:

Opening files: guess file extension if specified filename does not exist

This allows to open foo.osm when entering foo. Previously, a "does not exist" had been displayed.

File:
1 edited

Legend:

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

    r7865 r8202  
    1010import java.io.BufferedReader;
    1111import java.io.File;
     12import java.io.FilenameFilter;
    1213import java.io.IOException;
    1314import java.nio.charset.StandardCharsets;
     
    102103        private boolean recordHistory = false;
    103104
    104         public OpenFileTask(List<File> files, FileFilter fileFilter, String title) {
     105        public OpenFileTask(final List<File> files, final FileFilter fileFilter, final String title) {
    105106            super(title, false /* don't ignore exception */);
    106             this.files = new ArrayList<>(files);
    107107            this.fileFilter = fileFilter;
     108            this.files = new ArrayList<>(files.size());
     109            for (final File file : files) {
     110                if (file.exists()) {
     111                    this.files.add(file);
     112                } else {
     113                    // try to guess an extension using the specified fileFilter
     114                    final File[] matchingFiles = file.getParentFile().listFiles(new FilenameFilter() {
     115                        @Override
     116                        public boolean accept(File dir, String name) {
     117                            return name.startsWith(file.getName()) && fileFilter.accept(new File(dir, name));
     118                        }
     119                    });
     120                    if (matchingFiles.length == 1) {
     121                        // use the unique match as filename
     122                        this.files.add(matchingFiles[0]);
     123                    } else {
     124                        // add original filename for error reporting later on
     125                        this.files.add(file);
     126                    }
     127                }
     128            }
    108129        }
    109130
Note: See TracChangeset for help on using the changeset viewer.