Crossfire JXClient, Trunk  R20561
AccountCreateCommand.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2010 Nicolas Weeger.
19  */
20 
21 package com.realtime.crossfire.jxclient.gui.commands;
22 
30 import java.awt.Component;
31 import org.jetbrains.annotations.NotNull;
32 
37 public class AccountCreateCommand implements GUICommand {
38 
42  @NotNull
44 
48  @NotNull
49  private final Component element;
50 
56  public AccountCreateCommand(@NotNull final CommandCallback commandCallback, @NotNull final Component button) {
57  this.commandCallback = commandCallback;
58  element = button;
59  }
60 
64  @Override
65  public boolean canExecute() {
66  return true;
67  }
68 
72  @Override
73  public void execute() {
74  final Gui gui = GuiUtils.getGui(element);
75  if (gui == null) {
76  return;
77  }
78 
79  final GUIText loginField = gui.getFirstElement(GUIText.class, "account_login");
80  final GUIText passwordField = gui.getFirstElement(GUIText.class, "account_password");
81  final GUIText confirmField = gui.getFirstElement(GUIText.class, "account_password_confirm");
82 
83  if (loginField == null || passwordField == null || confirmField == null) {
84  return;
85  }
86 
87  final String login = loginField.getText();
88  final String password = passwordField.getText();
89  final String confirm = confirmField.getText();
90 
91  if (login.isEmpty()) {
92  final AbstractLabel error = gui.getFirstElement(GUILabelFailure.class, "account_create_error");
93  if (error != null) {
94  error.setText("Can't have an empty login!");
95  }
96  loginField.setActive(true);
97  return;
98  }
99 
100  if (password.isEmpty()) {
101  final AbstractLabel error = gui.getFirstElement(GUILabelFailure.class, "account_create_error");
102  if (error != null) {
103  error.setText("Can't have an empty password!");
104  }
105  confirmField.setText("");
106  passwordField.setActive(true);
107  return;
108  }
109  if (confirm.isEmpty()) {
110  confirmField.setActive(true);
111  return;
112  }
113  if (!confirm.equals(password)) {
114  final AbstractLabel error = gui.getFirstElement(GUILabelFailure.class, "account_create_error");
115  if (error != null) {
116  error.setText("Passwords don't match!");
117  }
118  passwordField.setText("");
119  confirmField.setText("");
120  passwordField.setActive(true);
121  return;
122  }
123 
124  commandCallback.accountCreate(login, password);
125  }
126 
127 }
Abstract base class for all label classes.
static Gui getGui(@NotNull final Component element)
Returns the Gui an element is part of.
Definition: GuiUtils.java:91
void setText(@NotNull final String text)
The label text.
Combines a list of GUIElements to for a gui.
Definition: Gui.java:43
void execute()
Executes the command.Does nothing if called while canExecute() returnsfalse .
AccountCreateCommand(@NotNull final CommandCallback commandCallback, @NotNull final Component button)
Creates a new instance.
void setActive(final boolean active)
Sets the active state of a GUI element.
String getText()
Returns the entered text.
Definition: GUIText.java:216
final CommandCallback commandCallback
The CommandCallback to use.
A GUIHTMLLabel that displays the last received "failure" message.
Utility class for Gui related functions.
Definition: GuiUtils.java:35
Interface that defines callback functions needed by commands.
boolean canExecute()
Returns whether this command may be executed.whether this command may be executed ...
final Component element
The Component to find information for account creation.
void setText(@NotNull final String text)
Sets the entered text.
Definition: GUIText.java:205
A GUICommand sending an account creation request.
void accountCreate(@NotNull String login, @NotNull String password)
Creates an account.
Abstract base class for text input fields.
Definition: GUIText.java:57