model-view
classes for building
highly maintainable and extensible composite components. This chapter explains
the model-view component architecture.
model
object and user-interface behavior into a view
object.
JBCL composite components extend the basic architecture by further separating out the display of data into an item painter
object, thus eliminating
from the view any need for a knowledge of data types.
The advantage of this further separation is the complete elimination
of data-type dependence in the view. A ListControl
can receive its list of items from an array of strings, from a data set
of a database, or from a file directory. The items themselves can be strings,
images, or proprietary data types. As long as the model object implements
the correct model interface (in this case, VectorModel
),
and the correct painters are available to display the items on the screen,
the ListControl
component functions well.
The VectorModel
interface is one of four interfaces defining
the model types in JBCL. They are discussed in Models
in JBCL.
Using a list as an example, the objects within the composite component work as shown here:
paint()
calls from the Java virtual machine and
calculates what needs painting.
Rectangle
in which to paint. The item painter paints the item in the Rectangle
.The simple 3-object (model, view, and item painter) structure works well as long as the data is always of a single, homogeneous type. Once the model begins to deliver multiple kinds of data to the view, the view needs multiple item painters to paint that data. The view cannot choose among these painters; it knows nothing about the data it passes except location.
Adding one more element, a view manager
, provides the capability of working with multiple data types. A view manager sits between the view and the item painters. For each request, it passes back to the view an item painter (or item editor
object) that handles the given data type.
paint()
calls from the Java virtual
machine and calculates what needs painting.
Object
, in this case a String
.
Object
.
The view manager examines the Object
, determines its type, and returns
a text item painter.
Object
and
a Rectangle
. The item painter paints the item on the screen.
Object
that is an Image
.
Object
.
In this implementation, the view manager examines the Object
, determines its type, and returns
an image item painter. In your own implementation of the ViewManger
interface, you could have the view manager behave differently.
Object
and a Rectangle
(the region for painting). The painter paints the
data item on the screen.View managers handle item editor objects as well as item painters. Because an editor must be data-type specific, it is more efficient to move the selection of editors out of the view and into the view manager.
As in the previous diagram, this list has both text and image items in it:
Object
at that location. This Object
is a String
.
Object
and a Rectangle
. In this implementation, the view manager examines the Object
, determines
its type, and returns the text editor object, which is instantiated (in
the case of text) in the specified Rectangle
. In your implementation of ViewManager
, you could have it behave differently.
ListControl
works, read Writing a composite component.