Package swingtree

Class ComponentDelegate<C extends JComponent,E>

java.lang.Object
swingtree.AbstractDelegate<C>
swingtree.ComponentDelegate<C,E>
Type Parameters:
C - The delegate (in most cases origin UI component) type parameter stored by this.
E - The event type parameter of the event stored by this.
Direct Known Subclasses:
ComponentMouseEventDelegate

public class ComponentDelegate<C extends JComponent,E> extends AbstractDelegate<C>
Instances of this are delegates for a specific components and events that are passed to user event action handlers (see Action), with the purpose of providing useful context information to the action handler.
You would typically use this to access and change the state of the component, schedule animations for the component or query the tree of neighboring components.
Here a nice usage example where the delegate is used to animate a button:

      button("I turn green when you hover over me")
      .onMouseEnter( it ->
          it.animateFor(0.5, TimeUnit.SECONDS, status -> {
              double highlight = 1 - status.progress() * 0.5;
              it.setBackgroundColor(highlight, 1, highlight);
          })
      )
      .onMouseExit( it ->
          it.animateFor(0.5, TimeUnit.SECONDS, status -> {
              double highlight = 0.5 + status.progress() * 0.5;
              it.setBackgroundColor(highlight, 1, highlight);
          })
      )
  
In this example the it parameter is a ComponentDelegate<JButton,MouseEvent> which can be used to access/modify the button, the event, the sibling components... ...but also exposes a nice API to schedule animations for the button.

