[refactoring] Moved Sets.loadDeck to a DeckImporterImplementation

This commit is contained in:
North 2012-04-08 16:52:21 +03:00
parent e9ac5a180b
commit 47bd74a24c
17 changed files with 218 additions and 172 deletions

View file

@ -1,72 +0,0 @@
/*
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of BetaSteward_at_googlemail.com.
*/
package mage.client.deckeditor;
import mage.cards.Card;
import mage.cards.decks.DeckCardLists;
import mage.sets.Sets;
/**
*
* @author BetaSteward_at_googlemail.com
*/
public class DecDeckImporter extends DeckImporterImpl {
@Override
protected void readLine(String line, DeckCardLists deckList) {
if (line.length() == 0 || line.startsWith("//")) return;
boolean sideboard = false;
if (line.startsWith("SB:")) {
line = line.substring(3).trim();
sideboard = true;
}
int delim = line.indexOf(' ');
String lineNum = line.substring(0, delim).trim();
String lineName = line.substring(delim).trim();
try {
int num = Integer.parseInt(lineNum);
Card card = Sets.findCard(lineName);
if (card == null)
sbMessage.append("Could not find card: '").append(lineName).append("' at line ").append(lineCount).append("\n");
else {
String cardName = card.getClass().getCanonicalName();
for (int i = 0; i < num; i++) {
if (!sideboard)
deckList.getCards().add(cardName);
else
deckList.getSideboard().add(cardName);
}
}
}
catch (NumberFormatException nfe) {
sbMessage.append("Invalid number: ").append(lineNum).append(" at line ").append(lineCount).append("\n");
}
}
}

View file

