This tutorial describes how to build your own custom widgets for the Insert/Edit form. In this example, we will create a simple TextArea widget. More complex widgets can simply build on the concepts covered in this tutorial. If you wish to build more complex widgets, I recommend reviewing the existing widgets in the console. This tutorial is meant to demonstrate the extensibility concepts.
org.scrashmeow.ojb.console.ui.control.widget.Widget
. It is preferred that
the appropriate base class is chosen depending on what kind of field the widget is targeted for.
For example: FieldDescriptorBasedWidget
, CollectionDescriptorBasedWidget
, ObjectReferenceDescriptorBasedWidget
are
all available in addition to the base class of all widgets BaseWidget
.
For the textarea widget, this widget is targeted torwards FieldDescriptors
, so we will extend the FieldDescriptorBasedWidget
.
Our textarea widget will allow for certain properties to override the look and feel of the widget. We have
chosen to support the rows
and cols
properties that correspond the the rows,cols attributes
of the <textarea> html tag. In order to support these attributes, we must simply provide JavaBean getter/setters
for each of the attributes.
Here is the sample Java code for the widget:
package org.scrashmeow.ojb.console.ui.control.widget; /** * A Sample Text Area Widget * * @author Alexander_Bibighaus */ public class TextAreaWidget extends FieldDescriptorBasedWidget { public TextAreaWidget() { super(); } /** * The getter for the cols attribute * * @return Integer */ public Integer getCols() { if (getAttribute("cols") == null) return null; return Integer.valueOf((String) getAttribute("cols")); } /** * The getter for the rows attribute * * @return Integer */ public Integer getRows() { if (getAttribute("rows") == null) return null; return Integer.valueOf((String) getAttribute("rows")); } /** * The setter for the rows attribute * * @param rows */ public void setRows(Integer rows) { setAttribute("rows", rows.toString()); } /** * The setter for the cols attribute * * @param cols */ public void setCols(Integer cols) { setAttribute("cols", cols.toString()); } }
Each tile is passed a bean under the name widget
. You may access the bean properties of these
widgets using the Struts HTML tags.
Each widget should pass the value the widget collected via the value found in the formName
attribute.
Below is an example of our TextArea tile
<!-- include the global include that all console jsp's should include --> <%@ include file="/includes/all.jsp" %> <!-- import our widget's bean (ie the class we just implemented ) --> <tiles:importAttribute name="widget" /> <!-- display our widget using the Struts-EL HTML Tag lib --> <html-el:textarea rows="${widget.rows}" cols="${widget.cols}" property="${widget.formName}" />
The TextArea's tile definitions looks like the following:
<definition name=".textareaWidget" path="/widgets/textarea.jsp"></definition>
The following snippet demonstrates how to define our Text Area widget.
<widget-definitions> ... <widget-definition id="textarea" definition=".textareaWidget" className="org.scrashmeow.ojb.console.ui.control.widget.TextAreaWidget" /> ... </widget-definitions>
<widget-associations> <widget-association type="LONGVARCHAR" widgetId="textarea" /> </widget-associations>
<class-descriptor name="org.scrashmeow.ojb.content.News"> <field-descriptor name="body"> <widget-descriptor id="textarea"> <widget-param> <name>rows</name> <value>10</value> </widget-param> <widget-param> <name>cols</name> <value>40</value> </widget-param> </widget-descriptor> </field-descriptor> </class-descriptor>
Copyright © 2003 Alexander Bibighaus et al. All rights Reserved.