Crossfire JXClient, Trunk  R20561
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-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.gui.button;
23 
30 import java.awt.Dimension;
31 import java.awt.event.ActionListener;
32 import java.awt.event.KeyEvent;
33 import java.awt.event.MouseEvent;
34 import javax.swing.Timer;
35 import org.jetbrains.annotations.NotNull;
36 import org.jetbrains.annotations.Nullable;
37 
42 public abstract class AbstractButton extends ActivatableGUIElement implements KeyPressedHandler {
43 
47  private static final long serialVersionUID = 1;
48 
52  private static final int TIMEOUT_FIRST = 350;
53 
57  private static final int TIMEOUT_SECOND = 80;
58 
62  private final boolean autoRepeat;
63 
67  @NotNull
68  private final CommandList commandList;
69 
73  @NotNull
74  private final ActionListener timeoutEvent = e -> execute();
75 
79  @NotNull
80  private final Timer timer = new Timer(TIMEOUT_FIRST, timeoutEvent);
81 
92  protected AbstractButton(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, final int transparency, final boolean autoRepeat, @NotNull final CommandList commandList) {
93  super(tooltipManager, elementListener, name, transparency);
94  this.autoRepeat = autoRepeat;
95  this.commandList = commandList;
96  timer.setDelay(TIMEOUT_SECOND);
97  }
98 
102  @Override
103  public void mouseClicked(@NotNull final MouseEvent e) {
104  super.mouseClicked(e);
105  final int b = e.getButton();
106  switch (b) {
107  case MouseEvent.BUTTON1:
108  if (!autoRepeat) {
109  execute();
110  }
111  setActive(false);
112  break;
113 
114  case MouseEvent.BUTTON2:
115  break;
116 
117  case MouseEvent.BUTTON3:
118  break;
119  }
120  }
121 
125  @Override
126  public void mouseReleased(@NotNull final MouseEvent e) {
127  super.mouseReleased(e);
128  final int b = e.getButton();
129  switch (b) {
130  case MouseEvent.BUTTON1:
131  if (autoRepeat) {
132  timer.stop();
133  }
134  setActive(false);
135  break;
136 
137  case MouseEvent.BUTTON2:
138  break;
139 
140  case MouseEvent.BUTTON3:
141  break;
142  }
143  }
144 
148  @Override
149  public void mousePressed(@NotNull final MouseEvent e) {
150  super.mousePressed(e);
151  final int b = e.getButton();
152  switch (b) {
153  case MouseEvent.BUTTON1:
154  setActive(true);
155  if (autoRepeat) {
156  execute();
157  timer.start();
158  }
159  break;
160 
161  case MouseEvent.BUTTON2:
162  break;
163 
164  case MouseEvent.BUTTON3:
165  break;
166  }
167  }
168 
172  @Override
173  public void mouseExited(@NotNull final MouseEvent e) {
174  super.mouseExited(e);
175  if (autoRepeat) {
176  timer.stop();
177  }
178  setActive(false);
179  }
180 
184  @Override
185  public void execute() {
186  commandList.execute();
187  }
188 
192  @Nullable
193  @Override
194  public Dimension getPreferredSize() {
195  return getMinimumSizeInt();
196  }
197 
201  @Nullable
202  @Override
203  public Dimension getMinimumSize() {
204  return getMinimumSizeInt();
205  }
206 
211  @NotNull
212  protected abstract Dimension getMinimumSizeInt();
213 
217  @Override
218  public boolean keyPressed(@NotNull final KeyEvent2 e) {
219  switch (e.getKeyCode()) {
220  case KeyEvent.VK_SPACE:
221  case KeyEvent.VK_ENTER:
222  execute();
223  return true;
224  }
225 
226  return false;
227  }
228 
229 }
boolean keyPressed(@NotNull final KeyEvent2 e)
Invoked when a key has been pressed.the key event for the key whether the key event has been consumed...
final TooltipManager tooltipManager
The TooltipManager to update.
final GUIElementListener elementListener
The GUIElementListener to notify.
abstract Dimension getMinimumSizeInt()
Returns the minimal size needed to display this component.
void mousePressed(@NotNull final MouseEvent e)
Will be called when the user has pressed the mouse inside this element.the mouse event relative to th...
void mouseClicked(@NotNull final MouseEvent e)
Will be called when the user has clicked (pressed+released) this element.This event will be delivered...
void mouseExited(@NotNull final MouseEvent e)
Will be called when the mouse has left the bounding box of this element.This function will not be cal...
void setActive(final boolean active)
Sets the active state of a GUI element.
static final int TIMEOUT_FIRST
The autorepeat delay initially.
Represents a pressed or released key.
Definition: KeyEvent2.java:33
final ActionListener timeoutEvent
The ActionListener for generating autorepeat events.
A GUIElement that can be set to active or inactive.
AbstractButton(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, final int transparency, final boolean autoRepeat, @NotNull final CommandList commandList)
Creates a new instance.
final CommandList commandList
The commands to execute when the button is elected.
static final int TIMEOUT_SECOND
The autorepeat delay for further repeats.
static final long serialVersionUID
The serial version UID.
final Timer timer
The Timer for auto-repeating buttons.
Interface for classes that may handle "pressed" key events.
final boolean autoRepeat
Whether this button should autorepeat.
void execute()
Execute the command list by calling GUICommand#execute() for each command in order.
void mouseReleased(@NotNull final MouseEvent e)
Will be called when the user has released the mouse.This event may be delivered even if no previous m...