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.controlList.add(new GuiButton(2, this.width / 2 - 100, 142, var1.translateKey("addServer.hideAddress") + ": " + (this.newServerData.func_82820_d() ? var1.translateKey("gui.yes") : var1.translateKey("gui.no"))));
044 this.serverName = new GuiTextField(this.fontRenderer, this.width / 2 - 100, 66, 200, 20);
045 this.serverName.setFocused(true);
046 this.serverName.setText(this.newServerData.serverName);
047 this.serverAddress = new GuiTextField(this.fontRenderer, this.width / 2 - 100, 106, 200, 20);
048 this.serverAddress.setMaxStringLength(128);
049 this.serverAddress.setText(this.newServerData.serverIP);
050 ((GuiButton)this.controlList.get(0)).enabled = this.serverAddress.getText().length() > 0 && this.serverAddress.getText().split(":").length > 0 && this.serverName.getText().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 }
060
061 /**
062 * Fired when a control is clicked. This is the equivalent of ActionListener.actionPerformed(ActionEvent e).
063 */
064 protected void actionPerformed(GuiButton par1GuiButton)
065 {
066 if (par1GuiButton.enabled)
067 {
068 if (par1GuiButton.id == 1)
069 {
070 this.parentGui.confirmClicked(false, 0);
071 }
072 else if (par1GuiButton.id == 0)
073 {
074 this.newServerData.serverName = this.serverName.getText();
075 this.newServerData.serverIP = this.serverAddress.getText();
076 this.parentGui.confirmClicked(true, 0);
077 }
078 else if (par1GuiButton.id == 2)
079 {
080 StringTranslate var2 = StringTranslate.getInstance();
081 this.newServerData.func_82819_b(!this.newServerData.func_82820_d());
082 ((GuiButton)this.controlList.get(2)).displayString = var2.translateKey("addServer.hideAddress") + ": " + (this.newServerData.func_82820_d() ? var2.translateKey("gui.yes") : var2.translateKey("gui.no"));
083 }
084 }
085 }
086
087 /**
088 * Fired when a key is typed. This is the equivalent of KeyListener.keyTyped(KeyEvent e).
089 */
090 protected void keyTyped(char par1, int par2)
091 {
092 this.serverName.textboxKeyTyped(par1, par2);
093 this.serverAddress.textboxKeyTyped(par1, par2);
094
095 if (par1 == 9)
096 {
097 if (this.serverName.isFocused())
098 {
099 this.serverName.setFocused(false);
100 this.serverAddress.setFocused(true);
101 }
102 else
103 {
104 this.serverName.setFocused(true);
105 this.serverAddress.setFocused(false);
106 }
107 }
108
109 if (par1 == 13)
110 {
111 this.actionPerformed((GuiButton)this.controlList.get(0));
112 }
113
114 ((GuiButton)this.controlList.get(0)).enabled = this.serverAddress.getText().length() > 0 && this.serverAddress.getText().split(":").length > 0 && this.serverName.getText().length() > 0;
115
116 if (((GuiButton)this.controlList.get(0)).enabled)
117 {
118 String var3 = this.serverAddress.getText().trim();
119 String[] var4 = var3.split(":");
120
121 if (var4.length > 2)
122 {
123 ((GuiButton)this.controlList.get(0)).enabled = false;
124 }
125 }
126 }
127
128 /**
129 * Called when the mouse is clicked.
130 */
131 protected void mouseClicked(int par1, int par2, int par3)
132 {
133 super.mouseClicked(par1, par2, par3);
134 this.serverAddress.mouseClicked(par1, par2, par3);
135 this.serverName.mouseClicked(par1, par2, par3);
136 }
137
138 /**
139 * Draws the screen and all the components in it.
140 */
141 public void drawScreen(int par1, int par2, float par3)
142 {
143 StringTranslate var4 = StringTranslate.getInstance();
144 this.drawDefaultBackground();
145 this.drawCenteredString(this.fontRenderer, var4.translateKey("addServer.title"), this.width / 2, 17, 16777215);
146 this.drawString(this.fontRenderer, var4.translateKey("addServer.enterName"), this.width / 2 - 100, 53, 10526880);
147 this.drawString(this.fontRenderer, var4.translateKey("addServer.enterIp"), this.width / 2 - 100, 94, 10526880);
148 this.serverName.drawTextBox();
149 this.serverAddress.drawTextBox();
150 super.drawScreen(par1, par2, par3);
151 }
152 }