Example #1
0
  protected static boolean saveFileAs() {
    JFileChooser fc = new JFileChooser();
    fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
    fc.setFileFilter(JvdExtensionFilter.getInstance());
    fc.setAcceptAllFileFilterUsed(false);

    int returnValue = fc.showSaveDialog(parent);

    if (returnValue != JFileChooser.APPROVE_OPTION) {
      return false;
    }

    File file = fc.getSelectedFile();

    if (!file.getAbsolutePath().endsWith(".jvd")) {
      file = new File(file.getAbsoluteFile() + ".jvd");
    }

    if (file.exists()) {
      int choice =
          JOptionPane.showOptionDialog(
              parent,
              "File " + file.getName() + " already exists. Do you want to replace it?",
              "Replace " + file.getName() + "?",
              JOptionPane.YES_NO_OPTION,
              JOptionPane.WARNING_MESSAGE,
              null,
              null,
              null);
      if (choice != JOptionPane.YES_OPTION) {
        return false;
      }
    }

    try (BufferedWriter bw =
        new BufferedWriter(
            new OutputStreamWriter(new FileOutputStream(file, false), StandardCharsets.UTF_8))) {
      int count = drawingModel.getSize();
      for (int i = 0; i < count; i++) {
        bw.write(drawingModel.getObject(i).toJVD());
        bw.newLine();
      }

    } catch (IOException e) {
      JOptionPane.showMessageDialog(
          parent, "Error saving file!", "Error!", JOptionPane.ERROR_MESSAGE);
    }

    drawingModel.changed = false;
    drawingModel.openedFile = file;
    return true;
  }