xapi.elemental.impl.ElementalServiceDefault Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xapi-gwt Show documentation
Show all versions of xapi-gwt Show documentation
This module exists solely to package all other gwt modules into a single
uber jar. This makes deploying to non-mavenized targets much easier.
Of course, you would be wise to inherit your dependencies individually;
the uber jar is intended for projects like collide,
which have complex configuration, and adding many jars would be a pain.
The newest version!
/**
*
*/
package xapi.elemental.impl;
import com.google.gwt.core.shared.GwtIncompatible;
import elemental.dom.Element;
import xapi.annotation.inject.SingletonDefault;
import xapi.elemental.api.ElementalService;
import xapi.elemental.api.PotentialNode;
import xapi.source.api.Lexer;
import xapi.source.impl.LexerForWords;
import xapi.source.impl.StringCharIterator;
import xapi.ui.html.X_Html;
import xapi.ui.html.impl.StyleServiceDefault;
import xapi.util.api.ConvertsValue;
/**
* @author "James X. Nelson ([email protected])"
*/
@SingletonDefault(implFor=ElementalService.class)
public class ElementalServiceDefault
extends StyleServiceDefault
implements ElementalService {
private Lexer lexer;
@Override
public E toElement(Class super T> cls, T obj) {
return this.toElementBuilder(cls).convert(obj).getElement();
}
@Override
public E toElement(Class super T> cls, Class> template, T obj) {
return this.toElementBuilder(cls, template).convert(obj).getElement();
}
public ConvertsValue> toElementBuilder(final Class super T> cls) {
return toElementBuilder(cls, cls);
}
static class UnsupportedConverter implements ConvertsValue {
@Override
public E convert(T from) {
throw new UnsupportedOperationException();
}
}
@Override
public ConvertsValue> toElementBuilder(final Class super T> cls, Class> template) {
return new UnsupportedConverter>() {
@SuppressWarnings({
"unchecked", "rawtypes"
} )
@Override
@GwtIncompatible
public PotentialNode convert(T from) {
Class c = cls;// Erase generics; gwt doesn't like them much
return new PotentialNode(X_Html.toHtml(template, c, from, ElementalServiceDefault.this));
}
};
}
@Override
public void loadGoogleFonts(String ... fonts) {
StringBuilder b = new StringBuilder(
"@import url("
+ "https://fonts.googleapis.com/css?family=");
for (int i = 0; i < fonts.length; i++) {
String font = fonts[i];
if (i > 0) {
b.append("|");
}
// TODO proper uri encoding later
b.append(font.replace(' ', '+'));
}
b.append(");");
addCss(b.toString(), 0);
}
@Override
public String enhanceMarkup(String markup) {
if (lexer == null) {
return markup;
}
try {
return lexer.lex(markup).toString();
} finally {
lexer.clear();
}
}
/**
* @return the lexer
*/
public Lexer getLexer() {
return lexer;
}
/**
* @param lexer the lexer to set
*/
@Override
public void setLexer(Lexer lexer) {
this.lexer = lexer;
}
@Override
public PotentialNode newNode(String tagname) {
return new PotentialNode(tagname);
}
@Override
public PotentialNode newNode(E node) {
return new PotentialNode(node);
}
@Override
public PotentialNode newNode() {
return new PotentialNode();
}
@Override
public E initialize(E element) {
return element;
}
@Override
public ConvertsValue asConverter() {
return new ConvertsValue() {
@Override
public E convert(E from) {
return initialize(from);
}
};
}
}