001    /**
002     * 
003     */
004    package jagafa.view;
005    
006    import jagafa.naming.CardNames;
007    import jagafa.object.Board;
008    import jagafa.object.Card;
009    import jagafa.object.Player;
010    
011    import java.awt.Color;
012    import java.awt.GridLayout;
013    import java.util.LinkedList;
014    import java.util.List;
015    
016    import javax.swing.BorderFactory;
017    import javax.swing.JPanel;
018    
019    /**
020     * A Panel with four CardButton objects that represent the current active Board
021     * 
022     */
023    public class BoardPanel extends JPanel {
024    
025            private static final int BOARD_SIZE = 4;
026    
027            private List<CardButton> buttonList_;
028    
029            private static final long serialVersionUID = -7141846003079501463L;
030    
031            /**
032             * Constructor: Set Layout and init CardButtons
033             */
034            public BoardPanel() {
035                    this.setLayout(new GridLayout(2, 2));
036                    this.buttonList_ = new LinkedList<CardButton>();
037                    this.setBorder(BorderFactory.createLineBorder(Color.BLACK));
038                    
039                    for (int i = 0; i < BOARD_SIZE; i++) {
040    
041                            CardButton button = new CardButton(null, CardButton.HORIZONTAL);
042                            this.add(button);
043                            this.buttonList_.add(button);
044                    }
045    
046                    this.setEnabled(false);
047                    this.setFocusable(false);
048    
049            }
050    
051            /**
052             * Reset the CardButtons to the buttons in the Board given
053             */
054            public void updateBoardUI(Board board) {
055    
056                    clearCards();
057    
058                    for (int i = 0; i < BOARD_SIZE; i++) {
059                            Player play = board.getPlayerOfPos(i);
060                            int playerNr = i;
061                            if (play != null) {
062                                    playerNr = Integer.valueOf(play.getName()).intValue();
063                                    Card c = board.get(i);
064    
065                                    this.getButton(getPosFor(playerNr)).setCardName(CardNames.getName(c));
066                            }
067                    }
068    
069            }
070    
071            /**
072             * Sets all Card names (reps. images) to null
073             */
074            private void clearCards() {
075                    for (int i = 0; i < BOARD_SIZE; i++) {
076                            this.getButton(i).setCardName(null);
077                    }
078            }
079    
080            /**
081             * Returns the button corresponding to the player with the number given
082             */
083            private int getPosFor(int playerNr) {
084                    switch (playerNr) {
085                    case 0:
086                            return 0;
087                    case 1:
088                            return 2;
089                    case 2:
090                            return 3;
091                    case 3:
092                            return 1;
093                    }
094                    
095                    
096                    
097                    return 0;
098            }
099    
100            /**
101             * Returns the button i in the button list
102             */
103            private CardButton getButton(int i) {
104                    return this.buttonList_.get(i);
105            }
106    }