It is currently 07 May 2024, 08:31
   
Text Size

Add any card to your quest cardpool

Post MTG Forge Related Programming Questions Here

Moderators: timmermac, Blacksmith, KrazyTheFox, Agetian, friarsol, CCGHQ Admins

Add any card to your quest cardpool

Postby mtgrares » 03 Mar 2010, 19:56

I added a menu option that allows you to add any card to your quest cardpool. The menu option is named "Cheat - Add Card" but feel free to rename it to whatever sounds best.

This method is probably too long since it is around 270 lines and should be broken in smaller sub-methods.
For better or worse I seem to have a knack for writing extremely long methods. :rolleyes:

This is the whole Gui_Quest_DeckEditor_Menu.setupMenu() method with the old and updated code combined.
All of the new code is marked.
Code: Select all
  private void setupMenu()
  {
    JMenuItem open   = new JMenuItem("Open");
    JMenuItem new2   = new JMenuItem("New");
    JMenuItem rename = new JMenuItem("Rename");
    JMenuItem save   = new JMenuItem("Save");
    JMenuItem copy   = new JMenuItem("Copy");
    JMenuItem delete = new JMenuItem("Delete");
    JMenuItem exit   = new JMenuItem("Exit");

    ////////////////////////////////////////////
    //below is new code

    //adds a card to human player's cardpool
    JMenuItem addCard = new JMenuItem("Cheat - Add Card");

    //add card
    addCard.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent a)
      {
        //sort cards by card name
        CardList cardList = AllZone.CardFactory.getAllCards();
        TableSorter sorter = new TableSorter(cardList, 1, true);
        cardList.sort(sorter);

        //create a new Card object with a different toString() method
        //so that that JList only shows the card's name
        //
        //this is alot of work just to make it a little
        //easier and prettier for the user, gui stuff is very complicated
        class BetterCard extends Card
        {
          private Card card;

          BetterCard(Card c)
          {
            card = c;

            //this line is very important
            //if you omit this, errors will occur
            this.setName(c.getName());
          }

          public String toString()
          {
            return card.getName();
          }
        }//BetterCard

        Card[] card = cardList.toArray();

        for(int i = 0; i < card.length; i++)
        {
          card[i] = new BetterCard(card[i]);
        }

        final JList list = new JList(card);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        //update the "card detail" on the right with the card info
        list.addListSelectionListener(new ListSelectionListener()
        {
          public void valueChanged(ListSelectionEvent e)
          {
            CardDetail cd = (CardDetail)deckDisplay;
            cd.updateCardDetail((Card)list.getSelectedValue());
          }
        });

        Object[] o = {"Add Card to Your Cardpool", new JScrollPane(list)};
        JOptionPane pane = new JOptionPane(o, JOptionPane.INFORMATION_MESSAGE, JOptionPane.OK_CANCEL_OPTION);

        JDialog dialog = pane.createDialog(null, "Cheat - Add Card");
        dialog.setModal(true);
        dialog.setVisible(true);

        Object choice = pane.getValue();
        boolean cancel = false;

        //there are a ton of ways to cancel
        if(
           choice == null ||
           choice.equals(JOptionPane.UNINITIALIZED_VALUE)
           )
          cancel = true;
        else
        {
          int n = ((Integer)choice).intValue();
          if(n == JOptionPane.CANCEL_OPTION)
            cancel = true;
        }

        if(cancel || list.getSelectedValue() == null)
        {
          //System.out.println("cancelled");
        }
        else
        {
          //show the choice that the user selected
          //System.out.println(list.getSelectedValue());

          Card c = (Card) list.getSelectedValue();

          Gui_Quest_DeckEditor g = (Gui_Quest_DeckEditor)deckDisplay;
          TableModel table = g.getTopTableModel();
          table.addCard(c);
          table.resort();
        }
      }//actionPerformed()
    });//add card


    //above is new code
    ///////////////////////////////////////


    //human
    open.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent a)
      {
        String deckName = getUserInput_OpenDeck(questData.getDeckNames());

        //check if user selected "cancel"
        if(deckName.equals(""))
          return;

        openHumanDeck(deckName);
      }
    });//open

    //human
    new2.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent a)
      {
        CardList cardpool = covertToCardList(questData.getCardpool());
        deckDisplay.updateDisplay(cardpool, new CardList());

        setHumanPlayer("");
      }
    });//new


    //human
    rename.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent a)
      {
        String name = getUserInput_GetDeckName(questData.getDeckNames());

        //check if user cancels
        if(name.equals(""))
          return;

        //is the current deck already saved and in QuestData?
        if(questData.getDeckNames().contains(currentDeck.getName()))
          questData.removeDeck(currentDeck.getName());//remove old deck

        currentDeck.setName(name);

        Deck deck = convertCardListToDeck(deckDisplay.getBottom());
        deck.setName(name);
        questData.addDeck(deck);

        setHumanPlayer(name);
      }
    });//rename



    //human
    save.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent a)
      {
        String name = currentDeck.getName();

        //check to see if name is set
        if(name.equals(""))
        {
          name = getUserInput_GetDeckName(questData.getDeckNames());

          //check if user cancels
          if(name.equals(""))
            return;
        }

        setHumanPlayer(name);

        Deck deck = convertCardListToDeck(deckDisplay.getBottom());
        deck.setName(name);

        questData.addDeck(deck);
      }
    });//save



    //human
    copy.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent a)
      {
        String name = getUserInput_GetDeckName(questData.getDeckNames());

        //check if user cancels
        if(name.equals(""))
          return;

        setHumanPlayer(name);

        Deck deck = convertCardListToDeck(deckDisplay.getBottom());
        deck.setName(name);

        questData.addDeck(deck);
      }
    });//copy


    //human
    delete.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent a)
      {
        if(currentDeck.getName().equals(""))
          return;

        int check = JOptionPane.showConfirmDialog(null, "Do you really want to delete this deck?", "Delete", JOptionPane.YES_NO_OPTION);
        if(check == JOptionPane.NO_OPTION)
          return;//stop here

        questData.removeDeck(currentDeck.getName());

        //show card pool
        CardList cardpool = covertToCardList(questData.getCardpool());
        deckDisplay.updateDisplay(cardpool, new CardList());

        setHumanPlayer("");
      }
    });//delete



    //human
    exit.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent a)
      {
        Gui_Quest_DeckEditor_Menu.this.close();
      }
    });

    JMenu deckMenu = new JMenu("Deck");
    deckMenu.add(open);
    deckMenu.add(new2);
    deckMenu.add(rename);
    deckMenu.add(save);
    deckMenu.add(copy);

    deckMenu.addSeparator();//new code
    deckMenu.add(addCard); //new code

    deckMenu.addSeparator();
    addImportExport(deckMenu, true);

    deckMenu.addSeparator();
    deckMenu.add(delete);
    deckMenu.addSeparator();
    deckMenu.add(exit);

    this.add(deckMenu);

  }//setupMenu()
