Sencha Inc. | HTML5 Apps

Blog

Ext GWT 3.0 Appearance Design

June 28, 2011 | Darrell Meyer

Ext GWT 3.0 Appearance Design

Currently in Ext GWT 2.0, widgets are responsible for creating their DOM structure directly. This is done either by manually creating the elements or by using an HTML fragment. The HTML can be generated directly or using an XTemplate. The CSS class names are then applied to the elements by the widgets. With this approach, a widget’s view is tightly bound to the widget itself, and CSS class names are generally hardcoded into the widget.

Although this approach works, there are a few limitations. First, it is very difficult to change the DOM structure of a component because the widget is tightly bound to the its current DOM structure. Second, it’s also difficult to change the style or look and feel of a widget because the CSS styles are part of the widget and are added directly to the widget’s elements.

With Ext GWT 3.0, we are introducing a new way of rendering the view and styling a widget. This approach is very flexible and has many advantages to the previous method. It supports swapping in different DOM structures (markup) with different styles.

The design revolves around a concept called an Appearance which is based on a new design introduced by Google. An Appearance is simply a class that controls the HTML structure and style of a view implementation for a widget. An Appearance is a design pattern, rather than a concrete implementation.

The best way to show how Appearances work is to walk though an example. There are many moving parts, but once you are comfortable with the concepts, it is very straightforward.

First, we create a simple widget that creates its DOM manually, and the CSS class names are assigned in the widget.

public class PushButton extends Component implements HasClickHandlers {
 
  private Element imageWrap;
  private Element textWrap;
 
  public PushButton(String text) {
	setElement(DOM.createDiv());
 
	imageWrap = DOM.createDiv();
	imageWrap.setClassName("testImage");
	getElement().appendChild(imageWrap);
 
	textWrap = DOM.createDiv();
	textWrap.setClassName("testText");
	getElement().appendChild(textWrap);
 
	setText(text);
	setStyleName("testButton");
	sinkEvents(Event.ONCLICK);
  }
 
  public void setText(String text) {
	textWrap.setInnerText(text);
  }
 
  public void setIcon(Image icon) {
	imageWrap.setInnerHTML("");
	imageWrap.appendChild(icon.getElement());
  }
 
  @Override
  public HandlerRegistration addClickHandler(ClickHandler handler) {
	return addDomHandler(handler, ClickEvent.getType());
  }
}
 
public class Test implements EntryPoint {
  @Override
  public void onModuleLoad() {
	PushButton button = new PushButton("Click Me");
	button.setIcon(new Image(ExampleImages.INSTANCE.add16()));
	button.setWidth(100);
	button.addClickHandler(new ClickHandler() {
  	  @Override
  	  public void onClick(ClickEvent event) {
    	    Info.display("Message", "The button was clicked");
  	  }
	});
	RootPanel.get().add(button);
  }
}
 
.testButton {
  border: 1px solid navy;
  font-size: 12px;
  padding: 4px;
}
 
.testButton .testImage {
  float: left;
}
 
.testButton .testText {
  text-align: center;
}

Here is a screen shot of the widget: Widget in Ext GWT 3

PushButton is a simple widget that displays an icon and text and fires a click event when fired. In the constructor, we manually create the DOM elements and assign the CSS class names.

Although this approach works, there are a few issues. First, since the widget creates the DOM directly, it is very difficult to change the appearance. In addition, the widget interacts with the elements directly as in the setText method. Also, the CSS for the widget is in an external CSS file. It will be better if the CSS could be part of a GWT CssResource to take advantage of the optimizations and features CssResource provides.

Now, let’s take this example and apply the Appearance design to it.

Create Appearance Interface

The first step is to create an interface that defines the interaction between the widget and an appearance instance.

public interface Appearance {
	void render(SafeHtmlBuilder sb);
	void onUpdateText(XElement parent, String text);
	void onUpdateIcon(XElement parent, Image icon);
  }

