@Override
  public void previewSpells() {
    boolean aBool = SettingsHandler.getPrintSpellsWithPC();
    SettingsHandler.setPrintSpellsWithPC(true);

    String templateFileName =
        PCGenSettings.getInstance().getProperty(PCGenSettings.SELECTED_SPELL_SHEET_PATH);
    if (StringUtils.isEmpty(templateFileName)) {
      delegate.showErrorMessage(
          Constants.APPLICATION_NAME, LanguageBundle.getString("in_spellNoSheet")); // $NON-NLS-1$
      return;
    }
    File templateFile = new File(templateFileName);

    File outputFile = BatchExporter.getTempOutputFilename(templateFile);

    boolean success;
    if (ExportUtilities.isPdfTemplate(templateFile)) {
      success = BatchExporter.exportCharacterToPDF(pcFacade, outputFile, templateFile);
    } else {
      success = BatchExporter.exportCharacterToNonPDF(pcFacade, outputFile, templateFile);
    }
    if (success) {
      try {
        Utility.viewInBrowser(outputFile);
      } catch (IOException e) {
        Logging.errorPrint("SpellSupportFacadeImpl.previewSpells failed", e);
        delegate.showErrorMessage(
            Constants.APPLICATION_NAME,
            LanguageBundle.getString("in_spellPreviewFail")); // $NON-NLS-1$
      }
    }
    SettingsHandler.setPrintSpellsWithPC(aBool);
  }
  /** 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);
  }
Exemple #3
0
 @Override
 public boolean performAnalysis(CDOMObject pc) {
   TaskStrategyMessage.sendStatus(this, "Finding Data Directories");
   campaignList = pc.getListFor(ListKey.CAMPAIGN);
   path = pc.get(ObjectKey.WRITE_DIRECTORY);
   if (path != null) {
     fileLabel.setText(path.getAbsolutePath());
   } else {
     PCGenSettings context = PCGenSettings.getInstance();
     String outputPathName =
         context.initProperty(PCGenSettings.CONVERT_OUTPUT_SAVE_PATH, SystemUtils.USER_DIR);
     path = new File(outputPathName);
   }
   pc.put(ObjectKey.WRITE_DIRECTORY, path);
   fireProgressEvent(ProgressEvent.ALLOWED);
   return true;
 }
/**
 * This stores some of the properties that pcgen uses. This class is mainly intended to be used to
 * store non-ui related properties
 *
 * @author Connor Petty <*****@*****.**>
 */
public class PCGenSettings extends PropertyContext {

  private static final PCGenSettings instance = new PCGenSettings();
  /**
   * This is the PropertyContext for the pcgen options, all keys that are used with this context
   * have a key name starting with 'OPTION'
   */
  public static final PropertyContext OPTIONS_CONTEXT =
      instance.createChildContext("pcgen.options");

  public static final String OPTION_SAVE_CUSTOM_EQUIPMENT = "saveCustomInLst";
  public static final String OPTION_ALLOWED_IN_SOURCES = "optionAllowedInSources";
  public static final String OPTION_SHOW_LICENSE = "showLicense";
  public static final String OPTION_SHOW_MATURE_ON_LOAD = "showMatureOnLoad";
  public static final String OPTION_SHOW_SPONSORS_ON_LOAD = "showSponsorsOnLoad";
  public static final String OPTION_CREATE_PCG_BACKUP = "createPcgBackup";
  public static final String OPTION_SHOW_HP_DIALOG_AT_LEVELUP = "showHPDialogAtLevelUp";
  public static final String OPTION_SHOW_STAT_DIALOG_AT_LEVELUP = "showStatDialogAtLevelUp";
  public static final String OPTION_SHOW_WARNING_AT_FIRST_LEVEL_UP = "showWarningAtFirstLevelUp";
  public static final String OPTION_AUTO_RESIZE_EQUIP = "autoResizeEquip";
  public static final String BROWSER_PATH = "browserPath";
  /** The key for the path to the character files. */
  public static final String PCG_SAVE_PATH = "pcgen.files.characters";

  public static final String PCP_SAVE_PATH = "pcgen.files.parties";
  public static final String CHAR_PORTRAITS_PATH = "pcgen.files.portaits";
  public static final String BACKUP_PCG_PATH = "pcgen.files.backupPcg";
  public static final String SELECTED_SPELL_SHEET_PATH = "pcgen.files.selectedSpellOutputSheet";
  public static final String RECENT_CHARACTERS = "recentCharacters";
  public static final String RECENT_PARTIES = "recentParties";

  private PCGenSettings() {
    super("options.ini");
    setProperty(
        PCG_SAVE_PATH,
        (ConfigurationSettings.getUserDir() + "/characters").replace('/', File.separatorChar));
    setProperty(
        PCP_SAVE_PATH,
        (ConfigurationSettings.getUserDir() + "/characters").replace('/', File.separatorChar));
    setProperty(
        CHAR_PORTRAITS_PATH,
        (ConfigurationSettings.getUserDir() + "/characters").replace('/', File.separatorChar));
    setProperty(
        BACKUP_PCG_PATH,
        (ConfigurationSettings.getUserDir() + "/characters").replace('/', File.separatorChar));
  }

  public static PCGenSettings getInstance() {
    return instance;
  }

  public static String getSelectedSpellSheet() {
    return getInstance().getProperty(SELECTED_SPELL_SHEET_PATH);
  }

  public static String getPcgDir() {
    return getInstance().getProperty(PCG_SAVE_PATH);
  }

  public static String getPortraitsDir() {
    return getInstance().getProperty(CHAR_PORTRAITS_PATH);
  }

  public static String getBackupPcgDir() {
    return getInstance().getProperty(BACKUP_PCG_PATH);
  }

  public static String getBrowserPath() {
    return OPTIONS_CONTEXT.getProperty(BROWSER_PATH);
  }

  public static boolean getCreatePcgBackup() {
    return OPTIONS_CONTEXT.initBoolean(PCGenSettings.OPTION_CREATE_PCG_BACKUP, true);
  }
}
  @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;
  }