/
ThemeManager.java
144 lines (123 loc) · 4.78 KB
/
ThemeManager.java
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* #%L
* GwtMaterial
* %%
* Copyright (C) 2015 - 2018 GwtMaterialDesign
* %%
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
* #L%
*/
package gwt.material.design.client.theme;
import com.google.gwt.core.client.GWT;
import gwt.material.design.client.base.MaterialWidget;
import gwt.material.design.client.theme.dark.DarkThemeLoader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;
@SuppressWarnings("unchecked")
public class
ThemeManager {
private static SortedSet<Theme> themes;
private static DarkThemeLoader darkTheme;
private static Map<Class, List<WidgetTheme>> themeCache = new HashMap<>();
public static void addTheme(Theme theme) {
if (!getThemes().contains(theme)) {
getThemes().add(theme);
for(WidgetTheme widgetTheme : theme.load()) {
widgetTheme.setTheme(theme);
addWidgetTheme(theme.getName(), widgetTheme);
}
} else {
GWT.log("Theme '" + theme.getName() + "' has already been " +
"loaded (ignoring '"+theme.getClass().getSimpleName()+"').");
}
}
public static SortedSet<Theme> getThemes() {
if (themes == null) {
themes = new TreeSet<>();
}
return themes;
}
public static List<WidgetTheme> getWidgetThemes(MaterialWidget object) {
return getWidgetThemes(object, object.getClass(), new ArrayList<>());
}
public static List<WidgetTheme> getWidgetThemes(MaterialWidget object, Class clazz, List<WidgetTheme> widgetThemes) {
List<WidgetTheme> cachedThemes = themeCache.get(clazz);
if (themes != null && cachedThemes == null) {
cachedThemes = new ArrayList<>();
for(Theme theme : themes) {
// Check by widgets class name first
WidgetTheme widgetTheme = theme.get(clazz.getName());
if (widgetTheme != null) {
cachedThemes.add(widgetTheme);
}
// Check by widget class selector
for (String styleName : object.getStyleName().split(" ")) {
WidgetTheme styleWidgetTheme = theme.get(styleName);
if (styleWidgetTheme != null) {
cachedThemes.add(styleWidgetTheme);
}
}
}
themeCache.put(clazz, cachedThemes);
}
if (cachedThemes != null) {
widgetThemes.addAll(cachedThemes);
}
// check the supertype
if (clazz.getSuperclass() != null && !clazz.getSuperclass().equals(MaterialWidget.class)) {
return getWidgetThemes(object, clazz.getSuperclass(), widgetThemes);
} else {
return widgetThemes;
}
}
public static List<WidgetTheme> applyLoad(MaterialWidget object) {
List<WidgetTheme> widgetThemes = getWidgetThemes(object);
for (WidgetTheme widgetTheme : widgetThemes) {
object = widgetTheme.onWidgetLoad(object);
}
return widgetThemes;
}
public static List<WidgetTheme> applyUnload(MaterialWidget object) {
List<WidgetTheme> widgetThemes = getWidgetThemes(object);
for (WidgetTheme widgetTheme : widgetThemes) {
object = widgetTheme.onWidgetUnload(object);
}
return widgetThemes;
}
public static Theme getThemeByName(String name) {
for (Theme theme : getThemes()) {
if (theme.getName().equals(name)) {
return theme;
}
}
return null;
}
public static <T extends MaterialWidget> void addWidgetTheme(String themeName, WidgetTheme<T> widgetTheme) {
Theme theme = getThemeByName(themeName);
assert theme != null : "cannot find theme with name " + themeName;
addWidgetTheme(theme, widgetTheme);
}
public static <T extends MaterialWidget> void addWidgetTheme(Theme theme, WidgetTheme<T> widgetTheme) {
theme.put(widgetTheme.getClassSelector(), widgetTheme);
}
public static DarkThemeLoader getDarkTheme() {
return darkTheme;
}
public static void setDarkTheme(DarkThemeLoader darkTheme) {
ThemeManager.darkTheme = darkTheme;
}
}