If you just want to cut-and-paste the new code, all of the new code is together except two lines near the end of the method.

deckMenu.addSeparator();//new code
deckMenu.add(addCard); //new code
mtgrares
DEVELOPER
 
Posts: 1352
Joined: 08 Sep 2008, 22:10
Has thanked: 3 times
Been thanked: 12 times

Re: Add any card to your quest cardpool

Postby Chris H. » 04 Mar 2010, 13:37

I am having some problems adding Rares contribution. I found the correct place to add his code to Gui_Quest_DeckEditor_Menu.java. When I paste in the new code I get a lot errors in Eclipse. Most of these errors look like a series of red dots and the message states:

    Syntax error on tokens, delete these tokens

I have seen this problem before and I have figured out how to delete the tokens by hand … but there must be an option within Eclipse to automate this type of deletion.

After deleting these tokens I still get a four errors.

    JList cannot be resolved to a type
    ListSelectionModel cannot be resolved
    ListSelectionListener cannot be resolved
    JDialog cannot be resolved to a type

While looking at the list of fixes it appears that some imports are missing. So I add all of the missing imports and I now only have one error.

    CardDetail cannot be resolved to a type

and this appears on this line:

Code: Select all
CardDetail cd = (CardDetail)deckDisplay;
`
The is a list of 11 posible fixes. I am not sure which would be the correct option.

`
Attachments
CardDetail cannot be resolved to a type.jpg
User avatar
Chris H.
Forge Moderator
 
Posts: 6320
Joined: 04 Nov 2008, 12:11
Location: Mac OS X Yosemite
Has thanked: 644 times
Been thanked: 643 times

Re: Add any card to your quest cardpool

Postby mtgrares » 05 Mar 2010, 18:26

CardDetail is a short interface with one method that I put in the file CardDetailUtil.java. It would be better if the CardDetail interface was in CardDetail.java. (My logic is a little screwy and I'm not 100% sure what I was trying to do. I know that I was trying to abstract the "card detail" panel on the right part of the screen because various parts of the program needed to update it.)

I'm presume you need to import CardDetail because it might be in a separate package or file but I'm not really sure. At worse you can remove the offending code. The code just updates the "card detail" so the user can see the card info.

Sorry about forgetting about the imports. ](*,)

And if it helps, here is my Gui_Quest_DeckEditor_Menu.java
Code: Select all
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.filechooser.FileFilter;

import java.util.*;
import java.io.*;