When the render method is called by the parent widget, the Appearance returns the HTML markup for the widget. This is done via a SafeHtmlBuilder instance which facilitates the building up of XSS-safe HTML from text snippets. The onUpdateText and onUpdateIcon methods are called when the Appearance needs to update the button’s text and icon.

Default Appearance

Next, we create the default implementation of the Appearance interface.

public static class DefaultAppearance implements Appearance {
 
	public interface Template extends XTemplates {
  	  @XTemplate(source = "DefaultAppearance.html")
  	  SafeHtml template(Style style);
	}
 
	public interface Style extends CssResource {
 
  	  String testButton();
 
  	  String testButtonText();
 
  	  String testButtonImage();
	}
 
	private final Style style;
	private final Template template;
 
	public interface Resources extends ClientBundle {
 
  	  @Source("DefaultAppearance.css")
  	    Style style();
	  }
 
	public DefaultAppearance() {
  	  this((Resources) GWT.create(Resources.class));
	}
 
	public DefaultAppearance(Resources resources) {
  	  this.style = resources.style();
  	  this.style.ensureInjected();
 
  	  this.template = GWT.create(Template.class);
	}
 
	@Override
	public void onUpdateIcon(XElement parent, Image icon) {
  	  XElement element = parent.selectNode("." + style.testButtonImage());
  	  element.removeChildren();
  	  element.appendChild(icon.getElement());
	}
 
	@Override
	public void onUpdateText(XElement parent, String text) {
  	  parent.selectNode("." + style.testButtonText()).setInnerText(text);
	}
 
	@Override
	public void render(SafeHtmlBuilder sb) {
  	  sb.append(template.template(style));
	}
 
  }

There are many parts to the default appearance. Let’s walk through them:

Style

We define our Style interface which is a CssResource. We will define the CSS that will be used by our component. By using CssResource, we are associating the CSS to our component directly rather than referencing styles pulled from an external style sheet. The CSS will be minimized and obfuscated. In addition, the CSS will be part of the application download and not retrieved via an additional HTTP request. If this widget is not used by an application, the CSS will not be part of the download. This is a vast improvement over the monolithic CSS file used in previous versions of Ext GWT.

public interface Style extends CssResource {
 
  	String testButton();
 
  	String testButtonText();
 
  	String testButtonImage();
}

Resource ClientBundle

Next, we create a Resources ClientBundle which will serve up any resources our widget needs.

public interface Resources extends ClientBundle {
  @Source("DefaultAppearance.css")
  Style style();
}

The CSS of DefaultAppearance is identical to the CSS used by our first example.

XTemplate

The Appearance uses an XTemplate to generate the DOM structure. The XTemplate is passed an instance of Style to be applied to the markup.

public interface Template extends XTemplates {
  @XTemplate(source = "DefaultAppearance.html")
  SafeHtml template(Style style);
}
    <div class="{style.testButton}">
      <div class="{style.testImage}"></div>
      <div class="{style.testText}"></div>
    </div>

Take notice in how the class names are pulled from the Style instance passed to the template. This is important as the actual CSS class names will be obfuscated.

Constructors

We then create two constructors, one that accepts an Appearance instance, and one that creates a default appearance instance .

Using GWT.create is important as it allows different Resource instances to be specified using deferred binding rules.

public DefaultAppearance() {
   this((Resources) GWT.create(Resources.class));
}
 
public DefaultAppearance(Resources resources) {
   this.style = resources.style();
   this.style.ensureInjected();
 
   this.template = GWT.create(Template.class);
}

Next we create our Template instance using GWT.create().

Appearance Implementation

We implement the methods of the Appearance interface.

@Override
  public void onUpdateIcon(XElement parent, Image icon) {
    XElement element = parent.selectNode("." + style.testButtonImage());
    element.removeChildren();
    element.appendChild(icon.getElement());
  }
 
  @Override
  public void onUpdateText(XElement parent, String text) {
    parent.selectNode("." + style.testButtonText()).setInnerText(text);
  }
 
  @Override
  public void render(SafeHtmlBuilder sb) {
    sb.append(template.template(style));
  }

