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;
}
}
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;
}
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);
}
Aggregations