001    /**
002     * 
003     */
004    package jagafa.view;
005    
006    import jagafa.flags.TestingFlags;
007    import jagafa.naming.CardNames;
008    import jagafa.object.Card;
009    import jagafa.object.Hand;
010    
011    import java.awt.Color;
012    import java.awt.Dimension;
013    import java.awt.event.ActionEvent;
014    import java.awt.event.ActionListener;
015    import java.util.LinkedList;
016    import java.util.List;
017    
018    import javax.swing.BorderFactory;
019    import javax.swing.JLayeredPane;
020    import javax.swing.event.EventListenerList;
021    
022    /**
023     * HandPanel is a Panel holding the CardButtons of a Players Hand
024     * 
025     */
026    public class HandPanel extends JLayeredPane implements ActionListener{
027    
028            private static final int COLORS = 4;
029    
030            private static final int HAND_SIZE = 9;
031    
032            private List<CardButton> cardButtons_;
033    
034            private static final long serialVersionUID = 5051908824891500716L;
035    
036            public static final int HORIZONTAL = 0;
037    
038            public static final int VERTICAL = 1;
039    
040            /**
041             * Constructor: Create a new HandPanel with the orientation given
042             * orientation = HORIZONTAL or VERTICAL
043             */
044            public HandPanel(int orientation, int cardHeight) {
045                    super();
046                    this.listeners_ = new EventListenerList();
047                    this.setBorder(BorderFactory.createLineBorder(Color.BLACK));
048                    if (orientation == HORIZONTAL) {
049                            //this.setLayout(new GridLayout(1, HAND_SIZE+3));
050                            //this.setLayout(new OverlayLayout(this));
051                    } else {
052                            //this.setLayout(new GridLayout(HAND_SIZE, 1));
053                            //this.setLayout(new OverlayLayout(this));
054                    }
055    
056                    this.cardButtons_ = new LinkedList<CardButton>();
057                    this.setPreferredSize(new Dimension(600,200));
058                    int height = TestingFlags.CARD_HEIGHT;
059                    double ratio = TestingFlags.CARD_RATIO;
060                    for (int i = 0; i < 9; i++) {
061                            CardButton button;
062                    
063                            if (orientation == HORIZONTAL) {
064                                    Card c = new Card((int) (Math.random() * COLORS), (int) (Math
065                                                    .random() * HAND_SIZE));
066                                    button = new CardButton(CardNames.getName(c), HORIZONTAL);
067                                    
068                                    
069                                    button.setBounds(i*50,0,(int)(height*ratio),height);
070                                    
071                            } else {
072                                    Card c = new Card((int) (Math.random() * COLORS), (int) (Math
073                                                    .random() * HAND_SIZE));
074    
075                                    button = new CardButton(CardNames.getName(c), VERTICAL);
076                                    button.setBounds(0,i*50,height,(int)(height*ratio));
077                                    
078                            }
079                            
080                            button.setName(String.valueOf(i));
081                            this.cardButtons_.add(button);
082                            this.add(button,new Integer(i),1);
083    
084                    }
085                    this.validate();
086            }
087    
088            /**
089             * Update the panel with the cards in the hand given
090             */
091            public void updateHandUI(Hand h) {
092                    for (int i = 0; i < h.size(); i++) {
093                            this.getButton(i).setCardName(h.get(i).toString());
094                            this.getButton(i).validate();
095                    }
096                    for (int j = 0;j<HAND_SIZE-h.size();j++){
097                            this.getButton(h.size()+j).setCardName(null);
098                    }
099                    this.validate();
100                    
101            }
102    
103            /**
104             * Returns CardButton i in the cardButtonList
105             */
106            private CardButton getButton(int i) {
107                    return this.cardButtons_.get(i);
108            }
109    
110            private EventListenerList listeners_;
111            
112            
113            /** 
114             * Add Listeners to all Cards in the Hand 
115             */
116            public void addHandListener(ActionListener l) {
117                    listeners_.add(ActionListener.class,l);
118                    for (int buttonNr = 0; buttonNr < HAND_SIZE; buttonNr++) {
119                            this.getButton(buttonNr).addActionListener(this);
120                    }
121            }
122    
123            
124            /** 
125             * Informs the Container (eg. JassFrame) of a buttonPressed event
126             */
127            private void cardPressed(CardButton source){
128                    Object[] listeners = this.listeners_.getListenerList();
129                    for (int i = listeners.length - 2; i >= 0; i -= 2) {
130                            if (listeners[i] == ActionListener.class) {
131                                    ActionEvent fooEvent = new ActionEvent(this, 0, source.getName());
132                                    ((ActionListener) listeners[i + 1]).actionPerformed(fooEvent);
133                            }
134                    }
135                    this.validate();
136            }
137    
138            /**
139             *  Called when a cardButton has been pressed
140             */
141            public void actionPerformed(ActionEvent e) {
142                    CardButton pressedCard = (CardButton) e.getSource();
143                    
144                    cardPressed(pressedCard);
145            }
146    
147    
148    }