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-dev Show documentation
Show all versions of xapi-dev Show documentation
Everything needed to run a comprehensive dev environment.
Just type X_ and pick a service from autocomplete;
new dev modules will be added as they are built.
The only dev service not included in the uber jar is xapi-dev-maven,
as it includes all runtime dependencies of maven, adding ~4 seconds to build time,
and 6 megabytes to the final output jar size (without xapi-dev-maven, it's ~1MB).
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 cls, T obj) {
return this.toElementBuilder(cls).convert(obj).getElement();
}
@Override
public E toElement(Class cls, Class template, T obj) {
return this.toElementBuilder(cls, template).convert(obj).getElement();
}
public ConvertsValue> toElementBuilder(final Class 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 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);
}
};
}
}