The Java half of the pie chart is defined in the ULCPieChart class. In contrast to UIPieChart, you can define ULCPieChart in a package of your choice. The ULC implementation uses the convention of prefixing faceless class names with the prefix ULC. When the faceless widget creates its UI counterpart, it passes the type name of the widget to be created to the UI Engine. By default, the type name corresponds to the class name without the ULC prefix. If desired, this default can be changed by overriding the typeString() method defined in the UlcProxy class.
Faceless half objects must retain the state of their corresponding UI halves. To do so, ULCPieChart stores the values, labels, and colors of the pie chart widget in its instance variables:
public class ULCPieChart extends ULCComponent { protected double[] fValues; protected String[] fColors; protected String[] fLabels; int fWidth; int fHeight; }
ULCPieChart identifies its UI half by implementing the typeString() method, as follows:
public String typeString() { return "com.ibm.ulc.examples.pieExtension.UIPieChart"; }
ULCPieChart has to implement the UI Engine requests symmetrically. First, it must transfer the widget state to the UI Engine; this is done by overriding the saveState() method. saveState() packages the arguments kept in its instance variables into an Anything:
public void saveState(Anything a) { super.saveState(a); a.put("w", fWidth); a.put("h", fHeight); Anything data= new Anything(); fillData(data); a.put("data", data); }
The setData() method is implemented in ULCPieChart as follows:
public void setData(String[] labels, double[] values, String[] colors) { fValues= new double[values.length]; fColors= new String[colors.length]; fLabels= new String[labels.length]; System.arraycopy(labels, 0, fLabels, 0, labels.length); System.arraycopy(values, 0, fValues, 0, values.length); System.arraycopy(colors, 0, fColors, 0, colors.length); Anything data= new Anything(); fillData(data); sendUI("setData", data); }
A request method needs only to package its arguments into an Anything. In this case, the arrays with the values, labels, and colors are wrapped into the Anything structure which is expected by the UI half. Then, the request is sent to the UI with sendUI(), which transmits the request name and the Anything argument to the UI Engine.
The last step is to handle action events from UIPieChart; to do this, you override handleRequest(). This method receives the name of the request with an Anything that stores the request data:
public void handleRequest(ORBConnection conn, String request, Anything args) { if (request.equals("event")) { // (1) String type= args.get("type", "???"); if (type.equals("action")) // (2) distributeToListeners(new ULCActionEvent(this, args.get("cmd", "???"))); // (3) return; } super.handleRequest(conn, request, args); // (4) }
These steps are implemented in handleRequest():