I'm also going on a bit of a document and re-factor effort to try and get things in shape as I am getting more requests to use the components (open-source dudes, do what you like!) I feel I should at least make them more usable and, well, better documented. In the meantime I'm (as usual) fiddling with one or two ideas that will hopefully mature enough to be added into the library.
Sweet ![]()
There is a magical method for each container, that can cause it to NOT hog mouse events. It simply dictates whether or not there is a component at a particular x,y co-ordinate. If there's not, Swing doesn't pass the mouse event on. After all, there's nothing there. Here it is (you can over-ride it)
public boolean contains(int x, int y);
That's it. If it returns false, then no events are passed on and the clicks fall through. I had started fiddling with this, but then stopped hitting a recursive dead end when I called getComponentAt(int x,int y) inside it. Of course, all I should have done is a little bit of work to implement my own getComponentAt() and I would have had my solution. Find out if there is something in the dock panel that isn't a spacer that's at that co-ordinate... if there's not return false and pretend it's got nothing to do with me.
Here's the implementation:
public boolean contains(int x, int y) {
Rectangle rect=new Rectangle();
//Have to implement our own componentAt here
for (Component comp : getComponents()){
rect=comp.getBounds(rect);
if (rect.contains(x,y)){
if (comp instanceof JSpacer){
mouseMoved(new Point(-1,-1));
return false;
}
return true;
}
}
//A little non-intuitive. If it's not over a normal component or a spacer, it's in the dock area
return true;
}
The final return true is a little bit tricky (it's after all like saying, I didn't find anything but yes send me the message). This stops the mouse going over little gaps causing a problem, in general, if it's over a spacer pretend I'm not there, otherwise consume the event. Doing this enabled me to remove the GlassPaneDock class entirely, and a WHOLE load of complexity went with it. It's also enabled me to do something else I wanted to, add the dock into a Frame's layered pane. This is useful because it means I can make things like combo-box drop downs and pop-up menus appear over it (play with the demo to make it happen if you wish). Which might be more appropriate in some cases. In the screen shot below see how the combo-box appears OVER the dock.
This "perfect" message handling has one problem, the
mouse can still fall between the cracks of dock icons
from time to time, but that's part of another tweak
I'm working towards. For now, the usual web-start and
source code update has been done!
![]()
I've also added Mouse Wheel support to the carousel,
which is pretty funky (well I think so!). Another big
thanks to Krill and Sebastien, it's all looking
better than it was.
![]()
Quite a few changes have been made here and there, one of my favourites was Krill's idea to add mouse wheel functionality, now why didn't I think of that? All very simple in the code as well....
public void mouseWheelMoved(MouseWheelEvent mouseWheelEvent) {
if (mouseWheelEvent.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
int amount = mouseWheelEvent.getWheelRotation();
if (lastWheeledTo==null){
lastWheeledTo=getFrontmost();
}
int lastPosition = layout.getComponentIndex(lastWheeledTo);
int frontMostPosition = layout.getComponentIndex(getComponent(0));
//Don't over spin
if (Math.abs(lastPosition-frontMostPosition)>layout.getComponentCount()/4){
return;
}
if (amount > 0) {
lastWheeledTo=layout.getPreviousComponent(lastWheeledTo);
} else {
lastWheeledTo=layout.getNextComponent(lastWheeledTo);
}
bringToFront(lastWheeledTo,true);
}
}
Basically all the code does is look for the mouse wheel to be rolled and try and bring the next component along to the front. I bet your are wondering about the over-spin bit? Well, the carousel always looks for the shortest route to be bringing a component to the front, so racing too far ahead suddenly spins it backwards, not what I wanted, but if we never let it go further than a quarter of the way around, it's always quickest to carry on moving in the same direction.
The next item, and I think you'll see it to great effect in Krill's application, is to improve how text is rendered and labels generated. Nice alpha blended backdrop in a rounded rectangle. Once again, there were a few e-mails backwards and forwards before we hit on the right solution. Fun!
Finally (and what started it all) items further back
are now more transparent (something I know a few of
you had raised) not to mention a couple of bad
practice items Krill had highlighted (laziness on my
part, but when you realize someone else is doing
things with your code!)
I have updated the source, as well the web-start
demo....