001    /**
002     * 
003     */
004    package jagafa.view.newui;
005    
006    import jagafa.JassRound;
007    import jagafa.naming.ESwissColorNames;
008    import jagafa.naming.ESwissValueNames;
009    import jagafa.object.Card;
010    import jagafa.object.CardList;
011    import jagafa.object.Hand;
012    import jagafa.object.Player;
013    import jagafa.stats.PlayerStats;
014    import jagafa.stats.RoundStats;
015    
016    import java.awt.Color;
017    import java.awt.GridLayout;
018    
019    import javax.swing.BorderFactory;
020    import javax.swing.BoxLayout;
021    import javax.swing.JComponent;
022    import javax.swing.JLabel;
023    import javax.swing.JPanel;
024    import javax.swing.JScrollPane;
025    import javax.swing.border.Border;
026    
027    /**
028     * StatView is a Frame displaying some game information including team scores,
029     * Bock lists and gone cards of every color
030     * 
031     */
032    public class RoundStatsPanel extends JPanel {
033    
034            private static final int COLORS = 4;
035    
036            private static final int STATS_YPOS = 410;
037    
038            private static final int STATS_HEIGHT = 325;
039    
040            private static final int STATS_WIDTH = 300;
041    
042            private static final long serialVersionUID = -5864758237368176044L;
043    
044            private JassRound round_;
045    
046            private JPanel gonePanel_;
047    
048            private JPanel scorePanel_;
049    
050            private JPanel colorPanel_;
051    
052            private JPanel bockPanel_;
053    
054            private PlayerStats playerStats_;
055    
056            private RoundStats roundStats_;
057    
058            public RoundStatsPanel(JassRound round) {
059                    this.round_ = round;
060                    this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
061    
062                    this.setSize(STATS_WIDTH, STATS_HEIGHT);
063                    this.setLocation(0, STATS_YPOS);
064                    addTitleBorder("Stats:");
065    
066                    this.playerStats_ = new PlayerStats(this.round_,this.round_.activePlayer());
067                    this.roundStats_ = new RoundStats(this.round_);
068                    
069                    if (this.round_ != null) {
070                            createScorePanel();
071                    //      createGonePanel();
072                            createBockPanel();
073                            createColorPanel();
074                    }
075            }
076    
077            private void addTitleBorder(String title) {
078                    Border rawBorder = BorderFactory.createLineBorder(Color.BLACK);
079    
080                    Border titledB = BorderFactory.createTitledBorder(rawBorder, title);
081                    this.setBorder(titledB);
082    
083            }
084    
085            /* (non-Javadoc)
086             * @see jagafa.view.newui.RoundStats#update()
087             */
088            public void update() {
089                    if (this.round_.getRules() == null) {
090                            return;
091                    }
092    
093                    this.playerStats_.update();
094                    this.roundStats_.update();
095    
096                    //this.updateGonePanel();
097                    this.updateBockPanel();
098                    this.updateScorePanel();
099                    this.updateColorPanel();
100                    this.validate();
101            }
102    
103            /**
104             * 
105             */
106            private void createColorPanel() {
107                    colorPanel_ = new JPanel();
108                    colorPanel_.setName("Colors in Hand:");
109                    colorPanel_.setLayout(new GridLayout(2, 2));
110    
111                    String labelText = "";
112                    for (int i = 0; i < COLORS; i++) {
113                            CardList colorC = this.playerStats_.getColorList()[i];
114    
115                            labelText += ESwissColorNames.getName(i) + " : " + colorC.size() + ", ";
116                    }
117    
118                    JLabel label = new JLabel(labelText);
119                    colorPanel_.add(label);
120                    this.add(colorPanel_);
121    
122                    Border border = BorderFactory.createTitledBorder(BorderFactory
123                                    .createLineBorder(Color.BLACK), "Colors in Hand:");
124                    this.colorPanel_.setBorder(border);
125    
126            }
127    
128            private void updateColorPanel() {
129                    this.colorPanel_.removeAll();
130    
131                    String labelText = "";
132                    for (int i = 0; i < COLORS; i++) {
133                            CardList colorC = this.playerStats_.getColorList()[i];
134    
135                            labelText += ESwissColorNames.getName(i) + " : " + colorC.size() + ", ";
136                    }
137    
138                    JLabel label = new JLabel(labelText);
139                    colorPanel_.add(label);
140    
141            }
142    
143            /**
144             * 
145             */
146            private void createScorePanel() {
147                    scorePanel_ = new JPanel();
148                    scorePanel_.setName("Scores:");
149                    Border border = BorderFactory.createTitledBorder(BorderFactory
150                                    .createLineBorder(Color.BLACK), "Scores:");
151                    this.scorePanel_.setBorder(border);
152    
153                    scorePanel_.setLayout(new GridLayout(1, 2));
154                    int sum1 = this.roundStats_.getScoreTeam1();
155                    int sum2 = this.roundStats_.getScoreTeam2();
156                    JLabel team1 = new JLabel("Player 0 and 2: " + sum1);
157                    JLabel team2 = new JLabel("Player 1 and 3: " + sum2);
158                    scorePanel_.add(team1);
159                    scorePanel_.add(team2);
160                    this.add(scorePanel_);
161    
162            }
163    
164            /**
165             * 
166             */
167            private void createGonePanel() {
168    
169                    gonePanel_ = new JPanel();
170                    gonePanel_.setName("Gone Cards:");
171                    Border border = BorderFactory.createTitledBorder(BorderFactory
172                                    .createLineBorder(Color.BLACK), "Gone Cards:");
173                    this.gonePanel_.setBorder(border);
174                    gonePanel_.setLayout(new GridLayout(1, COLORS));
175    
176                    this.add(new JScrollPane(gonePanel_));
177            }
178    
179            private void updateGonePanel() {
180                    this.gonePanel_.removeAll();
181                    for (int i = 0; i < COLORS; i++) {
182    
183                            CardList goneofcol = this.roundStats_.getGoneList()[i];
184    
185                            Hand h = new Hand(goneofcol);
186                            h.sort(this.round_.getRules());
187                            goneofcol = h;
188    
189                            String labelText = ESwissColorNames.getName(i) + " : " + goneofcol.size()
190                                            + "  ";
191                            for (int j = 0; j < goneofcol.size(); j++) {
192                                    Card c = goneofcol.get(j);
193    
194                                    labelText += " " + ESwissValueNames.getName(c.getValue());
195                            }
196    
197                            JLabel label = new JLabel(labelText);
198                            gonePanel_.add(label);
199    
200                    }
201    
202                    this.validate();
203            }
204    
205            /**
206             * 
207             */
208            private void updateBockPanel() {
209                    this.bockPanel_.removeAll();
210    
211                    if (round_.getRules().specialColor() != -1) {
212                            String trumpfe = "Anzahl Trümpfe: ";
213                            CardList trumpList = this.playerStats_.getTrumpfList();
214    
215                            int trumpfNo = trumpList.size();
216    
217                            trumpfe += trumpfNo;
218                            JLabel trumpfLabel = new JLabel(trumpfe);
219                            this.bockPanel_.add(trumpfLabel);
220                    }
221    
222                    for (int i = 0; i < 9; i++) {
223                            String labelText = new String();
224                            labelText += i + ". Bock: ";
225    
226                            JLabel bockLabel = new JLabel();
227                            CardList bockNth = this.playerStats_.getBockList()[i];
228                            for (int j = 0; j < bockNth.size(); j++) {
229                                    labelText += bockNth.get(j).toString() + ", ";
230    
231                            }
232                            bockLabel.setText(labelText);
233                            bockPanel_.add(bockLabel);
234                    }
235    
236                    bockPanel_.validate();
237                    this.validate();
238            }
239    
240            private void createBockPanel() {
241                    bockPanel_ = new JPanel();
242                    bockPanel_.setName("Bock Cards:");
243    
244                    bockPanel_.setLayout(new GridLayout(9, 1));
245                    this.add(new JScrollPane(bockPanel_));
246                    Border border = BorderFactory.createTitledBorder(BorderFactory
247                                    .createLineBorder(Color.BLACK), "Bock Cards:");
248                    this.bockPanel_.setBorder(border);
249            }
250    
251            private void updateScorePanel() {
252                    scorePanel_.removeAll();
253                    scorePanel_.setLayout(new GridLayout(2, 1));
254                    int sum1 = this.roundStats_.getScoreTeam1();
255                    int sum2 = this.roundStats_.getScoreTeam2();
256                    JLabel team1 = new JLabel("Player 0 and 2: " + sum1);
257                    JLabel team2 = new JLabel("Player 1 and 3: " + sum2);
258                    scorePanel_.add(team1);
259                    scorePanel_.add(team2);
260            }
261    
262            /* (non-Javadoc)
263             * @see jagafa.view.newui.RoundStats#setRound(jagafa.JassRound)
264             */
265            public void setRound(JassRound round) {
266                    this.round_ = round;
267    
268            }
269    
270            public JComponent getView() {
271                    return this;
272            }
273    
274            public void update(Player player) {
275                    this.playerStats_.setPlayer(player);
276                    update();
277                    
278            }
279    }