Fork me on GitHub

Using Resources

Decrease your memory footprint by using resource wrapper

Resources

The JRebirth Framework provides an useful way to deal with your local resources, we currently support :

  • Colors
  • Fonts
  • Images
  • Parameters
  • CSS (WIP)
  • FXML (WIP)

These resources can consume a lot of memory if you don't dispose them (especially big images and fonts) when you stop using them.
JRebirth provides a mechanism to store them weakly and to rebuild them if necessary in order to use the less memory as required. To do this JRebirth stores a ResourceParam lightweight object that contains all information to create the heavyweight resource.
This one is linked by a ResourceItem to facilitate its usage.

UML Diagram Overview:

There are 3 ways to declare resources, each one is available for all kind of resources with special feature for parameters.

With Resources.create(ResourceParam)

The first way is to hold static field declaration instantiated with custom 'factory' Resources (not named, with an overloaded create method to avoid later cast).
This static fields can be hold in any class you want but we recommend to store them into an Interface used 'as' an enum. It's better to have an interface per resource type to avoid big file that blends all resources.
Another interesting trick, is to add a static import on related static Resource.create method to shorten the resource declaration (Ctrl+Shift+M with Eclipse).

Static import declaration:

19
import static org.jrebirth.af.core.resource.Resources.create;

Example of Web color declaration:

35
36
/** The web color. */
ColorItem TEST_COLOR_WEB_1 = create(new WebColor("0088D3"));

With Enumeration

The other way to declare is a little bit complex, it implies to create an enum that implements a ResourceItem interface.
But it requires to add some copy/paste code into implemented methods.

Dynamically

Not implemented yet

To manage these resources we use a enum hack to cleanly define them and most important to have a concise way to use them without calling singleton getter or another complex set of methods.

So if you want to handle resources with JRebirth mechanism, you just have to create an enumeration that implement the interface of the resource you want, with a custom constructor.

Color

