Crossfire JXClient, Trunk
AbstractButton.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.button;
24 
35 import java.awt.Dimension;
36 import java.awt.event.ActionListener;
37 import java.awt.event.KeyEvent;
38 import java.awt.event.MouseEvent;
39 import javax.swing.Timer;
40 import org.jetbrains.annotations.NotNull;
41 import org.jetbrains.annotations.Nullable;
42 
47 public abstract class AbstractButton extends ActivatableGUIElement implements KeyPressedHandler {
48 
52  private static final long serialVersionUID = 1;
53 
57  private static final int TIMEOUT_FIRST = 350;
58 
62  private static final int TIMEOUT_SECOND = 80;
63 
67  private final boolean autoRepeat;
68 
72  @NotNull
73  private final CommandList commandList;
74 
78  @NotNull
79  private final NewCharModel newCharModel;
80 
84  @NotNull
85  private final ActionListener timeoutEvent = e -> execute();
86 
90  @NotNull
91  private final Timer timer = new Timer(TIMEOUT_FIRST, timeoutEvent);
92 
96  @NotNull
97  private final NewCharModelListener listener = this::updateEnabled;
98 
111  protected AbstractButton(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, final boolean opaque, final boolean autoRepeat, @NotNull final CommandList commandList, @NotNull final GuiFactory guiFactory, @NotNull final NewCharModel newCharModel) {
112  super(tooltipManager, elementListener, name, opaque, guiFactory);
113  this.autoRepeat = autoRepeat;
114  this.commandList = commandList;
115  this.newCharModel = newCharModel;
116  timer.setDelay(TIMEOUT_SECOND);
117 
120  updateEnabled();
121  }
122  }
123 
124  @Override
125  public void dispose() {
126  super.dispose();
127 
130  }
131  }
132 
133  @Override
134  public void mouseClicked(@NotNull final MouseEvent e) {
135  super.mouseClicked(e);
136 
137  if (!isEnabled()) {
138  return;
139  }
140 
141  final int b = e.getButton();
142  switch (b) {
143  case MouseEvent.BUTTON1:
144  if (!autoRepeat) {
145  execute();
146  }
147  setActive(false);
148  break;
149 
150  case MouseEvent.BUTTON2:
151  break;
152 
153  case MouseEvent.BUTTON3:
154  break;
155  }
156  }
157 
158  @Override
159  public void mouseReleased(@NotNull final MouseEvent e) {
160  super.mouseReleased(e);
161 
162  if (!isEnabled()) {
163  return;
164  }
165 
166  final int b = e.getButton();
167  switch (b) {
168  case MouseEvent.BUTTON1:
169  if (autoRepeat) {
170  timer.stop();
171  }
172  setActive(false);
173  break;
174 
175  case MouseEvent.BUTTON2:
176  break;
177 
178  case MouseEvent.BUTTON3:
179  break;
180  }
181  }
182 
183  @Override
184  public void mousePressed(@NotNull final MouseEvent e) {
185  super.mousePressed(e);
186 
187  if (!isEnabled()) {
188  return;
189  }
190 
191  final int b = e.getButton();
192  switch (b) {
193  case MouseEvent.BUTTON1:
194  if (autoRepeat) {
195  execute();
196  timer.start();
197  }
198  break;
199 
200  case MouseEvent.BUTTON2:
201  break;
202 
203  case MouseEvent.BUTTON3:
204  break;
205  }
206  }
207 
208  @Override
209  public void mouseExited(@NotNull final MouseEvent e) {
210  super.mouseExited(e);
211 
212  if (!isEnabled()) {
213  return;
214  }
215 
216  if (autoRepeat) {
217  timer.stop();
218  }
219  setActive(false);
220  }
221 
222  @Override
223  public void execute() {
224  if (isEnabled()) {
226  }
227  }
228 
229  @Nullable
230  @Override
231  @SuppressWarnings("MethodDoesntCallSuperMethod")
232  public Dimension getPreferredSize() {
233  return getMinimumSizeInt();
234  }
235 
236  @Nullable
237  @Override
238  @SuppressWarnings("MethodDoesntCallSuperMethod")
239  public Dimension getMinimumSize() {
240  return getMinimumSizeInt();
241  }
242 
247  @NotNull
248  protected abstract Dimension getMinimumSizeInt();
249 
250  @Override
251  public boolean keyPressed(@NotNull final KeyEvent2 e) {
252  if (!isEnabled()) {
253  return false;
254  }
255 
256  switch (e.getKeyCode()) {
257  case KeyEvent.VK_SPACE:
258  case KeyEvent.VK_ENTER:
259  execute();
260  return true;
261  }
262 
263  return false;
264  }
265 
269  private void updateEnabled() {
271  }
272 
273 }
com.realtime.crossfire.jxclient
com.realtime.crossfire.jxclient.gui.commandlist.CommandList.containsCommand
boolean containsCommand(@NotNull final Class<? extends GUICommand > command)
Returns whether this command list contains a command of the given type.
Definition: CommandList.java:135
com.realtime.crossfire.jxclient.skin.skin
Definition: DefaultJXCSkin.java:23
com.realtime.crossfire.jxclient.gui.button.AbstractButton.AbstractButton
AbstractButton(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, final boolean opaque, final boolean autoRepeat, @NotNull final CommandList commandList, @NotNull final GuiFactory guiFactory, @NotNull final NewCharModel newCharModel)
Creates a new instance.
Definition: AbstractButton.java:111
com.realtime.crossfire.jxclient.gui.button.AbstractButton.commandList
final CommandList commandList
The commands to execute when the button is elected.
Definition: AbstractButton.java:73
com.realtime.crossfire.jxclient.skin
com.realtime.crossfire.jxclient.gui.button.AbstractButton.TIMEOUT_SECOND
static final int TIMEOUT_SECOND
The autorepeat delay for further repeats.
Definition: AbstractButton.java:62
com.realtime.crossfire.jxclient.gui.label.NewCharModel.addListener
void addListener(@NotNull final NewCharModelListener listener)
Adds a listener to notify of changes.
Definition: NewCharModel.java:208
com.realtime.crossfire.jxclient.gui.label
Definition: AbstractLabel.java:23
com.realtime.crossfire.jxclient.gui.button.AbstractButton.mouseExited
void mouseExited(@NotNull final MouseEvent e)
Will be called when the mouse has left the bounding box of this element.
Definition: AbstractButton.java:209
com.realtime.crossfire.jxclient.gui.commandlist
Definition: CommandList.java:23
com.realtime.crossfire.jxclient.gui.button.AbstractButton.newCharModel
final NewCharModel newCharModel
The global NewCharModel instance.
Definition: AbstractButton.java:79
com.realtime.crossfire.jxclient.gui.button.AbstractButton.mouseClicked
void mouseClicked(@NotNull final MouseEvent e)
Will be called when the user has clicked (pressed+released) this element.
Definition: AbstractButton.java:134
com.realtime.crossfire.jxclient.gui.gui.ActivatableGUIElement
A GUIElement that can be set to active or inactive.
Definition: ActivatableGUIElement.java:33
com.realtime.crossfire.jxclient.gui.keybindings
Definition: InvalidKeyBindingException.java:23
com.realtime.crossfire.jxclient.gui.keybindings.KeyEvent2
Represents a pressed or released key.
Definition: KeyEvent2.java:34
com.realtime.crossfire.jxclient.gui.button.AbstractButton.dispose
void dispose()
Releases all allocated resources.
Definition: AbstractButton.java:125
com.realtime.crossfire.jxclient.gui.label.NewCharModelListener
Interface for listeners interested in NewCharModel related changes.
Definition: NewCharModelListener.java:30
com.realtime.crossfire.jxclient.gui.button.AbstractButton.mouseReleased
void mouseReleased(@NotNull final MouseEvent e)
Will be called when the user has released the mouse.
Definition: AbstractButton.java:159
com.realtime.crossfire.jxclient.gui.button.AbstractButton.getPreferredSize
Dimension getPreferredSize()
Definition: AbstractButton.java:232
com.realtime.crossfire.jxclient.gui.button.AbstractButton.timeoutEvent
final ActionListener timeoutEvent
The ActionListener for generating autorepeat events.
Definition: AbstractButton.java:85
com.realtime.crossfire.jxclient.gui.button.AbstractButton.getMinimumSizeInt
abstract Dimension getMinimumSizeInt()
Returns the minimal size needed to display this component.
com.realtime.crossfire.jxclient.gui
com.realtime.crossfire.jxclient.gui.button.AbstractButton.autoRepeat
final boolean autoRepeat
Whether this button should autorepeat.
Definition: AbstractButton.java:67
com.realtime.crossfire.jxclient.gui.label.NewCharModel
General information for creating new characters.
Definition: NewCharModel.java:43
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.guiFactory
final GuiFactory guiFactory
The global GuiFactory instance.
Definition: AbstractGUIElement.java:48
com.realtime.crossfire.jxclient.gui.label.NewCharModel.removeListener
void removeListener(@NotNull final NewCharModelListener listener)
Removes a listener to be notified of changes.
Definition: NewCharModel.java:216
com.realtime.crossfire.jxclient.gui.gui.ActivatableGUIElement.elementListener
final GUIElementListener elementListener
The GUIElementListener to notify.
Definition: ActivatableGUIElement.java:44
com.realtime.crossfire.jxclient.gui.button.AbstractButton.updateEnabled
void updateEnabled()
Updates the enabled state.
Definition: AbstractButton.java:269
com.realtime.crossfire.jxclient.gui.gui.KeyPressedHandler
Interface for classes that may handle "pressed" key events.
Definition: KeyPressedHandler.java:32
com.realtime.crossfire.jxclient.gui.button.AbstractButton.getMinimumSize
Dimension getMinimumSize()
Definition: AbstractButton.java:239
com.realtime.crossfire.jxclient.skin.skin.GuiFactory
Factory for creating Gui instances.
Definition: GuiFactory.java:41
com.realtime.crossfire.jxclient.gui.commandlist.CommandList.execute
void execute()
Execute the command list by calling GUICommand#execute() for each command in order.
Definition: CommandList.java:99
com.realtime.crossfire.jxclient.gui.commandlist.CommandList
A list of GUICommand instances.
Definition: CommandList.java:34
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.name
final String name
The name of this element.
Definition: AbstractGUIElement.java:77
com.realtime.crossfire.jxclient.gui.gui
Definition: AbstractGUIElement.java:23
com.realtime.crossfire.jxclient.gui.button.AbstractButton.TIMEOUT_FIRST
static final int TIMEOUT_FIRST
The autorepeat delay initially.
Definition: AbstractButton.java:57
com.realtime.crossfire.jxclient.gui.button.AbstractButton.keyPressed
boolean keyPressed(@NotNull final KeyEvent2 e)
Invoked when a key has been pressed.
Definition: AbstractButton.java:251
com.realtime.crossfire
com.realtime.crossfire.jxclient.gui.gui.TooltipManager
Manages the tooltip display.
Definition: TooltipManager.java:33
com.realtime
com.realtime.crossfire.jxclient.gui.button.AbstractButton.timer
final Timer timer
The Timer for auto-repeating buttons.
Definition: AbstractButton.java:91
com
com.realtime.crossfire.jxclient.gui.commands.AccountCreateCharacterCommand
A GUICommand sending a character creation request.
Definition: AccountCreateCharacterCommand.java:43
com.realtime.crossfire.jxclient.gui.button.AbstractButton
Abstract base class for button classes.
Definition: AbstractButton.java:47
com.realtime.crossfire.jxclient.gui.button.AbstractButton.listener
final NewCharModelListener listener
The listener attached to newCharModel.
Definition: AbstractButton.java:97
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.tooltipManager
final TooltipManager tooltipManager
The TooltipManager to update.
Definition: AbstractGUIElement.java:83
com.realtime.crossfire.jxclient.gui.button.AbstractButton.execute
void execute()
Executes the actions associated with this GUI element.
Definition: AbstractButton.java:223
com.realtime.crossfire.jxclient.gui.button.AbstractButton.mousePressed
void mousePressed(@NotNull final MouseEvent e)
Will be called when the user has pressed the mouse inside this element.
Definition: AbstractButton.java:184
com.realtime.crossfire.jxclient.gui.commands
Definition: AccountCreateCharacterCommand.java:23
com.realtime.crossfire.jxclient.gui.button.AbstractButton.serialVersionUID
static final long serialVersionUID
The serial version UID.
Definition: AbstractButton.java:52
com.realtime.crossfire.jxclient.gui.gui.ActivatableGUIElement.setActive
void setActive(final boolean active)
Sets the active state of a GUI element.
Definition: ActivatableGUIElement.java:115
com.realtime.crossfire.jxclient.gui.label.NewCharModel.hasNonServerFailureErrorText
boolean hasNonServerFailureErrorText()
Returns the error text to show.
Definition: NewCharModel.java:405
com.realtime.crossfire.jxclient.gui.gui.GUIElementListener
Listener for GUIElement related events.
Definition: GUIElementListener.java:32