Java Reference
In-Depth Information
PaintFrame.addShape() must be injected with the base name property so it can
perform the localization lookup. This probably isn't ideal, because it exposes imple-
mentation details.
Another approach is to focus on where the shape's name is set in the first place: in
the shape implementation's bundle activator. The following listing shows the activator
of the circle implementation.
Listing 5.5 Original circle bundle activator with hardcoded name
public class Activator implements BundleActivator {
public void start(BundleContext context) {
Hashtable dict = new Hashtable();
dict.put(SimpleShape.NAME_PROPERTY, "Circle");
dict.put(SimpleShape.ICON_PROPERTY,
new ImageIcon(this.getClass().getResource("circle.png")));
context.registerService(
SimpleShape.class.getName(), new Circle(), dict);
}
public void stop(BundleContext context) {}
}
The hardcoded shape name is assigned to the service property dictionary, and the
shape service is registered. The first thing you need to do is change the hardcoded
name into a lookup from a ResourceBundle . This code shows the necessary changes.
Listing 5.6 Modified circle bundle activator with ResourceBundle name lookup
public class Activator implements BundleActivator {
public static final String CIRCLE_NAME = "CIRCLE_NAME";
Defines
constant
B
public void start(BundleContext context) {
ResourceBundle rb = ResourceBundle.getBundle(
"org.foo.shape.circle.resource.Localize");
Hashtable dict = new Hashtable();
dict.put(SimpleShape.NAME_PROPERTY, rb.getString(CIRCLE_NAME));
dict.put(SimpleShape.ICON_PROPERTY,
new ImageIcon(this.getClass().getResource("circle.png")));
context.registerService(
SimpleShape.class.getName(), new Circle(), dict);
}
Creates
ResourceBundle C
public void stop(BundleContext context) {}
}
You modify the activator to look up the shape name using the key constant defined
at B from a ResourceBundle you create C , whose resulting value is assigned to the
service properties. Even though we won't go into the complete details of using
ResourceBundle objects, the important part in this example is when you define it. You
specify the base name of org.foo.shape.circle.resource.Localize . By default, this
refers to a Localize.properties file in the org.foo.shape.circle.resource package,
which contains a default mapping for your name key. You need to modify the circle
 
Search WWH ::




Custom Search