Server-side components provide the API for user applications to build their
user interface. Many applications do not ever need to bother with the
client-side implementation of the standard components, but those that use
their own GWT widgets need to have corresponding server-side components.
A server-side component has two basic tasks: it has to be able to serialize
its state variables to the corresponding client-side component, and
deserialize any user input received from the client. Much of these tasks is
taken care of by the component framework.
Component Tag Name
Server-side components are identified with a unique UIDL tag name, which
must be returned by the getTag() method. The tag
should follow XML rules for element names, that is, only characters a-z,
A-Z, 0-9, and _, and not begin with a number. Actually, as IT Mill Toolkit
Release 5 uses a JSON notation for serialization, the tag syntax is more
relaxed, but we nevertheless recommend using a stricter syntax. UIDL is
detailed in Chapter 10, User Interface Definition Language (UIDL) together with lists of reserved
tags. The server-side implementation of the Color Picker component defines
the tag as follows:
public String getTag() {
return "colorpicker";
}
On the client side, this tag is mapped to a GWT widget. The mapping from
server-side to client-side components is actually one to many; a
server-side component can manifest as several client-side components,
depending on its parameters. For example, a server-side
Button can manifest either as an
IButton or ICheckBox in
client, depending on the switchMode attribute. For
the client side, see the section called “Google Web Toolkit Widgets” above.
The implementation of the server-side component is broken down into
server-client serialization and client-server deserialization in the following
sections. We will also present the complete example of the server-side
implementation of the Color Picker component below.
Client-Server Deserialization
The server-side implementation of a component must be able to serialize
its data into a UIDL message that is sent to the client.
/** Paint (serialize) the component for the client. */
public void paintContent(PaintTarget target) throws PaintException {
// Superclass writes any common attributes in the paint target.
super.paintContent(target);
// Add the currently selected color as a variable in the paint target.
target.addVariable(this, "colorname", getColor());
}
-- This section is unfinished --
Extending Standard Components
Extending standard components is one way to develop new components that
have some additional features.
Every component needs to have an UIDL tag that is used to create and
communicate with widgets on the client-side. The tag is normally unique for
server-side components.
-- This section is unfinished --
Example: Color Picker Server-Side Component
The following example provides the complete server-side
ColorPicker component for the Color Picker
example. It has only one state variable: the currently selected color,
which is stored as the property of the component. Implementation of the
Property interface is provided in the
AbstractField superclass of the component. The UIDL
tag name for the component is colorpicker and the state
is communicated through the colorname variable.
package com.itmill.toolkit.demo.colorpicker;
import java.util.Map;
import com.itmill.toolkit.terminal.PaintException;
import com.itmill.toolkit.terminal.PaintTarget;
import com.itmill.toolkit.ui.*;
public class ColorPicker extends AbstractField {
public ColorPicker() {
super();
setValue(new String("white"));
}
/** The property value of the field is an Integer. */
public Class getType() {
return String.class;
}
/** Tag is the UIDL element name for client-server communications. */
public String getTag() {
return "colorpicker";
}
/** Set the currently selected color. */
public void setColor(String newcolor) {
// Sets the color name as the property of the component.
// Setting the property will automatically cause repainting of
// the component with paintContent().
setValue(newcolor);
}
/** Retrieve the currently selected color. */
public String getColor() {
return (String) getValue();
}
/** Paint (serialize) the component for the client. */
public void paintContent(PaintTarget target) throws PaintException {
// Superclass writes any common attributes in the paint target.
super.paintContent(target);
// Add the currently selected color as a variable in the paint target.
target.addVariable(this, "colorname", getColor());
}
/** Deserialize changes received from client. */
public void changeVariables(Object source, Map variables) {
// Sets the currently selected color
if (variables.containsKey("colorname") && !isReadOnly()) {
String newValue = (String) variables.get("colorname");
// Changing the property of the component will
// trigger a ValueChangeEvent
setValue(newValue,true);
}
}
}