~mchaten/gg/0.3

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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/*
(C) 2007,2008,2009 yura.net, Michael Chaten
This file is part of Good Game.
Good Game is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License
Good Game is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package net.sf.goodgame.client.gui;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.OverlayLayout;
import javax.swing.UIManager;
import net.sf.goodgame.client.game.GameClient;
import net.sf.goodgame.client.game.GameClientListener;
import net.sf.goodgame.client.game.GameList;
import net.sf.goodgame.client.game.GamesTable;
import net.sf.goodgame.client.game.GamesTableModel;
import net.sf.goodgame.client.gametype.GameType;
import net.sf.goodgame.client.gametype.GameTypeList;
import net.sf.goodgame.client.gametype.GameTypeListGUI;
import net.sf.goodgame.client.gui.chat.ChatBox;
import net.sf.goodgame.client.gui.chat.PrivateChatController;
import net.sf.goodgame.client.gui.translucency.GlassJPanel;
import net.sf.goodgame.client.gui.translucency.TranslucentJButton;
import net.sf.goodgame.client.gui.translucency.TranslucentJPanel;
import net.sf.goodgame.client.gui.translucency.TranslucentJScrollPane;
import net.sf.goodgame.client.networking.InputOutput;
import net.sf.goodgame.client.user.friends.FriendsListGUI;
import net.sf.goodgame.client.user.UserList;
import net.sf.goodgame.client.user.UserListGUI;
import net.sf.goodgame.client.user.friends.FriendsList;
import net.sf.goodgame.shared.UserInfoElement;
import net.sf.goodgame.shared.UserType;
import net.sf.goodgame.shared.games.Player;

/**
 *
 * @author mchaten
 */
public class Main extends TranslucentJPanel implements GoodGameUI, GameClientListener {

    private GameType currentGameType;
    private BufferedImage backpic;
    private BufferedImage logopic;
    private ImageIcon backpicIcon;
    private static URL codebase;
    private Color themeColor;
    private InputOutput io;
    private GlassJPanel glass;
    private JInternalFrame logbox;
    private JInternalFrame loadingbox;
    private JTextArea connectlog;
    private GameTypeList gameTypeList;
    private UserList userList;
    private ChatBox lobbyChatBox;
    private LobbyList lobbyList;
    private GameList gameList;
    private JPanel userActions;
    private String NOT_LOGGED_IN_LOOK = "NL";
    private String LOBBY_LOOK = "LL";
    private String GAME_LOOK = "GL";
    private Player myPlayer;
    private FriendsList friendsList;
    private PrivateChatController privateChatController;
    private CardLayout guiLayout;
    private GameClient currentGame;
    private FriendsListGUI friendsListGUI;
    private GamesTableModel model;
    private UserType myType;
    
    static {

        Font serif = new Font("SansSerif", Font.PLAIN, 12);
        UIManager.put("TextArea.font", serif);
    }

    public static URL getCodebase() {
        return codebase;
    }

