// 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:   TableFrame.java

import java.applet.Applet;
import java.awt.Button;
import java.awt.Checkbox;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Event;
import java.awt.Frame;
import java.awt.Image;
import java.awt.Label;
import java.awt.Panel;
import java.awt.Scrollbar;
import java.awt.TextArea;
import java.awt.TextComponent;
import java.awt.TextField;
import java.awt.Window;
import java.util.Vector;
import shared.C2S;

public class TableFrame extends Frame
{

    TableFrame(Connection connection, boolean flag, String as[], int ai[], String as1[][])
    {
        super(_German.GAME_NAME);
        gcontrol = new GameControl[25];
        gframe = new GameFrame[25];
        firstItem = true;
        ignorants = new Vector();
        con = connection;
        setIconImage(iconImage);
        setBackground(Color.white);
        TablePanel tablepanel = new TablePanel(logoImage);
        tablepanel.setLayout(null);
        tc = new TableComponent[25];
        Panel panel = new Panel();
        panel.setLayout(null);
        panel.reshape(0, 59, 504, 238);
        Scrollbar scrollbar = new Scrollbar(1, 0, 1, 0, 360);
        scrollbar.reshape(505, 59, 16, 238);
        tablepanel.add(scrollbar);
        scrollpan = new Panel();
        scrollpan.setLayout(null);
        scrollpan.reshape(0, 0, 502, 602);
        for(int i = 0; i < 25; i++)
        {
            tc[i] = new TableComponent(5, i * 24, i % 2, i, con);
            scrollpan.add(tc[i]);
        }

        panel.add(scrollpan);
        tablepanel.add(panel);
        pres = new Label(_German.CONNECTED + " (0) :");
        pres.setAlignment(1);
        pres.setBackground(lightRed);
        pres.reshape(526, 56, 102, 19);
        tablepanel.add(pres);
        pList = new java.awt.List(10, false);
        pList.reshape(526, 78, 102, 203);
        pList.setBackground(Color.white);
        tablepanel.add(pList);
        ic = new Checkbox(_German.IGNORE);
        ic.reshape(526, 282, 85, 15);
        ic.setBackground(Color.white);
        tablepanel.add(ic);
        chatBoard = new TextArea();
        chatBoard.reshape(182, 302, 447, 92);
        chatBoard.setEditable(false);
        tablepanel.add(chatBoard);
        firstMessage = true;
        chatInbox = new TextField();
        chatInbox.reshape(181, 396, 447, 26);
        tablepanel.add(chatInbox);
        bf = new Button(_German.EXIT_TABLE_SELECTOR);
        bf.setBackground(Color.lightGray);
        bf.reshape(5, 302, 64, 24);
        tablepanel.add(bf);
        ntb = new Button(_German.CREATE_NEW_TABLE);
        ntb.setBackground(Color.lightGray);
        ntb.reshape(73, 302, 104, 24);
        ntb.disable();
        tablepanel.add(ntb);
        Label label = new Label(_German.TIME_RESTRICTIONS + "(" + _German.MIN_ABBREVIATION + ")");
        label.setBackground(lightRed);
        label.setAlignment(1);
        label.reshape(5, 350, 172, 20);
        tablepanel.add(label);
        Label label1 = new Label("  " + _German.ONE_MOVE);
        label1.setBackground(lightBlue);
        label1.reshape(5, 374, 109, 21);
        tablepanel.add(label1);
        mc = new Choice();
        for(int j = 0; j < Const.MOVE_LIMIT.length; j++)
            mc.addItem(" " + Const.MOVE_LIMIT[j] + " ");

        mc.reshape(118, 374, 60, 20);
        mc.setBackground(Color.white);
        mc.select(1);
        tablepanel.add(mc);
        Label label2 = new Label("  " + _German.WHOLE_GAME);
        label2.setBackground(lightBlue);
        label2.reshape(5, 399, 109, 21);
        tablepanel.add(label2);
        gc = new Choice();
        for(int k = 0; k < Const.GAME_LIMIT.length; k++)
            gc.addItem(" " + Const.GAME_LIMIT[k] + " ");

        gc.reshape(118, 399, 60, 20);
        gc.setBackground(Color.white);
        gc.select(Const.GAME_LIMIT.length - 3);
        tablepanel.add(gc);
        add("Center", tablepanel);
        nop = as.length;
        int l = 0;
        for(int i1 = 0; i1 < nop; i1++)
        {
            int k1;
            for(k1 = 0; k1 < l && as[i1].compareTo(pList.getItem(k1)) > 0; k1++);
            pList.addItem(as[i1], k1);
            l++;
        }

        pres.setText(_German.CONNECTED + " (" + nop + ") :");
        int j1 = ai.length;
        for(int l1 = 0; l1 < j1; l1++)
            tc[l1].setStatus(l1, ai[l1], as1[l1]);

        tableCreationAllowance(flag);
        pack();
        show();
    }

