Search in sources :

Example 1 with DeckImporter

use of mage.cards.decks.importer.DeckImporter in project mage by magefree.

the class UpdateDeckTask method importFromFile.

private void importFromFile(java.awt.event.ActionEvent evt) {
    String lastFolder = MageFrame.getPreferences().get("lastImportFolder", "");
    if (!lastFolder.isEmpty()) {
        fcImportDeck.setCurrentDirectory(new File(lastFolder));
    }
    int ret = fcImportDeck.showOpenDialog(this);
    if (ret == JFileChooser.APPROVE_OPTION) {
        File file = fcImportDeck.getSelectedFile();
        MageFrame.getDesktop().setCursor(new Cursor(Cursor.WAIT_CURSOR));
        try {
            DeckImporter importer = DeckImporter.getDeckImporter(file.getPath());
            if (importer != null) {
                StringBuilder errorMessages = new StringBuilder();
                Deck newDeck = null;
                // file will be auto-fixed and saved on simple errors
                newDeck = Deck.load(importer.importDeck(file.getPath(), errorMessages, true));
                processAndShowImportErrors(errorMessages);
                if (newDeck != null) {
                    deck = newDeck;
                    refreshDeck();
                }
                // save last deck import folder
                try {
                    MageFrame.getPreferences().put("lastImportFolder", file.getCanonicalPath());
                } catch (IOException ex) {
                    logger.error("Error on save last used import folder: " + ex.getMessage());
                }
            } else {
                JOptionPane.showMessageDialog(MageFrame.getDesktop(), "Unknown deck format", "Error importing deck", JOptionPane.ERROR_MESSAGE);
            }
        } catch (Exception ex) {
            logger.fatal(ex);
        } finally {
            MageFrame.getDesktop().setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        }
    }
    fcImportDeck.setSelectedFile(null);
}
Also used : DeckImporter(mage.cards.decks.importer.DeckImporter) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException) DeckGeneratorException(mage.client.deck.generator.DeckGenerator.DeckGeneratorException) GameException(mage.game.GameException)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 DeckImporter (mage.cards.decks.importer.DeckImporter)1 DeckGeneratorException (mage.client.deck.generator.DeckGenerator.DeckGeneratorException)1 GameException (mage.game.GameException)1