package clueless.view;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.Iterator;
import java.util.LinkedList;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import clueless.controller.GameController;
import clueless.events.DisproveCardEvent;
import clueless.main.Main;
import clueless.model.Location;
import clueless.model.Room;
import clueless.model.Theory;
import clueless.model.Weapon;
import clueless.model.decks.Card;
import clueless.model.decks.CharacterCard;
import clueless.model.decks.LocationCard;
import clueless.model.decks.WeaponCard;
/**
* View for displaying cards to the user that disprove a suggestion.
* The user will need to pick which card to show the player who made the suggestion.
* @author T
*/
public class PickADisprovedCardView extends AbstractView {
/**The main panel for the card picker view.*/
private JPanel cardPanel = null;
/** The frame to hold the content */
private JFrame frame = null;
/** A handle to the controller. */
GameController controller;
protected LinkedList<Card> disprovedCards;
public PickADisprovedCardView(GameController controller, LinkedList<Card> disprovedCards, Theory theory){
if( controller == null){
throw new NullPointerException();
}
this.controller = controller;
controller.addView(this);
this.disprovedCards = disprovedCards;
this.frame = new JFrame("Disprove Suggestion");
cardPanel = new JPanel();
GridLayout gridLayout = new GridLayout(1, disprovedCards.size());
cardPanel.setLayout(gridLayout);
frame.setSize((disprovedCards.size()+1)*96, 200);
frame.setLocation((screen.width - frame.getWidth())/2, (screen.height - frame.getHeight())/2);
Iterator<Card> iter = disprovedCards.iterator();
//Loop through options to display cards
while(iter.hasNext())
{
/** Testing for adding card images */
BufferedImage buttonIcon = null;
URL resource = null;
/** Load respective images based on card type */
Card card = iter.next();
if(card instanceof WeaponCard)
{
WeaponCard weaponCard = ((WeaponCard)card);
switch(weaponCard.getWeaponType())
{
case CANDLESTICK:
resource = Main.class.getResource("/clueless/main/resources/weapons/Candlestick_card.gif");
break;
case KNIFE:
resource = Main.class.getResource("/clueless/main/resources/weapons/Knife_card.gif");
break;
case LEAD_PIPE:
resource = Main.class.getResource("/clueless/main/resources/weapons/Pipe_card.gif");
break;
case REVOLVER:
resource = Main.class.getResource("/clueless/main/resources/weapons/Revolver_card.gif");
break;
case ROPE:
resource = Main.class.getResource("/clueless/main/resources/weapons/Rope_card.gif");
break;
case WRENCH:
resource = Main.class.getResource("/clueless/main/resources/weapons/Wrench_card.gif");
break;
}
/** get URL to image */
try {
buttonIcon = ImageIO.read(resource);
} catch (IOException e) {
// TODO Auto-generated catch block
System.err.print(e.toString());
}
//Scale it to the UI size
ImageIcon icon = new ImageIcon(buttonIcon.getScaledInstance(96, 150, 0));
//Instantiate image button
WeaponButton button = new WeaponButton(icon, weaponCard.getWeaponType());
button.setBorder(new LineBorder(Color.BLACK, 5));
button.setBorderPainted(true);
//add actionEvent to Button
button.addActionListener(new SelectCard());
cardPanel.add(button);
}
else if (card instanceof CharacterCard)
{
CharacterCard characterCard = ((CharacterCard)card);
switch(characterCard.getCharacterName())
{
case COL_MUSTARD:
resource = Main.class.getResource("/clueless/main/resources/characters/ColMustard.gif");
break;
case MISS_SCARLET:
resource = Main.class.getResource("/clueless/main/resources/characters/MissScarlett.gif");
break;
case MR_GREEN:
resource = Main.class.getResource("/clueless/main/resources/characters/MrGreen.gif");
break;
case MRS_PEACOCK:
resource = Main.class.getResource("/clueless/main/resources/characters/MissPeacock.gif");
break;
case MRS_WHITE:
resource = Main.class.getResource("/clueless/main/resources/characters/MrsWhite.gif");
break;
case PROF_PLUM:
resource = Main.class.getResource("/clueless/main/resources/characters/ProfPlum.gif");
break;
}
/** get URL to image */
try {
buttonIcon = ImageIO.read(resource);
} catch (IOException e) {
// TODO Auto-generated catch block
System.err.print(e.toString());
}
//Scale it to the UI size
ImageIcon icon = new ImageIcon(buttonIcon.getScaledInstance(96, 150, 0));
//Instantiate image button
CharacterButton button = new CharacterButton(icon, characterCard.getCharacterName());
button.setBorder(new LineBorder(Color.BLACK, 5));
button.setBorderPainted(true);
//add actionEvent to Button
button.addActionListener(new SelectCard());
cardPanel.add(button);
}
else if (card instanceof LocationCard)
{
LocationCard locationCard = ((LocationCard)card);
switch(locationCard.getLocationName())
{
case BALLROOM:
resource = Main.class.getResource("/clueless/main/resources/rooms/Ballroom.gif");
break;
case BILLIARD_ROOM:
resource = Main.class.getResource("/clueless/main/resources/rooms/BilliardRoom.gif");
break;
case CONSERVATORY:
resource = Main.class.getResource("/clueless/main/resources/rooms/Conservatory.gif");
break;
case DINING_ROOM:
resource = Main.class.getResource("/clueless/main/resources/rooms/DiningRoom.gif");
break;
case HALL:
resource = Main.class.getResource("/clueless/main/resources/rooms/Hall.gif");
break;
case KITCHEN:
resource = Main.class.getResource("/clueless/main/resources/rooms/Kitchen.gif");
break;
case LIBRARY:
resource = Main.class.getResource("/clueless/main/resources/rooms/Library.gif");
break;
case LOUNGE:
resource = Main.class.getResource("/clueless/main/resources/rooms/Lounge.gif");
break;
case STUDY:
resource = Main.class.getResource("/clueless/main/resources/rooms/Study.gif");
break;
}//location card type swtich
/** get URL to image */
try {
buttonIcon = ImageIO.read(resource);
} catch (IOException e) {
// TODO Auto-generated catch block
System.err.print(e.toString());
}
//Scale it to the UI size
ImageIcon locationIcon = new ImageIcon(buttonIcon.getScaledInstance(96, 150, 0));
//Instantiate image button
RoomButton locationButton = new RoomButton(locationIcon, locationCard.getLocationName());
locationButton.setBorder(new LineBorder(Color.BLACK, 5));
locationButton.setBorderPainted(true);
locationButton.addActionListener(new SelectCard());
cardPanel.add(locationButton);
}//location else if
}
frame.getContentPane().add(cardPanel);
frame.setVisible(true);
}
private class SelectCard implements ActionListener{
@Override
public void actionPerformed(ActionEvent ae) {
//Get panel button that fired event is part of
JButton button = (JButton)ae.getSource();
JPanel parent = (JPanel)button.getParent();
Card selectedCardType = null;
//Store what card is selected
if(button instanceof CharacterButton)
{
CharacterButton character = (CharacterButton)button;
selectedCardType = new CharacterCard(controller, character.getType());
}
else if(button instanceof WeaponButton)
{
WeaponButton weapon = (WeaponButton)button;
selectedCardType = new WeaponCard(controller, weapon.getType());
}
else if(button instanceof RoomButton)
{
RoomButton location = (RoomButton)button;
selectedCardType = new LocationCard(controller, location.getType());//location.getType();
}
System.out.println(selectedCardType.getCardType());
controller.fireDisproveCardEvent(new DisproveCardEvent(this, selectedCardType));
controller.removeModel(selectedCardType);
frame.dispose();
}
}
}