    public boolean action(Event event, Object obj)
    {
        if(event.target == ic)
        {
            String s = pList.getSelectedItem();
            if(ic.getState())
            {
                if(!ignorants.contains(s))
                    ignorants.addElement(s);
            } else
            if(ignorants.contains(s))
                ignorants.removeElement(s);
        }
        if(event.target == bf)
        {
            for(int i = 0; i < 10; i++)
                if(gframe[i] != null)
                    gframe[i].shutdown();

            con._kickMeOut();
            dispose();
        } else
        if(event.target == ntb)
        {
            int j = Const.MOVE_LIMIT[mc.getSelectedIndex()];
            int k = Const.GAME_LIMIT[gc.getSelectedIndex()];
            con._newTable(k, j);
            ntb.disable();
        } else
        if(event.target == chatInbox)
        {
            String s1 = chatInbox.getText();
            if(!s1.equals(""))
            {
                if(s1.equalsIgnoreCase("/PING"))
                {
                    con._ping();
                } else
                {
                    if(s1.length() > 80)
                        s1 = s1.substring(0, 80) + "... <ciach!>";
                    con._tableSay(-1, 0, s1);
                }
                chatInbox.setText("");
            }
        }
        return false;
    }

    void addPlayer(String s)
    {
        int i = pList.countItems();
        int j;
        for(j = 0; j < i && s.compareTo(pList.getItem(j)) > 0; j++);
        pList.addItem(s, j);
        if(firstItem)
        {
            pList.select(j);
            firstItem = false;
        }
        pList.getSelectedItem();
        nop++;
        pres.setText(_German.CONNECTED + " (" + nop + ") :");
    }

    void gameMoveProp(int i, int j, int ai[])
    {
        if(gframe[i] != null && gframe[i].gameId == j)
            gframe[i].moveProp(ai);
    }

    void gameMover(int i, int j, int k, int l)
    {
        if(gframe[i] != null && gframe[i].gameId == j)
            gframe[i].gameMover(k, l);
    }

    void gameNewPoints(int i, int j, int k, int l)
    {
        if(gframe[i] != null && gframe[i].gameId == j)
            gframe[i].newPoints(k, l);
    }

    void gameNewTime(int i, int j, int k)
    {
        if(gframe[i] != null && gframe[i].gameId == j)
            gframe[i].newTime(k);
    }

    void gameOver(int i, int j)
    {
        tc[i].finishedGame();
        if(gframe[i] != null && gframe[i].gameId == j)
            gframe[i].gameOver();
    }

    void gameStart(int i, int j, int k, String as[], int ai[])
    {
        if(gframe[i] != null && gframe[i].gameId == j)
            gframe[i].startGame(k, as, ai);
        tc[i].startGame();
    }

    void getDisposed()
    {
        for(int i = 0; i < 10; i++)
            if(gframe[i] != null)
                gframe[i].shutdown();

        dispose();
    }

