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 GuiScreenServerList extends GuiScreen
009    {
010        /**
011         * Remembers the last hostname or IP address entered into text field between invocations of the GUI.
012         */
013        private static String lastServerName = "";
014    
015        /** Needed a change as a local variable was conflicting on construct */
016        private final GuiScreen guiScreen;
017        private final ServerData field_73993_c;
018        private GuiTextField serverTextField;
019    
020        public GuiScreenServerList(GuiScreen par1GuiScreen, ServerData par2ServerData)
021        {
022            this.guiScreen = par1GuiScreen;
023            this.field_73993_c = par2ServerData;
024        }
025    
026        /**
027         * Called from the main game loop to update the screen.
028         */
029        public void updateScreen()
030        {
031            this.serverTextField.updateCursorCounter();
032        }
033    
034        /**
035         * Adds the buttons (and other controls) to the screen in question.
036         */
037        public void initGui()
038        {
039            StringTranslate var1 = StringTranslate.getInstance();
040            Keyboard.enableRepeatEvents(true);
041            this.controlList.clear();
042            this.controlList.add(new GuiButton(0, this.width / 2 - 100, this.height / 4 + 96 + 12, var1.translateKey("selectServer.select")));
043            this.controlList.add(new GuiButton(1, this.width / 2 - 100, this.height / 4 + 120 + 12, var1.translateKey("gui.cancel")));
044            this.serverTextField = new GuiTextField(this.fontRenderer, this.width / 2 - 100, 116, 200, 20);
045            this.serverTextField.setMaxStringLength(128);
046            this.serverTextField.setFocused(true);
047            this.serverTextField.setText(lastServerName);
048            ((GuiButton)this.controlList.get(0)).enabled = this.serverTextField.getText().length() > 0 && this.serverTextField.getText().split(":").length > 0;
049        }
050    
051        /**
052         * Called when the screen is unloaded. Used to disable keyboard repeat events
053         */
054        public void onGuiClosed()
055        {
056            Keyboard.enableRepeatEvents(false);
057            lastServerName = this.serverTextField.getText();
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.guiScreen.confirmClicked(false, 0);
070                }
071                else if (par1GuiButton.id == 0)
072                {
073                    this.field_73993_c.serverIP = this.serverTextField.getText();
074                    this.guiScreen.confirmClicked(true, 0);
075                }
076            }
077        }
078    
079        /**
080         * Fired when a key is typed. This is the equivalent of KeyListener.keyTyped(KeyEvent e).
081         */
082        protected void keyTyped(char par1, int par2)
083        {
084            if (this.serverTextField.textboxKeyTyped(par1, par2))
085            {
086                ((GuiButton)this.controlList.get(0)).enabled = this.serverTextField.getText().length() > 0 && this.serverTextField.getText().split(":").length > 0;
087            }
088            else if (par2 == 28)
089            {
090                this.actionPerformed((GuiButton)this.controlList.get(0));
091            }
092        }
093    
094        /**
095         * Called when the mouse is clicked.
096         */
097        protected void mouseClicked(int par1, int par2, int par3)
098        {
099            super.mouseClicked(par1, par2, par3);
100            this.serverTextField.mouseClicked(par1, par2, par3);
101        }
102    
103        /**
104         * Draws the screen and all the components in it.
105         */
106        public void drawScreen(int par1, int par2, float par3)
107        {
108            StringTranslate var4 = StringTranslate.getInstance();
109            this.drawDefaultBackground();
110            this.drawCenteredString(this.fontRenderer, var4.translateKey("selectServer.direct"), this.width / 2, this.height / 4 - 60 + 20, 16777215);
111            this.drawString(this.fontRenderer, var4.translateKey("addServer.enterIp"), this.width / 2 - 100, 100, 10526880);
112            this.serverTextField.drawTextBox();
113            super.drawScreen(par1, par2, par3);
114        }
115    }