//presumes AllZone.QuestData is not null
public class Gui_Quest_DeckEditor_Menu extends JMenuBar
{
  //this should be false in the public version
  //if true, the Quest Deck editor will let you edit the computer's decks
  private final boolean canEditComputerDecks;

  private final String deckEditorName = "Deck Editor";

  //used for import and export, try to made the gui user friendly
  private static File previousDirectory = null;

  private Command exitCommand;
  private QuestData questData;
  private Deck currentDeck;//mostly getName() and getGameType() is used

  //the class DeckDisplay is in the file "Gui_DeckEditor_Menu.java"
  private DeckDisplay deckDisplay;


  public Gui_Quest_DeckEditor_Menu(DeckDisplay d, Command exit)
  {
    //is a file named "edit" in this directory
    //lame but it works, I don't like 2 versions of MTG Forge floating around
    //one that lets you edit the AI decks and one that doesn't
    File f = new File("edit");
    if(f.exists())
      canEditComputerDecks = true;
    else
      canEditComputerDecks = false;

    deckDisplay = d;
    d.setTitle(deckEditorName);

    questData = AllZone.QuestData;
    exitCommand = exit;

    setupMenu();

    if(canEditComputerDecks)
      setupComputerMenu();
  }

  private void addImportExport(JMenu menu, final boolean isHumanMenu)
  {
    JMenuItem import2 = new JMenuItem("Import");
    JMenuItem export  = new JMenuItem("Export");

    import2.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent a)
      {
        importDeck(isHumanMenu);
      }
    });//import

    export.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent a)
      {
        exportDeck();
      }
    });//export

    menu.add(import2);
    menu.add(export);

  }//addImportExport()

  private void exportDeck()
  {
    File filename = getExportFilename();

    if(filename == null)
      return;

    //write is an Object variable because you might just
    //write one Deck object
    Deck deck = convertCardListToDeck(deckDisplay.getBottom());;
    Object write = deck;

    deck.setName(filename.getName());


    try{
      ObjectOutputStream out = new ObjectOutputStream(
                               new FileOutputStream(filename));
      out.writeObject(write);
      out.flush();
      out.close();
    }
    catch(Exception ex)
    {
      JOptionPane.showMessageDialog(null, "Sorry there has been an error - " +ex.getMessage());
      throw new RuntimeException("Gui_Quest_DeckEditor_Menu : exportDeck() error, " +ex);
    }

    exportDeckText(getExportDeckText(deck), filename.getAbsolutePath());

  }//exportDeck()

  private String getExportDeckText(Deck aDeck)
  {
    //convert Deck into CardList
    CardList all = new CardList();
    for(int i = 0; i < aDeck.countMain(); i++)
    {
      String cardName = aDeck.getMain(i);
      Card c = AllZone.CardFactory.getCard(cardName, "");

      all.add(c);
    }

    //sort by card name
    all.sort(new TableSorter(all, 1, true));

    //remove all copies of cards
    //make a singleton
    CardList noCopies = new CardList();
    for(int i = 0; i < all.size(); i++)
    {
      Card c = all.get(i);

      if(! noCopies.containsName(c.getName()))
        noCopies.add(c);
    }


    String text = "";
    String newLine = "\r\n";
    int count = 0;

    text = all.size() +" Total Cards" +newLine +newLine;

    //creatures
    text += all.getType("Creature").size() +" Creatures" +newLine;
    text += "-------------" +newLine;

    for(int i = 0; i < noCopies.size(); i++)
    {
      Card c = noCopies.get(i);
      if(c.isCreature())
      {
        count = all.getName(c.getName()).size();
        text += count +"x " +c.getName() +newLine;
      }
    }

    //count spells, arg! this is tough
    CardListFilter cf = new CardListFilter()
    {
      public boolean addCard(Card c)
      {
        return !(c.isCreature() || c.isLand());
      }
    };//CardListFilter
    count = all.filter(cf).size();

    //spells
    text += newLine +count +" Spells" +newLine;
    text += "----------" +newLine;

    for(int i = 0; i < noCopies.size(); i++)
    {
      Card c = noCopies.get(i);
      if(!(c.isCreature() || c.isLand()))
      {
        count = all.getName(c.getName()).size();
        text += count +"x " +c.getName() +newLine;
      }
    }

    //land
    text += newLine +all.getType("Land").size() +" Land" +newLine;
    text += "--------" +newLine;

    for(int i = 0; i < noCopies.size(); i++)
    {
      Card c = noCopies.get(i);
      if(c.isLand())
      {
        count = all.getName(c.getName()).size();
        text += count +"x " +c.getName() +newLine;
      }
    }

    text += newLine;

    return text;
  }//getExportDeckText

  private void exportDeckText(String deckText, String filename)
  {

    //remove ".deck" extension
    int cut = filename.indexOf(".");
    filename = filename.substring(0, cut);

    try{
      FileWriter writer = new FileWriter(filename +".txt");
      writer.write(deckText);

      writer.flush();
      writer.close();
    }
    catch(Exception ex)
    {
      throw new RuntimeException("Gui_Quest_DeckEditor_Menu : exportDeckText() error, " +ex.getMessage() +" : " +ex.getStackTrace());
    }
  }//exportDeckText()


  private FileFilter getFileFilter()
  {
    FileFilter filter = new FileFilter()
    {
      public boolean accept(File f)
      {
        return f.getName().endsWith(".deck") || f.isDirectory();
      }
      public String getDescription() {return "Deck File .deck";}
    };

    return filter;
  }//getFileFilter()

  private File getExportFilename()
  {
    Object o = null;

    JFileChooser save = new JFileChooser(previousDirectory);

    save.setDialogTitle("Export Deck Filename");
    save.setDialogType(JFileChooser.SAVE_DIALOG);
    save.addChoosableFileFilter(getFileFilter());
    save.setSelectedFile(new File(currentDeck.getName() +".deck"));

    int returnVal = save.showSaveDialog(null);

    if(returnVal == JFileChooser.APPROVE_OPTION)
    {
      File file = save.getSelectedFile();
      String check = file.getAbsolutePath();

      previousDirectory = file.getParentFile();

      if(check.endsWith(".deck"))
        return file;
      else
        return new File(check +".deck");
    }

    return null;
  }//getExportFilename()

  private void importDeck(boolean isHumanDeck)
  {
    File file = getImportFilename();

    if(file == null)
      return;

    Object check = null;

    try{
      ObjectInputStream in = new ObjectInputStream(
                             new FileInputStream(file));
      check = in.readObject();

      in.close();
    }
    catch(Exception ex)
    {
      JOptionPane.showMessageDialog(null, "Sorry there has been an error - " +ex.getMessage());

      throw new RuntimeException("Gui_Quest_DeckEditor_Menu : importDeck() error, " +ex);
    }

    Deck deck = (Deck) check;
    deckDisplay.setTitle(deckEditorName +" - " +deck.getName());

    CardList cardpool;

    if(isHumanDeck)
    {
      questData.addDeck(deck);

      //convert ArrayList of card names (Strings), into Card objects
      cardpool = new CardList();
      ArrayList list = questData.getCardpool();

      for(int i = 0; i < list.size(); i++)
        cardpool.add(AllZone.CardFactory.getCard(list.get(i).toString(), ""));
    }
    else
    {
      questData.ai_addDeck(deck);
      cardpool = AllZone.CardFactory.getAllCards();
    }

    //convert Deck main to CardList
    CardList deckList = new CardList();
    for(int i = 0; i < deck.countMain(); i++)
      deckList.add(AllZone.CardFactory.getCard(deck.getMain(i), ""));

    //update gui
    deckDisplay.updateDisplay(cardpool, deckList);

  }//importDeck()



  private File getImportFilename()
  {
    JFileChooser chooser = new JFileChooser(previousDirectory);

    chooser.addChoosableFileFilter(getFileFilter());
    int returnVal = chooser.showOpenDialog(null);

    if(returnVal == JFileChooser.APPROVE_OPTION)
    {
      File file = chooser.getSelectedFile();
      previousDirectory = file.getParentFile();
      return file;
    }

    return null;
  }//openFileDialog()

  //edit the AI decks
  private void setupComputerMenu()
  {
    JMenuItem open   = new JMenuItem("Open");
    JMenuItem new2   = new JMenuItem("New");
    JMenuItem rename = new JMenuItem("Rename");
    JMenuItem save   = new JMenuItem("Save");
    JMenuItem copy   = new JMenuItem("Copy");
    JMenuItem delete = new JMenuItem("Delete");
    JMenuItem exit   = new JMenuItem("Exit");


    JMenuItem viewAllDecks = new JMenuItem("View All Decks");




    //AI
    viewAllDecks.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent a)
      {
        ArrayList nameList = questData.ai_getDeckNames();
        Collections.sort(nameList);

        Deck deck;
        String allText = "";

        for(int i = 0; i < nameList.size(); i++)
        {
          deck = questData.ai_getDeck(nameList.get(i).toString());
          allText += deck.getName() +"\r\n";
          allText += getExportDeckText(deck);
          allText += "++++++++++++++++++++++++++++++++++++++++++++++++++++++ \r\n \r\n";
        }

        JTextArea area = new JTextArea(allText, 30, 30);
        JOptionPane.showMessageDialog(null, new JScrollPane(area));

      }//actionPerformed()
    });//viewAllDecks

    //AI
    open.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent a)
      {
        String deckName = getUserInput_OpenDeck(questData.ai_getDeckNames());

        //check if user selected "cancel"
        if(deckName.equals(""))
          return;

        setComputerPlayer(deckName);

        Deck d = questData.ai_getDeck(deckName);
        CardList deck = new CardList();

        for(int i = 0; i < d.countMain(); i++)
          deck.add(AllZone.CardFactory.getCard(d.getMain(i), ""));

        CardList cardpool = AllZone.CardFactory.getAllCards();

        deckDisplay.updateDisplay(cardpool, deck);

      }
    });//open

    //AI
    new2.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent a)
      {
        deckDisplay.updateDisplay(AllZone.CardFactory.getAllCards(), new CardList());

        setComputerPlayer("");
      }
    });//new


    //AI
    rename.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent a)
      {
        String name = getUserInput_GetDeckName(questData.ai_getDeckNames());

        //check if user cancels
        if(name.equals(""))
          return;

        //is the current deck already saved and in QuestData?
        if(questData.ai_getDeckNames().contains(currentDeck.getName()))
          questData.ai_removeDeck(currentDeck.getName());//remove old deck

        currentDeck.setName(name);

        Deck deck = convertCardListToDeck(deckDisplay.getBottom());
        deck.setName(name);
        questData.ai_addDeck(deck);


        setComputerPlayer(name);
      }
    });//rename



    //AI
    save.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent a)
      {
        String name = currentDeck.getName();

        //check to see if name is set
        if(name.equals(""))
        {
          name = getUserInput_GetDeckName(questData.ai_getDeckNames());

          //check if user cancels
          if(name.equals(""))
            return;
        }

        setComputerPlayer(name);

        Deck deck = convertCardListToDeck(deckDisplay.getBottom());
        deck.setName(name);

        questData.ai_addDeck(deck);
      }
    });//save



    //AI
    copy.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent a)
      {
        String name = getUserInput_GetDeckName(questData.ai_getDeckNames());

        //check if user cancels
        if(name.equals(""))
          return;

        setComputerPlayer(name);

        Deck deck = convertCardListToDeck(deckDisplay.getBottom());
        deck.setName(name);

        questData.ai_addDeck(deck);
      }
    });//copy


    //AI
    delete.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent a)
      {
        if(currentDeck == null || currentDeck.getName().equals(""))
          return;

        int check = JOptionPane.showConfirmDialog(null, "Do you really want to delete this deck?", "Delete", JOptionPane.YES_NO_OPTION);
        if(check == JOptionPane.NO_OPTION)
          return;//stop here

        questData.ai_removeDeck(currentDeck.getName());

        //show card pool
        CardList cardpool = AllZone.CardFactory.getAllCards();
        deckDisplay.updateDisplay(cardpool, new CardList());

        setComputerPlayer("");
      }
    });//delete



    //AI
    exit.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent a)
      {
        Gui_Quest_DeckEditor_Menu.this.close();
      }
    });

    JMenu deckMenu = new JMenu("AI Deck");

    deckMenu.add(open);
    deckMenu.add(rename);
    deckMenu.add(new2);
    deckMenu.add(save);
    deckMenu.add(copy);

    deckMenu.addSeparator();
    addImportExport(deckMenu, false);

    deckMenu.add(viewAllDecks);

    deckMenu.addSeparator();
    deckMenu.add(delete);
    deckMenu.addSeparator();
    deckMenu.add(exit);

    this.add(deckMenu);

  }//setupComputerMenu()


  private void openHumanDeck(String deckName)
  {
    setHumanPlayer(deckName);

    CardList cardpool = covertToCardList(questData.getCardpool());

    //covert Deck main to CardList
    Deck d = questData.getDeck(deckName);
    CardList deck = new CardList();

    for(int i = 0; i < d.countMain(); i++)
    {
      deck.add(AllZone.CardFactory.getCard(d.getMain(i), ""));

      //remove any cards that are in the deck from the card pool
     //have to add the "if" since maybe "extra" cards might be added
     if(cardpool.containsName(d.getMain(i)))
      cardpool.remove(d.getMain(i));
    }

    deckDisplay.updateDisplay(cardpool, deck);
  }//openHumanDeck

  //the usual menu options that will be used
  private void setupMenu()
  {
    JMenuItem open   = new JMenuItem("Open");
    JMenuItem new2   = new JMenuItem("New");
    JMenuItem rename = new JMenuItem("Rename");
    JMenuItem save   = new JMenuItem("Save");
    JMenuItem copy   = new JMenuItem("Copy");
    JMenuItem delete = new JMenuItem("Delete");
    JMenuItem exit   = new JMenuItem("Exit");

    ////////////////////////////////////////////
    //below is new code

    //adds a card to human player's cardpool
    JMenuItem addCard = new JMenuItem("Cheat - Add Card");

    //add card
    addCard.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent a)
      {
        //sort cards by card name
        CardList cardList = AllZone.CardFactory.getAllCards();
        TableSorter sorter = new TableSorter(cardList, 1, true);
        cardList.sort(sorter);

        //create a new Card object with a different toString() method
        //so that that JList only shows the card's name
        //
        //this is alot of work just to make it a little
        //easier and prettier for the user, gui stuff is very complicated
        class BetterCard extends Card
        {
          private Card card;

          BetterCard(Card c)
          {
            card = c;

            //this line is very important
            //if you omit this, errors will occur
            this.setName(c.getName());
          }

          public String toString()
          {
            return card.getName();
          }
        }//BetterCard

        Card[] card = cardList.toArray();

        for(int i = 0; i < card.length; i++)
        {
          card[i] = new BetterCard(card[i]);
        }

        final JList list = new JList(card);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        //update the "card detail" on the right with the card info
        list.addListSelectionListener(new ListSelectionListener()
        {
          public void valueChanged(ListSelectionEvent e)
          {
            CardDetail cd = (CardDetail)deckDisplay;
            cd.updateCardDetail((Card)list.getSelectedValue());
          }
        });

        Object[] o = {"Add Card to Your Cardpool", new JScrollPane(list)};
        JOptionPane pane = new JOptionPane(o, JOptionPane.INFORMATION_MESSAGE, JOptionPane.OK_CANCEL_OPTION);

        JDialog dialog = pane.createDialog(null, "Cheat - Add Card");
        dialog.setModal(true);
        dialog.setVisible(true);

        Object choice = pane.getValue();
        boolean cancel = false;

        //there are a ton of ways to cancel
        if(
           choice == null ||
           choice.equals(JOptionPane.UNINITIALIZED_VALUE)
           )
          cancel = true;
        else
        {
          int n = ((Integer)choice).intValue();
          if(n == JOptionPane.CANCEL_OPTION)
            cancel = true;
        }

        if(cancel || list.getSelectedValue() == null)
        {
          //System.out.println("cancelled");
        }
        else
        {
          //show the choice that the user selected
          //System.out.println(list.getSelectedValue());

          Card c = (Card) list.getSelectedValue();

          Gui_Quest_DeckEditor g = (Gui_Quest_DeckEditor)deckDisplay;
          TableModel table = g.getTopTableModel();
          table.addCard(c);
          table.resort();
        }
      }//actionPerformed()
    });//add card


    //above is new code
    ///////////////////////////////////////


    //human
    open.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent a)
      {
        String deckName = getUserInput_OpenDeck(questData.getDeckNames());

        //check if user selected "cancel"
        if(deckName.equals(""))
          return;

        openHumanDeck(deckName);
      }
    });//open

    //human
    new2.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent a)
      {
        CardList cardpool = covertToCardList(questData.getCardpool());
        deckDisplay.updateDisplay(cardpool, new CardList());

        setHumanPlayer("");
      }
    });//new


    //human
    rename.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent a)
      {
        String name = getUserInput_GetDeckName(questData.getDeckNames());

        //check if user cancels
        if(name.equals(""))
          return;

        //is the current deck already saved and in QuestData?
        if(questData.getDeckNames().contains(currentDeck.getName()))
          questData.removeDeck(currentDeck.getName());//remove old deck

        currentDeck.setName(name);

        Deck deck = convertCardListToDeck(deckDisplay.getBottom());
        deck.setName(name);
        questData.addDeck(deck);

        setHumanPlayer(name);
      }
    });//rename



    //human
    save.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent a)
      {
        String name = currentDeck.getName();

        //check to see if name is set
        if(name.equals(""))
        {
          name = getUserInput_GetDeckName(questData.getDeckNames());

          //check if user cancels
          if(name.equals(""))
            return;
        }

        setHumanPlayer(name);

        Deck deck = convertCardListToDeck(deckDisplay.getBottom());
        deck.setName(name);

        questData.addDeck(deck);
      }
    });//save



    //human
    copy.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent a)
      {
        String name = getUserInput_GetDeckName(questData.getDeckNames());

        //check if user cancels
        if(name.equals(""))
          return;

        setHumanPlayer(name);

        Deck deck = convertCardListToDeck(deckDisplay.getBottom());
        deck.setName(name);

        questData.addDeck(deck);
      }
    });//copy


    //human
    delete.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent a)
      {
        if(currentDeck.getName().equals(""))
          return;

        int check = JOptionPane.showConfirmDialog(null, "Do you really want to delete this deck?", "Delete", JOptionPane.YES_NO_OPTION);
        if(check == JOptionPane.NO_OPTION)
          return;//stop here

        questData.removeDeck(currentDeck.getName());

        //show card pool
        CardList cardpool = covertToCardList(questData.getCardpool());
        deckDisplay.updateDisplay(cardpool, new CardList());

        setHumanPlayer("");
      }
    });//delete



    //human
    exit.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent a)
      {
        Gui_Quest_DeckEditor_Menu.this.close();
      }
    });

    JMenu deckMenu = new JMenu("Deck");
    deckMenu.add(open);
    deckMenu.add(new2);
    deckMenu.add(rename);
    deckMenu.add(save);
    deckMenu.add(copy);

    deckMenu.addSeparator();//new code
    deckMenu.add(addCard); //new code

    deckMenu.addSeparator();
    addImportExport(deckMenu, true);

    deckMenu.addSeparator();
    deckMenu.add(delete);
    deckMenu.addSeparator();
    deckMenu.add(exit);

    this.add(deckMenu);

  }//setupMenu()

  private Deck convertCardListToDeck(CardList list)
  {
    //put CardList into Deck main
    Deck deck = new Deck(Constant.GameType.Sealed);

    for(int i = 0; i < list.size(); i++)
      deck.addMain(list.get(i).getName());

    return deck;
  }


  //needs to be public because Gui_Quest_DeckEditor.show(Command) uses it
  public void setHumanPlayer(String deckName)
  {
    //the gui uses this, Gui_Quest_DeckEditor
    currentDeck = new Deck(Constant.GameType.Sealed);
    currentDeck.setName(deckName);

    deckDisplay.setTitle(deckEditorName +" - " +deckName);
  }

  private void setComputerPlayer(String deckName)
  {
    //the gui uses this, Gui_Quest_DeckEditor
    currentDeck = new Deck(Constant.GameType.Constructed);
    currentDeck.setName(deckName);

    deckDisplay.setTitle(deckEditorName +" - " +deckName);
  }

  //only accepts numbers, letters or dashes up to 20 characters in length
  private String cleanString(String in)
  {
    String out = "";
    char[] c = in.toCharArray();

    for(int i = 0; i < c.length && i < 20; i++)
      if(Character.isLetterOrDigit(c[i]) || c[i] == '-' || c[i] == '_' || c[i] == ' ')
        out += c[i];

    return out;
  }

  //if user cancels, returns ""
  private String getUserInput_GetDeckName(ArrayList nameList)
  {
    Object o = JOptionPane.showInputDialog(null, "", "Deck Name", JOptionPane.OK_CANCEL_OPTION);

    if(o == null)
      return "";

    String deckName = cleanString(o.toString());

    if(nameList.contains(deckName) || deckName.equals(""))
    {
      JOptionPane.showMessageDialog(null, "Please pick another deck name, a deck currently has that name.");
      return getUserInput_GetDeckName(nameList);
    }

    return deckName;
  }//getUserInput_GetDeckName()


  //if user cancels, it will return ""
  private String getUserInput_OpenDeck(ArrayList deckNameList)
  {
    ArrayList choices = deckNameList;
    if(choices.size() == 0)
    {
      JOptionPane.showMessageDialog(null, "No decks found", "Open Deck", JOptionPane.PLAIN_MESSAGE);
      return "";
    }

    Object o = JOptionPane.showInputDialog(null, "Deck Name", "Open Deck", JOptionPane.OK_CANCEL_OPTION, null, choices.toArray(), choices.toArray()[0]);

    if(o == null)
      return "";

    return o.toString();
  }//getUserInput_OpenDeck()

  //used by Gui_Quest_DeckEditor
  public void close()
  {
    exitCommand.execute();
  }

  //used by Gui_Quest_DeckEditor
  public String getDeckName()
  {
    return currentDeck.getName();
  }

  //used by Gui_Quest_DeckEditor
  public String getGameType()
  {
    return currentDeck.getDeckType();
  }


  //returns CardList of Card objects,
  //argument ArrayList holds String card names
  public static CardList covertToCardList(ArrayList list)
  {
    CardList c = new CardList();
    Card card;
    for(int i = 0; i < list.size(); i++)
    {
      card = AllZone.CardFactory.getCard(list.get(i).toString(), "");
      c.add(card);
    }

    return c;
  }

}
And for completeness here is my old CardDetailUtil.java
Code: Select all
import javax.swing.*;

