001: /*
002: * GWT-Ext Widget Library
003: * Copyright(c) 2007-2008, GWT-Ext.
004: * licensing@gwt-ext.com
005: *
006: * http://www.gwt-ext.com/license
007: */
008: package com.gwtext.client.core;
009:
010: import com.google.gwt.core.client.JavaScriptObject;
011: import com.gwtext.client.util.JavaScriptObjectHelper;
012:
013: import java.util.*;
014:
015: /**
016: * The Dom object spec. Can be configured with specs of children too. If no tag is specified then a div will
017: * be automatically generated with the specified attributes.
018: *
019: * @see DomHelper#append(String, DomConfig)
020: * @see ExtElement#createChild(DomConfig)
021: */
022: public class DomConfig {
023:
024: private String tag;
025: private String id;
026: private String cls;
027: private String style;
028: private String html;
029: private List children;
030:
031: private Map otherConfig = new HashMap();
032:
033: /**
034: * Create a new DomConfig using a "div" tag.
035: */
036: public DomConfig() {
037: this .tag = "div";
038: }
039:
040: /**
041: * Create a new DomConfig.
042: *
043: * @param tag the tag name
044: */
045: public DomConfig(String tag) {
046: this .tag = tag;
047: }
048:
049: /**
050: * Constructor.
051: *
052: * @param tag the element tag name
053: * @param id the element id
054: */
055: public DomConfig(String tag, String id) {
056: this .tag = tag;
057: this .id = id;
058: }
059:
060: /**
061: * Constructor.
062: *
063: * @param tag the element tag name
064: * @param id the element id
065: * @param cls the element CSS class name
066: */
067: public DomConfig(String tag, String id, String cls) {
068: this .tag = tag;
069: this .id = id;
070: this .cls = cls;
071: }
072:
073: /**
074: * Constructor.
075: *
076: * @param tag the element tag name
077: * @param id the element id
078: * @param cls the element CSS class name
079: * @param html the innerHTML for the element.
080: */
081: public DomConfig(String tag, String id, String cls, String html) {
082: this .tag = tag;
083: this .id = id;
084: this .cls = cls;
085: this .html = html;
086: }
087:
088: /**
089: * Set the CSS style.
090: *
091: * @param style the CSS style
092: */
093: public void setStyle(String style) {
094: this .style = style;
095: }
096:
097: /**
098: * Add a child element.
099: *
100: * @param child the child element config
101: * @return this
102: */
103: public DomConfig addChild(DomConfig child) {
104: if (html != null) {
105: throw new IllegalArgumentException(
106: "Dom spec cannot have inner html and child elelents");
107: }
108: if (children == null)
109: children = new ArrayList();
110: children.add(child);
111: return this ;
112: }
113:
114: public void addAttribute(String name, String value) {
115: otherConfig.put(name, value);
116: }
117:
118: public JavaScriptObject getJsObject() {
119: JavaScriptObject jsObj = JavaScriptObjectHelper.createObject();
120: if (tag != null)
121: JavaScriptObjectHelper.setAttribute(jsObj, "tag", tag);
122: if (id != null)
123: JavaScriptObjectHelper.setAttribute(jsObj, "id", id);
124: if (cls != null)
125: JavaScriptObjectHelper.setAttribute(jsObj, "cls", cls);
126: if (style != null)
127: JavaScriptObjectHelper.setAttribute(jsObj, "style", style);
128: if (html != null)
129: JavaScriptObjectHelper.setAttribute(jsObj, "html", html);
130:
131: for (Iterator iterator = otherConfig.keySet().iterator(); iterator
132: .hasNext();) {
133: String attribute = (String) iterator.next();
134: String value = (String) otherConfig.get(attribute);
135: JavaScriptObjectHelper
136: .setAttribute(jsObj, attribute, value);
137: }
138:
139: if (children != null) {
140: JavaScriptObject[] childrenJS = new JavaScriptObject[children
141: .size()];
142: int i = 0;
143: for (Iterator it = children.iterator(); it.hasNext(); i++) {
144: DomConfig config = (DomConfig) it.next();
145: childrenJS[i] = config.getJsObject();
146: }
147: JavaScriptObjectHelper.setAttribute(jsObj, "children",
148: childrenJS);
149: }
150: return jsObj;
151: }
152: }
|