    public boolean handleEvent(Event event)
    {
        switch(event.id)
        {
        case 701: // Event.LIST_SELECT
            String s = pList.getItem(((Integer)event.arg).intValue());
            ic.setState(ignorants.contains(s));
            return true;

        case 201: // Event.WINDOW_DESTROY
            for(int i = 0; i < 10; i++)
                if(gframe[i] != null)
                    gframe[i].shutdown();

            con._kickMeOut();
            dispose();
            return true;

        case 601: // Event.SCROLL_LINE_UP
        case 602: // Event.SCROLL_LINE_DOWN
        case 603: // Event.SCROLL_PAGE_UP
        case 604: // Event.SCROLL_PAGE_DOWN
        case 605: // Event.SCROLL_ABSOLUTE
            int j = ((Integer)event.arg).intValue();
            scrollpan.move(0, -j);
            return true;
        }
        return super.handleEvent(event);
    }

    void moveAccepted(int i, int j, boolean flag)
    {
        if(gframe[i] != null && gframe[i].gameId == j)
            gframe[i].moveAccepted(flag);
    }

    void newBricks(int i, int j, int ai[])
    {
        if(gframe[i] != null && gframe[i].gameId == j)
            gframe[i].newBricks(ai);
    }

    void removePlayer(String s)
    {
        int i = pList.countItems();
        for(int j = 0; j < i; j++)
        {
            if(!s.equals(pList.getItem(j)))
                continue;
            pList.delItem(j);
            break;
        }

        String s1 = pList.getSelectedItem();
        if(s1 != null)
            ic.setState(ignorants.contains(s1));
        else
            ic.setState(false);
        nop--;
        pres.setText(_German.CONNECTED + " (" + nop + ") :");
    }

    void say(int i, int j, String s)
    {
        if(s.length() > 0 && s.charAt(0) == '<')
        {
            for(int k = s.indexOf("> ", 0); k != -1; k = s.indexOf("> ", k))
            {
                if(ignorants.contains(s.substring(1, k)))
                    return;
                k += 2;
            }

        }
        if(i == -1)
        {
            if(firstMessage)
            {
                chatBoard.appendText("\n*** " + _German.INFO_PREFIX + _German.version + ", " + s);
                firstMessage = false;
            } else
            {
                chatBoard.appendText("\n" + s);
            }
        } else
        if(gframe[i] != null && gframe[i].gameId == j)
            gframe[i].display(s);
    }

    public static void staticInit(Applet applet)
    {
        logoImage = Const.getImage("logo.gif");
        iconImage = Const.getImage("icon.gif");
    }

    void tableCreationAllowance(boolean flag)
    {
        if(flag && con.ownTable == -1)
            ntb.enable();
        else
            ntb.disable();
    }

    void tableHost(int i, int j, int k, String s)
    {
        if(gframe[i] != null && gframe[i].gameId == j)
            gframe[i].newHost(k, s);
    }

    void tableJoin(int i, int j, String s, int k)
    {
        if(!tc[i].alreadyStarted)
        {
            tc[i].addPlayer(s, k);
            if(gframe[i] != null && gframe[i].gameId == j)
                gframe[i].addPlayer(s, k);
        }
    }

    void tableLeave(int i, int j, int k)
    {
        tc[i].removePlayer(k);
        if(gframe[i] != null && gframe[i].gameId == j)
            gframe[i].removePlayer(k);
    }

    void tableStart(int i, int j, String as[], int k, int l)
    {
        gcontrol[i] = new GameControl(k, l, i, j, con);
        gframe[i] = new GameFrame(gcontrol[i], con, i, j, as);
    }

    void tableStop(int i, int j)
    {
        gcontrol[i] = null;
        if(gframe[i] != null)
            gframe[i] = null;
        tc[i].stopGame();
    }

    static final int NOT = 25;
    static final int moveit = 20;
    Connection con;
    C2S prot;
    int nop;
    Panel scrollpan;
    GameControl gcontrol[];
    GameFrame gframe[];
    TableComponent tc[];
    Label pres;
    java.awt.List pList;
    TextArea chatBoard;
    TextField chatInbox;
    Button bf;
    Button ntb;
    Choice mc;
    Choice gc;
    Checkbox ic;
    Checkbox pt;
    Checkbox tt;
    boolean firstMessage;
    static Image logoImage;
    static Image iconImage;
    static Color lightRed = new Color(255, 235, 235);
    static Color lightBlue = new Color(235, 235, 255);
    boolean firstItem;
    Vector ignorants;

}
