Esempio n. 1
0
  private File askUserForCustomSettingsFile() {
    String title = tr("Choose profile file");

    FileFilter filter =
        new FileFilter() {
          @Override
          public boolean accept(File f) {
            return f.isDirectory()
                || f.getName().toLowerCase().endsWith(".xml")
                    && f.getName().toLowerCase().startsWith(schemaKey);
          }

          @Override
          public String getDescription() {
            return tr("JOSM custom settings files (*.xml)");
          }
        };
    JFileChooser fc =
        DiskAccessAction.createAndOpenFileChooser(
            false, false, title, filter, JFileChooser.FILES_ONLY, "customsettings.lastDirectory");
    if (fc != null) {
      File sel = fc.getSelectedFile();
      if (!sel.getName().endsWith(".xml")) sel = new File(sel.getAbsolutePath() + ".xml");
      if (!sel.getName().startsWith(schemaKey)) {
        System.out.println(
            sel.getParentFile().getAbsolutePath() + "/" + schemaKey + "_" + sel.getName());
        sel =
            new File(sel.getParentFile().getAbsolutePath() + "/" + schemaKey + "_" + sel.getName());
      }
      return sel;
    }
    return null;
  }
Esempio n. 2
0
    @Override
    public void actionPerformed(ActionEvent arg0) {
      FileFilter filter =
          new FileFilter() {
            @Override
            public boolean accept(File f) {
              return f.isDirectory() || Utils.hasExtension(f, "gpx", "gpx.gz");
            }

            @Override
            public String getDescription() {
              return tr("GPX Files (*.gpx *.gpx.gz)");
            }
          };
      AbstractFileChooser fc =
          DiskAccessAction.createAndOpenFileChooser(
              true, false, null, filter, JFileChooser.FILES_ONLY, null);
      if (fc == null) return;
      File sel = fc.getSelectedFile();

      try {
        outerPanel.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

        for (int i = gpxLst.size() - 1; i >= 0; i--) {
          GpxDataWrapper wrapper = gpxLst.get(i);
          if (wrapper.file != null && sel.equals(wrapper.file)) {
            cbGpx.setSelectedIndex(i);
            if (!sel.getName().equals(wrapper.name)) {
              JOptionPane.showMessageDialog(
                  Main.parent,
                  tr("File {0} is loaded yet under the name \"{1}\"", sel.getName(), wrapper.name),
                  tr("Error"),
                  JOptionPane.ERROR_MESSAGE);
            }
            return;
          }
        }
        GpxData data = null;
        try (InputStream iStream = createInputStream(sel)) {
          GpxReader reader = new GpxReader(iStream);
          reader.parse(false);
          data = reader.getGpxData();
          data.storageFile = sel;

        } catch (SAXException x) {
          Main.error(x);
          JOptionPane.showMessageDialog(
              Main.parent,
              tr("Error while parsing {0}", sel.getName()) + ": " + x.getMessage(),
              tr("Error"),
              JOptionPane.ERROR_MESSAGE);
          return;
        } catch (IOException x) {
          Main.error(x);
          JOptionPane.showMessageDialog(
              Main.parent,
              tr("Could not read \"{0}\"", sel.getName()) + '\n' + x.getMessage(),
              tr("Error"),
              JOptionPane.ERROR_MESSAGE);
          return;
        }

        loadedGpxData.add(data);
        if (gpxLst.get(0).file == null) {
          gpxLst.remove(0);
        }
        gpxLst.add(new GpxDataWrapper(sel.getName(), data, sel));
        cbGpx.setSelectedIndex(cbGpx.getItemCount() - 1);
      } finally {
        outerPanel.setCursor(Cursor.getDefaultCursor());
      }
    }