Package swingtree.style
Class StyleSheet
java.lang.Object
swingtree.style.StyleSheet
An abstract class intended to be extended to create custom CSS look-alike
source code based style sheets for your Swing application.
Note that the
A style sheet object is in essence merely a collection of
StyleTrait
s and corresponding Styler
lambdas
which are used by the SwingTree style engine
to calculate component StyleConf
configurations
in a functional and side effect free manner.
Implement the configure()
method and
use the add(StyleTrait, Styler)
method to
add StyleTrait
s and corresponding Styler
lambdas
to the style sheet.
There are also various factory methods for creating StyleTrait
s
in the form of id(String)
, group(String)
, group(Enum)
, type(Class)
.
This is designed to make your style sheet code more readable and maintainable.
Here an example of how this class is
typically used to create a custom style sheet:
class MyStyleSheet extends StyleSheet {
{@literal @}Override
protected void configure() {
add(group("MyButton"), it -> it
.margin(12)
.padding(16)
.backgroundColor(Color.YELLOW)
);
add(type(JLabel.class).id("Foo"), it-> it
.borderRadius(5)
.gradient("Bar", ... )
);
add(group(Group.ERROR), it -> it
.backgroundColor(Color.RED)
.borderColor(Color.YELLOW)
.borderWidth(2)
);
}
}
This API design is inspired by the CSS styling language, and the use of immutable objects
is a key feature of the style API, which makes it possible to safely compose
Styler
lambdas into any kind of style inheritance hierarchy
without having to worry about side effects.
Note that the
configure()
method, here the Styler
lambdas
are intended to be registered, is not called eagerly in the constructor of the style sheet,
but rather lazily when the style sheet is first used to calculate
the style for a particular component through the
applyTo(JComponent)
or applyTo(JComponent, StyleConf)
methods.-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected <C extends JComponent>
voidadd
(StyleTrait<C> trait, Styler<C> traitStyler) Use this to register style rules in youconfigure()
implementation by providing aStyleTrait
targeting the components you want to style (seeid(String)
,group(String)
,group(Enum)
,type(Class)
), and a correspondingStyler
lambda which will be applied to the components targeted by theStyleTrait
.applyTo
(JComponent toBeStyled) Applies the style sheet to the given component.applyTo
(JComponent toBeStyled, StyleConf startingStyle) Applies the style sheet to the given component using the supplied startingStyleConf
as a basis.protected abstract void
Override this method to configure the style sheet by addingStyleTrait
s and correspondingStyler
lambdas to the style sheet through theadd(StyleTrait, Styler)
method.protected <E extends Enum<E>>
StyleTrait<JComponent>group
(E group) A factory method for aStyleTrait
targeting components belonging to the given enum group (seeUIForAnySwing.group(Enum...)
.protected StyleTrait<JComponent>
A factory method for aStyleTrait
targeting components belonging to the given string group (seeUIForAnySwing.group(String...)
.protected StyleTrait<JComponent>
A factory method for aStyleTrait
targeting components with the given id/name (seeComponent.setName(String)
).static StyleSheet
none()
A factory method for getting the empty style sheet representing no style whatsoever.final void
Essentially (re)initiates the style sheet by clearing all the traits and then calling theconfigure()
method to add new traits to the style sheet.protected <C extends JComponent>
StyleTrait<C>A factory method for aStyleTrait
targeting components which are of a given type (seeObject.getClass()
.
-
Field Details
-
log
protected final org.slf4j.Logger log
-
-
Constructor Details
-
StyleSheet
protected StyleSheet() -
StyleSheet
-
-
Method Details
-
none
A factory method for getting the empty style sheet representing no style whatsoever. It is especially useful instead of null.- Returns:
- A style sheet without any traits and stylers.
-
reconfigure
public final void reconfigure()Essentially (re)initiates the style sheet by clearing all the traits and then calling theconfigure()
method to add new traits to the style sheet. Use this method if your style sheet has a more advanced meta configuration which requires that theStyleTrait
s andStyler
s of this style sheet to change dynamically. For example, during the new configuration you may want to add a different set of traits with differentStyler
s depending on the current theme of the application, which the user can change at runtime (don't forget to repaint the components). -
id
A factory method for aStyleTrait
targeting components with the given id/name (seeComponent.setName(String)
). This is intended to be used in theconfigure()
method of the style sheet. Note that this method does not set the id/name of the component, it expects there to be a component with the given id/name already in the component hierarchy so that a correspondingStyler
lambda can be applied to it.
This is intended to be used in theconfigure()
method of the style sheet.
Here an example of how to use this method in theconfigure()
method:add(id("myButton"), it -> it.backgroundColor(Color.CYAN));
- Parameters:
id
- The id/name of the component to target.- Returns:
- A
StyleTrait
targeting components with the given id/name.
-
group
A factory method for aStyleTrait
targeting components belonging to the given string group (seeUIForAnySwing.group(String...)
. A group is conceptually similar to a CSS class, meaning that you can add a group to any component and then target all components belonging to that group with a singleStyleTrait
. Note that this method does not add the group to any component, it expects there to be a component with the given group already in the component hierarchy so that a correspondingStyler
lambda can be applied to it.
This is intended to be used in theconfigure()
method of the style sheet.
Here an example of how to use this method in theconfigure()
method:
Although usingadd(group("myGroup"), it -> it.backgroundColor(Color.RED));
String
s is a convenient way of grouping components, it is not ideal with respect to compile time safety. Please usegroup(Enum)
andUIForAnySwing.group(Enum[])
instead...- Parameters:
group
- The group to target in the form of a string.- Returns:
- A
StyleTrait
targeting components belonging to the given group.
-
group
A factory method for aStyleTrait
targeting components belonging to the given enum group (seeUIForAnySwing.group(Enum...)
. A group is conceptually similar to a CSS class, meaning that you can add a group to any component and then target all components belonging to that group with a singleStyleTrait
. Note that this method does not add the group to any component, it expects there to be a component with the given group already in the component hierarchy so that a correspondingStyler
lambda can be applied to it.
This is intended to be used in theconfigure()
method of the style sheet.
Here an example of how to use this method in theconfigure()
method:add(group(Group.ERROR), it -> it.backgroundColor(Color.RED));
- Type Parameters:
E
- The type of the enum defining the group to target.- Parameters:
group
- The group to target in the form of an enum.- Returns:
- A
StyleTrait
targeting components belonging to the given group.
-
type
A factory method for aStyleTrait
targeting components which are of a given type (seeObject.getClass()
. Note that this method does not set the type of any component, it expects there to be a component of the given type already in the component hierarchy so that a correspondingStyler
lambda can be applied to it.
This is intended to be used in theconfigure()
method of the style sheet.
Here an example of how to use this method in theconfigure()
method:add(type(JButton.class), it -> it.backgroundColor(Color.RED));
- Type Parameters:
C
- The type of the components to target for styling.- Parameters:
type
- The type of the component to target.- Returns:
- A
StyleTrait
targeting components of the given type.
-
add
Use this to register style rules in youconfigure()
implementation by providing aStyleTrait
targeting the components you want to style (seeid(String)
,group(String)
,group(Enum)
,type(Class)
), and a correspondingStyler
lambda which will be applied to the components targeted by theStyleTrait
.
Here an example of how to use this method in theconfigure()
method:@Override protected void configure() { add(id("arial-button"), it -> it.font(new Font("Arial", Font.BOLD, 12))); add(type(JButton).group("FooBar"), it -> it.borderRadius(5)); add(group(Group.ERROR), it -> it.backgroundColor(Color.RED)); // ... }
- Type Parameters:
C
- The type of the components targeted by theStyleTrait
.- Parameters:
trait
- TheStyleTrait
targeting the components you want to style.traitStyler
- TheStyler
lambda which will be applied to the components targeted by theStyleTrait
.
-
configure
protected abstract void configure()Override this method to configure the style sheet by addingStyleTrait
s and correspondingStyler
lambdas to the style sheet through theadd(StyleTrait, Styler)
method.
Example:@Override protected void configure() { add(type(JComponent.class), it -> it .backgroundColor(new Color(0.7f, 0.85f, 1f)) .padding(4) .margin(5) ); add(type(JButton.class), it -> it .padding(12) .margin(16) .gradient("default", shade -> shade .strategy(ShadingStrategy.TOP_LEFT_TO_BOTTOM_RIGHT) .colors(it.component().getBackground().brighter(), Color.CYAN) ) ); // ... }
-
applyTo
Applies the style sheet to the given component. Note that the style sheet is already configured at this point, because theconfigure()
method is called in the constructor of the style sheet.
Example:MyStyleSheet styleSheet = new MyStyleSheet(); JComboBox<String> comboBox = new JComboBox<>(); styleSheet.applyTo(comboBox);
- Parameters:
toBeStyled
- The component to apply the style sheet to.- Returns:
- The
StyleConf
that was applied to the component.
-
applyTo
Applies the style sheet to the given component using the supplied startingStyleConf
as a basis. Note that the style sheet is already configured at this point, because theconfigure()
method is called in the constructor of the style sheet.Example:
MyStyleSheet styleSheet = new MyStyleSheet(); JComboBox<String> comboBox = new JComboBox<>(); styleSheet.applyTo(comboBox, Style.none());
- Parameters:
toBeStyled
- The component to apply the style sheet to.startingStyle
- TheStyleConf
to start with when applying the style sheet.- Returns:
- The
StyleConf
that was applied to the component. - Throws:
NullPointerException
- If either argument is null.
-