22 package com.realtime.crossfire.jxclient.gui.misc;
27 import java.awt.Color;
28 import java.awt.Dimension;
29 import java.awt.Graphics;
30 import java.awt.Point;
31 import java.awt.event.MouseEvent;
32 import java.io.IOException;
33 import java.io.Writer;
34 import java.text.DateFormat;
35 import java.text.SimpleDateFormat;
36 import java.util.Date;
37 import org.jetbrains.annotations.NotNull;
38 import org.jetbrains.annotations.Nullable;
70 private final DateFormat
simpleDateFormat =
new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss,SSS ");
105 public MouseTracker(
final boolean debugGui, @Nullable
final Writer debugMouse) {
124 @SuppressWarnings(
"UnusedParameters")
128 final double distance = clickPosition.distanceSq(e.getLocationOnScreen());
129 if (distance > CLICK_DISTANCE) {
130 debugMouseWrite(
"mouseDragged: distance "+distance+
" is too far for a click event; click point="+clickPosition+
", current point="+e.getLocationOnScreen());
134 if (mouseElement != null) {
138 if (isDragging && mouseElement != null) {
185 if (tmpIsClicked && element != null) {
189 element.mouseClicked(e);
241 if (element != null) {
255 if (component != null) {
256 final String text = component.
getName();
258 g.setColor(Color.BLACK);
259 g.fillRect(0, 2, dimension.width+4, dimension.height+8);
260 g.setColor(Color.RED);
261 g.drawString(text, 2, 16);
280 }
catch (
final IOException ex) {
281 System.err.println(
"Cannot write mouse debug: "+ex.getMessage());
283 throw new AssertionError(ex);
293 if (this.activeComponent == activeComponent) {
297 if (this.activeComponent != null) {
298 this.activeComponent.setChanged();
301 if (this.activeComponent != null) {
313 if (this.isDragging == isDragging) {
326 if (this.isClicked == isClicked) {
void mouseClicked(@Nullable final AbstractGUIElement element, @NotNull final MouseEvent e)
Handles a mouse clicked event.
void enterElement(@Nullable final AbstractGUIElement element, @NotNull final MouseEvent e)
Sets a new mouseElement and generate entered/exited events.
static int getElementY(@NotNull final Component element)
Returns an element's absolute screen coordinate.
final boolean debugGui
Whether GUI elements should be highlighted.
MouseTracker(final boolean debugGui, @Nullable final Writer debugMouse)
Creates a new instance.
static Dimension getTextDimension(@NotNull final String text, @NotNull final FontMetrics fontMetrics)
Returns the extents of a string when rendered in a given Font on this component.
void setChanged()
Records that the contents have changed and must be repainted.
void mouseDragged(@Nullable final GUIElement element, @NotNull final MouseEvent e)
Handles a mouse dragged event.
final Writer debugMouse
The Writer to write mouse debug to or.
void mouseMoved(@NotNull MouseEvent e)
Will be called when the mouse moves within this component.
void mouseEntered(@Nullable final AbstractGUIElement element, @NotNull final MouseEvent e)
Handles a mouse entered event.
Interface defining an abstract GUI element.
void setClicked(final boolean isClicked)
Updates isClicked.
void debugMouseWrite(@NotNull final CharSequence message)
Writes a message to the mouse debug.
String getName()
Returns the internal name of this gui element.The name is used in skin files for identifying an eleme...
boolean isClicked
Whether a button release event is considered a "click".
GUIElement mouseElement
The gui element in which the mouse is.
void mousePressed(@Nullable final AbstractGUIElement element, @NotNull final MouseEvent e)
Handles a mouse pressed event.
Utility class for Gui related functions.
boolean isDragging
Whether a dragging operation is in progress.
void mouseExited(@NotNull MouseEvent e)
Will be called when the mouse has left the bounding box of this element.
final Point clickPosition
The position that was clicked if isClicked is set.
void mouseReleased(@NotNull MouseEvent e)
Will be called when the user has released the mouse.
void setActiveComponent(@Nullable final AbstractGUIElement activeComponent)
Updates activeComponent.
void mouseExited(@NotNull final MouseEvent e)
Handles a mouse exited event.
void mouseDragged(@NotNull MouseEvent e)
Will be called when the mouse moves within this component while the button is pressed.
void mouseReleased(@Nullable final AbstractGUIElement element, @NotNull final MouseEvent e)
Handles a mouse released event.
Abstract base class for GUI elements to be shown in Guis.
static int getElementX(@NotNull final Component element)
Returns an element's absolute screen coordinate.
Tracks mouse actions and delivers mouse events to affected GUIElement.
void setDragging(final boolean isDragging)
Updates isDragging.
final DateFormat simpleDateFormat
A formatter for timestamps.
void paintActiveComponent(@NotNull final Graphics g)
Marks the active component in a Graphics instance.
AbstractGUIElement activeComponent
The active component.
static final int CLICK_DISTANCE
The maximum distance the mouse may have moved between the mouse pressed and the mouse released even t...
void mousePressed(@NotNull MouseEvent e)
Will be called when the user has pressed the mouse inside this element.
void mouseMoved(@Nullable final AbstractGUIElement element, @NotNull final MouseEvent e)
Handles a mouse moved event.