Crossfire JXClient, Trunk
AbstractGUIElement.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.gui;
24 
27 import java.awt.event.MouseEvent;
28 import javax.swing.JComponent;
29 import org.jetbrains.annotations.NotNull;
30 import org.jetbrains.annotations.Nullable;
31 
37 public abstract class AbstractGUIElement extends JComponent implements GUIElement {
38 
42  private static final long serialVersionUID = 1;
43 
47  @NotNull
48  private final GuiFactory guiFactory;
49 
54  @Nullable
56 
61  private boolean isDefault;
62 
66  private boolean inhibitListeners;
67 
71  private boolean ignore;
72 
76  @NotNull
77  private final String name;
78 
82  @NotNull
84 
88  @NotNull
90 
94  private boolean pendingChange;
95 
100  @NotNull
101  private final Runnable setChangedRunnable = new Runnable() {
102 
103  @Override
104  public void run() {
105  synchronized (setChangedRunnable) {
106  pendingChange = false;
107  if (inhibitListeners) {
108  return;
109  }
110  }
111  final Gui parent = guiFactory.getGui(AbstractGUIElement.this);
112  if (parent != null) {
113  parent.repaint();
114  }
115  if (isVisible() && changedListener != null) {
117  }
118  }
119 
120  };
121 
130  protected AbstractGUIElement(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, final boolean opaque, @NotNull final GuiFactory guiFactory) {
131  this.guiFactory = guiFactory;
132  setDoubleBuffered(false);
133  this.tooltipManager = tooltipManager;
134  this.elementListener = elementListener;
135  this.name = name;
136  setOpaque(opaque);
137  setFocusable(false);
138  }
139 
140  @Override
141  public void dispose() {
142  }
143 
144  @NotNull
145  @Override
146  @SuppressWarnings("MethodDoesntCallSuperMethod")
147  public String toString() {
148  return name;
149  }
150 
151  @Override
152  public boolean isDefault() {
153  return isDefault;
154  }
155 
156  @Override
157  public void setDefault(final boolean isDefault) {
158  this.isDefault = isDefault;
159  }
160 
161  @Override
162  public void setIgnore() {
163  ignore = true;
164  }
165 
166  @Override
167  public boolean isIgnore() {
168  return ignore;
169  }
170 
171  @NotNull
172  @Override
173  @SuppressWarnings("MethodDoesntCallSuperMethod")
174  public String getName() {
175  return name;
176  }
177 
178  @Override
179  public void mouseClicked(@NotNull final MouseEvent e) {
180  if (isEnabled()) {
182  }
183  }
184 
185  @Override
186  public void mouseEntered(@NotNull final MouseEvent e) {
187  if (isEnabled()) {
189  }
190  }
191 
192  @Override
193  public void mouseExited(@NotNull final MouseEvent e) {
194  if (isEnabled()) {
196  }
197  }
198 
199  @Override
200  public void mousePressed(@NotNull final MouseEvent e) {
201  if (isEnabled()) {
203  }
204  }
205 
206  @Override
207  public void mouseReleased(@NotNull final MouseEvent e) {
208  }
209 
210  @Override
211  public void mouseMoved(@NotNull final MouseEvent e) {
212  }
213 
214  @Override
215  public void mouseDragged(@NotNull final MouseEvent e) {
216  }
217 
218  @Override
219  public void mouseWheelMoved(final int wheelRotation) {
220  }
221 
222  @Override
223  public void setChanged() {
224  synchronized (setChangedRunnable) {
225  if (!inhibitListeners && !pendingChange) {
226  pendingChange = true;
228  }
229  }
230  }
231 
236  @Nullable
237  public abstract TooltipText getTooltip();
238 
246  @Nullable
247  protected TooltipText newTooltipText(@Nullable final String tooltipText) {
248  if (tooltipText == null) {
249  return null;
250  }
251 
252  final Gui gui = guiFactory.getGui(this);
253  if (gui == null) {
254  return null;
255  }
256 
257  return new TooltipText(tooltipText, gui.getComponent().getX()+getX(), gui.getComponent().getY()+getY(), getWidth(), getHeight());
258  }
259 
264  protected void tooltipChanged() {
265  if (isEnabled()) {
267  }
268  }
269 
270  @Override
272  this.changedListener = changedListener;
273  }
274 
278  public void inhibitListeners() {
279  synchronized (setChangedRunnable) {
280  inhibitListeners = true;
281  }
282  }
283 
288  public int getDialogBorderTop() {
289  return 0;
290  }
291 
296  public int getDialogBorderLeft() {
297  return 0;
298  }
299 
304  public int getDialogBorderRight() {
305  return 0;
306  }
307 
312  public int getDialogBorderBottom() {
313  return 0;
314  }
315 
316 }
com.realtime.crossfire.jxclient.util.SwingUtilities2
Utility class for Swing related functions.
Definition: SwingUtilities2.java:34
com.realtime.crossfire.jxclient
com.realtime.crossfire.jxclient.skin.skin
Definition: DefaultJXCSkin.java:23
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.pendingChange
boolean pendingChange
Used to avoid refreshing items all the time.
Definition: AbstractGUIElement.java:94
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.tooltipChanged
void tooltipChanged()
Must be called whenever the tooltip may have changed.
Definition: AbstractGUIElement.java:264
com.realtime.crossfire.jxclient.gui.gui.Gui
Combines a list of GUIElements to for a gui.
Definition: Gui.java:49
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.toString
String toString()
Definition: AbstractGUIElement.java:147
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.gui.AbstractGUIElement.inhibitListeners
boolean inhibitListeners
If set, change listeners will not be notified.
Definition: AbstractGUIElement.java:66
com.realtime.crossfire.jxclient.skin
com.realtime.crossfire.jxclient.gui.gui.TooltipManager.unsetElement
void unsetElement(@NotNull AbstractGUIElement guiElement)
Removes the tooltip of a GUI element.
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.setChanged
void setChanged()
Records that the contents have changed and must be repainted.
Definition: AbstractGUIElement.java:223
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.setChangedListener
void setChangedListener(@Nullable final GUIElementChangedListener changedListener)
Sets the GUIElementChangedListener to be notified.
Definition: AbstractGUIElement.java:271
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.gui.TooltipManager.tooltipTextChanged
void tooltipTextChanged(@NotNull GUIElement element)
Reports a possibly changed tooltip text.
com.realtime.crossfire.jxclient.util.SwingUtilities2.invokeLater
static void invokeLater(@NotNull final Runnable runnable)
Calls SwingUtilities#invokeLater(Runnable) if not on the EDT or calls the Runnable directly if on the...
Definition: SwingUtilities2.java:73
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.elementListener
final GUIElementListener elementListener
The GUIElementListener to notify.
Definition: AbstractGUIElement.java:89
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.isDefault
boolean isDefault()
Returns whether this element is the default element.
Definition: AbstractGUIElement.java:152
com.realtime.crossfire.jxclient.gui.gui.GUIElementChangedListener.notifyChanged
void notifyChanged()
Called whenever the changed flag was set while a GUIElement was visible.
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.getTooltip
abstract TooltipText getTooltip()
Returns the current tooltip text.
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.inhibitListeners
void inhibitListeners()
Prevents change listeners to be notified.
Definition: AbstractGUIElement.java:278
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.mouseMoved
void mouseMoved(@NotNull final MouseEvent e)
Will be called when the mouse moves within this component.
Definition: AbstractGUIElement.java:211
com.realtime.crossfire.jxclient.gui.gui.GUIElement
Interface defining an abstract GUI element.
Definition: GUIElement.java:33
com.realtime.crossfire.jxclient.gui.gui.GUIElementListener.raiseDialog
void raiseDialog(@NotNull AbstractGUIElement component)
The Gui of a Component should be raised.
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.isDefault
boolean isDefault
Whether this element is the default element.
Definition: AbstractGUIElement.java:61
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.isIgnore
boolean isIgnore()
Returns whether this gui element is to be ignored for user interaction.
Definition: AbstractGUIElement.java:167
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.changedListener
GUIElementChangedListener changedListener
The GUIElementChangedListener to be notified whenever this element has changed.
Definition: AbstractGUIElement.java:55
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.mouseDragged
void mouseDragged(@NotNull final MouseEvent e)
Will be called when the mouse moves within this component while the button is pressed.
Definition: AbstractGUIElement.java:215
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.getDialogBorderTop
int getDialogBorderTop()
Returns the size of the dialog's top border.
Definition: AbstractGUIElement.java:288
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.getDialogBorderBottom
int getDialogBorderBottom()
Returns the size of the dialog's bottom border.
Definition: AbstractGUIElement.java:312
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.getDialogBorderLeft
int getDialogBorderLeft()
Returns the size of the dialog's left border.
Definition: AbstractGUIElement.java:296
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.getDialogBorderRight
int getDialogBorderRight()
Returns the size of the dialog's right border.
Definition: AbstractGUIElement.java:304
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.guiFactory
final GuiFactory guiFactory
The global GuiFactory instance.
Definition: AbstractGUIElement.java:48
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.newTooltipText
TooltipText newTooltipText(@Nullable final String tooltipText)
Creates a TooltipText instance relative to this instance.
Definition: AbstractGUIElement.java:247
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.mouseReleased
void mouseReleased(@NotNull final MouseEvent e)
Will be called when the user has released the mouse.
Definition: AbstractGUIElement.java:207
com.realtime.crossfire.jxclient.util
Definition: Codec.java:23
com.realtime.crossfire.jxclient.gui.gui.Gui.getComponent
JComponent getComponent()
Returns the JComponent for this instance.
Definition: Gui.java:161
com.realtime.crossfire.jxclient.gui.gui.GUIElementChangedListener
Interface for listeners interested in the changed flag of GUIElement instances.
Definition: GUIElementChangedListener.java:30
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.getName
String getName()
Returns the internal name of this gui element.
Definition: AbstractGUIElement.java:174
com.realtime.crossfire.jxclient.gui.gui.TooltipText
Information for displaying tooltips.
Definition: TooltipText.java:31
com.realtime.crossfire.jxclient.skin.skin.GuiFactory
Factory for creating Gui instances.
Definition: GuiFactory.java:41
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.serialVersionUID
static final long serialVersionUID
The serial version UID.
Definition: AbstractGUIElement.java:42
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.AbstractGUIElement.ignore
boolean ignore
Whether this gui element should be ignored for user interaction.
Definition: AbstractGUIElement.java:71
com.realtime.crossfire
com.realtime.crossfire.jxclient.gui.gui.TooltipManager
Manages the tooltip display.
Definition: TooltipManager.java:33
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.mouseWheelMoved
void mouseWheelMoved(final int wheelRotation)
Will be called when the mouse wheel has been moved.
Definition: AbstractGUIElement.java:219
com.realtime
com
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.setDefault
void setDefault(final boolean isDefault)
Sets whether this element is the default element.
Definition: AbstractGUIElement.java:157
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.mouseExited
void mouseExited(@NotNull final MouseEvent e)
Will be called when the mouse has left the bounding box of this element.
Definition: AbstractGUIElement.java:193
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.mouseClicked
void mouseClicked(@NotNull final MouseEvent e)
Will be called when the user has clicked (pressed+released) this element.
Definition: AbstractGUIElement.java:179
com.realtime.crossfire.jxclient.gui.gui.Gui.repaint
void repaint()
Repaints this component.
Definition: Gui.java:686
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.setChangedRunnable
final Runnable setChangedRunnable
The Runnable that implements the code of setChanged() which must run on the EDT.
Definition: AbstractGUIElement.java:101
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.tooltipManager
final TooltipManager tooltipManager
The TooltipManager to update.
Definition: AbstractGUIElement.java:83
com.realtime.crossfire.jxclient.gui.gui.TooltipManager.setElement
void setElement(@NotNull AbstractGUIElement guiElement)
Displays the tooltip for a GUI element.
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.mouseEntered
void mouseEntered(@NotNull final MouseEvent e)
Will be called when the mouse has entered the bounding box of this element.
Definition: AbstractGUIElement.java:186
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.AbstractGUIElement
AbstractGUIElement(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, final boolean opaque, @NotNull final GuiFactory guiFactory)
Creates a new instance.
Definition: AbstractGUIElement.java:130
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.setIgnore
void setIgnore()
Marks this gui element to be ignored for user interaction.
Definition: AbstractGUIElement.java:162
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.dispose
void dispose()
Releases all allocated resources.
Definition: AbstractGUIElement.java:141
com.realtime.crossfire.jxclient.gui.gui.GUIElementListener
Listener for GUIElement related events.
Definition: GUIElementListener.java:32
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.mousePressed
void mousePressed(@NotNull final MouseEvent e)
Will be called when the user has pressed the mouse inside this element.
Definition: AbstractGUIElement.java:200