summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/view/impl/PlayerPanel.java
blob: 55a06c321e9702e183794deebb124331c857b631 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package jrummikub.view.impl;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

import jrummikub.util.Event;
import jrummikub.util.IEvent;
import jrummikub.view.IPlayerPanel;

@SuppressWarnings("serial")
public class PlayerPanel extends JPanel implements IPlayerPanel {
  private final static DecimalFormat secondFormat = new DecimalFormat("00");
  
  private Board board;

  private JLabel currentPlayerNameLabel;
  private JButton sortByNumberButton;
  private JButton sortByColorButton;
  private JProgressBar timeBar;
  private JButton endTurnButton;
  
  private Event sortByNumberEvent = new Event();
  private Event sortByColorEvent = new Event();
  private Event endTurnEvent = new Event();
  
  
  @Override
  public Board getBoard() {
    return board;
  }

  @Override
  public void setCurrentPlayerName(String playerName) {
    currentPlayerNameLabel.setText(playerName);
  }
  
  @Override
  public
  void setTimeLeft(int time) {
    timeBar.setValue(time);
    timeBar.setString(Integer.toString(time/60) + ":" + secondFormat.format(time%60));
  }
  
  @Override
  public IEvent getSortByNumberEvent() {
    return sortByNumberEvent;
  }
  
  @Override
  public IEvent getSortByColorEvent() {
    return sortByColorEvent;
  }
  
  @Override
  public IEvent getEndTurnEvent() {
    return endTurnEvent;
  }
  
  
  JPanel createLeftPanel() {
    JPanel panel = new JPanel();
    panel.setPreferredSize(new Dimension(0, 0));
    GridBagLayout layout = new GridBagLayout(); 
    GridBagConstraints c = new GridBagConstraints();
    panel.setLayout(layout);
    panel.setOpaque(false);
    
    
    currentPlayerNameLabel = new JLabel();
    currentPlayerNameLabel.setHorizontalAlignment(JLabel.CENTER);
    currentPlayerNameLabel.setHorizontalTextPosition(JLabel.CENTER);
    currentPlayerNameLabel.setVerticalAlignment(JLabel.CENTER);
    currentPlayerNameLabel.setVerticalTextPosition(JLabel.CENTER);
    currentPlayerNameLabel.setPreferredSize(new Dimension(180, 30));
    c.weightx = 0;
    c.weighty = 1;
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.insets = new Insets(25, 0, 0, 0);
    layout.setConstraints(currentPlayerNameLabel, c);
    panel.add(currentPlayerNameLabel);
    
    sortByNumberButton = new JButton("<html><center>Sort by<br>number");
    sortByNumberButton.setPreferredSize(new Dimension(85, 50));
    sortByNumberButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent arg0) {
        sortByNumberEvent.fire();
      }});
    
    c.gridwidth = GridBagConstraints.RELATIVE;
    c.gridheight = GridBagConstraints.REMAINDER;
    c.insets = new Insets(15, 0, 20, 5);
    layout.setConstraints(sortByNumberButton, c);
    panel.add(sortByNumberButton);
    
    sortByColorButton = new JButton("<html><center>Sort by<br>color");
    sortByColorButton.setPreferredSize(new Dimension(85, 50));
    sortByColorButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent arg0) {
        sortByColorEvent.fire();
      }});
    
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.insets = new Insets(15, 5, 20, 0);
    layout.setConstraints(sortByColorButton, c);
    panel.add(sortByColorButton);
    
    return panel;
  }
  
  JPanel createRightPanel() {
    JPanel panel = new JPanel();
    panel.setPreferredSize(new Dimension(0, 0));
    GridBagLayout layout = new GridBagLayout(); 
    GridBagConstraints c = new GridBagConstraints();
    panel.setLayout(layout);
    panel.setOpaque(false);
    
    timeBar = new JProgressBar(0, 60);
    timeBar.setStringPainted(true);
    timeBar.setPreferredSize(new Dimension(180, 30));
    c.weightx = 0;
    c.weighty = 1;
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.insets = new Insets(25, 0, 0, 0);
    layout.setConstraints(timeBar, c);
    panel.add(timeBar);
    
    endTurnButton = new JButton("End turn");
    endTurnButton.setPreferredSize(new Dimension(180, 50));
    endTurnButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent arg0) {
        endTurnEvent.fire();
      }});
    
    c.gridheight = GridBagConstraints.REMAINDER;
    c.insets = new Insets(15, 0, 20, 0);
    layout.setConstraints(endTurnButton, c);
    panel.add(endTurnButton);
    
    return panel;
  }
  
  
  PlayerPanel() {
    GridBagLayout layout = new GridBagLayout(); 
    GridBagConstraints c = new GridBagConstraints();
    setLayout(layout);
    
    setBackground(Color.LIGHT_GRAY);

    JPanel leftPanel = createLeftPanel();
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 1;
    c.weighty = 1;
    c.gridheight = GridBagConstraints.REMAINDER;
    layout.setConstraints(leftPanel, c);
    add(leftPanel);
    
    board = new Board();
    c.weightx = 3;
    layout.setConstraints(board, c);
    add(board);
    
    JPanel rightPanel = createRightPanel();
    c.weightx = 1;
    c.gridwidth = GridBagConstraints.REMAINDER;
    layout.setConstraints(rightPanel, c);
    add(rightPanel);
  }
}