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