For some more examples please take a look at the living swing-tree documentation where you can browse a large collection of examples demonstrating how to use the API of this class.

  • Constructor Details

    • ComponentDelegate

      public ComponentDelegate(C component, E event)
      Creates a new delegate for the specified component and event.
      Parameters:
      component - The component for which the delegate is created.
      event - The event that represents the action that was triggered either by the user or by the system.
  • Method Details

    • getComponent

      public final C getComponent()
      Exposes the underlying JComponent from which this delegate and user event actions originate.

      Threading: A JComponent is owned by the GUI thread (the AWT Event Dispatch Thread). Under a non-coupled threading model (see EventProcessor), event actions may be invoked on the application thread rather than on the EDT. To prevent unsafe access to Swing state, this method requires the calling thread to be the GUI thread and throws an IllegalStateException otherwise.

      If you need to access the component from the application thread, use forComponent(Consumer) instead, which will safely dispatch the supplied lambda to the GUI thread for you.

      Returns:
      The component for which the current Action originated.
      Throws:
      IllegalStateException - If this method is called from a non-Swing thread.
      See Also:
    • forComponent

      public final void forComponent(Consumer<C> action)
      Provides a thread-safe way to access the underlying JComponent of this delegate by passing a Consumer lambda which will always be executed on the GUI thread (the AWT Event Dispatch Thread).

      This is the recommended counterpart to getComponent() when the calling code may run on the application thread (e.g. under a decoupled EventProcessor). If the current thread is already the GUI thread, the lambda is executed immediately; otherwise it is dispatched via UI.run(Runnable).

      Example:

      
            it.forComponent( button -> button.setText("Updated on the EDT") );
        
      Parameters:
      action - The action consuming the component, which will be executed by the Swing thread.
      See Also:
    • getEvent

      public final E getEvent()
      Exposes the event that represents the action that was triggered either by the user or by the system.
      Returns:
      An object holding relevant information about an event that was triggered.
    • getSiblings

      public final sprouts.Tuple<JComponent> getSiblings()
      Exposes the "siblings", which consist of all the child components of the parent of the delegated component, except for the delegated component itself.

      Threading: Sibling components are part of the Swing component tree, which is owned by the GUI thread (the AWT Event Dispatch Thread). This method requires the calling thread to be the GUI thread and throws an IllegalStateException otherwise. Under a non-coupled EventProcessor, event actions are usually invoked on the application thread, in which case you must use forSiblings(Consumer) instead, which dispatches the supplied lambda to the GUI thread for you.

      Returns:
      A tuple (immutable list) of all siblings excluding the component from which this instance originated.
      Throws:
      IllegalStateException - If this method is called from a non-Swing thread.
      See Also:
    • forSiblings

      public final void forSiblings(Consumer<sprouts.Tuple<JComponent>> action)
      Provides a thread-safe way to access the sibling components of this delegate by passing a Consumer lambda which will always be executed on the GUI thread (the AWT Event Dispatch Thread).

      This is the recommended counterpart to getSiblings() when the calling code may run on the application thread (e.g. under a decoupled EventProcessor). If the current thread is already the GUI thread, the lambda is executed immediately; otherwise it is dispatched via UI.run(Runnable).

      Parameters:
      action - The action consuming a list of all siblings (excluding the component from which this instance originated), which will be executed by the Swing thread.
      See Also:
    • getSiblingsOfType

      public final <T extends JComponent> List<T> getSiblingsOfType(Class<T> type)
      Allows you to query the sibling components of the delegated component of the specified type. So a list of all siblings which are of the specified type will be returned, excluding the currently delegated component itself.

      Threading: Sibling components are part of the Swing component tree, which is owned by the GUI thread (the AWT Event Dispatch Thread). This method requires the calling thread to be the GUI thread and throws an IllegalStateException otherwise. Under a non-coupled EventProcessor, event actions are usually invoked on the application thread, in which case you must use forSiblingsOfType(Class, Consumer) instead, which dispatches the supplied lambda to the GUI thread for you.

      Type Parameters:
      T - The type of the sibling components to return.
      Parameters:
      type - The type class of the sibling components to return.
      Returns:
      A list of all siblings of the specified type, excluding the component from which this instance originated.
      Throws:
      IllegalStateException - If this method is called from a non-Swing thread.
      See Also:
    • forSiblingsOfType

      public final <T extends JComponent> void forSiblingsOfType(Class<T> type, Consumer<List<T>> action)
      Provides a thread-safe way to access the sibling components of this delegate filtered by the given type by passing a Consumer lambda which will always be executed on the GUI thread (the AWT Event Dispatch Thread).

      This is the recommended counterpart to getSiblingsOfType(Class) when the calling code may run on the application thread (e.g. under a decoupled EventProcessor). If the current thread is already the GUI thread, the lambda is executed immediately; otherwise it is dispatched via UI.run(Runnable).

      Type Parameters:
      T - The JComponent type of the sibling components to return.
      Parameters:
      type - The type class of the sibling components to return.
      action - The action consuming a list of all siblings of the specified type, excluding the component from which this instance originated, which will be executed by the Swing thread.
      See Also:
    • getSiblinghood

      public final sprouts.Tuple<JComponent> getSiblinghood()
      This method provides a convenient way to access all the children of the parent component of the component this delegate is for, including the delegated component itself.

      Threading: Sibling components are part of the Swing component tree, which is owned by the GUI thread (the AWT Event Dispatch Thread). This method requires the calling thread to be the GUI thread and throws an IllegalStateException otherwise. Under a non-coupled EventProcessor, event actions are usually invoked on the application thread, in which case you must use forSiblinghood(Consumer) instead, which dispatches the supplied lambda to the GUI thread for you.

      Returns:
      A tuple (immutable list) of all siblings including the component from which this instance originated.
      Throws:
      IllegalStateException - If this method is called from a non-Swing thread.
      See Also:
    • forSiblinghood

      public final void forSiblinghood(Consumer<sprouts.Tuple<JComponent>> action)
      Provides a thread-safe way to access the entire siblinghood of this delegate (the delegated component and its siblings) by passing a Consumer lambda which will always be executed on the GUI thread (the AWT Event Dispatch Thread).

      This is the recommended counterpart to getSiblinghood() when the calling code may run on the application thread (e.g. under a decoupled EventProcessor). If the current thread is already the GUI thread, the lambda is executed immediately; otherwise it is dispatched via UI.run(Runnable).

      Parameters:
      action - The action consuming a list of all siblings (including the component from which this instance originated), which will be executed by the Swing thread.
      See Also:
    • getSiblinghoodOfType

      public final <T extends JComponent> sprouts.Tuple<T> getSiblinghoodOfType(Class<T> type)
      Allows you to query the entire siblinghood of the delegated component filtered by the specified type. So a list of all siblings which are of the specified type will be returned, possibly including the delegated component itself.

      Threading: Sibling components are part of the Swing component tree, which is owned by the GUI thread (the AWT Event Dispatch Thread). This method requires the calling thread to be the GUI thread and throws an IllegalStateException otherwise. Under a non-coupled EventProcessor, event actions are usually invoked on the application thread, in which case you must use forSiblinghoodOfType(Class, Consumer) instead, which dispatches the supplied lambda to the GUI thread for you.

      Type Parameters:
      T - The JComponent type of the sibling components to return.
      Parameters:
      type - The type of the sibling components to return.
      Returns:
      A list of all siblings of the specified type, including the component from which this instance originated.
      Throws:
      IllegalStateException - If this method is called from a non-Swing thread.
      See Also:
    • forSiblinghoodOfType

      public final <T extends JComponent> void forSiblinghoodOfType(Class<T> type, Consumer<sprouts.Tuple<T>> action)
      Provides a thread-safe way to access the entire siblinghood of this delegate (including the delegated component itself) filtered by the given type, by passing a Consumer lambda which will always be executed on the GUI thread (the AWT Event Dispatch Thread).

      This is the recommended counterpart to getSiblinghoodOfType(Class) when the calling code may run on the application thread (e.g. under a decoupled EventProcessor). If the current thread is already the GUI thread, the lambda is executed immediately; otherwise it is dispatched via UI.run(Runnable).

      Type Parameters:
      T - The JComponent type of the sibling components to return.
      Parameters:
      type - The type of the sibling components to return.
      action - The action consuming a tuple (immutable list) of all siblings of the specified type, including the component from which this instance originated, which will be executed by the Swing thread.
      See Also: