Bear with me... I don't think this is too subjective but maybe I'm wrong.
Recently I wanted to factor out some repetitive code which drew a custom Bitmap background on our BlackBerry app.
(This question is not really about BlackBerry though, so I'll provide some details here about the BB GUI so that non-BB Java people can weigh in...)
The class FullScreen is from the BB API - it has a method paint(Graphics) which the framework calls to draw the screen and any components added to it. It's possible to override this to do custom painting - like drawing a Bitmap background before any other painting happens (newer BB APIs provide a Background class but our app has to work on older phones).
I wanted to have a bunch of screens all with the same background, which each did some kind of custom painting... here's what I came up with:
abstract public class BGFullScreen extends FullScreen {
Bitmap bg;
public BGFullScreen(Manager mgr, long style) {
super(mgr, style);
bg = Bitmap.getBitmapResource("bg.jpg");
}
abstract protected void innerPaint(Graphics g);
protected void paint(Graphics g) {
g.drawBitmap(new XYRect(0, 0, bg.getWidth(), bg.getHeight()), bg, 0, 0);
innerPaint(g);
super.paint(g);
}
}
Each screen will then subclass this abstract class and implement innerPaint(). This way when the BB framework calls the paint() method, each screen can have custom painting stuff happen AFTER the background is drawn (so any painting happens on top of the background) but BEFORE the components of the screen are drawn, when FullScreen.paint() is called.
(I came up with this because I've been studying Common Lisp at home, and it occurred to me that what I wanted to do was something like the interleaved method combination in CLOS)
Here's a sample implementation of the abstract class above:
public class MainAppScreen extends BGFullScreen {
public MainAppScreen() {
super(new VerticalFieldManager(), 0);
// add some components to the screen:
add(new ButtonField(...));
add(...)
}
protected void innerPaint(Graphics g) {
// stuff drawn will be on top of background and under buttons
g.draw(...)
}
}
Basically I want a child class to implement a method gets called in-between its parent's implementation and its grandparent's implementation of the same method. I couldn't figure out any other way to do this in Java...
Is this idomatic Java? Is this actually really common and it's a dumb question? Is this actually horrible design? (BB experts, how can I do this any other way?)
Edited to add: This does work as described - the drawing happens in the order I want.