interface CardDetail
{
  public void updateCardDetail(Card c);
}

public class CardDetailUtil
{
  public static void updateCardDetail(Card card, JTextArea area, JPanel cardDetailPanel, JPanel picturePanel, JLabel label[])
  {
    if(card == null)
        return;

    for(int i = 0; i < label.length; i++)
      label[i].setText("");

    area.setText("");

    if(card.isLand())
        label[0].setText(card.getName());
    else
        label[0].setText(card.getName() +"  - " +card.getManaCost());

    label[1].setText(GuiDisplayUtil.formatCardType(card));

    if(card.isCreature())
    {
        String stats = "" +card.getAttack() +" / "  +card.getDefense();
        label[2].setText(stats);
    }

    if(card.isCreature())
        label[3].setText("Damage: " +card.getDamage() +" Assigned Damage: " +card.getAssignedDamage());

    String uniqueID = card.getUniqueNumber() +" ";
    label[4].setText("Card ID  " +uniqueID);

    area.setText(card.getText());

    cardDetailPanel.setBorder(GuiDisplayUtil.getBorder(card));

    //picture
    picturePanel.removeAll();
    picturePanel.add(GuiDisplayUtil.getPicture(card));
    picturePanel.revalidate();
  }//updateCardDetail
}
mtgrares
DEVELOPER
 
