Java Reference
In-Depth Information
Listing 3.17 Processing shapes in ShapeTracker
private void processBundle(int action, Bundle bundle) {
Dictionary dict = bundle.getHeaders();
String name = (String) dict.get(SimpleShape.NAME_PROPERTY);
if (name == null) {
return;
}
Checks
if bundle
is an
extension
B
C
Adds shape to
paint frame
switch (action) {
case ADDED:
String iconPath = (String) dict.get(SimpleShape.ICON_PROPERTY);
Icon icon = new ImageIcon(bundle.getResource(iconPath));
String className = (String) dict.get(SimpleShape.CLASS_PROPERTY);
m_frame.addShape(name, icon,
new DefaultShape(m_context, bundle.getBundleId(), className));
break;
case REMOVED:
m_frame.removeShape(name);
break;
}
}
ShapeTracker overrides BundleTracker 's addedBundle() and removedBundle()
abstract methods to invoke processBundle() in either case. You determine whether
the bundle is an extension by probing its manifest for the Extension-Name property B .
Any bundle without this property in its manifest is ignored. If the bundle being added
contains a shape, the code grabs the metadata from the bundle's manifest headers and
adds the shape to the paint frame wrapped as a DefaultShape C . For the icon meta-
data, you use Bundle.getResource() to load it. If the bundle being removed contains
a shape, you remove the shape from the paint frame D .
DefaultShape , shown in listing 3.18, serves two purposes. It implements the
SimpleShape interface and is responsible for lazily creating the shape implementation
using the Extension-Class metadata. It also serves as a placeholder for the shape if
and when the shape is removed from the application. You didn't have to deal with this
situation in the original paint program, but now shape implementations can appear
or disappear at any time when bundles are installed, started, stopped, and uninstalled.
In such situations, the DefaultShape draws a placeholder icon on the paint canvas for
any departed shape implementations.
Removes
shape
D
Listing 3.18 DefaultShape example
class DefaultShape implements SimpleShape {
private SimpleShape m_shape;
private ImageIcon m_icon;
private BundleContext m_context;
private long m_bundleId;
private String m_className;
Default
constructor
public DefaultShape() {}
 
Search WWH ::




Custom Search