Skip to content

Commit 3c0f7b8

Browse files
committed
Font Resource Injector.
1 parent 91c9aa0 commit 3c0f7b8

File tree

6 files changed

+164
-3
lines changed

6 files changed

+164
-3
lines changed

gwt-material/src/main/java/gwt/material/design/client/base/ToastPosition.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
/*
2+
* #%L
3+
* GwtMaterial
4+
* %%
5+
* Copyright (C) 2015 - 2021 GwtMaterialDesign
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
120
package gwt.material.design.client.base;
221

322
public enum ToastPosition {

gwt-material/src/main/java/gwt/material/design/client/events/OpeningEvent.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
99
* You may obtain a copy of the License at
10-
*
10+
*
1111
* http://www.apache.org/licenses/LICENSE-2.0
12-
*
12+
*
1313
* Unless required by applicable law or agreed to in writing, software
1414
* distributed under the License is distributed on an "AS IS" BASIS,
1515
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -48,4 +48,4 @@ public Type<OpeningHandler> getAssociatedType() {
4848
protected void dispatch(OpeningHandler handler) {
4949
handler.onOpening(this);
5050
}
51-
}
51+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* #%L
3+
* GwtMaterial
4+
* %%
5+
* Copyright (C) 2015 - 2021 GwtMaterialDesign
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package gwt.material.design.client.font;
21+
22+
public class Font {
23+
24+
private String resourceUrl;
25+
private String name;
26+
27+
public Font() {
28+
}
29+
30+
public Font(String resourceUrl, String name) {
31+
this.resourceUrl = resourceUrl;
32+
this.name = name;
33+
}
34+
35+
public Font(String name) {
36+
this.name = name;
37+
}
38+
39+
public String getResourceUrl() {
40+
return resourceUrl;
41+
}
42+
43+
public void setResourceUrl(String resourceUrl) {
44+
this.resourceUrl = resourceUrl;
45+
}
46+
47+
public String getName() {
48+
return name;
49+
}
50+
51+
public void setName(String name) {
52+
this.name = name;
53+
}
54+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* #%L
3+
* GwtMaterial
4+
* %%
5+
* Copyright (C) 2015 - 2021 GwtMaterialDesign
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package gwt.material.design.client.font;
21+
22+
import com.google.gwt.dom.client.Document;
23+
import com.google.gwt.dom.client.Element;
24+
import com.google.gwt.dom.client.LinkElement;
25+
import com.google.gwt.dom.client.StyleElement;
26+
import gwt.material.design.client.base.DefaultHtmlSanitizer;
27+
28+
public class FontResourceInjector {
29+
30+
private static Element headElement;
31+
private static Font defaultFont;
32+
33+
public static void inject(Font font) {
34+
String name = getDefaultFont().getName();
35+
if (createFontUrlLink(font.getResourceUrl())) {
36+
name = font.getName();
37+
}
38+
39+
// Will generate a style tag containing the font name
40+
StyleElement styleElement = Document.get().createStyleElement();
41+
styleElement.setInnerSafeHtml(new DefaultHtmlSanitizer().sanitize("body * { font-family:" + name + ";}"));
42+
getHeadElement().appendChild(styleElement);
43+
}
44+
45+
public static Font getDefaultFont() {
46+
if (defaultFont == null) {
47+
defaultFont = new OpenSansFont();
48+
}
49+
return defaultFont;
50+
}
51+
52+
public static void setDefaultFont(Font defaultFont) {
53+
FontResourceInjector.defaultFont = defaultFont;
54+
}
55+
56+
protected static boolean createFontUrlLink(String resourceUrl) {
57+
if (resourceUrl != null) {
58+
LinkElement fontUrlLink = Document.get().createLinkElement();
59+
fontUrlLink.setHref(resourceUrl);
60+
fontUrlLink.setRel("stylesheet");
61+
getHeadElement().appendChild(fontUrlLink);
62+
return true;
63+
}
64+
return false;
65+
}
66+
67+
public static Element getHeadElement() {
68+
if (headElement == null) {
69+
headElement = Document.get().getElementsByTagName("head").getItem(0);
70+
}
71+
return headElement;
72+
}
73+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package gwt.material.design.client.font;
2+
3+
public class OpenSansFont extends Font {
4+
5+
public static String RESOURCE = "https://fonts.googleapis.com/css2?family=Open+Sans:wght@300;400;600;700&display=swap";
6+
public static String NAME = "Open Sans";
7+
8+
public OpenSansFont() {
9+
super(RESOURCE, NAME);
10+
}
11+
}

gwt-material/src/main/java/gwt/material/design/client/ui/MaterialToast.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,10 @@ public String getId() {
209209
return element != null ? (String) element.attr("id") : null;
210210
}
211211

212+
public void addWidget(Widget... widgets) {
213+
this.widgets = widgets;
214+
}
215+
212216
public JQueryElement getElement() {
213217
return element;
214218
}

0 commit comments

Comments
 (0)