Posts: 1352
Joined: 08 Sep 2008, 22:10
Has thanked: 3 times
Been thanked: 12 times

Re: Add any card to your quest cardpool

Postby mtgrares » 05 Mar 2010, 19:39

And I'm not sure if you cut-and-pasted the whole Gui_Quest_DeckEditor_Menu class but if you did, make sure to add "package forge;" to the top of the file.
mtgrares
DEVELOPER
 
Posts: 1352
Joined: 08 Sep 2008, 22:10
Has thanked: 3 times
Been thanked: 12 times

Re: Add any card to your quest cardpool

Postby Chris H. » 05 Mar 2010, 19:54

Thanks for the help, Rares. On my first attempt, I pasted the two new code sections in the appropriate place. I used your message to guide me to where the code had to go. That worked out OK. The token errors to be deleted was just a minor problem. Sometimes the indentation of the code when pasted in will cause problems for those of us who use Eclipse.

It looks like you are giving me the code for the entire java file and this should make it easier for me to merge it in. There are a number a good people who still like to play quest mode and I think that your addition will please them. :D
User avatar
Chris H.
Forge Moderator
 
Posts: 6320
Joined: 04 Nov 2008, 12:11
Location: Mac OS X Yosemite
Has thanked: 644 times
Been thanked: 643 times

Re: Add any card to your quest cardpool

Postby Chris H. » 05 Mar 2010, 22:04

OK, It looks like there has been a rewrite of some of the code base which in turn is making this a challenge for me. I can take care of the token indentation problem and I can add all of the missing imports except for one.

I can not import the CardDetailUtil.java as that has been replaced. I have commented out the two lines which refer to CardDetail. At this point the project will build with no errors and with only 1 warning. The warning refers to:

Type safety: The expression of type TableSorter needs unchecked conversion to conform to Comparator<Card>

Forge will now run with only this warning and the "cheat add card" is working. When you select a card to add from the list, no info is displayed in the card detail and picture panels.

I think that I should add to this to the SVN. I have this working well enough for the moment. :D
User avatar
Chris H.
Forge Moderator
 
Posts: 6320
Joined: 04 Nov 2008, 12:11
Location: Mac OS X Yosemite
Has thanked: 644 times
Been thanked: 643 times

Re: Add any card to your quest cardpool

Postby DennisBergkamp » 05 Mar 2010, 23:50

Yeah sure, the warning is not a big deal.
User avatar
DennisBergkamp
AI Programmer
 
Posts: 2602
Joined: 09 Sep 2008, 15:46
Has thanked: 0 time
Been thanked: 0 time

Re: Add any card to your quest cardpool