@ -25,15 +25,10 @@
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of BetaSteward_at_googlemail.com.
*/
/*
* DeckEditorPanel.java
*
* Created on Feb 18, 2010, 2:47:04 PM
*/
package mage.client.deckeditor;
import mage.cards.decks.importer.DeckImporter;
import mage.cards.decks.importer.DeckImporterUtil;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
@ -334,6 +329,7 @@ public class DeckEditorPanel extends javax.swing.JPanel {
btnSave.setText("Save");
btnSave.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSaveActionPerformed(evt);
}
@ -341,6 +337,7 @@ public class DeckEditorPanel extends javax.swing.JPanel {
btnLoad.setText("Load");
btnLoad.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnLoadActionPerformed(evt);
}
@ -348,6 +345,7 @@ public class DeckEditorPanel extends javax.swing.JPanel {
btnNew.setText("New");
btnNew.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnNewActionPerformed(evt);
}
@ -355,6 +353,7 @@ public class DeckEditorPanel extends javax.swing.JPanel {
btnExit.setText("Exit");
btnExit.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnExitActionPerformed(evt);
}
@ -363,6 +362,7 @@ public class DeckEditorPanel extends javax.swing.JPanel {
btnImport.setText("Import");
btnImport.setName("btnImport"); // NOI18N
btnImport.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnImportActionPerformed(evt);
}
@ -371,6 +371,7 @@ public class DeckEditorPanel extends javax.swing.JPanel {
btnSubmit.setText("Submit");
btnSubmit.setName("btnSubmit"); // NOI18N
btnSubmit.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSubmitActionPerformed(evt);
}
@ -379,6 +380,7 @@ public class DeckEditorPanel extends javax.swing.JPanel {
btnAddLand.setText("Add Land");
btnAddLand.setName("btnAddLand"); // NOI18N
btnAddLand.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAddLandActionPerformed(evt);
}
@ -480,7 +482,7 @@ public class DeckEditorPanel extends javax.swing.JPanel {
File file = fcSelectDeck.getSelectedFile();
try {
setCursor(new Cursor(Cursor.WAIT_CURSOR));
deck = Deck.load(Sets.loadDeck(file.getPath()), true);
deck = Deck.load(DeckImporterUtil.importDeck(file.getPath()), true);
} catch (GameException ex) {
JOptionPane.showMessageDialog(MageFrame.getDesktop(), ex.getMessage(), "Error loading deck", JOptionPane.ERROR_MESSAGE);
} catch (Exception ex) {
@ -550,9 +552,13 @@ public class DeckEditorPanel extends javax.swing.JPanel {
File file = fcImportDeck.getSelectedFile();
try {
setCursor(new Cursor(Cursor.WAIT_CURSOR));
DeckImporter importer = getDeckImporter(file.getPath());
DeckImporter importer = DeckImporterUtil.getDeckImporter(file.getPath());
if (importer != null) {
deck = Deck.load(importer.importDeck(file.getPath()));
String errors = importer.getErrors();
if(!errors.isEmpty()){
JOptionPane.showMessageDialog(MageFrame.getDesktop(), errors, "Error importing deck", JOptionPane.ERROR_MESSAGE);
}
}
else {
JOptionPane.showMessageDialog(MageFrame.getDesktop(), "Unknown deck format", "Error importing deck", JOptionPane.ERROR_MESSAGE);
@ -586,17 +592,6 @@ public class DeckEditorPanel extends javax.swing.JPanel {
refreshDeck();
}
public DeckImporter getDeckImporter(String file) {
if (file.toLowerCase().endsWith("dec"))
return new DecDeckImporter();
else if (file.toLowerCase().endsWith("mwdeck"))
return new MWSDeckImporter();
else if (file.toLowerCase().endsWith("txt"))
return new TxtDeckImporter();
else
return null;
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private mage.client.cards.BigCard bigCard;
private javax.swing.JButton btnExit;

View file

@ -1,41 +0,0 @@
/*
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of BetaSteward_at_googlemail.com.
*/
package mage.client.deckeditor;
import mage.cards.decks.DeckCardLists;
/**
*
* @author BetaSteward_at_googlemail.com
*/
public interface DeckImporter {
public DeckCardLists importDeck(String file);
}

View file

@ -1,81 +0,0 @@
/*
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of BetaSteward_at_googlemail.com.
*/
package mage.client.deckeditor;
import java.io.File;
import java.util.Scanner;
import javax.swing.JOptionPane;
import mage.cards.decks.DeckCardLists;
import mage.client.MageFrame;
import org.apache.log4j.Logger;
/**
*
* @author BetaSteward_at_googlemail.com
*/
public abstract class DeckImporterImpl implements DeckImporter {
private final static Logger logger = Logger.getLogger(DeckImporterImpl.class);
protected StringBuilder sbMessage = new StringBuilder();
protected int lineCount;
@Override
public DeckCardLists importDeck(String file) {
File f = new File(file);
DeckCardLists deckList = new DeckCardLists();
lineCount = 0;
sbMessage.setLength(0);
try {
Scanner scanner = new Scanner(f);
try {
while (scanner.hasNextLine()) {
String line = scanner.nextLine().trim();
lineCount++;
readLine(line, deckList);
}
if (sbMessage.length() > 0) {
logger.fatal(sbMessage);
JOptionPane.showMessageDialog(MageFrame.getDesktop(), sbMessage.toString(), "Error importing deck", JOptionPane.ERROR_MESSAGE);
}
}
catch (Exception ex) {
JOptionPane.showMessageDialog(MageFrame.getDesktop(), ex.getMessage(), "Error importing deck", JOptionPane.ERROR_MESSAGE);
logger.fatal(null, ex);
}
finally {
scanner.close();
}
} catch (Exception ex) {
logger.fatal(null, ex);
}
return deckList;
}
protected abstract void readLine(String line, DeckCardLists deckList);
}

View file

@ -1,91 +0,0 @@
/*
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of BetaSteward_at_googlemail.com.
*/
package mage.client.deckeditor;
import javax.swing.JOptionPane;
import mage.cards.Card;
import mage.cards.ExpansionSet;
import mage.cards.decks.DeckCardLists;
import mage.client.MageFrame;
import mage.sets.Sets;
/**
*
* @author BetaSteward_at_googlemail.com
*/
public class MWSDeckImporter extends DeckImporterImpl {
@Override
protected void readLine(String line, DeckCardLists deckList) {
if (line.length() == 0 || line.startsWith("//")) return;
boolean sideboard = false;
if (line.startsWith("SB:")) {
line = line.substring(3).trim();
sideboard = true;
}
int delim = line.indexOf(' ');
String lineNum = line.substring(0, delim).trim();
String setCode = "";
if (line.indexOf('[') != -1 ) {
int setStart = line.indexOf('[') + 1;
int setEnd = line.indexOf(']');
setCode = line.substring(setStart, setEnd).trim();
delim = setEnd;
}
String lineName = line.substring(delim + 1).trim();
try {
int num = Integer.parseInt(lineNum);
ExpansionSet set = null;
if (setCode.length() > 0)
set = Sets.findSet(setCode);
Card card;
if (set != null) {
card = set.findCard(lineName);
}
else {
card = Sets.findCard(lineName);
}
if (card == null)
sbMessage.append("Could not find card: '").append(lineName).append("' at line ").append(lineCount).append("\n");
else {
String cardName = card.getClass().getCanonicalName();
for (int i = 0; i < num; i++) {
if (!sideboard)
deckList.getCards().add(cardName);
else
deckList.getSideboard().add(cardName);
}
}
}
catch (NumberFormatException nfe) {
sbMessage.append("Invalid number: ").append(lineNum).append(" at line ").append(lineCount).append("\n");
}
}
}

View file

@ -1,73 +0,0 @@
/*
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of BetaSteward_at_googlemail.com.
*/
package mage.client.deckeditor;
import mage.cards.Card;
import mage.cards.decks.DeckCardLists;
import mage.sets.Sets;
/**
*
* @author BetaSteward_at_googlemail.com
*/
public class TxtDeckImporter extends DeckImporterImpl {
private boolean sideboard = false;
@Override
protected void readLine(String line, DeckCardLists deckList) {
if (line.length() == 0 || line.startsWith("//")) return;
if (line.startsWith("Sideboard")) {
sideboard = true;
return;
}
int delim = line.indexOf(' ');
String lineNum = line.substring(0, delim).trim();
String lineName = line.substring(delim).trim();
try {
int num = Integer.parseInt(lineNum);
Card card = Sets.findCard(lineName);
if (card == null)
sbMessage.append("Could not find card: '").append(lineName).append("' at line ").append(lineCount).append("\n");
else {
String cardName = card.getClass().getCanonicalName();
for (int i = 0; i < num; i++) {
if (!sideboard)
deckList.getCards().add(cardName);
else
deckList.getSideboard().add(cardName);
}
}
}
catch (NumberFormatException nfe) {
sbMessage.append("Invalid number: ").append(lineNum).append(" at line ").append(lineCount).append("\n");
}
}
}