001package net.minecraft.client.gui;
002
003import cpw.mods.fml.relauncher.Side;
004import cpw.mods.fml.relauncher.SideOnly;
005import net.minecraft.client.multiplayer.ServerData;
006import net.minecraft.util.StringTranslate;
007import org.lwjgl.input.Keyboard;
008
009@SideOnly(Side.CLIENT)
010public class GuiScreenServerList extends GuiScreen
011{
012    /** Needed a change as a local variable was conflicting on construct */
013    private final GuiScreen guiScreen;
014
015    /** Instance of ServerData. */
016    private final ServerData theServerData;
017    private GuiTextField serverTextField;
018
019    public GuiScreenServerList(GuiScreen par1GuiScreen, ServerData par2ServerData)
020    {
021        this.guiScreen = par1GuiScreen;
022        this.theServerData = par2ServerData;
023    }
024
025    /**
026     * Called from the main game loop to update the screen.
027     */
028    public void updateScreen()
029    {
030        this.serverTextField.updateCursorCounter();
031    }
032
033    /**
034     * Adds the buttons (and other controls) to the screen in question.
035     */
036    public void initGui()
037    {
038        StringTranslate stringtranslate = StringTranslate.getInstance();
039        Keyboard.enableRepeatEvents(true);
040        this.buttonList.clear();
041        this.buttonList.add(new GuiButton(0, this.width / 2 - 100, this.height / 4 + 96 + 12, stringtranslate.translateKey("selectServer.select")));
042        this.buttonList.add(new GuiButton(1, this.width / 2 - 100, this.height / 4 + 120 + 12, stringtranslate.translateKey("gui.cancel")));
043        this.serverTextField = new GuiTextField(this.fontRenderer, this.width / 2 - 100, 116, 200, 20);
044        this.serverTextField.setMaxStringLength(128);
045        this.serverTextField.setFocused(true);
046        this.serverTextField.setText(this.mc.gameSettings.lastServer);
047        ((GuiButton)this.buttonList.get(0)).enabled = this.serverTextField.getText().length() > 0 && this.serverTextField.getText().split(":").length > 0;
048    }
049
050    /**
051     * Called when the screen is unloaded. Used to disable keyboard repeat events
052     */
053    public void onGuiClosed()
054    {
055        Keyboard.enableRepeatEvents(false);
056        this.mc.gameSettings.lastServer = this.serverTextField.getText();
057        this.mc.gameSettings.saveOptions();
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.theServerData.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.buttonList.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.buttonList.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 stringtranslate = StringTranslate.getInstance();
109        this.drawDefaultBackground();
110        this.drawCenteredString(this.fontRenderer, stringtranslate.translateKey("selectServer.direct"), this.width / 2, this.height / 4 - 60 + 20, 16777215);
111        this.drawString(this.fontRenderer, stringtranslate.translateKey("addServer.enterIp"), this.width / 2 - 100, 100, 10526880);
112        this.serverTextField.drawTextBox();
113        super.drawScreen(par1, par2, par3);
114    }
115}