Crossfire JXClient, Trunk  R20561
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-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.gui.gui;
23 
25 import java.awt.Transparency;
26 import java.awt.event.MouseEvent;
27 import javax.swing.JComponent;
28 import org.jetbrains.annotations.NotNull;
29 import org.jetbrains.annotations.Nullable;
30 
36 public abstract class AbstractGUIElement extends JComponent implements GUIElement {
37 
41  private static final long serialVersionUID = 1;
42 
47  @Nullable
49 
54  private boolean isDefault;
55 
59  private boolean ignore;
60 
64  @NotNull
65  private final String name;
66 
70  @NotNull
72 
76  @NotNull
78 
82  private boolean pendingChange;
83 
88  @NotNull
89  private final Runnable setChangedRunnable = new Runnable() {
90 
91  @Override
92  public void run() {
93  synchronized (setChangedRunnable) {
94  pendingChange = false;
95  }
96  final Gui parent = GuiUtils.getGui(AbstractGUIElement.this);
97  if (parent != null) {
98  parent.repaint();
99  }
100  if (isVisible()) {
101  if (changedListener != null) {
102  changedListener.notifyChanged();
103  }
104  }
105  }
106 
107  };
108 
116  protected AbstractGUIElement(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, final int transparency) {
117  setDoubleBuffered(false);
118  this.tooltipManager = tooltipManager;
119  this.elementListener = elementListener;
120  this.name = name;
121  setOpaque(transparency != Transparency.TRANSLUCENT);
122  setFocusable(false);
123  }
124 
128  @Override
129  public void dispose() {
130  }
131 
135  @NotNull
136  @Override
137  public String toString() {
138  return name;
139  }
140 
144  @Override
145  public boolean isDefault() {
146  return isDefault;
147  }
148 
152  @Override
153  public void setDefault(final boolean isDefault) {
154  this.isDefault = isDefault;
155  }
156 
160  @Override
161  public void setIgnore() {
162  ignore = true;
163  }
164 
168  @Override
169  public boolean isIgnore() {
170  return ignore;
171  }
172 
176  @NotNull
177  @Override
178  public String getName() {
179  return name;
180  }
181 
185  @Override
186  public void mouseClicked(@NotNull final MouseEvent e) {
187  elementListener.raiseDialog(this);
188  }
189 
193  @Override
194  public void mouseEntered(@NotNull final MouseEvent e, final boolean debugGui) {
195  tooltipManager.setElement(this);
196  }
197 
201  @Override
202  public void mouseExited(@NotNull final MouseEvent e) {
203  tooltipManager.unsetElement(this);
204  }
205 
209  @Override
210  public void mousePressed(@NotNull final MouseEvent e) {
211  elementListener.raiseDialog(this);
212  }
213 
217  @Override
218  public void mouseReleased(@NotNull final MouseEvent e) {
219  }
220 
224  @Override
225  public void mouseMoved(@NotNull final MouseEvent e) {
226  }
227 
231  @Override
232  public void mouseDragged(@NotNull final MouseEvent e) {
233  }
234 
238  @Override
239  public void setChanged() {
240  synchronized (setChangedRunnable) {
241  if (!pendingChange) {
242  pendingChange = true;
243  SwingUtilities2.invokeLater(setChangedRunnable);
244  }
245  }
246  }
247 
251  @Override
252  public void setTooltipText(@Nullable final String tooltipText) {
253  tooltipManager.setTooltipText(this, tooltipText);
254  }
255 
259  @Override
260  public void setTooltipText(@Nullable final String tooltipText, final int x, final int y, final int w, final int h) {
261  tooltipManager.setTooltipText(this, tooltipText, x, y, w, h);
262  }
263 
267  @Override
268  public boolean hasTooltipText() {
269  return tooltipManager.hasTooltipText(this);
270  }
271 
275  @Override
276  public void setChangedListener(@Nullable final GUIElementChangedListener changedListener) {
277  this.changedListener = changedListener;
278  }
279 
280 }
static Gui getGui(@NotNull final Component element)
Returns the Gui an element is part of.
Definition: GuiUtils.java:91
void unsetElement(@NotNull GUIElement guiElement)
Removes the tooltip of a GUI element.
Combines a list of GUIElements to for a gui.
Definition: Gui.java:43
final Runnable setChangedRunnable
The Runnable that implements the code of setChanged() which must run on the EDT.
final TooltipManager tooltipManager
The TooltipManager to update.
void setChangedListener(@Nullable final GUIElementChangedListener changedListener)
Sets the GUIElementChangedListener to be notified.Note that at most one such listener may be set per ...
void setChanged()
Records that the contents have changed and must be repainted.
void setElement(@NotNull GUIElement guiElement)
Displays the tooltip for a GUI element.
final GUIElementListener elementListener
The GUIElementListener to notify.
boolean isIgnore()
Returns whether this gui element is to be ignored for user interaction.whether this gui element is ig...
Interface defining an abstract GUI element.
Definition: GUIElement.java:32
static final long serialVersionUID
The serial version UID.
boolean ignore
Whether this gui element should be ignored for user interaction.
AbstractGUIElement(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, final int transparency)
Creates a new instance.
void mouseDragged(@NotNull final MouseEvent e)
Will be called when the mouse moves within this component while the button is pressed.This event will be delivered after mouseMoved(MouseEvent). Note: if the mouse leaves this element's bounding box while the mouse button is still pressed, furthermouseDragged (but nomouseMoved ) events will be generated. the mouse event relative to this element
boolean isDefault
Whether this element is the default element.
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...
static void invokeLater(@NotNull final Runnable runnable)
Calls SwingUtilities#invokeLater(Runnable) if not on the EDT or calls the Runnable directly if on the...
String getName()
Returns the internal name of this gui element.The name is used in skin files for identifying an eleme...
GUIElementChangedListener changedListener
The GUIElementChangedListener to be notified whenever this element has changed.
Utility class for Gui related functions.
Definition: GuiUtils.java:35
void setTooltipText(@Nullable final String tooltipText)
Sets the tooltip text to show when the mouse is inside this element.the text to show ornull to disab...
void mouseEntered(@NotNull final MouseEvent e, final boolean debugGui)
Will be called when the mouse has entered the bounding box of this element.the mouse event relative t...
void setTooltipText(@NotNull AbstractGUIElement element, @Nullable String tooltipText)
Updates the tooltip text of a GUI element.
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...
Utility class for Swing related functions.
Interface for listeners interested in the changed flag of GUIElement instances.
Abstract base class for GUI elements to be shown in Guis.
boolean hasTooltipText()
Returns whether the tooltip is enabled.whether the tooltip is enabled
boolean hasTooltipText(AbstractGUIElement element)
Returns whether the tooltip is enabled.
void setDefault(final boolean isDefault)
Sets whether this element is the default element.The default element is selected with the ENTER key...
boolean isDefault()
Returns whether this element is the default element.The default element is selected with the ENTER ke...
void mouseClicked(@NotNull final MouseEvent e)
Will be called when the user has clicked (pressed+released) this element.This event will be delivered...
void raiseDialog(@NotNull Component component)
The Gui of a Component should be raised.
void setTooltipText(@Nullable final String tooltipText, final int x, final int y, final int w, final int h)
Sets the tooltip text to show when the mouse is inside this element.the text to show, ornull to disable the tooltip for this element the x coordinate the y coordinate the width the height
void mouseMoved(@NotNull final MouseEvent e)
Will be called when the mouse moves within this component.before. the mouse event relative to this el...
void setIgnore()
Marks this gui element to be ignored for user interaction.
void notifyChanged()
Called whenever the changed flag was set while a GUIElement was visible.
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...
boolean pendingChange
Used to avoid refreshing items all the time.