All Downloads are FREE. Search and download functionalities are using the official Maven repository.

xapi.dev.ui.html.HtmlGeneratorContext Maven / Gradle / Ivy

Go to download

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.dev.ui.html;

import static xapi.collect.X_Collect.newStringMap;
import static xapi.collect.X_Collect.newStringMultiMap;

import java.util.LinkedHashMap;
import java.util.Map;

import com.google.gwt.core.ext.typeinfo.JClassType;
import com.google.gwt.core.ext.typeinfo.JMethod;

import xapi.annotation.common.Property;
import xapi.annotation.compile.Import;
import xapi.collect.api.StringTo;
import xapi.collect.api.StringTo.Many;
import xapi.ui.html.api.Css;
import xapi.ui.html.api.El;
import xapi.ui.html.api.Html;
import xapi.ui.html.api.HtmlTemplate;

/**
 * A collection of data for a given class pertaining to the pertinent html
 * generator annotations that are discovered from any given type (a class plus
 * it's methods).
 *
 * @author "James X. Nelson ([email protected])"
 *
 */
public class HtmlGeneratorContext extends HtmlGeneratorNode {

  protected final StringTo allMethods = newStringMap(JMethod.class);
  protected final Many allCss = newStringMultiMap(Css.class);
  protected final JClassType cls;

  protected final StringTo allNodes = newStringMap(HtmlGeneratorNode.class);
  protected final StringTo myNodes = newStringMap(HtmlGeneratorNode.class);


  public HtmlGeneratorContext(JClassType cls) {
    super(cls);
    this.cls = cls;

    for (JClassType type : cls.getFlattenedSupertypeHierarchy()) {
      for (JMethod method : type.getMethods()) {
        String enclosingType = method.getEnclosingType().getQualifiedSourceName();
        if (enclosingType.equals(Object.class.getName())) {
          break;
        }
        if (method.isAbstract() || enclosingType.equals(cls.getQualifiedSourceName())) {
          HtmlGeneratorNode node = new HtmlGeneratorNode(method);
          if (!allNodes.containsKey(method.getName())) {
            allNodes.put(method.getName(), node);
          }
          if (method.getEnclosingType() == cls) {
            myNodes.put(method.getName(), node);
          }
        }
      }
    }

    allNodes.put("", this);
  }

  public void clear() {

    allCss.clear();
    allNodes.clear();
  }

  /**
   * @param html
   * @param name
   */
  public void addHtml(String name, Html html) {

    if (html == null)
      return;
    for (El el : html.body()) {
      addEl(name, el);
    }
    for (Css css : html.css()) {
      addCss(name, css);
    }
  }

  /**
   * @param template
   */
  public void addTemplate(String name, HtmlTemplate template) {

    allNodes.get(name).addTemplate(template);
  }

  /**
   * @param name
   * @param imports
   */
  public void addImports(String name, Import... imports) {

    if (imports.length > 0 &&
       (imports.length != 1 || imports[0] != null)) {
      allNodes.get(name).addImports(imports);
    }
  }

  /**
   * @param name
   * @param imports
   */
  public void addImport(String name, Import imports) {

    if (imports != null) {
      allNodes.get(name).addImports(imports);
    }
  }

  public void addCss(String name, Css css) {

    if (css != null) {
      allCss.get(Integer.toString(css.priority())).add(css);
    }
  }

  /**
   * @param name
   * @param el
   */
  public void addEl(String name, El el) {
    if (el != null) {
      allNodes.get(name).addEl(el);
    }
  }

  /**
   * @param name
   * @param method
   */
  public void addMethod(String name, JMethod method) {
    allMethods.put(name, method);
  }

  public Iterable getTemplates(String key) {
    return allNodes.get(key).getTemplates();
  }

  public Iterable getElements(String key) {
    return allNodes.get(key).getElements();
  }

  public Map getProperties(El el) {
    LinkedHashMap properties = new LinkedHashMap<>();
    for (Property prop : el.properties()) {
      properties.put(prop.name(), prop.value());
    }
    if (el.id().length()>0) {
      properties.put("id", el.id());
    }
    if (el.src().length()>0) {
      properties.put("src", el.src());
    }
    if (el.type().length()>0) {
      properties.put("type", el.type());
    }
    if (el.href().length()>0) {
      properties.put("href", el.href());
    }
    if (el.value().length()>0) {
      properties.put("value", el.value());
    }
    if (el.rel().length()>0) {
      properties.put("rel", el.rel());
    }
    return properties;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy