// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   BoggleClient.java

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;

public class BoggleClient
    implements ActionListener, WindowListener
{

    BoggleClient()
    {
        window = new JFrame("Boggle");
        JMenuBar jmenubar = new JMenuBar();
        JMenu jmenu = new JMenu("Game", false);
        JMenu jmenu1 = new JMenu("Help", false);
        JMenuItem jmenuitem = new JMenuItem("New");
        JMenuItem jmenuitem1 = new JMenuItem("View Highscores");
        JMenuItem jmenuitem2 = new JMenuItem("Exit");
        JMenuItem jmenuitem3 = new JMenuItem("Help");
        JMenuItem jmenuitem4 = new JMenuItem("About");
        BoggleGrid bogglegrid = new BoggleGrid(-1);
        Score score;
        try
        {
            score = new Score(0);
        }
        catch(IOException ioexception)
        {
            System.err.println(ioexception);
        }
        jmenu.add(jmenuitem);
        jmenu.add(jmenuitem1);
        jmenu.addSeparator();
        jmenu.add(jmenuitem2);
        jmenu1.add(jmenuitem3);
        jmenu1.addSeparator();
        jmenu1.add(jmenuitem4);
        jmenubar.add(jmenu);
        jmenubar.add(jmenu1);
        window.getContentPane().setLayout(null);
        window.setJMenuBar(jmenubar);
        window.setSize(250, 352);
        window.setResizable(false);
        window.setVisible(true);
        window.setDefaultCloseOperation(0);
        window.addWindowListener(this);
        jmenuitem.addActionListener(this);
        jmenuitem1.addActionListener(this);
        jmenuitem2.addActionListener(this);
        jmenuitem3.addActionListener(this);
        jmenuitem4.addActionListener(this);
    }

    public void actionPerformed(ActionEvent actionevent)
    {
        if(actionevent.getActionCommand().equals("New"))
        {
            if(bg != null)
            {
                if(bg.isRunning())
                {
                    int i = JOptionPane.showConfirmDialog(window, "Do you really wish to abandon the current game?", "New Game", 0);
                    if(i == 1)
                        return;
                }
                window.getContentPane().remove(bg);
                bg.stop();
            }
            try
            {
                window.setCursor(new Cursor(3));
                bg = new BoggleGame(serverAddress);
            }
            catch(IOException ioexception)
            {
                System.err.println(ioexception);
                JOptionPane.showMessageDialog(window, "An error occured when trying to create a new game.\nYou should check that the server is running and is accessable from your network.\nFor more information on this error, you should check the Java console.", "Communications error", 0);
                return;
            }
            finally
            {
                window.setCursor(Cursor.getDefaultCursor());
            }
            window.getContentPane().add(bg);
            bg.setVisible(true);
            window.repaint();
            return;
        }
        if(actionevent.getActionCommand().equals("View Highscores"))
        {
            boolean flag = false;
            if(hst != null)
                hst.dispose();
            try
            {
                window.setCursor(new Cursor(3));
                Socket socket = new Socket(InetAddress.getByName(serverAddress), 1978);
                DataOutputStream dataoutputstream = new DataOutputStream(socket.getOutputStream());
                dataoutputstream.writeInt(14);
                dataoutputstream.flush();
                ObjectInputStream objectinputstream = new ObjectInputStream(socket.getInputStream());
                hst = (HighScoreTable)objectinputstream.readObject();
                objectinputstream.close();
                hst.setDefaultCloseOperation(2);
                hst.show();
            }
            catch(UnknownHostException unknownhostexception)
            {
                System.err.println(unknownhostexception);
                flag = true;
            }
            catch(StreamCorruptedException streamcorruptedexception)
            {
                System.err.println(streamcorruptedexception);
                flag = true;
            }
            catch(IOException ioexception1)
            {
                System.err.println(ioexception1);
                flag = true;
            }
            catch(ClassNotFoundException classnotfoundexception)
            {
                System.err.println(classnotfoundexception);
                flag = true;
            }
            finally
            {
                window.setCursor(Cursor.getDefaultCursor());
            }
            if(flag)
                JOptionPane.showMessageDialog(window, "An error occured when trying to download the highscore table.\nYou should check that the server is running and is accessable from your network.\nFor more information on this error, you should check the Java console.", "Communications error", 0);
            return;
        }
        if(actionevent.getActionCommand().equals("Exit"))
        {
            if(bg != null && bg.isRunning())
            {
                int j = JOptionPane.showConfirmDialog(window, "Do you really wish to abandon the current game?", "Exit", 0);
                if(j == 1)
                    return;
            }
            System.exit(0);
        }
        if(actionevent.getActionCommand().equals("Help"))
            if(help == null)
                help = new Help();
            else
                help.show();
        if(actionevent.getActionCommand().equals("About"))
            JOptionPane.showMessageDialog(window, "Boggle\nMichael McTernan 1999, mm7323@cs.bris.ac.uk\nCOMS40203 Assignment", "About Boggle", 1);
    }

    public static void main(String args[])
    {
        BoggleGrid bogglegrid = new BoggleGrid(-1);
        if(args.length > 0)
            serverAddress = args[0];
        else
            serverAddress = new String("127.0.0.1");
        BoggleClient boggleclient = new BoggleClient();
    }

    public void windowActivated(WindowEvent windowevent)
    {
    }

    public void windowClosed(WindowEvent windowevent)
    {
    }

    public void windowClosing(WindowEvent windowevent)
    {
        if(bg != null && bg.isRunning())
        {
            int i = JOptionPane.showConfirmDialog(window, "Do you really wish to abandon the current game?", "Exit", 0);
            if(i == 1)
                return;
        }
        System.exit(0);
    }

    public void windowDeactivated(WindowEvent windowevent)
    {
    }

    public void windowDeiconified(WindowEvent windowevent)
    {
    }

    public void windowIconified(WindowEvent windowevent)
    {
    }

    public void windowOpened(WindowEvent windowevent)
    {
    }

    private JFrame window;
    private BoggleGame bg;
    private HighScoreTable hst;
    private Help help;
    private static String serverAddress;
}