The render method uses the XTemplate to generate the HTML markup. The text and image methods are implemented by working against the parent element that is passed to the methods.

Appearances are not passed the parent widget and should not maintain state, so they can be reused and also be used without the parent widget as in GWT Cells. For example, you may want to insert a push button into a grid cell.

Now that we have defined our Appearance and have a concrete implementation, we can implement the widget.

private final Appearance appearance;
 
  public AppearancePushButton(String text) {
	this(text, (Appearance) GWT.create(DefaultAppearance.class));
  }
 
  public AppearancePushButton(String text, Appearance appearance) {
	this.appearance = appearance;
 
	SafeHtmlBuilder sb = new SafeHtmlBuilder();
	this.appearance.render(sb);
 
        setElement(XDOM.create(sb.toSafeHtml()));
	setText(text);
	sinkEvents(Event.ONCLICK);
  }
 
  @Override
  public HandlerRegistration addClickHandler(ClickHandler handler) {
	return addDomHandler(handler, ClickEvent.getType());
  }
 
  public void setText(String text) {
	appearance.onUpdateText(getElement(), text);
  }
 
  public void setImage(Image icon) {
	appearance.onUpdateIcon(getElement(), icon);
}

You will notice that the widget is not bound to the DOM or styles of the Appearance directly. Rather, the widget delegates its view work to the appearance.

Now, using deferred binding, or directly, push button can work with different appearances. There are several reason to have multiple appearances:

Different browser capabilities. You may have an Appearance that uses CSS3 gradients and CSS3 rounded corners and a different appearance that uses images and a different DOM structure to look the same as the CSS3 version.

You can have different appearances for different devices such as desktop and mobile. This is great as you can have a single widget that works in both desktop and browser rather than having two different components.

Theming is also supported. A theme module can override the default styles or markup used by a widget.

Summary

The Appearance design is a very flexible way to generate the view for a widget. The customizations can happen at three levels:

  • Different Appearances
  • Different Resources within an Appearance
  • Different XTemplates within an Appearance

All three axes can be controlled using deferred binding rules and programmatically.

An example rule might look like this:

<replace-with class="PushButton.CustomAppearance">
<when-type-is class="PushButton.Appearance">
<any>
<when-property-is name="user.agent" value="ie6">
<when-property-is name="user.agent" value="ie8">
</when-property-is></when-property-is></any>
</when-type-is></replace-with>

We are applying the Appearance pattern to all widgets in the library. This will result in both better performance and customization possibilities.

There are 10 responses. Add yours.

Alain Ekambi

3 years ago

Me too….

Darrell Meyer Sencha Employee

3 years ago

You are both correct. Our implementation is based on Appearance design introduced by GWT and documented in the CellBackedWidgets wiki article:

http://code.google.com/p/google-web-toolkit/wiki/CellBackedWIdgets#Appearance_Pattern

Parama

3 years ago

When is EXT GWT 3.0 scheduled to be released?

andr83

3 years ago

Looks interesting smile
And when can we expect ExtGWT 3 beta?

Darrell Meyer

3 years ago

3.0 Developer Preview 1 is now available: http://www.sencha.com/blog/ext-gwt-3-dev-preview-1/

Haixu Huang

3 years ago

Exciting! Waiting for Ext GWT 3.0 for so long while.

Baskor

3 years ago

Can we expect a touch/mobile theme in the future?

Dimitris

3 years ago

I for one don’t like it. You have effectively turned a lean widget into a monster. Compare the lines of code and the number of different artifacts that have to be put together to form a single widget. I am not 100% against the idea. Pluggable CSS resources is a desirable thing, but moving the entire *structure* out of the widget is not.

Ordenadores baratos

3 years ago

I would love a touch theme too!
Any chance we can get it?

Comments are Gravatar enabled. Your email address will not be shown.

Commenting is not available in this channel entry.