Java Reference
In-Depth Information
public DefaultShape(BundleContext context, long bundleId,
String className) {
m_context = context;
m_bundleId = bundleId;
m_className = className;
}
Constructor with
extension data
public void draw(Graphics2D g2, Point p) {
if (m_context != null) {
try {
if (m_shape == null) {
Bundle bundle = m_context.getBundle(m_bundleId);
Class clazz = bundle.loadClass(m_className);
m_shape = (SimpleShape) clazz.newInstance();
}
m_shape.draw(g2, p);
return;
} catch (Exception ex) {}
}
Creates
extension and
delegates to it
if available
if (m_icon == null) {
try {
m_icon = new ImageIcon(this.getClass().getResource(
"underc.png"));
} catch (Exception ex) {
ex.printStackTrace();
g2.setColor(Color.red);
g2.fillRect(0, 0, 60, 60);
return;
}
}
g2.drawImage(m_icon.getImage(), 0, 0, null);
}
}
In summary, when the paint application is started, its activator creates and opens a
ShapeTracker .This tracks STARTED and STOPPED bundle events, interrogating the asso-
ciated bundle for extension metadata. For every started extension bundle, it adds a new
DefaultShape for the bundle to the paint frame, which creates the shape implementa-
tion, if needed, using the extension metadata. When the bundle stops, the Shape-
Tracker removes the shape from the paint frame. When a drawn shape is no longer
available, the DefaultShape is used to draw a placeholder shape on the canvas instead.
If the departed shape reappears, the placeholder is removed and the real shape is
drawn on the canvas again.
Now you have a dynamically extensible paint program, as demonstrated in sec-
tion 3.2.1. Although we didn't show the activator for the paint program, it's reasonably
simple and only creates the framework and shape tracker on start and disposes of them
on stop. Overall, this is a good example of how easy it is to make a modularized appli-
cation take advantage of the lifecycle layer to make it dynamically extensible. As a bonus,
you no longer need to export the implementation packages of the shape implementa-
tions. What you're still missing at this point is a discussion about how the lifecycle and
module layers interact with each other, which we'll get into next.
Draws default
image if no
extension
 
Search WWH ::




Custom Search