/** Handles the clicking of the <b>Add</b> button on the GUI. */
  public void handleOpen() {
    File defaultFile = new File(PCGenSettings.getPcgDir());
    JFileChooser chooser = new JFileChooser();
    chooser.setCurrentDirectory(defaultFile);

    String[] pcgs = new String[] {FILENAME_PCG, FILENAME_PCP};
    SimpleFileFilter ff =
        new SimpleFileFilter(pcgs, LanguageBundle.getString("in_pcgen_file")); // $NON-NLS-1$
    chooser.addChoosableFileFilter(ff);
    chooser.setFileFilter(ff);
    chooser.setMultiSelectionEnabled(true);
    Component component = GMGenSystem.inst;
    Cursor originalCursor = component.getCursor();
    component.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

    int option = chooser.showOpenDialog(GMGenSystem.inst);

    if (option == JFileChooser.APPROVE_OPTION) {
      for (File selectedFile : chooser.getSelectedFiles()) {
        if (PCGFile.isPCGenCharacterOrPartyFile(selectedFile)) {
          messageHandler.handleMessage(
              new RequestOpenPlayerCharacterMessage(this, selectedFile, false));
        }
      }
    } else {
      /* this means the file is invalid */
    }

    GMGenSystem.inst.setCursor(originalCursor);
  }
  @Override
  public void exportSpells() {
    final String template =
        PCGenSettings.getInstance().getProperty(PCGenSettings.SELECTED_SPELL_SHEET_PATH);
    if (StringUtils.isEmpty(template)) {
      delegate.showErrorMessage(
          Constants.APPLICATION_NAME, LanguageBundle.getString("in_spellNoSheet")); // $NON-NLS-1$
      return;
    }
    String ext = template.substring(template.lastIndexOf('.'));

    // Get the name of the file to output to.
    JFileChooser fcExport = new JFileChooser();
    fcExport.setCurrentDirectory(new File(PCGenSettings.getPcgDir()));
    fcExport.setDialogTitle(
        LanguageBundle.getString("InfoSpells.export.spells.for")
            + charDisplay.getDisplayName()); // $NON-NLS-1$

    if (fcExport.showSaveDialog(null) != JFileChooser.APPROVE_OPTION) {
      return;
    }
    final String aFileName = fcExport.getSelectedFile().getAbsolutePath();
    if (aFileName.length() < 1) {
      delegate.showErrorMessage(
          Constants.APPLICATION_NAME,
          LanguageBundle.getString("InfoSpells.must.set.filename")); // $NON-NLS-1$
      return;
    }

    try {
      final File outFile = new File(aFileName);

      if (outFile.isDirectory()) {
        delegate.showErrorMessage(
            Constants.APPLICATION_NAME,
            LanguageBundle.getString("InfoSpells.can.not.overwrite.directory")); // $NON-NLS-1$
        return;
      }

      if (outFile.exists()) {
        int reallyClose =
            JOptionPane.showConfirmDialog(
                null,
                LanguageBundle.getFormattedString(
                    "InfoSpells.confirm.overwrite", outFile.getName()), // $NON-NLS-1$
                LanguageBundle.getFormattedString("InfoSpells.overwriting", outFile.getName()),
                JOptionPane.YES_NO_OPTION); // $NON-NLS-1$

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

      // Output the file
      File templateFile = new File(template);
      boolean success;
      if (ExportUtilities.isPdfTemplate(templateFile)) {
        success = BatchExporter.exportCharacterToPDF(pcFacade, outFile, templateFile);
      } else {
        success = BatchExporter.exportCharacterToNonPDF(pcFacade, outFile, templateFile);
      }

      if (!success) {
        delegate.showErrorMessage(
            Constants.APPLICATION_NAME,
            LanguageBundle.getFormattedString(
                "InfoSpells.export.failed", charDisplay.getDisplayName())); // $NON-NLS-1$
      }
    } catch (Exception ex) {
      Logging.errorPrint(
          LanguageBundle.getFormattedString(
              "InfoSpells.export.failed", charDisplay.getDisplayName()),
          ex); //$NON-NLS-1$
      delegate.showErrorMessage(
          Constants.APPLICATION_NAME,
          LanguageBundle.getFormattedString(
              "InfoSpells.export.failed.retry", charDisplay.getDisplayName())); // $NON-NLS-1$
    }
  }
  // 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;
  }