001    /**
002     * 
003     */
004    package jagafa.view.newui;
005    
006    import jagafa.flags.TestingFlags;
007    import jagafa.object.Hand;
008    import jagafa.util.view.AbsConstr;
009    import jagafa.util.view.AbsoluteLayout;
010    import jagafa.view.BoardPanel;
011    import jagafa.view.HandPanel;
012    
013    import java.awt.Dimension;
014    import java.awt.event.ActionEvent;
015    import java.awt.event.ActionListener;
016    import java.util.LinkedList;
017    import java.util.List;
018    
019    import javax.swing.JPanel;
020    import javax.swing.event.EventListenerList;
021    
022    /**
023     * JassFrame is a Frame holding the four HandPanel and the BoardPanel Panels 
024     */
025    public class JassRoundPanel extends JPanel implements ActionListener {
026    
027            private static final int PANEL_WIDTH = 700;
028    
029            private static final int PANEL_HEIGHT = 580;
030    
031            private static final int PLAYERS = 4;
032    
033            private static final long serialVersionUID = 4602876072322144533L;
034    
035            private static final int VERT_HANDY = (int)(TestingFlags.CARD_HEIGHT*TestingFlags.CARD_RATIO)+10;//70;
036    
037            private static final int HORIZ_HANDX = 140;
038    
039            private static final int VERT_HANDHEIGHT = (int)(10.0*TestingFlags.CARD_OVERLAP);//492;
040    
041            private static final int VERT_HANDWIDTH = TestingFlags.CARD_HEIGHT; // 100
042    
043            private static final int HORIZ_HANDHEIGHT = VERT_HANDWIDTH; // 100
044    
045            private static final int HORIZ_HANDWIDTH = VERT_HANDHEIGHT;
046    
047            private List<HandPanel> handPanels_;
048    
049            private HandPanel handPanel1_;
050    
051            private HandPanel handPanel2_;
052    
053            private HandPanel handPanel3_;
054    
055            private HandPanel handPanel4_;
056    
057            private BoardPanel boardPanel_;
058    
059            private EventListenerList listeners_ = new EventListenerList();
060    
061            public JassRoundPanel() {
062                    this.handPanels_ = new LinkedList<HandPanel>();
063    
064                    initComponents();
065    
066                    /* TODO: Add automatic hand hiding on computer players */
067                    if (TestingFlags.showAllHands_) {
068    
069                    } else {
070                            hideHand(1);
071                            hideHand(2);
072                            hideHand(3);
073                            hideHand(0);
074                    }
075    
076                    installHandListeners();
077    
078                    this.setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));
079            }
080    
081            public void hideHand(int i) {
082                    this.handPanels_.get(i).setVisible(false);
083            }
084    
085            public void showHand(int i) {
086                    this.handPanels_.get(i).setVisible(true);
087            }
088    
089            /**
090             * This method is called from within the constructor to initialize the form.
091             * WARNING: Do NOT modify this code. The content of this method is always
092             * regenerated by the Form Editor.
093             */
094            private void initComponents() {
095                    handPanel1_ = new HandPanel(HandPanel.HORIZONTAL, TestingFlags.CARD_HEIGHT);
096                    handPanel2_ = new HandPanel(HandPanel.VERTICAL, TestingFlags.CARD_HEIGHT);
097                    handPanel3_ = new HandPanel(HandPanel.HORIZONTAL, TestingFlags.CARD_HEIGHT);
098                    handPanel4_ = new HandPanel(HandPanel.VERTICAL, TestingFlags.CARD_HEIGHT);
099                    boardPanel_ = new BoardPanel();
100    
101                    this.setLayout(new AbsoluteLayout());
102    
103                    createHandPanels();
104    
105            }
106    
107            /**
108             * 
109             */
110            private void createHandPanels() {
111                    this.add(handPanel1_, new AbsConstr(HORIZ_HANDX, 0, HORIZ_HANDWIDTH,
112                                    HORIZ_HANDHEIGHT));
113    
114                    handPanel1_.setName(String.valueOf(0));
115                    handPanels_.add(handPanel1_);
116    
117                    this.add(handPanel2_, new AbsConstr(0, VERT_HANDY, VERT_HANDWIDTH,
118                                    VERT_HANDHEIGHT));
119                    handPanels_.add(handPanel2_);
120                    handPanel2_.setName(String.valueOf(1));
121    
122                    this.add(handPanel3_, new AbsConstr(HORIZ_HANDX, 550, HORIZ_HANDWIDTH,
123                                    HORIZ_HANDHEIGHT));
124                    handPanels_.add(handPanel3_);
125                    handPanel3_.setName(String.valueOf(2));
126    
127                    this.add(handPanel4_, new AbsConstr(690, VERT_HANDY, VERT_HANDWIDTH,
128                                    VERT_HANDHEIGHT));
129                    handPanels_.add(handPanel4_);
130                    handPanel4_.setName(String.valueOf(3));
131    
132                    boardPanel_.setLayout(new java.awt.GridLayout(2, 2));
133    
134                    int boardPanelHeight = 300;
135                    this.add(boardPanel_, new AbsConstr(300, 220,
136                                    (int) (TestingFlags.CARD_RATIO * boardPanelHeight), boardPanelHeight));
137            }
138    
139            // Install the listeners for the hands
140            private void installHandListeners() {
141    
142                    for (int i = 0; i < PLAYERS; i++) {
143                            HandPanel hPanel = this.getHandPanel(i);
144                            hPanel.addHandListener(this);
145                    }
146            }
147    
148            /*
149             * Is called by the handPanel's cardPressed() method
150             */
151            public void actionPerformed(ActionEvent e) {
152                    String button = e.getActionCommand();
153                    String hand = ((HandPanel) e.getSource()).getName();
154    
155                    this.cardPressed(hand + button);
156    
157            }
158    
159            // Installs the top card-listeners (Controller)
160            public void addCardListener(ActionListener l) {
161                    this.listeners_.add(ActionListener.class, l);
162            }
163    
164            /** 
165             * Informs the top Listener (Controller) that a cardButton has been pressed
166             * MESSAGE contains the hand- and button numbers of the cardButton
167             */
168            private void cardPressed(String message) {
169                    Object[] listeners = this.listeners_.getListenerList();
170                    for (int i = listeners.length - 2; i >= 0; i -= 2) {
171                            if (listeners[i] == ActionListener.class) {
172                                    ActionEvent fooEvent = new ActionEvent(this, 0, message);
173                                    ((ActionListener) listeners[i + 1]).actionPerformed(fooEvent);
174                            }
175                    }
176            }
177    
178            /**
179             * Returns the HandPanel i
180             * ??? i = 0 --> top, i= 1 --> left, i = 2 --> bottom, i=3 --> right ??? 
181             */
182            private HandPanel getHandPanel(int i) {
183                    return this.handPanels_.get(i);
184            }
185    
186            /**
187             * Returns the BoardPanel object
188             */
189            public BoardPanel getBoardPanel() {
190                    return this.boardPanel_;
191            }
192    
193            public void updateHandPanel(Hand hand, int i) {
194                    this.getHandPanel(i).updateHandUI(hand);
195    
196            }
197    
198    }