For example to manage web color, (basic hexadecimal string #00CC00), you have to use this declaration:

35
36
/** The web color. */
ColorItem TEST_COLOR_WEB_1 = create(new WebColor("0088D3"));

Hereafter an example of an interface that hold multiple colors.

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
 * The class <strong>TestColors</strong>.
 *
 * @author Sébastien Bordes
 */
public interface TestColors {
 
    /**************************************************************************************/
    /** ___________________________________Web Colors.___________________________________ */
    /**************************************************************************************/
 
    /** The web color. */
    ColorItem TEST_COLOR_WEB_1 = create(new WebColor("0088D3"));
 
    /** The web color. */
    ColorItem TEST_COLOR_WEB_2 = create(new WebColor("0D88D3", 0.4));
 
    /** The web color. */
    ColorItem TEST_COLOR_WEB_3 = create(new WebColor("0087D3", 1.0));
 
    /**************************************************************************************/
    /** __________________________________Gray Colors.___________________________________ */
    /**************************************************************************************/
 
    /** The gray color. */
    ColorItem TEST_COLOR_GRAY_1 = create(new GrayColor(0.3));
 
    /** The gray color. */
    ColorItem TEST_COLOR_GRAY_2 = create(new GrayColor(0.74, 0.9));
 
    /** The gray color. */
    ColorItem TEST_COLOR_GRAY_3 = create(new GrayColor(0.5, 1.0));
 
    /**************************************************************************************/
    /** ___________________________________HSB Colors.___________________________________ */
    /**************************************************************************************/
 
    /** The hsb color. */
    ColorItem TEST_COLOR_HSB_1 = create(new HSBColor(96.0, 0.4, 0.9));
 
    /** The hsb color. */
    ColorItem TEST_COLOR_HSB_2 = create(new HSBColor(45.0, 0.3, 0.8, 0.45));
 
    /** The hsb color. */
    ColorItem TEST_COLOR_HSB_3 = create(new HSBColor(153.0, 0.6, 0.75, 1.0));
 
    /**************************************************************************************/
    /** _________________________________RGB 01 Colors.__________________________________ */
    /**************************************************************************************/
 
    /** The rgb 0-1 color. */
    ColorItem TEST_COLOR_RGB01_1 = create(new RGB01Color(0.22, 0.752, 0.78));
 
    /** The rgb 0-1 color. */
    ColorItem TEST_COLOR_RGB01_2 = create(new RGB01Color(0.78, 0.653, 0.85, 0.12));
 
    /** The rgb 0-1 color. */
    ColorItem TEST_COLOR_RGB01_3 = create(new RGB01Color(0.96, 0.851, 0.41, 1.0));
 
    /**************************************************************************************/
    /** ___________________________________RGB 255 Colors._______________________________ */
    /**************************************************************************************/
 
    /** The rgb 255 color. */
    ColorItem TEST_COLOR_RGB255_1 = create(new RGB255Color(107, 69, 251));
 
    /** The rgb 255 color. */
    ColorItem TEST_COLOR_RGB255_2 = create(new RGB255Color(255, 248, 189, 70 / 100));
 
    /** The rgb 255 color. */
    ColorItem TEST_COLOR_RGB255_3 = create(new RGB255Color(112, 60, 63, 1.0));
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
 * Get more info at : www.jrebirth.org .
 * Copyright JRebirth.org © 2011-2013
 * Contact : sebastien.bordes@jrebirth.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jrebirth.af.core.resource.color;
 
import java.util.Arrays;
import java.util.List;
 
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
 
/**
 * The class <strong>WebColor</strong> used to create a Web Color.
 *
 * @author Sébastien Bordes
 */
public class WebColor extends AbstractBaseColor {
 
    /** The hexadecimal string value [0-9A-F]{6} (without 0x or #). */
    private final StringProperty hexProperty = new SimpleStringProperty();
 
    /**
     * Default Constructor.
     *
     * @param hex the hexadecimal value [0-9A-F]{6} (without 0x or #)
     */
    public WebColor(final String hex) {
        super();
        this.hexProperty.set(hex);
    }
 
    /**
     * Default Constructor.
     *
     * @param hex the hexadecimal value [0-9A-F]{6} (without 0x or #)
     * @param opacity the color opacity [0.0-1.0]
     */
    public WebColor(final String hex, final double opacity) {
        super(opacity);
        this.hexProperty.set(hex);
    }
 
    /**
     * Return the hexadecimal string value [0-9A-F]{6} (without 0x or #).
     *
     * @return Returns the hexadecimal value.
     */
    public String hex() {
        return this.hexProperty.get();
    }
 
    /**
     * @return Returns the hex property.
     */
    public StringProperty hexProperty() {
        return this.hexProperty;
    }
 
    /**
     * {@inheritDoc}
     */
    @Override
    public void parse(final String... parameters) {
        if (parameters.length >= 1) {
 
            String hexValue = parameters[0];
            if (hexValue.startsWith("0x")) {
                hexValue = hexValue.substring(2);
            }
            if (hexValue.charAt(0) == '#') {
                hexValue = hexValue.substring(1);
            }
 
            switch (hexValue.length()) {
 
                case 3: // 0x r g b
                    this.hexProperty.set(hexValue);
                    break;
 
                case 8: // 0x rr gg bb oo
                    // Not managed yet
                    break;
 
                case 6: // 0x rr gg bb
                default:
                    this.hexProperty.set(hexValue);
                    break;
 
            }
 
        }
        // Opacity
        if (parameters.length == 2) {
            opacityProperty().set(Double.parseDouble(parameters[1]));
        }
 
    }
 
    /**
     * {@inheritDoc}
     */
    @Override
    protected List<? extends Object> getFieldValues() {
        return Arrays.asList(hex(), opacity());
    }
 
}

But this interface doesn't explain how to register a resource, so let's see an example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
 * Get more info at : www.jrebirth.org .
 * Copyright JRebirth.org © 2011-2013
 * Contact : sebastien.bordes@jrebirth.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jrebirth.af.presentation.resources;
 
import static org.jrebirth.af.core.resource.Resources.create;
 
import org.jrebirth.af.api.resource.color.ColorItem;
import org.jrebirth.af.core.resource.color.RGB255Color;
import org.jrebirth.af.core.resource.color.WebColor;
 
/**
 * The class <strong>PrezColors</strong>.
 *
 * @author Sébastien Bordes
 *
 */
public interface PrezColors {
 
    /** Color for slide title, white. */
    ColorItem SLIDE_TITLE = create(new WebColor("FFFFFF", 1.0));
 
    /** Color for slide title, white. */
    ColorItem SUB_TITLE = create(new WebColor("FFF6EF", 1.0));
 
    /** Color for blue shape, xxx. */
    ColorItem SHAPE_BLUE = create(new WebColor("3495CE", 1.0));
 
    /** Color for drop shadow, black. */
    ColorItem DROP_SHADOW = create(new WebColor("000000", 0.8));
 
    /** Color for inner shadow, white. */
    ColorItem INNER_SHADOW = create(new WebColor("FFFFFE", 0.3));
 
    /** Color for first gradient, xxx. */
    ColorItem GRADIENT_1 = create(new WebColor("1AA2AC", 1.0));
 
    /** Color for second gradient, xxx. */
    ColorItem GRADIENT_2 = create(new WebColor("F04F24", 1.0));
 
    /** Color for third gradient, xxxx. */
    ColorItem GRADIENT_3 = create(new WebColor("FFF200", 1.0));
 
    /** Color for splash text, xxx. */
    ColorItem SPLASH_TEXT = create(new RGB255Color(60, 60, 70));
 
}

Font

For example to manage the Turtlesfont, you have to use this declaration:

37
38
/** The real font. */
FontItem TEST_REAL_FONT_1 = create(new RealFont(TestFontNames.Turtles, 10.0));

Hereafter an example of an interface that hold multiple colors.

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
 * The class <strong>TestFonts</strong>.
 *
 * @author Sébastien Bordes
 */
public interface TestFonts {
 
    /**************************************************************************************/
    /** ___________________________________Real Fonts.___________________________________ */
    /**************************************************************************************/
 
    /** The real font. */
    FontItem TEST_REAL_FONT_1 = create(new RealFont(TestFontNames.Turtles, 10.0));
 
    /** The real font. */
    FontItem TEST_REAL_FONT_2 = create(new RealFont(TestFontNames.Turtles, 12.0));
 
    /**************************************************************************************/
    /** _________________________________Family Fonts.___________________________________ */
    /**************************************************************************************/
 
    /** The family font. */
    FontItem TEST_FAMILY_FONT_1 = create(new FamilyFont("serif", 10.0, FontWeight.BOLD, FontPosture.ITALIC));
 
    /** The family font. */
    FontItem TEST_FAMILY_FONT_2 = create(new FamilyFont("sansserif", 16.0, FontWeight.BOLD));
 
    /** The family font. */
    FontItem TEST_FAMILY_FONT_3 = create(new FamilyFont("monospaced", 17.0, FontPosture.ITALIC));
 
    /** The family font. */
    FontItem TEST_FAMILY_FONT_4 = create(new FamilyFont("monospaced", 8.0));
 
}

Parameters