There are a few constraints that led to the current API. First, there
are dozens of element types, many of which have unique methods.  Dumping
all of these methods into one uber ElementBuilder would mean a class
with hundreds of methods, and we would need a lot of runtime checks to
ensure the HTML is valid.  That in turn requires a lot of state
checking, which is prone to errors.  I'm a fan of restricting users by
restricting the API.

Additionally, we have to deal with Java type casting.  end() cannot
return the previous builder unless it is parameterized to do so, but
then the return value also need to be parameterized.  You end up with an
unbounded chain of parameterized types, which Java doesn't support.

One option to fix the end() thing is to take the previous builder as an
argument.
DivElementBuilder div = new DivElementBuilder();
div.id("myDiv").startSelect().startOption("test").end().end(DivElementBuilder.class).startSelect();

In the case above, end() would return an ElementBuilderBase, which might
be enough.  Its a little more verbose, but not too bad.  Users can
choose to chain builders or to break them apart into chunks, or a
combination of both.


http://gwt-code-reviews.appspot.com/1455802/diff/1/user/src/com/google/gwt/dom/builder/shared/ElementBuilderBase.java
File user/src/com/google/gwt/dom/builder/shared/ElementBuilderBase.java
(right):

http://gwt-code-reviews.appspot.com/1455802/diff/1/user/src/com/google/gwt/dom/builder/shared/ElementBuilderBase.java#newcode48
user/src/com/google/gwt/dom/builder/shared/ElementBuilderBase.java:48: T
appendText(String text);
On 2011/06/09 22:52:28, skybrian wrote:
Perhaps just "text" instead of "appendText"?

something.startDiv().text("Some text").endDiv();

Done.

http://gwt-code-reviews.appspot.com/1455802/diff/1/user/src/com/google/gwt/dom/builder/shared/ElementBuilderFactory.java
File
user/src/com/google/gwt/dom/builder/shared/ElementBuilderFactory.java
(right):

http://gwt-code-reviews.appspot.com/1455802/diff/1/user/src/com/google/gwt/dom/builder/shared/ElementBuilderFactory.java#newcode62
user/src/com/google/gwt/dom/builder/shared/ElementBuilderFactory.java:62:
public abstract DivElementBuilder createDivElementBuilder();
On 2011/06/09 22:52:28, skybrian wrote:
Hmm... I wonder if we should have another class with static methods to
kick this
off?

Elements.startDiv().text("Hello!").endDiv().toSafeHtml();

This is the class to kick things off.  The methods aren't static because
we want it to be easy to mock, but you can only access the singleton
instance via ElementBuilderFactory.get().

http://gwt-code-reviews.appspot.com/1455802/

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Reply via email to