![]() |
![]() |
||||||
|
|||||||
As the Inspector example shows, it is straightforward to describe objects in markup, and pass them to objects of arbitrary classes in a type-safe manner as bean properties. The only requirement for this is that the receiving object provide a property setter method that accepts an object of the appropriate type for each property. While this mechanism for setting bean properties is general and quite easy to implement, there is an even easier way to associate arbitrary objects with presentation components, in particular. This other approach does not require you to code special property setter methods. The tradeoff is that in your Java™ code, you must either infer the type of the property from its context or discover it through reflection. Still, the simplicity of this approach is worth the price in many cases. Properties set using this mechanism are called client properties. Client Properties All standard presentation components (excluding Box in older versions of the JRE) support keyed associations of arbitrary objects as client properties, which can subsequently be retrieved using a simple key lookup. The syntax for client properties is identical to that for bean properties — A client property is also represented by the property element type, and the key used to perform a lookup is specified through the key attribute of a property element. The property value may be specified inline within the property element as its element content. Alternatively, you may use the href attribute from the XLink namespace to associate a remote resource with the property element. In either case, you must attach the property to a presentation component by adding the property element to the element representing the presentation component. Once a property has been attached to a presentation component, you can look up the property by invoking getClientProperty() on the realized form of the presentation component. The key passed to this method must match the key specified in the markup. That key is considered to be a part of the public interface of the object that performs the lookup because it forms a contract between the markup and the object’s class. The following code fragment shows how easy it is to attach properties to components using the property element:
<button title="Place Order">
<property key="nextPage">checkout.xml</property> </button> Assuming that the realized form of the button is called myButton in your Java code, you can retrieve the property as follows:
String uri =
(String) myButton.getClientProperty("nextPage");
In this case, the value of the uri variable will be equal to the string "checkout.xml". |
|||||||
|
Copyright © 2002 eNode, Inc. All Rights Reserved. |