Package swingtree.style
Class ComponentExtension<C extends JComponent>
java.lang.Object
swingtree.style.ComponentExtension<C>
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 Summary
Modifier and TypeMethodDescriptionvoid
addAnimatedPainter
(AnimationStatus status, UI.Layer layer, UI.ComponentArea clipArea, Painter painter) Use this to add aPainter
based animation to the component.void
addAnimatedStyler
(AnimationStatus state, Styler<C> styler) Use this to add aStyler
based animation to the component.void
addDragAwayConf
(Function<Position, DragAwayComponentConf<C>> supplier) void
Adds aStyler
to the component.void
applyAndInstallStyle
(StyleConf styleConf, boolean force) Applies the givenStyleConf
to the component after which a newStyleEngine
is installed for the component.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.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.void
Removes all animations from the component.static <C extends JComponent>
ComponentExtension<C> from
(C comp) Returns theComponentExtension
associated with the given component.void
gatherApplyAndInstallStyle
(boolean force) Allows for the retrieval of a specificShape
which represents a specific area of the component identified by the givenUI.ComponentArea
.getDragAwayConf
(Position mousePosition) If it exists, this method invokes the user internal configurator function previously set by theaddDragAwayConf(Function)
method and returns an optional of the resultingDragAwayComponentConf
object, which holds the configuration for starting a drag away operation using the AWT nativeDragSource
.<P> P
Allows for extra state to be attached to the component extension.getStyle()
Exposes the currentStyleConf
configuration of the component, which holds all the SwingTree style information needed to render the component.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.final boolean
Checks if the component has the given id.final boolean
Checks if the component has the given id.static void
initializeFor
(JComponent comp) Initializes the given component with a newComponentExtension
.void
SwingTree overrides the default Swing look and feel to enable custom styling and animation capabilities.final <E extends Enum<E>>
voidsetId
(E id) Sets the id of the component based on an enum.final void
Sets the id of the component.final <E extends Enum<E>>
voidsetStyleGroups
(E... groupTags) This method is used byUIForAnySwing.group(String...)
to attach so called group tags to a component.void
setStyleGroups
(String... groupTags) This method is used byUIForAnySwing.group(String...)
to attach so called group tags to a component.int
This method tries to hash everything relevant in the visual appearance of the component and it subcomponents into a single integer value.
-
Method Details
-
from
Returns theComponentExtension
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
Initializes the given component with a newComponentExtension
. 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.
-
addDragAwayConf
-
getDragAwayConf
If it exists, this method invokes the user internal configurator function previously set by theaddDragAwayConf(Function)
method and returns an optional of the resultingDragAwayComponentConf
object, which holds the configuration for starting a drag away operation using the AWT nativeDragSource
.- Parameters:
mousePosition
- The current mouse position.- Returns:
- An optional of the resulting
DragAwayComponentConf
object.
-
getOrSet
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
This method is used byUIForAnySwing.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 thesetStyleGroups(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
This method is used byUIForAnySwing.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 thesetStyleGroups(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
Sets the id of the component. The id is used by the SwingTree style engine to apply styles to components with the same id, which is conceptually similar to CSS ids.
The preferred way to set the id is by using an enum to avoid typos and to get better compile time type safety.- Parameters:
id
- The id to set.
-
setId
Sets the id of the component based on an enum. The id is used by the SwingTree style engine to apply styles to components with the same id, which is conceptually similar to CSS ids.
This is the preferred way to set the id, as it offers better compile time type safety. Also check out theUIForAnySwing.id(Enum)
to set the id as part of a UI declaration.- Type Parameters:
E
- The type of the enum.- Parameters:
id
- The id to set.
-
hasId
Checks if the component has the given id.- Parameters:
id
- The id to check.- Returns:
true
if the component has the given id.
-
hasId
Checks if the component has the given id.- Parameters:
id
- The id to check.- Returns:
true
if the component has the given id.
-
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
String
s.
-
belongsToGroup
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
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
Exposes the currentStyleConf
configuration of the component, which holds all the SwingTree style information needed to render the component. -
getComponentArea
Allows for the retrieval of a specificShape
which represents a specific area of the component identified by the givenUI.ComponentArea
. The following areas are available:UI.ComponentArea.ALL
- The entire component, which is the union of all other clip areas (INTERIOR + EXTERIOR + BORDER + CONTENT
).UI.ComponentArea.INTERIOR
- The inner component area, which is defined asALL - EXTERIOR - BORDER
.UI.ComponentArea.EXTERIOR
- The outer component area, which can be expressed asALL - INTERIOR - BORDER
, orALL - CONTENT
.UI.ComponentArea.BORDER
- The border of the component, which is the area between the inner and outer component area and which can be expressed asALL - INTERIOR - EXTERIOR
.UI.ComponentArea.BODY
- The body of the component is the inner component area including the border area. It can be expressed asALL - EXTERIOR
, orINTERIOR + BORDER
.
- Parameters:
area
- The area of the component to retrieve.- Returns:
- An optional
Shape
which represents the given area of the component or an empty optional. If the area is not available, then this means that the style of the component did not lead to the calculation of the given area. This may happen for theEXTERIOR
in case of there being no margin or corner radius, and theBORDER
in case of there being no border width.
-
clearAnimations
public void clearAnimations() -
addAnimatedPainter
public void addAnimatedPainter(AnimationStatus status, UI.Layer layer, UI.ComponentArea clipArea, Painter painter) Use this to add aPainter
based animation to the component.- Parameters:
status
- TheAnimationStatus
which defines when the animation is active.layer
- TheUI.Layer
which defines the layer on which the animation is rendered.clipArea
- TheUI.ComponentArea
which defines the area of the component which is animated.painter
- ThePainter
which defines how the animation is rendered.
-
addAnimatedStyler
Use this to add aStyler
based animation to the component.- Parameters:
state
- TheAnimationStatus
which defines when the animation is active.styler
- TheStyler
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
Adds aStyler
to the component. The styler will be used to calculate the style of the component.- Parameters:
styler
- The styler to add.
-
gatherStyle
- Returns:
- A new immutable
StyleConf
configuration.
-
gatherApplyAndInstallStyle
public void gatherApplyAndInstallStyle(boolean force) Calculates a newStyleConf
object based on theStyler
lambdas associated with the component and then applies it to the component after which a newStyleEngine
is installed for the component. If the calculated style is the same as the current style, nothing happens except in case theforce
parameter is set totrue
.- Parameters:
force
- If set totrue
, the style will be applied even if it is the same as the current style.
-
applyAndInstallStyle
Applies the givenStyleConf
to the component after which a newStyleEngine
is installed for the component. If the given style is the same as the current style, nothing happens except in case theforce
parameter is set totrue
.- Parameters:
styleConf
- The style to apply.force
- If set totrue
, the style will be applied even if it is the same as the current style.
-
viewStateHashCode
public int viewStateHashCode()This method tries to hash everything relevant in the visual appearance of the component and it subcomponents into a single integer value. It is based on the current SwingTree style information as well as more general component information like the current value of a slider, text of a text component, etc.
You may use this for rough cache invalidation purposes. So when you want to render the component into a
BufferedImage
and then only rerender it if the state hash changes, you can use this method.
But keep in mind however, it cannot capture look and feel related changes which are not controlled by SwingTree.
So this hash code is not a perfect solution, but it can be useful in some cases. Like visualizing a drag and drop of a component...- Returns:
- The current state hash of the component and all of it subcomponents, which includes SwingTree style information, as well as component specific information.
-