Class ComponentExtension<C extends JComponent>

java.lang.Object
swingtree.style.ComponentExtension<C>

public final class ComponentExtension<C extends JComponent> extends Object
Is attached to UI components in the form of a client property. It exists to give Swing-Tree components some custom style and animation capabilities.
  • Method Details

    • from

      public static <C extends JComponent> ComponentExtension<C> from(C comp)
      Returns the ComponentExtension associated with the given component. If the component does not have an extension, a new one is created and associated with the component.
      Type Parameters:
      C - The type of the component.
      Parameters:
      comp - The component for which to get the extension.
      Returns:
      The extension associated with the component.
    • initializeFor

      public static void initializeFor(JComponent comp)
      Initializes the given component with a new ComponentExtension. This method is called by a SwingTree builder node when it receives and builds a new component. The former extension of the component is replaced by a new one.
      Parameters:
      comp - The component to initialize.
    • getOrSet

      public <P> P getOrSet(Class<P> type, Supplier<P> fetcher)
      Allows for extra state to be attached to the component extension. (Conceptually similar to how Swing components can have client properties.)
      If the component already has an object of the given type attached, that object is returned. Otherwise, the given fetcher is used to create a new object of the given type, which is then attached to the component and returned.
      Type Parameters:
      P - The type of the extra state.
      Parameters:
      type - The type of the extra state to attach.
      fetcher - A supplier which is used to create a new object of the given type.
      Returns:
      The extra state object of the given type which is attached to the component.
    • setStyleGroups

      public void setStyleGroups(String... groupTags)
      This method is used by UIForAnySwing.group(String...) to attach so called group tags to a component.
      They are used by the SwingTree style engine to apply styles with the same tags, which is conceptually similar to CSS classes.
      It is advised to use the setStyleGroups(Enum[]) method instead of this method, as the usage of enums for modelling group tags offers much better compile time type safety!
      Parameters:
      groupTags - An array of group tags.
    • setStyleGroups

      @SafeVarargs public final <E extends Enum<E>> void setStyleGroups(E... groupTags)
      This method is used by UIForAnySwing.group(String...) to attach so called group tags to a component.
      They are used by the SwingTree style engine to apply styles with the same tags, which is conceptually similar to CSS classes.
      It is advised to use this method over the setStyleGroups(String[]) method, as the usage of enums for modelling group tags offers much better compile time type safety!
      Type Parameters:
      E - The type of the enum.
      Parameters:
      groupTags - An array of group tags.
    • setId

      public final void setId(String id)
    • setId

      public final <E extends Enum<E>> void setId(E id)
    • hasId

      public final boolean hasId(String id)
    • hasId

      public final boolean hasId(Enum<?> id)
    • getStyleGroups

      public List<String> getStyleGroups()
      A component can have multiple group tags, which are used by the SwingTree style engine to apply styles with the same tags, which is conceptually similar to CSS classes. This method returns the group tags associated with the component.
      Returns:
      The group tags associated with the component in the form of an unmodifiable list of Strings.
    • belongsToGroup

      public boolean belongsToGroup(String group)
      A style group is a tag which is used by the SwingTree style engine to apply styles to things with the same tags making it conceptually similar to CSS classes. This method lets you check if the component belongs to a given String based group.
      Parameters:
      group - The group to check.
      Returns:
      true if the component belongs to the given group.
    • belongsToGroup

      public boolean belongsToGroup(Enum<?> group)
      A style group is a tag which is used by the SwingTree style engine to apply styles to things with the same tags making it conceptually similar to CSS classes. This method lets you check if the component belongs to a given enum based group.
      Parameters:
      group - The group to check.
      Returns:
      true if the component belongs to the given group.
    • getStyle

      public StyleConf getStyle()
      Exposes the current StyleConf configuration of the component, which holds all the SwingTree style information needed to render the component.
      Returns:
      The current StyleConf configuration of the component which is calculated based on the Styler lambdas associated with the component.
    • clearAnimations

      public void clearAnimations()
      Removes all animations from the component. This includes both Painter based animations as well as Styler based animations.
    • addAnimatedPainter

      public void addAnimatedPainter(AnimationState state, UI.Layer layer, UI.ComponentArea clipArea, Painter painter)
      Use this to add a Painter based animation to the component.
      Parameters:
      state - The AnimationState which defines when the animation is active.
      layer - The UI.Layer which defines the layer on which the animation is rendered.
      clipArea - The UI.ComponentArea which defines the area of the component which is animated.
      painter - The Painter which defines how the animation is rendered.
    • addAnimatedStyler

      public void addAnimatedStyler(AnimationState state, Styler<C> styler)
      Use this to add a Styler based animation to the component.
      Parameters:
      state - The AnimationState which defines when the animation is active.
      styler - The Styler which defines how the style of the component is changed during the animation.
    • installCustomUIIfPossible

      public void installCustomUIIfPossible()
      SwingTree overrides the default Swing look and feel to enable custom styling and animation capabilities. This method is used to install the custom look and feel for the component, if possible.
    • addStyler

      public void addStyler(Styler<C> styler)
      Adds a Styler to the component. The styler will be used to calculate the style of the component.
      Parameters:
      styler - The styler to add.
    • gatherStyle

      public StyleConf gatherStyle()
      Calculates a new StyleConf object based on the Styler lambdas associated with the component...
      Returns:
      A new immutable StyleConf configuration.
    • gatherApplyAndInstallStyle

      public void gatherApplyAndInstallStyle(boolean force)
      Calculates a new StyleConf object based on the Styler lambdas associated with the component and then applies it to the component after which a new StyleEngine is installed for the component. If the calculated style is the same as the current style, nothing happens except in case the force parameter is set to true.
      Parameters:
      force - If set to true, the style will be applied even if it is the same as the current style.
    • applyAndInstallStyle

      public void applyAndInstallStyle(StyleConf styleConf, boolean force)
      Applies the given StyleConf to the component after which a new StyleEngine is installed for the component. If the given style is the same as the current style, nothing happens except in case the force parameter is set to true.
      Parameters:
      styleConf - The style to apply.
      force - If set to true, the style will be applied even if it is the same as the current style.