Search in sources :

Example 6 with PCGIOHandler

use of pcgen.io.PCGIOHandler in project pcgen by PCGen.

the class CharacterManager method openPcInternal.

@SuppressWarnings("unchecked")
private static PlayerCharacter openPcInternal(File file, UIDelegate delegate, DataSetFacade dataset, boolean blockLoadedMessage) {
    @SuppressWarnings("rawtypes") List campaigns = ListFacades.wrap(dataset.getCampaigns());
    final PCGIOHandler ioHandler = new PCGIOHandler();
    final PlayerCharacter newPC;
    try {
        newPC = new PlayerCharacter(campaigns);
        newPC.setFileName(file.getAbsolutePath());
        ioHandler.read(newPC, file.getAbsolutePath());
        // Ensure any custom equipment held by the character is added to the dataset's list
        dataset.refreshEquipment();
        if (!showLoadNotices(true, ioHandler.getErrors(), file.getName(), delegate)) {
            // if we've had errors, then abort trying to add the new PC, it's most likely "broken"
            return null;
        }
        if (!showLoadNotices(false, ioHandler.getWarnings(), file.getName(), delegate)) {
            return null;
        }
        Logging.log(Logging.INFO, //$NON-NLS-1$
        "Loaded character " + newPC.getName() + " - " + //$NON-NLS-1$
        file.getAbsolutePath());
        // if it's not broken, then only warnings should have been generated, and we won't count those
        // Register the character so that future checks to see if file already loaded will work
        Globals.getPCList().add(newPC);
        if (!blockLoadedMessage) {
            messageHandler.handleMessage(new PlayerCharacterWasLoadedMessage(delegate, newPC));
        }
        return newPC;
    } catch (final Exception e) {
        //$NON-NLS-1$
        Logging.errorPrint("Unable to load character " + file, e);
        delegate.showErrorMessage(//$NON-NLS-1$
        LanguageBundle.getString("in_cmLoadErrorTitle"), //$NON-NLS-1$
        LanguageBundle.getFormattedString(//$NON-NLS-1$
        "in_cmLoadErrorMessage", file, e.getMessage()));
        return null;
    }
}
Also used : PlayerCharacterWasLoadedMessage(pcgen.pluginmgr.messages.PlayerCharacterWasLoadedMessage) PCGIOHandler(pcgen.io.PCGIOHandler) PlayerCharacter(pcgen.core.PlayerCharacter) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException)

Example 7 with PCGIOHandler

use of pcgen.io.PCGIOHandler in project pcgen by PCGen.

the class PCGTrackerPlugin method savePC.

/**
	 * Checks whether a character can be saved, and if so, calls
	 * it's {@code save} method.
	 *
	 * @param aPC The PlayerCharacter to save
	 * @param saveas boolean if {@code true}, ask for file name
	 *
	 * @return {@code true} if saved; {@code false} if save as cancelled
	 */
// TODO use pcgen save methods rather than implementing it again
private 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 = { FILENAME_PCG };
        FileFilter ff = new FileNameExtensionFilter(LanguageBundle.getString("in_pcgen_file_char"), //$NON-NLS-1$
        pcgs);
        fc.setFileFilter(ff);
        fc.setSelectedFile(prevFile);
        PropertyChangeListener 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, //$NON-NLS-1$
                LanguageBundle.getString("in_savePcDirOverwrite"), Constants.APPLICATION_NAME, JOptionPane.ERROR_MESSAGE);
                return false;
            }
            if (file.exists() && (newPC || !file.getName().equals(prevFile.getName()))) {
                int reallyClose = JOptionPane.showConfirmDialog(GMGenSystem.inst, //$NON-NLS-1$
                LanguageBundle.getFormattedString(//$NON-NLS-1$
                "in_savePcConfirmOverMsg", file.getName()), //$NON-NLS-1$
                LanguageBundle.getFormattedString("in_savePcConfirmOverTitle", file.getName()), JOptionPane.YES_NO_OPTION);
                if (reallyClose != JOptionPane.YES_OPTION) {
                    return false;
                }
            }
            aPC.setFileName(file.getAbsolutePath());
        } else {
            return false;
        }
    } else {
        // simple save
        file = prevFile;
    }
    try {
        (new PCGIOHandler()).write(aPC, null, null, file);
    } catch (Exception ex) {
        //$NON-NLS-1$
        String formattedString = LanguageBundle.getFormattedString("in_saveFailMsg", aPC.getDisplay().getDisplayName());
        JOptionPane.showMessageDialog(null, formattedString, Constants.APPLICATION_NAME, JOptionPane.ERROR_MESSAGE);
        Logging.errorPrint(formattedString);
        Logging.errorPrint(ex.getMessage(), ex);
        return false;
    }
    return true;
}
Also used : PCGIOHandler(pcgen.io.PCGIOHandler) JFileChooser(javax.swing.JFileChooser) PropertyChangeListener(java.beans.PropertyChangeListener) FileFilter(javax.swing.filechooser.FileFilter) FileNameExtensionFilter(javax.swing.filechooser.FileNameExtensionFilter) PCGFile(pcgen.io.PCGFile) File(java.io.File)

Example 8 with PCGIOHandler

use of pcgen.io.PCGIOHandler in project pcgen by PCGen.

the class CharacterFacadeImpl method save.

/**
	 * Save the character to disc using its filename. Note this method is not 
	 * part of the CharacterFacade and should only be used by the 
	 * ChracterManager class.
	 * 
	 * @throws NullPointerException 
	 * @throws IOException If the write fails
	 */
public void save() throws NullPointerException, IOException {
    GameMode mode = (GameMode) dataSet.getGameMode();
    List<CampaignFacade> campaigns = ListFacades.wrap(dataSet.getCampaigns());
    (new PCGIOHandler()).write(theCharacter, mode, campaigns, file.get());
    theCharacter.setDirty(false);
}
Also used : GameMode(pcgen.core.GameMode) PCGIOHandler(pcgen.io.PCGIOHandler) CampaignFacade(pcgen.facade.core.CampaignFacade)

Aggregations

PCGIOHandler (pcgen.io.PCGIOHandler)8 File (java.io.File)3 PCGFile (pcgen.io.PCGFile)3 GameMode (pcgen.core.GameMode)2 PlayerCharacter (pcgen.core.PlayerCharacter)2 CampaignFacade (pcgen.facade.core.CampaignFacade)2 SourceSelectionFacade (pcgen.facade.core.SourceSelectionFacade)2 PropertyChangeListener (java.beans.PropertyChangeListener)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 JFileChooser (javax.swing.JFileChooser)1 FileFilter (javax.swing.filechooser.FileFilter)1 FileNameExtensionFilter (javax.swing.filechooser.FileNameExtensionFilter)1 GameModeFacade (pcgen.facade.core.GameModeFacade)1 PCGVer2Creator (pcgen.io.PCGVer2Creator)1 PlayerCharacterWasLoadedMessage (pcgen.pluginmgr.messages.PlayerCharacterWasLoadedMessage)1