Table of Contents Previous Section

Simplifying Interfaces

Another benefit of reusable components is that they let you work at a higher level of abstraction than would be possible by working directly with HTML code or with WebObjects' dynamic elements. You (or someone else) can create a component that encapsulates a solution to a possibly complicated programming problem, and then reuse that solution again and again without having to be concerned with the details of its implementation. Examples of this kind of component include:

To illustrate this feature, consider a simple reusable component, an alert panel like the one shown in Figure 28.

Figure 28. An Alert Panel

This panel is similar to the navigation table shown in Figure 27, but as you'll see, most of the component's attributes are customizable.

To use this component, you simply declare its position within the HTML page and give it a name:

	<HTML>
	<HEAD>
		<TITLE>Alert</TITLE>
	</HEAD>
	<BODY>

		<WEBOBJECT NAME = "ALERT"></WEBOBJECT>

	</BODY>
	</HTML>
The declarations file specifies the value for each of the panel's attributes, either by assigning a constant value or by binding the attributes value to a variable in the component's code (as with the alertString and infoString attributes here):

	ALERT: AlertPanel {
		alertString = alertTitle;
		alertFontColor = "#A00000";
		alertFontSize = 6;
		infoString = alertDescription;
		infoFontSize = 4;
		infoFontColor = "#500000";
		tableWidth = "50%";
	};
The component's code defines the alertTitle and alertDescription instance variables or methods, which set the text that's displayed in the upper and lower panes of the alert panel. The alertDescription method could, for example, consult a database to determine the release date of the video.

WebObjects Builder makes working with reusable components such as AlertPanel even easier. Component clients can simply drag the alert panel into their components and use the Inspector to set the bindings. They don't need to manually edit the declarations file to set these bindings. To set this up, you, as the component creator, edit a file named AlertPanel.api specifying both required and optional attributes. You could, for example, export only the alertTitle and infoString attributes (leaving the other attributes private) using this AlertPanel.api file:

	Required = (alertTitle, infoString);
	Optional = ();
See the WebObjects Tools and Techniques online book for more information.

AlertPanel is one of several components included in a sample application called ReusableComponents. This application demonstrates and documents how to create and use reusable components. If you look at the source code for AlertPanel, you'll notice that it's moderately complicated and, in fact, relies on other reusable components for its implementation. However, WebObjects lets you think of the AlertPanel component as a black box. You simply position the component in your HTML template, specify its attributes in the declarations file, and implement any associated dynamic behavior in your code.

Table of Contents Next Section