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