Search in sources :

Example 1 with UIDelegate

use of pcgen.facade.core.UIDelegate in project pcgen by PCGen.

the class FtlMigrationTest method loadCharacter.

/**
	 * @param characterFile
	 * @return
	 */
private CharacterFacade loadCharacter(String characterFilename) {
    File file = new File(characterFilename);
    if (!PCGFile.isPCGenCharacterFile(file)) {
        Logging.errorPrint("Invalid character file specified: " + file.getAbsolutePath());
        return null;
    }
    // Load data
    UIDelegate uiDelegate = new ConsoleUIDelegate();
    SourceSelectionFacade sourcesForCharacter = CharacterManager.getRequiredSourcesForCharacter(file, uiDelegate);
    Logging.log(Logging.INFO, "Loading sources " + sourcesForCharacter.getCampaigns() + " using game mode " + sourcesForCharacter.getGameMode());
    SourceFileLoader loader = new SourceFileLoader(sourcesForCharacter, uiDelegate);
    loader.execute();
    // Load character
    CharacterFacade character = CharacterManager.openCharacter(file, uiDelegate, loader.getDataSetFacade());
    return character;
}
Also used : ConsoleUIDelegate(pcgen.system.ConsoleUIDelegate) SourceSelectionFacade(pcgen.facade.core.SourceSelectionFacade) SourceFileLoader(pcgen.persistence.SourceFileLoader) PCGFile(pcgen.io.PCGFile) File(java.io.File) CharacterFacade(pcgen.facade.core.CharacterFacade) ConsoleUIDelegate(pcgen.system.ConsoleUIDelegate) UIDelegate(pcgen.facade.core.UIDelegate)

Example 2 with UIDelegate

use of pcgen.facade.core.UIDelegate in project pcgen by PCGen.

the class Main method startupWithoutGUI.

private static boolean startupWithoutGUI() {
    loadProperties(false);
    validateEnvironment(false);
    PCGenTaskExecutor executor = new PCGenTaskExecutor();
    executor.addPCGenTask(createLoadPluginTask());
    executor.addPCGenTask(new GameModeFileLoader());
    executor.addPCGenTask(new CampaignFileLoader());
    executor.execute();
    UIDelegate uiDelegate = new ConsoleUIDelegate();
    BatchExporter exporter = new BatchExporter(exportSheet, uiDelegate);
    boolean result = true;
    if (partyFile != null) {
        result = exporter.exportParty(partyFile, outputFile);
    }
    if (characterFile != null) {
        result = exporter.exportCharacter(characterFile, outputFile);
    }
    return result;
}
Also used : CampaignFileLoader(pcgen.persistence.CampaignFileLoader) GameModeFileLoader(pcgen.persistence.GameModeFileLoader) UIDelegate(pcgen.facade.core.UIDelegate)

Example 3 with UIDelegate

use of pcgen.facade.core.UIDelegate in project pcgen by PCGen.

the class CharacterManager method saveCharacter.

/**
	 * Saves this character to the character's file specified
	 * by character.getFileRef().getReference()
	 * This is expected to be called before a character is to
	 * be removed from the list of open characters.
	 * @param character the character to be saved
	 * @return true if the save succeeded, false if not 
	 */
public static boolean saveCharacter(CharacterFacade character) {
    File file = character.getFileRef().get();
    if (StringUtils.isBlank(file.getName())) {
        return false;
    }
    Logging.log(Logging.INFO, //$NON-NLS-1$
    "Saving character " + character.getNameRef().get() + " - " + //$NON-NLS-1$
    file.getAbsolutePath());
    if (character instanceof CharacterFacadeImpl) {
        UIDelegate delegate = character.getUIDelegate();
        try {
            ((CharacterFacadeImpl) character).save();
        } catch (final NullPointerException e) {
            Logging.errorPrint("Could not save " + character.getNameRef().get(), e);
            delegate.showErrorMessage(Constants.APPLICATION_NAME, "Could not save " + character.getNameRef().get());
            return false;
        } catch (final IOException e) {
            Logging.errorPrint("Could not save " + character.getNameRef().get(), e);
            delegate.showErrorMessage(Constants.APPLICATION_NAME, "Could not save " + character.getNameRef().get() + " due to the error:\n" + e.getMessage());
            return false;
        }
    } else {
        Logging.errorPrint("Could not save " + character.getNameRef().get() + " due to unexpected class of character: " + character.getClass().getCanonicalName());
        return false;
    }
    recentCharacters.addRecentFile(file);
    return true;
}
Also used : IOException(java.io.IOException) PCGFile(pcgen.io.PCGFile) File(java.io.File) CharacterFacadeImpl(pcgen.gui2.facade.CharacterFacadeImpl) UIDelegate(pcgen.facade.core.UIDelegate)

Example 4 with UIDelegate

use of pcgen.facade.core.UIDelegate in project pcgen by PCGen.

the class DataLoadTest method testLoadSources.

/**
	 * Test the load of the current source. This will check for any load errors or warnings but ignores deprecation warnings.  
	 */
@Test
public void testLoadSources() {
    UIDelegate uiDelegate = new MockUIDelegate();
    SourceFileLoader loader = new SourceFileLoader(sourceSelection, uiDelegate);
    errors = new ArrayList<>();
    loader.addPCGenTaskListener(this);
    loader.execute();
    List<String> errorList = new ArrayList<>();
    List<String> warningList = new ArrayList<>();
    for (LogRecord logRecord : errors) {
        if (logRecord.getLevel().intValue() > Logging.WARNING.intValue()) {
            errorList.add(logRecord.getMessage());
        } else if (logRecord.getLevel().intValue() > Logging.INFO.intValue()) {
            warningList.add(logRecord.getMessage());
        }
    }
    assertEquals("Errors encountered while loading " + sourceSelection, "", StringUtils.join(errorList, ",\n"));
    assertEquals("Warnings encountered while loading " + sourceSelection, "", StringUtils.join(errorList, ",\n"));
}
Also used : LogRecord(java.util.logging.LogRecord) MockUIDelegate(pcgen.gui2.facade.MockUIDelegate) ArrayList(java.util.ArrayList) SourceFileLoader(pcgen.persistence.SourceFileLoader) MockUIDelegate(pcgen.gui2.facade.MockUIDelegate) UIDelegate(pcgen.facade.core.UIDelegate) Test(org.junit.Test)

Example 5 with UIDelegate

use of pcgen.facade.core.UIDelegate in project pcgen by PCGen.

the class PCGenFrame method showSaveCharacterChooser.

/**
	 * This brings up a file chooser allows the user to select
	 * the location that a character should be saved to.
	 * @param character the character to be saved
	 */
boolean showSaveCharacterChooser(CharacterFacade character) {
    PCGenSettings context = PCGenSettings.getInstance();
    String parentPath = lastCharacterPath;
    if (parentPath == null) {
        parentPath = context.getProperty(PCGenSettings.PCG_SAVE_PATH);
    }
    chooser.setCurrentDirectory(new File(parentPath));
    File file = character.getFileRef().get();
    File prevFile = file;
    if (file == null || StringUtils.isEmpty(file.getName())) {
        file = new File(parentPath, character.getNameRef().get() + Constants.EXTENSION_CHARACTER_FILE);
    }
    chooser.setSelectedFile(file);
    chooser.resetChoosableFileFilters();
    FileFilter filter = new FileNameExtensionFilter("Pcg files only", "pcg");
    chooser.addChoosableFileFilter(filter);
    chooser.setFileFilter(filter);
    int ret = chooser.showSaveDialog(this);
    if (ret == JFileChooser.APPROVE_OPTION) {
        file = chooser.getSelectedFile();
        if (!file.getName().endsWith(Constants.EXTENSION_CHARACTER_FILE)) {
            file = new File(file.getParent(), file.getName() + Constants.EXTENSION_CHARACTER_FILE);
        }
        UIDelegate delegate = character.getUIDelegate();
        if (file.isDirectory()) {
            delegate.showErrorMessage(Constants.APPLICATION_NAME, //$NON-NLS-1$
            LanguageBundle.getString("in_savePcDirOverwrite"));
            return showSaveCharacterChooser(character);
        }
        if (file.exists() && (prevFile == null || !file.getName().equals(prevFile.getName()))) {
            boolean overwrite = delegate.showWarningConfirm(LanguageBundle.getFormattedString(//$NON-NLS-1$
            "in_savePcConfirmOverTitle", file.getName()), LanguageBundle.getFormattedString(//$NON-NLS-1$
            "in_savePcConfirmOverMsg", file.getName()));
            if (!overwrite) {
                return showSaveCharacterChooser(character);
            }
        }
        try {
            character.setFile(file);
            prepareForSave(character, false);
            if (!reallySaveCharacter(character)) {
                return showSaveCharacterChooser(character);
            }
            lastCharacterPath = chooser.getCurrentDirectory().toString();
            return true;
        } catch (Exception e) {
            //$NON-NLS-1$
            Logging.errorPrint("Error saving character to new file " + file, e);
            delegate.showErrorMessage(Constants.APPLICATION_NAME, LanguageBundle.getFormattedString("in_saveFailMsg", //$NON-NLS-1$
            file.getName()));
        }
    }
    return false;
}
Also used : PCGenSettings(pcgen.system.PCGenSettings) FileFilter(javax.swing.filechooser.FileFilter) FileNameExtensionFilter(javax.swing.filechooser.FileNameExtensionFilter) PCGFile(pcgen.io.PCGFile) File(java.io.File) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) UIDelegate(pcgen.facade.core.UIDelegate)

Aggregations

UIDelegate (pcgen.facade.core.UIDelegate)5 File (java.io.File)3 PCGFile (pcgen.io.PCGFile)3 IOException (java.io.IOException)2 SourceFileLoader (pcgen.persistence.SourceFileLoader)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 LogRecord (java.util.logging.LogRecord)1 FileFilter (javax.swing.filechooser.FileFilter)1 FileNameExtensionFilter (javax.swing.filechooser.FileNameExtensionFilter)1 Test (org.junit.Test)1 CharacterFacade (pcgen.facade.core.CharacterFacade)1 SourceSelectionFacade (pcgen.facade.core.SourceSelectionFacade)1 CharacterFacadeImpl (pcgen.gui2.facade.CharacterFacadeImpl)1 MockUIDelegate (pcgen.gui2.facade.MockUIDelegate)1 CampaignFileLoader (pcgen.persistence.CampaignFileLoader)1 GameModeFileLoader (pcgen.persistence.GameModeFileLoader)1 ConsoleUIDelegate (pcgen.system.ConsoleUIDelegate)1 PCGenSettings (pcgen.system.PCGenSettings)1