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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
|
package jrummikub.view.impl;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.io.File;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.border.MatteBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import jrummikub.model.Position;
import jrummikub.model.Stone;
import jrummikub.model.StoneColor;
import jrummikub.model.StoneSet;
import jrummikub.util.Event;
import jrummikub.util.Event1;
import jrummikub.util.IEvent;
import jrummikub.util.IEvent1;
import jrummikub.util.IListener;
import jrummikub.util.Pair;
import jrummikub.view.IGameListPanel;
import jrummikub.view.IHandPanel;
import jrummikub.view.ILoginPanel;
import jrummikub.view.IPlayerPanel;
import jrummikub.view.IScorePanel;
import jrummikub.view.ISettingsPanel;
import jrummikub.view.ITablePanel;
import jrummikub.view.IView;
/**
* Implementation of the top-level view interface
*/
@SuppressWarnings("serial")
public class View extends JFrame implements IView {
private final static float PLAYER_PANEL_RATIO = 0.14f;
private final static int PLAYER_PANEL_BORDER_WIDTH = 1;
private final static int PLAYER_PANEL_MAX_HEIGHT = 180 + PLAYER_PANEL_BORDER_WIDTH;
private JLayeredPane layeredPane;
private JPanel mainLayer;
private JMenuBar menuBar;
private TablePanel table;
private PlayerPanel playerPanel;
private StartTurnPanel startTurnPanel;
private PausePanel pausePanel;
private WinPanel winPanel;
private SettingsPanel settingsPanel;
private LoginPanel loginPanel;
private ScorePanel scorePanel;
private GameListPanel gameListPanel;
private BottomPanelType bottomPanelType;
private JFileChooser chooser;
private Event menuNewGameEvent = new Event();
private Event menuQuitEvent = new Event();
private Event networkGameEvent = new Event();
private Event1<File> loadEvent = new Event1<File>();
private Event1<File> saveEvent = new Event1<File>();
private static int even(double d) {
return 2 * (int) (d / 2);
}
@Override
public ISettingsPanel getSettingsPanel() {
return settingsPanel;
}
@Override
public ILoginPanel getLoginPanel() {
return loginPanel;
}
@Override
public IScorePanel getScorePanel() {
return scorePanel;
}
@Override
public ITablePanel getTablePanel() {
return table;
}
@Override
public IHandPanel getHandPanel() {
return playerPanel.getHandPanel();
}
@Override
public IPlayerPanel getPlayerPanel() {
return playerPanel;
}
@Override
public IGameListPanel getGameListPanel() {
return gameListPanel;
}
@Override
public IEvent getMenuNewGameEvent() {
return menuNewGameEvent;
}
@Override
public IEvent getMenuQuitEvent() {
return menuQuitEvent;
}
@Override
public IEvent1<File> getLoadEvent() {
return loadEvent;
}
@Override
public IEvent1<File> getSaveEvent() {
return saveEvent;
}
@Override
public IEvent getPauseEvent() {
return playerPanel.getPauseEvent();
}
@Override
public IEvent getEndPauseEvent() {
return pausePanel.getEndPauseEvent();
}
@Override
public IEvent getNetworkGameEvent() {
return networkGameEvent;
}
@Override
public void clearView() {
showScorePanel(false);
showSettingsPanel(false);
showLoginPanel(false);
showGameListPanel(false);
getHandPanel().setStones(Collections.<Pair<Stone, Position>> emptyList());
getTablePanel().setStoneSets(
Collections.<Pair<StoneSet, Position>> emptyList());
setSelectedStones(Collections.<Stone> emptyList());
}
private void createFileChooser() {
chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JRummikub-Spiele", "rum");
chooser.setFileFilter(filter);
}
private void createMenuBar() {
menuBar = new JMenuBar();
JMenu gameMenu = new JMenu("Spiel");
menuBar.add(gameMenu);
gameMenu.add(createMenuItem("Neu", menuNewGameEvent));
gameMenu.add(createMenuItem("Netzwerkspiel...", networkGameEvent));
gameMenu.addSeparator();
JMenuItem loadItem = new JMenuItem("Laden...");
loadItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
load();
}
});
gameMenu.add(loadItem);
JMenuItem saveItem = new JMenuItem("Speichern...");
saveItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
save();
}
});
gameMenu.add(saveItem);
gameMenu.addSeparator();
gameMenu.add(createMenuItem("Beenden", menuQuitEvent));
setJMenuBar(menuBar);
}
private JMenuItem createMenuItem(String text, final Event event) {
JMenuItem item = new JMenuItem(text);
item.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
event.emit();
}
});
return item;
}
private void load() {
int returnVal = chooser.showOpenDialog(View.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
loadEvent.emit(chooser.getSelectedFile());
}
}
private void save() {
int returnVal = chooser.showSaveDialog(View.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
saveEvent.emit(chooser.getSelectedFile());
}
}
/**
* Create a new instance of the view
*/
public View() {
super("JRummikub");
setLayout(null);
setSize(1000, 700);
setDefaultCloseOperation(EXIT_ON_CLOSE);
createFileChooser();
createMenuBar();
layeredPane = new JLayeredPane();
layeredPane.setLayout(null);
add(layeredPane);
createMainLayer();
createSettingsPanel();
layeredPane.setLayer(settingsPanel, JLayeredPane.POPUP_LAYER);
layeredPane.add(settingsPanel);
loginPanel = new LoginPanel();
loginPanel.setVisible(false);
layeredPane.setLayer(loginPanel, JLayeredPane.POPUP_LAYER);
layeredPane.add(loginPanel);
gameListPanel = new GameListPanel();
gameListPanel.setVisible(false);
layeredPane.setLayer(gameListPanel, JLayeredPane.POPUP_LAYER);
layeredPane.add(gameListPanel);
scorePanel = new ScorePanel();
scorePanel.setVisible(false);
layeredPane.setLayer(scorePanel, JLayeredPane.POPUP_LAYER);
layeredPane.add(scorePanel);
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
rescale();
}
});
setVisible(true);
}
private void createSettingsPanel() {
settingsPanel = new SettingsPanel();
settingsPanel.setVisible(false);
settingsPanel.getNetworkGameEvent().add(new IListener() {
@Override
public void handle() {
networkGameEvent.emit();
}
});
settingsPanel.getLoadGameEvent().add(new IListener() {
@Override
public void handle() {
load();
}
});
}
private void createMainLayer() {
mainLayer = new JPanel();
mainLayer.setLayout(null);
layeredPane.add(mainLayer);
table = new TablePanel();
mainLayer.add(table);
playerPanel = new PlayerPanel();
playerPanel.setBorder(new MatteBorder(PLAYER_PANEL_BORDER_WIDTH, 0, 0, 0,
Color.BLACK));
mainLayer.add(playerPanel);
startTurnPanel = new StartTurnPanel();
startTurnPanel.setVisible(false);
mainLayer.add(startTurnPanel);
pausePanel = new PausePanel();
pausePanel.setVisible(false);
mainLayer.add(pausePanel);
winPanel = new WinPanel();
winPanel.setVisible(false);
mainLayer.add(winPanel);
}
@Override
public void enablePauseMode(boolean enable) {
table.enablePauseMode(enable);
doSetBottomPanel(enable ? null : bottomPanelType);
pausePanel.setVisible(enable);
}
private void rescale() {
int width = getContentPane().getWidth(), height = getContentPane()
.getHeight();
layeredPane.setBounds(0, 0, width, height);
mainLayer.setBounds(0, 0, width, height);
int playerPanelHeight = even(Math.pow((double) width * width * height,
1 / 3.0) * PLAYER_PANEL_RATIO)
+ PLAYER_PANEL_BORDER_WIDTH;
if (playerPanelHeight > PLAYER_PANEL_MAX_HEIGHT)
playerPanelHeight = PLAYER_PANEL_MAX_HEIGHT;
int tableHeight = height - playerPanelHeight;
table.setBounds(0, 0, width, tableHeight);
table.validate();
playerPanel.setBounds(0, tableHeight, width, playerPanelHeight);
startTurnPanel.setBounds(0, tableHeight, width, playerPanelHeight);
pausePanel.setBounds(0, tableHeight, width, playerPanelHeight);
winPanel.setBounds(0, tableHeight, width, playerPanelHeight);
settingsPanel.setBounds(width / 4, height / 4, width / 2, height / 2);
scorePanel.setBounds(width / 8, height / 4, width * 3 / 4, height / 2);
loginPanel.setBounds(width / 3, height / 3, width / 3, height / 3);
gameListPanel.setBounds(width / 4, height / 4, width / 2, height / 2);
}
@Override
public void setSelectedStones(Collection<Stone> stones) {
table.setSelectedStones(stones);
playerPanel.getHandPanel().setSelectedStones(stones);
}
@Override
public void showSettingsPanel(boolean show) {
if (show) {
settingsPanel.resetTabbedPane();
}
settingsPanel.setVisible(show);
}
@Override
public void showLoginPanel(boolean show) {
loginPanel.setVisible(show);
}
@Override
public void showGameListPanel(boolean show) {
if (show) {
gameListPanel.reset();
}
gameListPanel.setVisible(show);
}
@Override
public void showScorePanel(boolean show) {
scorePanel.setVisible(show);
}
@Override
public void setCurrentPlayerName(String playerName) {
playerPanel.setCurrentPlayerName(playerName);
startTurnPanel.setCurrentPlayerName(playerName);
pausePanel.setCurrentPlayerName(playerName);
}
@Override
public void setCurrentPlayerColor(Color color) {
playerPanel.setCurrentPlayerColor(color);
}
@Override
public void setCurrentPlayerHasLaidOut(boolean hasLaidOut) {
playerPanel.setHasLaidOut(hasLaidOut);
}
@Override
public IEvent getStartTurnEvent() {
return startTurnPanel.getStartTurnEvent();
}
@Override
public IEvent getNewRoundEvent() {
return winPanel.getNewRoundEvent();
}
@Override
public IEvent getNewGameEvent() {
return winPanel.getNewGameEvent();
}
@Override
public IEvent getEndProgramEvent() {
return winPanel.getEndProgramEvent();
}
@SuppressWarnings("unchecked")
private List<Pair<Stone, Position>> createDecorationStones() {
Pair<Stone, Position> stoneJ = new Pair<Stone, Position>(new Stone(-'J',
StoneColor.BLACK), new Position(2.5f, 0));
Pair<Stone, Position> stoneR = new Pair<Stone, Position>(new Stone(-'R',
StoneColor.ORANGE), new Position(3.5f, 0));
Pair<Stone, Position> stoneu1 = new Pair<Stone, Position>(new Stone(-'u',
StoneColor.BLUE), new Position(4.5f, 0));
Pair<Stone, Position> stonem1 = new Pair<Stone, Position>(new Stone(-'m',
StoneColor.RED), new Position(5.5f, 0));
Pair<Stone, Position> stonem2 = new Pair<Stone, Position>(new Stone(-'m',
StoneColor.GREEN), new Position(6.5f, 0));
Pair<Stone, Position> stonei = new Pair<Stone, Position>(new Stone(-'i',
StoneColor.VIOLET), new Position(7.5f, 0));
Pair<Stone, Position> stonek = new Pair<Stone, Position>(new Stone(-'k',
StoneColor.AQUA), new Position(8.5f, 0));
Pair<Stone, Position> stoneu2 = new Pair<Stone, Position>(new Stone(-'u',
StoneColor.GRAY), new Position(9.5f, 0));
Pair<Stone, Position> stoneb = new Pair<Stone, Position>(new Stone(-'b',
StoneColor.BLACK), new Position(10.5f, 0));
Pair<Stone, Position> stone1 = new Pair<Stone, Position>(new Stone(
StoneColor.RED), new Position(2, 1));
Pair<Stone, Position> stone2 = new Pair<Stone, Position>(new Stone(13,
StoneColor.BLACK), new Position(5, 1));
Pair<Stone, Position> stone3 = new Pair<Stone, Position>(new Stone(13,
StoneColor.ORANGE), new Position(6, 1));
Pair<Stone, Position> stone4 = new Pair<Stone, Position>(new Stone(13,
StoneColor.BLUE), new Position(7, 1));
Pair<Stone, Position> stone5 = new Pair<Stone, Position>(new Stone(13,
StoneColor.RED), new Position(8, 1));
Pair<Stone, Position> stone6 = new Pair<Stone, Position>(new Stone(
StoneColor.BLACK), new Position(11, 1));
return Arrays
.asList(stoneJ, stoneR, stoneu1, stonem1, stonem2, stonei, stonek,
stoneu2, stoneb, stone1, stone2, stone3, stone4, stone5, stone6);
}
@Override
public void setBottomPanel(BottomPanelType type) {
bottomPanelType = type;
doSetBottomPanel(type);
}
private void doSetBottomPanel(BottomPanelType type) {
startTurnPanel.setVisible(type == BottomPanelType.START_TURN_PANEL);
winPanel.setVisible(type == BottomPanelType.WIN_PANEL);
playerPanel.setVisible(type != BottomPanelType.START_TURN_PANEL
&& type != BottomPanelType.WIN_PANEL && type != null);
if (type == BottomPanelType.START_GAME_PANEL) {
table.setStoneSets(Collections.<Pair<StoneSet, Position>> emptyList());
playerPanel.getHandPanel().setStones(createDecorationStones());
}
playerPanel.showButtons(type != BottomPanelType.START_GAME_PANEL);
playerPanel.enableButtons(type != BottomPanelType.COMPUTER_HAND_PANEL);
}
}
|