    public Main(final URL codebase, final String hostname, final int port) {
        super(0.0f);
        setArt("back.jpg", "logo.png", "blue");
        Main.setCodeBase(codebase);
        InetSocketAddress address = new InetSocketAddress(hostname, port);
        io = new InputOutput(address,new GUIDispatcher(this));

        privateChatController = new PrivateChatController(io.getOutgoingConnection());
        io.getIncomingConnection().setPrivateChatController(privateChatController);

        TranslucentJPanel top = new TranslucentJPanel(0.0f);
        top.setLayout(new BoxLayout(top, BoxLayout.LINE_AXIS));

        gameTypeList = new GameTypeListGUI(io.getOutgoingConnection());
        TranslucentJButton createGameButton = new TranslucentJButton("Create Game", 0.6f);
        createGameButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                createGame();
            }
        });
        TranslucentJPanel gametypewindow = new TranslucentJPanel(new BorderLayout(), 0.5f);
        JLabel gameTypeLabel = new JLabel(" Game Types:");
        gameTypeLabel.setForeground(Color.white);
        gametypewindow.add(gameTypeLabel, BorderLayout.NORTH);
        gametypewindow.add(new TranslucentJScrollPane(gameTypeList.getComponent(), 0.5f));
        gametypewindow.add(createGameButton, BorderLayout.SOUTH);
        gametypewindow.setPreferredSize(new Dimension(200, top.getSize().height));
        gametypewindow.setMaximumSize(new Dimension(300, 100000));

        model = new GamesTableModel();
        gameList = new GamesTable(model, io.getOutgoingConnection());
        JLabel gamesLabel = new JLabel(" Games: ");
        gamesLabel.setForeground(Color.white);
        JPanel gameswindow = new TranslucentJPanel(new BorderLayout(), 0.5f);
        gameswindow.add(gamesLabel, BorderLayout.NORTH);
        gameswindow.add(new TranslucentJScrollPane(gameList.getComponent(), 0.5f));

        JPanel bottom = new TranslucentJPanel(0.0f);
        bottom.setLayout(new BoxLayout(bottom, BoxLayout.PAGE_AXIS));
        JPanel chatAndUserPanel = new TranslucentJPanel(0.0f);
        chatAndUserPanel.setLayout(new BoxLayout(chatAndUserPanel, BoxLayout.LINE_AXIS));
        chatAndUserPanel.setPreferredSize(new Dimension(Integer.MAX_VALUE, 500));
        lobbyChatBox = new ChatBox("Null", io.getOutgoingConnection(), false);

        userList = new UserListGUI(privateChatController, io.getOutgoingConnection());
        
        JButton logoutButton = new TranslucentJButton("Logout", 0.8f);
        logoutButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                logout();
            }
        });
        friendsListGUI = new FriendsListGUI(getPrivateChatController(),io.getOutgoingConnection());
        friendsList = null;
        JButton friends = new TranslucentJButton("Friends", 0.8f);

        friends.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                friendsListGUI.show();
            }
        });
        userActions = new TranslucentJPanel(0.0f);

        userActions.add(logoutButton);
        userActions.add(friends);

        userList.getComponent().add(userActions, BorderLayout.SOUTH);
        userList.getComponent().setPreferredSize(new Dimension(200, bottom.getSize().height));
        userList.getComponent().setMaximumSize(new Dimension(300, 10000));

        chatAndUserPanel.add(Box.createRigidArea(new Dimension(10, 0)));
        chatAndUserPanel.add(lobbyChatBox);
        chatAndUserPanel.add(Box.createRigidArea(new Dimension(10, 0)));
        chatAndUserPanel.add(userList.getComponent());
        chatAndUserPanel.add(Box.createRigidArea(new Dimension(10, 0)));

        JPanel lobbyListPanel = new TranslucentJPanel(0.0f);
        lobbyListPanel.setLayout(new BoxLayout(lobbyListPanel, BoxLayout.LINE_AXIS));

        lobbyList = new LobbyList(io.getOutgoingConnection());

        lobbyListPanel.add(Box.createRigidArea(new Dimension(10, 0)));
        lobbyListPanel.add(lobbyList);
        lobbyListPanel.add(Box.createRigidArea(new Dimension(10, 10)));

        bottom.add(chatAndUserPanel);
        bottom.add(Box.createRigidArea(new Dimension(0, 10)));
        bottom.add(lobbyListPanel);
        bottom.setPreferredSize(new Dimension(1000, 768 / 2 - 20));

        top.add(Box.createRigidArea(new Dimension(10, 0)));
        top.add(gametypewindow);
        top.add(Box.createRigidArea(new Dimension(10, 0)));
        top.add(gameswindow);
        top.add(Box.createRigidArea(new Dimension(10, 0)));

        TranslucentJPanel lobbyLook = new TranslucentJPanel(0.0f);
        lobbyLook.setLayout(new BoxLayout(lobbyLook, BoxLayout.PAGE_AXIS));
        lobbyLook.add(Box.createRigidArea(new Dimension(0, 10)));
        lobbyLook.add(top);
        lobbyLook.add(Box.createRigidArea(new Dimension(0, 10)));
        lobbyLook.add(bottom);
        lobbyLook.add(Box.createRigidArea(new Dimension(0, 10)));
        guiLayout = new CardLayout();
        setLayout(guiLayout);
        add(lobbyLook, LOBBY_LOOK);

        Component notLoggedInLook = new LoginPanel(this,io).getComponent();
        add(notLoggedInLook,NOT_LOGGED_IN_LOOK);
        
        guiLayout.show(this, NOT_LOGGED_IN_LOOK);
        io.getIncomingConnection().setPrivateChatController(privateChatController);
        io.getIncomingConnection().setChatListener(lobbyChatBox);
        io.getIncomingConnection().setGameClient(null);
        io.getIncomingConnection().setGameList(gameList);
        io.getIncomingConnection().setGameTypeList(gameTypeList);
        io.getIncomingConnection().setUI(this);
        io.getIncomingConnection().setLobbyList(lobbyList);
        io.getIncomingConnection().setUserList(userList);
    }

    @Override
    public void joinGame(GameType gameType) {
        if (currentGame == null) {
            currentGame = gameType.getGameClient(codebase.toString());
        }
        currentGame.setGameClientListener(this);
        add(currentGame.getGamePanel(), GAME_LOOK);
        guiLayout.show(this, GAME_LOOK);
    }
