Crossfire JXClient, Trunk  R20561
AccountPasswordCommand.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) 2011 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 AccountPasswordCommand implements GUICommand {
38 
42  @NotNull
44 
48  @NotNull
49  private final Component element;
50 
57  public AccountPasswordCommand(@NotNull final CommandCallback commandCallback, @NotNull final Component button) {
58  this.commandCallback = commandCallback;
59  element = button;
60  }
61 
65  @Override
66  public boolean canExecute() {
67  return true;
68  }
69 
73  @Override
74  public void execute() {
75  final Gui gui = GuiUtils.getGui(element);
76  if (gui == null) {
77  return;
78  }
79 
80  final GUIText currentPasswordField = gui.getFirstElement(GUIText.class, "account_password_current");
81  final GUIText newPasswordField = gui.getFirstElement(GUIText.class, "account_password_new");
82  final GUIText confirmPasswordField = gui.getFirstElement(GUIText.class, "account_password_confirm");
83 
84  if (currentPasswordField == null || newPasswordField == null || confirmPasswordField == null) {
85  return;
86  }
87 
88  final String currentPassword = currentPasswordField.getText();
89  final String newPassword = newPasswordField.getText();
90  final String confirmPassword = confirmPasswordField.getText();
91 
92  if (currentPassword.isEmpty()) {
93  final AbstractLabel error = gui.getFirstElement(GUILabelFailure.class, "account_password_error");
94  if (error != null) {
95  error.setText("Please enter your current password!");
96  }
97  return;
98  }
99 
100  if (newPassword.isEmpty()) {
101  final AbstractLabel error = gui.getFirstElement(GUILabelFailure.class, "account_password_error");
102  if (error != null) {
103  error.setText("Can't have an empty password!");
104  }
105  return;
106  }
107  if (!confirmPassword.equals(newPassword)) {
108  final AbstractLabel error = gui.getFirstElement(GUILabelFailure.class, "account_password_error");
109  if (error != null) {
110  error.setText("Passwords don't match!");
111  }
112  return;
113  }
114 
115  commandCallback.accountPassword(currentPassword, newPassword);
116  }
117 
118 }
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.
boolean canExecute()
Returns whether this command may be executed.whether this command may be executed ...
Combines a list of GUIElements to for a gui.
Definition: Gui.java:43
AccountPasswordCommand(@NotNull final CommandCallback commandCallback, @NotNull final Component button)
Creates a new instance.
String getText()
Returns the entered text.
Definition: GUIText.java:216
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.
A GUICommand sending an account password change request.
void execute()
Executes the command.Does nothing if called while canExecute() returnsfalse .
final Component element
The Component to find the Gui containing the fields.
void accountPassword(@NotNull String currentPassword, @NotNull String newPassword)
Change the account password.
final CommandCallback commandCallback
The CommandCallback to use.
Abstract base class for text input fields.
Definition: GUIText.java:57