Example #1
0
 public void importData(FileImporter importer, List<File> files) {
   if (importer.isBatchImporter()) {
     if (canceled) return;
     String msg = trn("Opening {0} file...", "Opening {0} files...", files.size(), files.size());
     getProgressMonitor().setCustomText(msg);
     getProgressMonitor().indeterminateSubTask(msg);
     if (importer.importDataHandleExceptions(
         files, getProgressMonitor().createSubTaskMonitor(files.size(), false))) {
       successfullyOpenedFiles.addAll(files);
     }
   } else {
     for (File f : files) {
       if (canceled) return;
       getProgressMonitor()
           .indeterminateSubTask(tr("Opening file ''{0}'' ...", f.getAbsolutePath()));
       if (importer.importDataHandleExceptions(
           f, getProgressMonitor().createSubTaskMonitor(1, false))) {
         successfullyOpenedFiles.add(f);
       }
     }
   }
 }
Example #2
0
    @Override
    protected void realRun() throws SAXException, IOException, OsmTransferException {
      if (files == null || files.isEmpty()) return;

      /** Find the importer with the chosen file filter */
      FileImporter chosenImporter = null;
      for (FileImporter importer : ExtensionFileFilter.importers) {
        if (fileFilter == importer.filter) {
          chosenImporter = importer;
        }
      }
      /**
       * If the filter hasn't been changed in the dialog, chosenImporter is null now. When the
       * filter has been set explicitly to AllFormatsImporter, treat this the same.
       */
      if (chosenImporter instanceof AllFormatsImporter) {
        chosenImporter = null;
      }
      getProgressMonitor().setTicksCount(files.size());

      if (chosenImporter != null) {
        // The importer was explicitly chosen, so use it.
        List<File> filesNotMatchingWithImporter = new LinkedList<File>();
        List<File> filesMatchingWithImporter = new LinkedList<File>();
        for (final File f : files) {
          if (!chosenImporter.acceptFile(f)) {
            if (f.isDirectory()) {
              SwingUtilities.invokeLater(
                  new Runnable() {
                    @Override
                    public void run() {
                      JOptionPane.showMessageDialog(
                          Main.parent,
                          tr(
                              "<html>Cannot open directory ''{0}''.<br>Please select a file.</html>",
                              f.getAbsolutePath()),
                          tr("Open file"),
                          JOptionPane.ERROR_MESSAGE);
                    }
                  });
              // TODO when changing to Java 6: Don't cancel the
              // task here but use different modality. (Currently 2 dialogs
              // would block each other.)
              return;
            } else {
              filesNotMatchingWithImporter.add(f);
            }
          } else {
            filesMatchingWithImporter.add(f);
          }
        }

        if (!filesNotMatchingWithImporter.isEmpty()) {
          alertFilesNotMatchingWithImporter(filesNotMatchingWithImporter, chosenImporter);
        }
        if (!filesMatchingWithImporter.isEmpty()) {
          importData(chosenImporter, filesMatchingWithImporter);
        }
      } else {
        // find appropriate importer
        MultiMap<FileImporter, File> importerMap = new MultiMap<FileImporter, File>();
        List<File> filesWithUnknownImporter = new LinkedList<File>();
        List<File> urlFiles = new LinkedList<File>();
        FILES:
        for (File f : files) {
          for (FileImporter importer : ExtensionFileFilter.importers) {
            if (importer.acceptFile(f)) {
              importerMap.put(importer, f);
              continue FILES;
            }
          }
          if (urlFileFilter.accept(f)) {
            urlFiles.add(f);
          } else {
            filesWithUnknownImporter.add(f);
          }
        }
        if (!filesWithUnknownImporter.isEmpty()) {
          alertFilesWithUnknownImporter(filesWithUnknownImporter);
        }
        List<FileImporter> importers = new ArrayList<FileImporter>(importerMap.keySet());
        Collections.sort(importers);
        Collections.reverse(importers);

        Set<String> fileHistory = new LinkedHashSet<String>();
        Set<String> failedAll = new HashSet<String>();

        for (FileImporter importer : importers) {
          List<File> files = new ArrayList<File>(importerMap.get(importer));
          importData(importer, files);
          // suppose all files will fail to load
          List<File> failedFiles = new ArrayList<File>(files);

          if (recordHistory && !importer.isBatchImporter()) {
            // remove the files which didn't fail to load from the failed list
            failedFiles.removeAll(successfullyOpenedFiles);
            for (File f : successfullyOpenedFiles) {
              fileHistory.add(f.getCanonicalPath());
            }
            for (File f : failedFiles) {
              failedAll.add(f.getCanonicalPath());
            }
          }
        }

        for (File urlFile : urlFiles) {
          try {
            BufferedReader reader = new BufferedReader(new FileReader(urlFile));
            String line;
            while ((line = reader.readLine()) != null) {
              Matcher m = Pattern.compile(".*(http://.*)").matcher(line);
              if (m.matches()) {
                String url = m.group(1);
                Main.main.menu.openLocation.openUrl(false, url);
              }
            }
            Utils.close(reader);
          } catch (Exception e) {
            Main.error(e);
          }
        }

        if (recordHistory) {
          Collection<String> oldFileHistory = Main.pref.getCollection("file-open.history");
          fileHistory.addAll(oldFileHistory);
          // remove the files which failed to load from the list
          fileHistory.removeAll(failedAll);
          int maxsize = Math.max(0, Main.pref.getInteger("file-open.history.max-size", 15));
          Main.pref.putCollectionBounded("file-open.history", maxsize, fileHistory);
        }
      }
    }