 |
Crossfire JXClient, Trunk
|
Go to the documentation of this file.
23 package com.realtime.crossfire.jxclient.gui.misc;
29 import java.awt.Color;
30 import java.awt.Component;
31 import java.awt.Container;
32 import java.awt.Dimension;
33 import java.awt.Graphics;
34 import java.awt.Point;
35 import java.awt.event.MouseEvent;
36 import java.awt.event.MouseWheelEvent;
37 import java.io.IOException;
38 import java.io.Writer;
39 import java.time.LocalDateTime;
40 import java.time.format.DateTimeFormatter;
41 import java.util.Locale;
42 import java.util.regex.Pattern;
43 import javax.swing.JRootPane;
44 import org.jetbrains.annotations.NotNull;
45 import org.jetbrains.annotations.Nullable;
90 private static final DateTimeFormatter
FORMATTER = DateTimeFormatter.ofPattern(
"yyyy-MM-dd HH:mm:ss,SSS ", Locale.ENGLISH);
146 @SuppressWarnings(
"UnusedParameters")
150 final double distance =
clickPosition.distanceSq(e.getLocationOnScreen());
152 debugMouseWrite(
"mouseDragged: distance "+distance+
" is too far for a click event; click point="+
clickPosition+
", current point="+e.getLocationOnScreen());
207 if (tmpIsClicked && element !=
null) {
211 element.mouseClicked(e);
263 if (element !=
null) {
264 element.mouseEntered(e);
277 if (element ==
null) {
281 final int wheelRotation = e.getWheelRotation();
283 element.mouseWheelMoved(wheelRotation);
292 if (component !=
null) {
295 final String text1 =
formatName(component)+
" "+component.getWidth()+
"x"+component.getHeight()+
"+"+x1+
"+"+y1;
297 final Container parent = component.getParent();
300 final String text2 = parent ==
null ?
"" :
formatName(parent)+
" "+parent.getWidth()+
"x"+parent.getHeight()+
"+"+x2+
"+"+y2;
305 g.setColor(Color.BLACK);
306 g.fillRect(0, 2, Math.max(dimension1.width, dimension2.width)+4, dimension1.height+dimension2.height+16);
308 g.setColor(Color.RED);
309 g.drawString(text1, 2, dimension1.height+8-6);
310 g.drawRect(x1, y1, component.getWidth()-1, component.getHeight()-1);
312 if (parent !=
null) {
313 g.setColor(Color.ORANGE);
314 g.drawString(text2, 2, dimension1.height+dimension2.height+16-6);
315 g.drawRect(x2, y2, parent.getWidth()-1, parent.getHeight()-1);
326 private static String
formatName(@NotNull
final Component component) {
327 return PATTERN_PACKAGE_NAME.matcher(component.getClass().getName()).replaceAll(
"")+
"/"+component.getName();
344 }
catch (
final IOException ex) {
345 System.err.println(
"Cannot write mouse debug: "+ex.getMessage());
347 throw new AssertionError(ex);
361 if (this.activeComponent !=
null) {
362 this.activeComponent.setChanged();
366 final JRootPane rootPane = tmp.getRootPane();
367 if (rootPane !=
null) {
374 if (this.activeComponent !=
null) {
378 final JRootPane rootPane = tmp.getRootPane();
379 if (rootPane !=
null) {
void setDragging(final boolean isDragging)
Updates isDragging.
void mouseMoved(@NotNull MouseEvent e)
Will be called when the mouse moves within this component.
void mousePressed(@Nullable final AbstractGUIElement element, @NotNull final MouseEvent e)
Handles a mouse pressed event.
void mouseReleased(@NotNull MouseEvent e)
Will be called when the user has released the mouse.
GUIElement mouseElement
The gui element in which the mouse is.
static final Pattern PATTERN_PACKAGE_NAME
Matches the package name of a fully qualified class name.
void setChanged()
Records that the contents have changed and must be repainted.
Abstract base class for GUI elements to be shown in Guis.
final Point clickPosition
The position that was clicked if isClicked is set.
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.
boolean isClicked
Whether a button release event is considered a "click".
MouseTracker(final boolean debugGui, @Nullable final Writer debugMouse, @NotNull final GuiFactory guiFactory)
Creates a new instance.
final boolean debugGui
Whether GUI elements should be highlighted.
int getElementX(@NotNull final AbstractGUIElement element)
Returns an element's absolute screen coordinate.
void paintActiveComponent(@NotNull final Graphics g)
Marks the active component in a Graphics instance.
int getElementY(@NotNull final AbstractGUIElement element)
Returns an element's absolute screen coordinate.
void mouseExited(@NotNull final MouseEvent e)
Handles a mouse exited event.
boolean isDragging
Whether a dragging operation is in progress.
void mouseMoved(@Nullable final AbstractGUIElement element, @NotNull final MouseEvent e)
Handles a mouse moved event.
static final int CLICK_DISTANCE
The maximum distance the mouse may have moved between the mouse pressed and the mouse released even t...
final GuiFactory guiFactory
The global GuiFactory instance.
Interface defining an abstract GUI element.
static final DateTimeFormatter FORMATTER
A formatter for timestamps.
void mouseClicked(@Nullable final AbstractGUIElement element, @NotNull final MouseEvent e)
Handles a mouse clicked event.
Utility class for Gui related functions.
void enterElement(@Nullable final AbstractGUIElement element, @NotNull final MouseEvent e)
Sets a new mouseElement and generate entered/exited events.
void setClicked(final boolean isClicked)
Updates isClicked.
void mouseDragged(@Nullable final GUIElement element, @NotNull final MouseEvent e)
Handles a mouse dragged event.
static String formatName(@NotNull final Component component)
Returns the name to display for a Component.
final Writer debugMouse
The Writer to write mouse debug to or.
void mouseWheelMoved(@Nullable final GUIElement element, @NotNull final MouseWheelEvent e)
Handles a mouse wheel event.
Factory for creating Gui instances.
AbstractGUIElement activeComponent
The active component.
void mouseDragged(@NotNull MouseEvent e)
Will be called when the mouse moves within this component while the button is pressed.
void mouseEntered(@Nullable final AbstractGUIElement element, @NotNull final MouseEvent e)
Handles a mouse entered event.
void mouseExited(@NotNull MouseEvent e)
Will be called when the mouse has left the bounding box of this element.
void mouseReleased(@Nullable final AbstractGUIElement element, @NotNull final MouseEvent e)
Handles a mouse released event.
void setActiveComponent(@Nullable final AbstractGUIElement activeComponent)
Updates activeComponent.
void mousePressed(@NotNull MouseEvent e)
Will be called when the user has pressed the mouse inside this element.
void debugMouseWrite(@NotNull final CharSequence message)
Writes a message to the mouse debug.
Tracks mouse actions and delivers mouse events to affected GUIElement.