3.4 Implementing Your Own Metawidget

Metawidget creates widgets native to a particular UI framework. If your chosen UI framework is not supported 'out of the box', you may need to implement your own.

Metawidgets are not required to extend any base class or implement any interface. However, it is recommended developers familiarize themselves with existing Metawidgets (such as UIMetawidget) to make their API similar. In particular, it is recommended Metawidget APIs use the 'direction of inheritance' for 'overriding choice of widget' with other concerns (such as layout and binding) implemented orthogonally as in Figure 3.2.

Figure 3.2. Recommended Metawidget API Design

Recommended Metawidget API Design

Whilst there is no one Metawidget base class, a number of convenience classes are provided:

MetawidgetMixin

Mixins are a frequently used workaround for multiple inheritance in Java. Classes extend the mixin as an inner class and override its methods as needed.

All the built-in Metawidgets use MetawidgetMixin to ease their implementation. The mixin provides pre-built functionality such as deciding when to use single versus compound widgets, support for overriding widgets, incorporating stubs, and changing between read-only and active modes.

Android

For frameworks based on Android, org.metawidget.android.widget.AndroidMetawidget provides many hooks to override widget creation, most notably AndroidMetawidget.buildActiveWidget.

Android already defines a separation between Views and ViewGroups. org.metawidget.android.widget.layout.Layout and its subclasses automate the use of existing Android ViewGroups.

GWT

For frameworks based on GWT, org.metawidget.gwt.client.ui.GwtMetawidget provides many hooks to override widget creation, most notably GwtMetawidget.buildActiveWidget.

Java Server Faces

For frameworks based on JSF, org.metawidget.faces.component.UIMetawidget provides base widget functionality. See org.metawidget.faces.component.html.HtmlMetawidget for example usage.

JSF already defines a clean separation between widgets and their renderers. org.metawidget.faces.renderkit.LayoutRenderer and its subclasses leverage this to support different layouts.

Java Server Pages

For frameworks based on JSP, org.metawidget.jsp.tagext.MetawidgetTag and the more commonly used org.metawidget.jsp.tagext.html.BaseHtmlMetawidgetTag provide base taglib functionality. See StrutsMetawidgetTag for example usage.

MetawidgetTag also defines a clean separation between choosing widgets and laying them out. org.metawidget.jsp.tagext.Layout and its subclasses can perform the layout for all JSP-based frameworks.

Swing

For frameworks based on Swing, org.metawidget.swing.SwingMetawidget provides many hooks to override widget creation, most notably SwingMetawidget.buildActiveWidget.

Swing already defines a clean separation between widgets and layout managers. org.metawidget.swing.layout.Layout and its subclasses automate the use of existing Swing LayoutManagers.