22 package com.realtime.crossfire.jxclient.gui.gui;
27 import java.awt.Component;
28 import java.awt.Dimension;
29 import java.awt.event.KeyEvent;
30 import java.util.Collection;
31 import java.util.EnumSet;
32 import javax.swing.JComponent;
33 import org.jetbrains.annotations.NotNull;
34 import org.jetbrains.annotations.Nullable;
43 public class Gui extends JComponent {
133 if (initialPositionSet && getX() == x && getY() == y) {
137 if ((getWidth() == 0 || getHeight() == 0) && (initialPositionSet || x != 0 || y != 0)) {
138 throw new IllegalStateException(
"width="+getWidth()+
", height="+getHeight()+
", initialPositionSet="+initialPositionSet+
", x="+x+
", y="+y);
141 initialPositionSet =
true;
160 return autoSize != null;
185 final int count = getComponentCount();
186 for (
int i = 0; i < count; i++) {
187 final Component component = getComponent(i);
204 if (defaultElement != null) {
218 public <T extends GUIElement> T getFirstElementEndingWith(@NotNull
final Class<T> class_, @NotNull
final String ending) {
219 final int count = getComponentCount();
220 for (
int i = 0; i < count; i++) {
221 final Component component = getComponent(i);
222 if (component.isVisible() && component instanceof
GUIElement) {
224 if (class_.isAssignableFrom(element.getClass()) && element.
getName().endsWith(ending)) {
225 return class_.cast(element);
242 public <T extends GUIElement> T getFirstElementNotEndingWith(@NotNull
final Class<T> class_, @NotNull
final String ending) {
243 final int count = getComponentCount();
244 for (
int i = 0; i < count; i++) {
245 final Component component = getComponent(i);
246 if (component.isVisible() && component instanceof
GUIElement) {
248 if (class_.isAssignableFrom(element.getClass()) && !element.
getName().endsWith(ending)) {
249 return class_.cast(element);
264 public <T extends GUIElement> T getFirstElement(@NotNull
final Class<T> class_) {
265 final int count = getComponentCount();
266 for (
int i = 0; i < count; i++) {
267 final Component component = getComponent(i);
268 if (component.isVisible() && component instanceof
GUIElement) {
270 if (class_.isAssignableFrom(element.getClass())) {
271 return class_.cast(element);
288 Component component = findComponentAt(x, y);
289 while (component != null) {
291 return (AbstractGUIElement)component;
293 component = component.getParent();
306 if (forcedActive != null && forcedActive != activeElement) {
315 if (previousActiveElement != null) {
318 assert this.activeElement != null;
319 this.activeElement.activeChanged();
321 guiAutoCloseListener = null;
327 this.activeElement = null;
328 assert previousActiveElement != null;
331 if (guiAutoCloseListener != null) {
333 guiAutoCloseListener = null;
346 return this.activeElement != null && this.activeElement ==
activeElement;
355 if (activeElement != null) {
371 final KeyPressedHandler keyListener = (KeyPressedHandler)activeElement;
377 switch (e.getKeyCode()) {
378 case KeyEvent.VK_ENTER:
379 case KeyEvent.VK_SPACE:
381 if (defaultElement != null) {
398 if (activeElement == null) {
403 if (!textArea.
getName().equals(
"command")) {
407 assert activeElement != null;
421 public <T extends GUIElement> T getFirstElement(@NotNull
final Class<T> class_, @NotNull
final String name) {
422 final int count = getComponentCount();
423 for (
int i = 0; i < count; i++) {
424 final Component component = getComponent(i);
425 if (component.isVisible() && component instanceof
GUIElement) {
427 if (class_.isAssignableFrom(element.getClass()) && element.
getName().equals(name)) {
428 return class_.cast(element);
449 hideInStates.add(state);
458 return hideInStates.contains(state);
477 return getX() <= x && x < getX()+getWidth() && getY() <= y && y < getY()+getHeight();
495 final String name = getName();
496 return (name == null ?
"" : name)+
"["+getWidth()+
"x"+getHeight()+
"]";
504 public void autoSize(
final int screenWidth,
final int screenHeight) {
507 if (extent != null) {
508 final Dimension preferredSize = getPreferredSize();
509 setBounds(extent.
getX(screenWidth, screenHeight, preferredSize.width, preferredSize.height), extent.
getY(screenWidth, screenHeight, preferredSize.width, preferredSize.height), extent.
getW(screenWidth, screenHeight, preferredSize.width, preferredSize.height), extent.
getH(screenWidth, screenHeight, preferredSize.width, preferredSize.height));
510 }
else if (!initialPositionSet) {
511 final Dimension preferredSize = getPreferredSize();
514 if (defaultX == null) {
515 x = (screenWidth-preferredSize.width)/2;
517 x = defaultX.
evaluate(screenWidth, screenHeight, preferredSize.width, preferredSize.height);
521 if (defaultY == null) {
522 y = (screenHeight-preferredSize.height)/2;
524 y = defaultY.
evaluate(screenWidth, screenHeight, preferredSize.width, preferredSize.height);
526 setSize(preferredSize.width, preferredSize.height);
527 if (defaultX != null && defaultY != null) {
528 setPosition(x-preferredSize.width/2, y-preferredSize.height/2);
533 showDialog(getX(), getY(), screenWidth, screenHeight);
569 public void showDialog(
final int x,
final int y,
final int windowWidth,
final int windowHeight) {
576 newX = Math.max(Math.min(x, windowWidth-getWidth()), 0);
577 newY = Math.max(Math.min(y, windowHeight-getHeight()), 0);
boolean saveDialog
Whether this dialog retains its position across restarts.
static final long serialVersionUID
The serial version UID.
Interface for clients interested in auto-close events of Gui instances.
AbstractGUIElement getElementFromPoint(final int x, final int y)
Determines the GUIElement for a given coordinate.
void activateDefaultElement()
Activates the first default gui element of this gui.
Combines a list of GUIElements to for a gui.
KeyBindings getKeyBindings()
Returns the key bindings instance for this gui.
void setDefaultPosition(@NotNull final Expression defaultX, @NotNull final Expression defaultY)
Sets the default position for this dialog.
void setForcedActive(@Nullable final ActivatableGUIElement forcedActive)
Sets an ActivatableGUIElement that is always active.
Encapsulates the extent of a GUI element.
final KeyBindings keyBindings
The KeyBindings for this gui.
void setAutoSize(@Nullable final Extent autoSize)
Sets the auto-size state.
boolean keyPressed(@NotNull KeyEvent2 e)
Invoked when a key has been pressed.
void showDialog(final int x, final int y, final int windowWidth, final int windowHeight)
Sets the position of a dialog but makes sure the dialog is fully visible.
boolean isSaveDialog()
Returns whether this dialog retains its position across restarts.
int evaluate(final int width, final int height, final int prefWidth, final int prefHeight)
Evaluates the expression into a constant.
void setModal(final boolean modal)
Sets the modal state.
Gui(@NotNull final KeyBindings keyBindings)
Creates a new instance.
final Collection< RendererGuiState > hideInStates
The gui states that do not show this dialog.
String getName()
Returns the internal name of this gui element.
boolean isModal()
Returns the modal state.
boolean isActiveElement(@Nullable final ActivatableGUIElement activeElement)
Returns whether a given gui element is the active element of this dialog.
boolean isWithinDrawingArea(final int x, final int y)
Returns whether a given point is within this dialog's drawing area.
void setActive(final boolean active)
Sets the active state of a GUI element.
void setActiveElementActive(final boolean active)
Activates or deactivates the GUI element owning the focus.
boolean modal
Whether this dialog is modal.
void autoClosed()
Called when a dialog has been auto-closed.
Represents a pressed or released key.
boolean initialPositionSet
Whether an initial position has been set.
Expression defaultY
The default y-coordinate for this dialog.
void autoSize(final int screenWidth, final int screenHeight)
Auto-resizes the dialog.
An expression yielding an integer value derived from a screen resolution.
Interface defining an abstract GUI element.
void setPosition(final int x, final int y)
Sets the position of this dialog.
boolean handleKeyPress(@NotNull final KeyEvent2 e)
Dispatches a key press KeyEvent.
A GUIElement that can be set to active or inactive.
int getY(final int width, final int height, final int prefWidth, final int prefHeight)
Returns the y coordinate.
int getW(final int width, final int height, final int prefWidth, final int prefHeight)
Returns the width.
void setActiveElement(@NotNull final ActivatableGUIElement activeElement, final boolean active)
Sets the gui element owning the focus.
ActivatableGUIElement getDefaultElement()
Returns the first default gui element of this gui.
int getX(final int width, final int height, final int prefWidth, final int prefHeight)
Returns the x coordinate.
All gui states of JXCWindowRenderer.
void setGuiAutoCloseListener(@Nullable final GuiAutoCloseListener guiAutoCloseListener)
Sets the GuiAutoCloseListener to be notified when this dialog becomes inactive.
void hideInState(@NotNull final RendererGuiState state)
Hides the dialog in a state.
boolean isAutoSize()
Returns whether this dialog is an auto-size dialog.
int getH(final int width, final int height, final int prefWidth, final int prefHeight)
Returns the height.
Utility class for Swing related functions.
boolean deactivateCommandInput()
Deactivates the command text input field of this dialog.
boolean isHidden(@NotNull final RendererGuiState state)
Returns whether this gui is visible in a state.
Abstract base class for GUI elements to be shown in Guis.
abstract void activeChanged()
Will be called whenever the active state has changed.
GuiAutoCloseListener guiAutoCloseListener
If set, the auto-close listener to notify if this dialog looses the active gui element.
Interface for classes that may handle "pressed" key events.
ActivatableGUIElement activeElement
The gui element which has the focus.
Extent autoSize
The extent of the dialog if it is auto-sizing or.
Manages a set of key bindings.
boolean isDefault()
Returns whether this element is the default element.The default element is selected with the ENTER ke...
boolean handleKeyPress(@NotNull final KeyEvent2 e)
Executes a "key press" event.
Expression defaultX
The default x-coordinate for this dialog.
void setSaveDialog()
Makes this dialog retain its position across restarts.
static void invokeAndWait(@NotNull final Runnable runnable)
Calls SwingUtilities#invokeAndWait(Runnable) if not on the EDT or calls the Runnable directly if on t...
abstract void execute()
Executes the actions associated with this GUI element.
ActivatableGUIElement forcedActive
If non-.