Postby mtgrares » 08 Mar 2010, 22:17

OK, It looks like there has been a rewrite of some of the code base which in turn is making this a challenge for me.
That is another oops on my fault. I was just working from the ancient code on my home machine. I should have checked my work against the new code. Thanks for fixing my mistakes. (As a programmer I hate making mistakes, but of course I made a ton of them. Thankfully not all of my mistakes are so visible. :lol:)
mtgrares
DEVELOPER
 
Posts: 1352
Joined: 08 Sep 2008, 22:10
Has thanked: 3 times
Been thanked: 12 times

Re: Add any card to your quest cardpool

Postby Chris H. » 08 Mar 2010, 22:35

mtgrares wrote:I was just working from the ancient code on my home machine. I should have checked my work against the new code.
`
The HQ pics caused java heap errors on some people's computers. There were some extensive code re-writes in the last several weeks to fix this situation ... it is not easy to keep up with the this quick pace of advancements. :wink:
User avatar
Chris H.
Forge Moderator
 
Posts: 6320
Joined: 04 Nov 2008, 12:11
Location: Mac OS X Yosemite
Has thanked: 644 times
Been thanked: 643 times


Return to Developer's Corner

Who is online

Users browsing this forum: No registered users and 19 guests


Who is online

In total there are 19 users online :: 0 registered, 0 hidden and 19 guests (based on users active over the past 10 minutes)
Most users ever online was 4143 on 23 Jan 2024, 08:21

Users browsing this forum: No registered users and 19 guests

Login Form