//Start GameClientListener methods.

    @Override
    public InputOutput getIO() {
        return io;
    }

    @Override
    public PrivateChatController getPrivateChatController() {
        return privateChatController;
    }

    @Override
    public Player whoAmI() {
        return myPlayer;
    }
//End GameClientListener Methods.

    @Override
    public void leaveGame() {
        if (currentGame != null) {

            this.remove(currentGame.getGamePanel());
            currentGame.destroy();
            currentGame = null;
        }
        guiLayout.show(this, LOBBY_LOOK);
        gameList.reset();
        userList.reset();
        io.getIncomingConnection().setPrivateChatController(privateChatController);
        io.getIncomingConnection().setChatListener(lobbyChatBox);
        io.getIncomingConnection().setGameClient(null);
        io.getIncomingConnection().setGameList(gameList);
        io.getIncomingConnection().setGameTypeList(gameTypeList);
        io.getIncomingConnection().setUI(this);
        io.getIncomingConnection().setLobbyList(lobbyList);
        io.getIncomingConnection().setUserList(userList);
        io.getIncomingConnection().setFriendsListener(friendsList);
    }

    void connect() {
        showConnectingDialog();
        io.getOutgoingConnection().connect();
    }

    void disconnect() {
        io.disconnect();
    }

    @Override
    public void connected() {
        glass.setVisible(false);
        logbox.setVisible(false);
    }

    @Override
    public void connecting(String message) {
        connectlog.append(message + "\n");
        connectlog.setCaretPosition(connectlog.getDocument().getLength());
    }

    @Override
    public void disconnected() {
        lobbyList.clearList();
        gameTypeList.reset();
        userList.reset();
        guiLayout.show(this, LOBBY_LOOK);
        int result = JOptionPane.showConfirmDialog(null, "You have been disconnected, Would you like to reconnect?", "Disconnected", JOptionPane.YES_NO_OPTION);
        if (result == JOptionPane.YES_OPTION) {
            glass.setVisible(true);
            logbox.setVisible(true);
            io.getOutgoingConnection().connect();
        } else {
            exit();
        }
    }

    @Override
    public void showError(String error) {
        JOptionPane.showMessageDialog(this, error, "Error", JOptionPane.ERROR_MESSAGE);
    }

    @Override
    public void login(String name, UserType type) {
        myPlayer = new Player(name);
        myType = type;
        friendsList = new FriendsList();
        io.getIncomingConnection().setFriendsListener(friendsList);
        friendsListGUI.setFriendsList(friendsList);
        CardLayout layout = this.guiLayout;
        layout.show(this, LOBBY_LOOK);
    }

    public UserType getMyType() {
        return myType;
    }

    public FriendsList getFriendsList() {
        return friendsList;
    }
    
    private void logout() {
        io.getOutgoingConnection().logout();
        guiLayout.show(this,NOT_LOGGED_IN_LOOK);
    }

    @Override
    public void serverMessage(String message) {
        JOptionPane.showMessageDialog(this, message, "Message from Server", JOptionPane.INFORMATION_MESSAGE);
    }

    @Override
    public void switchGameType(GameType gt) {
        currentGameType = gt;
        gameList.setGameTableLayout(gt.getLayout());
        gameList.reset();
    }

    @Override
    public void switchLobby(short id) {
        gameList.reset();
        userList.reset();
        lobbyChatBox.setName(lobbyList.getLobby(id).NAME);
    }

    @Override
    public void showUserInfo(String username,List<UserInfoElement> userInfos) {
        userList.setUserInfo(username, userInfos);
    }

    void setArt(String back, String logo, String color) {
        if (back != null && logo != null && color != null) {
            try {
                backpic = ImageIO.read(this.getClass().getResource(back));
                logopic = ImageIO.read(this.getClass().getResource(logo));
                backpicIcon = new ImageIcon(backpic);
                themeColor = Color.blue;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void paintComponent(Graphics g) {

        int w = backpic.getWidth();
        int h = backpic.getHeight();

        for (int i = 0; i < getWidth(); i += w) {
            for (int j = 0; j < getHeight(); j += h) {
                g.drawImage(backpic, i, j, this);
            }
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            if (System.getProperty("os.name").contains("Windows")) {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } else //There are some bugs in the linux look and feel for GTK that are not there in windows.
            {
                UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = new Dimension(1024, 768);
        frameSize.height = ((frameSize.height > screenSize.height) ? screenSize.height
                : frameSize.height);
        frameSize.width = ((frameSize.width > screenSize.width) ? screenSize.width
                : frameSize.width);
        JFrame gui = new JFrame("Good Game");

        gui.setLocation((screenSize.width - frameSize.width) / 2,
                (screenSize.height - frameSize.height) / 2);
        gui.setSize(frameSize);
        Main main;
        try {
            main = new Main(new File(".").toURI().toURL(), "localhost", 13375);
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return;
        }
        gui.getRootPane().setContentPane(main);
        // gui.setIconImage(Toolkit.getDefaultToolkit().getImage(
        // this.class.getResource("icon.gif") ));
        // gui.setResizable(false);
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // gui.pack();

        main.connect();
        gui.setVisible(true);
    }

    private void exit() {
        if (io != null) {
            io.disconnect();
        }
        System.exit(0);
    }

    private void showConnectingDialog() {

        if (glass == null) {
            makeConnectingDialog();
        }

        glass.setVisible(true);
        logbox.setVisible(true);

    }

    private void createGame() {
        io.getOutgoingConnection().createGame();
    }
    private void makeConnectingDialog() {

        glass = new GlassJPanel(themeColor);
        System.out.println(getRootPane());
        System.out.println(getRootPane().getLayeredPane());
        getRootPane().getLayeredPane().setLayout(new OverlayLayout(getRootPane().getLayeredPane()));

        loadingbox = new JInternalFrame("loading...");
        JProgressBar loadingbar = new JProgressBar();
        loadingbar.setIndeterminate(true);
        loadingbox.getContentPane().add(loadingbar);

        logbox = new JInternalFrame("connecting...");

        JPanel cancelpanel = new JPanel();

        JButton cancel = new JButton("Cancel");
        cancel.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                exit();
            }
        });
        cancelpanel.add(cancel);

        connectlog = new JTextArea();
        connectlog.setEditable(false);

        logbox.getContentPane().add(new JScrollPane(connectlog));
        logbox.getContentPane().add(cancelpanel, BorderLayout.SOUTH);

        Dimension d = new Dimension(300, 200);

        logbox.setMinimumSize(d);
        logbox.setMaximumSize(d);
        logbox.setPreferredSize(d);

        d = new Dimension(300, 50);

        loadingbox.setMinimumSize(d);
        loadingbox.setMaximumSize(d);
        loadingbox.setPreferredSize(d);


        glass.setVisible(false);
        logbox.setVisible(false);
        loadingbox.setVisible(false);

        getRootPane().getLayeredPane().add(glass, JLayeredPane.PALETTE_LAYER);
        getRootPane().getLayeredPane().add(logbox, JLayeredPane.MODAL_LAYER);
        getRootPane().getLayeredPane().add(loadingbox, JLayeredPane.MODAL_LAYER);
    }

    
    public static URL getCodeBase() {

        return codebase;

    }
    private static void setCodeBase(URL _codebase) {
        codebase = _codebase;
    }

    
}