Crossfire JXClient, Trunk
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) 2005-2008 Yann Chachkoff
19  * Copyright (C) 2006-2017,2019-2023 Andreas Kirschbaum
20  * Copyright (C) 2010-2012,2014-2018,2020-2023 Nicolas Weeger
21  */
22 
23 package com.realtime.crossfire.jxclient.gui.commands;
24 
33 import org.jetbrains.annotations.NotNull;
34 
39 public class AccountPasswordCommand implements GUICommand {
40 
44  @NotNull
46 
50  @NotNull
51  private final AbstractGUIElement element;
52 
56  @NotNull
57  private final GuiFactory guiFactory;
58 
66  public AccountPasswordCommand(@NotNull final CommandCallback commandCallback, @NotNull final AbstractGUIElement button, @NotNull final GuiFactory guiFactory) {
67  this.commandCallback = commandCallback;
68  element = button;
69  this.guiFactory = guiFactory;
70  }
71 
72  @Override
73  public boolean canExecute() {
74  return true;
75  }
76 
77  @Override
78  public void execute() {
79  final Gui gui = guiFactory.getGui(element);
80  if (gui == null) {
81  return;
82  }
83 
84  final GUIText currentPasswordField = gui.getFirstElement(GUIText.class, "account_password_current");
85  final GUIText newPasswordField = gui.getFirstElement(GUIText.class, "account_password_new");
86  final GUIText confirmPasswordField = gui.getFirstElement(GUIText.class, "account_password_confirm");
87 
88  if (currentPasswordField == null || newPasswordField == null || confirmPasswordField == null) {
89  return;
90  }
91 
92  final String currentPassword = currentPasswordField.getText();
93  final String newPassword = newPasswordField.getText();
94  final String confirmPassword = confirmPasswordField.getText();
95 
96  if (newPassword.isEmpty()) {
97  final AbstractLabel error = gui.getFirstElement(GUILabelFailure.class, "account_password_error");
98  if (error != null) {
99  error.setText("Can't have an empty password!");
100  }
101  return;
102  }
103  if (!confirmPassword.equals(newPassword)) {
104  final AbstractLabel error = gui.getFirstElement(GUILabelFailure.class, "account_password_error");
105  if (error != null) {
106  error.setText("Passwords don't match!");
107  }
108  return;
109  }
110 
111  commandCallback.accountPassword(currentPassword, newPassword);
112  }
113 
114 }
com.realtime.crossfire.jxclient.gui.textinput.GUIText
Abstract base class for text input fields.
Definition: GUIText.java:61
com.realtime.crossfire.jxclient
com.realtime.crossfire.jxclient.skin.skin
Definition: DefaultJXCSkin.java:23
com.realtime.crossfire.jxclient.gui.gui.Gui
Combines a list of GUIElements to for a gui.
Definition: Gui.java:49
com.realtime.crossfire.jxclient.skin.skin.GuiFactory.getGui
Gui getGui(@NotNull final AbstractGUIElement element)
Returns the Gui an element is part of.
Definition: GuiFactory.java:110
com.realtime.crossfire.jxclient.gui.commandlist.GUICommand
An executable command.
Definition: GUICommand.java:29
com.realtime.crossfire.jxclient.gui.commands.AccountPasswordCommand.execute
void execute()
Executes the command.
Definition: AccountPasswordCommand.java:78
com.realtime.crossfire.jxclient.skin
com.realtime.crossfire.jxclient.gui.commands.AccountPasswordCommand.commandCallback
final CommandCallback commandCallback
The CommandCallback to use.
Definition: AccountPasswordCommand.java:45
com.realtime.crossfire.jxclient.gui.commands.AccountPasswordCommand
A GUICommand sending an account password change request.
Definition: AccountPasswordCommand.java:39
com.realtime.crossfire.jxclient.gui.label
Definition: AbstractLabel.java:23
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement
Abstract base class for GUI elements to be shown in Guis.
Definition: AbstractGUIElement.java:37
com.realtime.crossfire.jxclient.gui.label.AbstractLabel
Abstract base class for all label classes.
Definition: AbstractLabel.java:43
com.realtime.crossfire.jxclient.gui.textinput
Definition: ActivateCommandInputCommand.java:23
com.realtime.crossfire.jxclient.gui.textinput.CommandCallback.accountPassword
void accountPassword(@NotNull String currentPassword, @NotNull String newPassword)
Change the account password.
com.realtime.crossfire.jxclient.gui.commandlist
Definition: CommandList.java:23
com.realtime.crossfire.jxclient.gui.commands.AccountPasswordCommand.guiFactory
final GuiFactory guiFactory
The global GuiFactory instance.
Definition: AccountPasswordCommand.java:57
com.realtime.crossfire.jxclient.gui.commands.AccountPasswordCommand.AccountPasswordCommand
AccountPasswordCommand(@NotNull final CommandCallback commandCallback, @NotNull final AbstractGUIElement button, @NotNull final GuiFactory guiFactory)
Creates a new instance.
Definition: AccountPasswordCommand.java:66
com.realtime.crossfire.jxclient.gui.label.AbstractLabel.setText
void setText(@NotNull final String text)
Sets the label text.
Definition: AbstractLabel.java:120
com.realtime.crossfire.jxclient.gui
com.realtime.crossfire.jxclient.gui.commands.AccountPasswordCommand.canExecute
boolean canExecute()
Returns whether this command may be executed.
Definition: AccountPasswordCommand.java:73
com.realtime.crossfire.jxclient.skin.skin.GuiFactory
Factory for creating Gui instances.
Definition: GuiFactory.java:41
com.realtime.crossfire.jxclient.gui.gui
Definition: AbstractGUIElement.java:23
com.realtime.crossfire
com.realtime.crossfire.jxclient.gui.label.GUILabelFailure
A GUIHTMLLabel that displays the last received "failure" message.
Definition: GUILabelFailure.java:39
com.realtime.crossfire.jxclient.gui.textinput.CommandCallback
Interface that defines callback functions needed by commands.
Definition: CommandCallback.java:33
com.realtime
com
com.realtime.crossfire.jxclient.gui.gui.Gui.getFirstElement
public< T extends GUIElement > T getFirstElement(@NotNull final Class< T > class_)
Returns the first gui element of this gui belonging to the given class.
Definition: Gui.java:291
com.realtime.crossfire.jxclient.gui.textinput.GUIText.getText
String getText()
Returns the entered text.
Definition: GUIText.java:234
com.realtime.crossfire.jxclient.gui.commands.AccountPasswordCommand.element
final AbstractGUIElement element
The AbstractGUIElement to find the Gui containing the fields.
Definition: AccountPasswordCommand.java:51