    001    package net.minecraft.src;
    002
    003    import cpw.mods.fml.common.Side;
    004    import cpw.mods.fml.common.asm.SideOnly;
    005    import org.lwjgl.input.Keyboard;
    006
    007    @SideOnly(Side.CLIENT)
    008    public class GuiScreenAddServer extends GuiScreen
    009    {
    010        /** This GUI's parent GUI. */
    011        private GuiScreen parentGui;
    012        private GuiTextField serverAddress;
    013        private GuiTextField serverName;
    014
    015        /** ServerData to be modified by this GUI */
    016        private ServerData newServerData;
    017
    018        public GuiScreenAddServer(GuiScreen par1GuiScreen, ServerData par2ServerData)
    019        {
    020            this.parentGui = par1GuiScreen;
    021            this.newServerData = par2ServerData;
    022        }
    023
    024        /**
    025         * Called from the main game loop to update the screen.
    026         */
    027        public void updateScreen()
    028        {
    029            this.serverName.updateCursorCounter();
    030            this.serverAddress.updateCursorCounter();
    031        }
    032
    033        /**
    034         * Adds the buttons (and other controls) to the screen in question.
    035         */
    036        public void initGui()
    037        {
    038            StringTranslate var1 = StringTranslate.getInstance();
    039            Keyboard.enableRepeatEvents(true);
    040            this.controlList.clear();
    041            this.controlList.add(new GuiButton(0, this.width / 2 - 100, this.height / 4 + 96 + 12, var1.translateKey("addServer.add")));
    042            this.controlList.add(new GuiButton(1, this.width / 2 - 100, this.height / 4 + 120 + 12, var1.translateKey("gui.cancel")));
    043            this.serverName = new GuiTextField(this.fontRenderer, this.width / 2 - 100, 76, 200, 20);
    044            this.serverName.setFocused(true);
    045            this.serverName.setText(this.newServerData.serverName);
    046            this.serverAddress = new GuiTextField(this.fontRenderer, this.width / 2 - 100, 116, 200, 20);
    047            this.serverAddress.setMaxStringLength(128);
    048            this.serverAddress.setText(this.newServerData.serverIP);
    049            ((GuiButton)this.controlList.get(0)).enabled = this.serverAddress.getText().length() > 0 && this.serverAddress.getText().split(":").length > 0 && this.serverName.getText().length() > 0;
    050        }
    051
    052        /**
    053         * Called when the screen is unloaded. Used to disable keyboard repeat events
    054         */
    055        public void onGuiClosed()
    056        {
    057            Keyboard.enableRepeatEvents(false);
    058        }
    059
    060        /**
    061         * Fired when a control is clicked. This is the equivalent of ActionListener.actionPerformed(ActionEvent e).
    062         */
    063        protected void actionPerformed(GuiButton par1GuiButton)
    064        {
    065            if (par1GuiButton.enabled)
    066            {
    067                if (par1GuiButton.id == 1)
    068                {
    069                    this.parentGui.confirmClicked(false, 0);
    070                }
    071                else if (par1GuiButton.id == 0)
    072                {
    073                    this.newServerData.serverName = this.serverName.getText();
    074                    this.newServerData.serverIP = this.serverAddress.getText();
    075                    this.parentGui.confirmClicked(true, 0);
    076                }
    077            }
    078        }
    079
    080        /**
    081         * Fired when a key is typed. This is the equivalent of KeyListener.keyTyped(KeyEvent e).
    082         */
    083        protected void keyTyped(char par1, int par2)
    084        {
    085            this.serverName.textboxKeyTyped(par1, par2);
    086            this.serverAddress.textboxKeyTyped(par1, par2);
    087
    088            if (par1 == 9)
    089            {
    090                if (this.serverName.isFocused())
    091                {
    092                    this.serverName.setFocused(false);
    093                    this.serverAddress.setFocused(true);
    094                }
    095                else
    096                {
    097                    this.serverName.setFocused(true);
    098                    this.serverAddress.setFocused(false);
    099                }
    100            }
    101
    102            if (par1 == 13)
    103            {
    104                this.actionPerformed((GuiButton)this.controlList.get(0));
    105            }
    106
    107            ((GuiButton)this.controlList.get(0)).enabled = this.serverAddress.getText().length() > 0 && this.serverAddress.getText().split(":").length > 0 && this.serverName.getText().length() > 0;
    108
    109            if (((GuiButton)this.controlList.get(0)).enabled)
    110            {
    111                String var3 = this.serverAddress.getText().trim();
    112                String[] var4 = var3.split(":");
    113
    114                if (var4.length > 2)
    115                {
    116                    ((GuiButton)this.controlList.get(0)).enabled = false;
    117                }
    118            }
    119        }
    120
    121        /**
    122         * Called when the mouse is clicked.
    123         */
    124        protected void mouseClicked(int par1, int par2, int par3)
    125        {
    126            super.mouseClicked(par1, par2, par3);
    127            this.serverAddress.mouseClicked(par1, par2, par3);
    128            this.serverName.mouseClicked(par1, par2, par3);
    129        }
    130
    131        /**
    132         * Draws the screen and all the components in it.
    133         */
    134        public void drawScreen(int par1, int par2, float par3)
    135        {
    136            StringTranslate var4 = StringTranslate.getInstance();
    137            this.drawDefaultBackground();
    138            this.drawCenteredString(this.fontRenderer, var4.translateKey("addServer.title"), this.width / 2, this.height / 4 - 60 + 20, 16777215);
    139            this.drawString(this.fontRenderer, var4.translateKey("addServer.enterName"), this.width / 2 - 100, 63, 10526880);
    140            this.drawString(this.fontRenderer, var4.translateKey("addServer.enterIp"), this.width / 2 - 100, 104, 10526880);
    141            this.serverName.drawTextBox();
    142            this.serverAddress.drawTextBox();
    143            super.drawScreen(par1, par2, par3);
    144        }
    145    }