Exemplo n.º 1
0
 protected void uninstallListeners(JFileChooser fc) {
   if (propertyChangeListener != null) {
     fc.removePropertyChangeListener(propertyChangeListener);
   }
   fc.removePropertyChangeListener(getModel());
   SwingUtilities.replaceUIInputMap(fc, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, null);
   SwingUtilities.replaceUIActionMap(fc, null);
 }
Exemplo n.º 2
0
  // TODO use pcgen save methods rather than implementing it again
  public boolean savePC(PlayerCharacter aPC, boolean saveas) {
    boolean newPC = false;
    File prevFile;
    File file = null;
    String aPCFileName = aPC.getFileName();

    if (aPCFileName.isEmpty()) {
      prevFile =
          new File(
              PCGenSettings.getPcgDir(),
              aPC.getDisplay().getDisplayName() + Constants.EXTENSION_CHARACTER_FILE);
      aPCFileName = prevFile.getAbsolutePath();
      newPC = true;
    } else {
      prevFile = new File(aPCFileName);
    }

    if (saveas || newPC) {
      JFileChooser fc = ImagePreview.decorateWithImagePreview(new JFileChooser());
      String[] pcgs = new String[] {FILENAME_PCG};
      SimpleFileFilter ff =
          new SimpleFileFilter(pcgs, LanguageBundle.getString("in_pcgen_file_char")); // $NON-NLS-1$
      fc.setFileFilter(ff);
      fc.setSelectedFile(prevFile);

      FilenameChangeListener listener = new FilenameChangeListener(aPCFileName, fc);

      fc.addPropertyChangeListener(listener);

      int returnVal = fc.showSaveDialog(GMGenSystem.inst);
      fc.removePropertyChangeListener(listener);

      if (returnVal == JFileChooser.APPROVE_OPTION) {
        file = fc.getSelectedFile();

        if (!PCGFile.isPCGenCharacterFile(file)) {
          file = new File(file.getParent(), file.getName() + Constants.EXTENSION_CHARACTER_FILE);
        }

        if (file.isDirectory()) {
          JOptionPane.showMessageDialog(
              null,
              LanguageBundle.getString("in_savePcDirOverwrite"), // $NON-NLS-1$
              Constants.APPLICATION_NAME,
              JOptionPane.ERROR_MESSAGE);

          return false;
        }

        if (file.exists() && (newPC || !file.getName().equals(prevFile.getName()))) {
          int reallyClose =
              JOptionPane.showConfirmDialog(
                  GMGenSystem.inst,
                  LanguageBundle.getFormattedString(
                      "in_savePcConfirmOverMsg", //$NON-NLS-1$
                      file.getName()),
                  LanguageBundle.getFormattedString(
                      "in_savePcConfirmOverTitle", file.getName()), // $NON-NLS-1$
                  JOptionPane.YES_NO_OPTION);

          if (reallyClose != JOptionPane.YES_OPTION) {
            return false;
          }
        }

        aPC.setFileName(file.getAbsolutePath());
      } else { // not saving

        return false;
      }
    } else { // simple save
      file = prevFile;
    }

    try {
      (new PCGIOHandler()).write(aPC, null, null, file);
    } catch (Exception ex) {
      String formattedString =
          LanguageBundle.getFormattedString(
              "in_saveFailMsg", aPC.getDisplay().getDisplayName()); // $NON-NLS-1$
      JOptionPane.showMessageDialog(
          null, formattedString, Constants.APPLICATION_NAME, JOptionPane.ERROR_MESSAGE);
      Logging.errorPrint(formattedString);
      Logging.errorPrint(ex.getMessage(), ex);

      return false;
    }

    return true;
  }