tags:

views:

53

answers:

1

I found this blocking GlassPane class on the web and I'm curious to know if some of you see any problem with it.

public final class BlockingGlassPane extends JComponent implements AWTEventListener {

// Events will be consumed for this window.
private Window parentWindow;
// Focus will be returned to this component.
private Component lastFocusOwner;

private final Toolkit toolkit;

public BlockingGlassPane() {
    super();
    setOpaque(false);
    addMouseListener(new MouseAdapter() {
    });
    addKeyListener(new KeyAdapter() {
    });
    setInputVerifier(new InputVerifier() {
        @Override
        public boolean verify(JComponent anInput) {
            return false;
        }
    });
    toolkit = Toolkit.getDefaultToolkit();
}

@Override
public void setVisible(boolean b) {
    if (b) {
        if (parentWindow == null) {
            parentWindow = SwingUtilities.windowForComponent(this);
        }
        Component focusOwner = parentWindow.getFocusOwner();
        if (focusOwner != this) {
            lastFocusOwner = focusOwner;
        }
        toolkit.addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK);
        toolkit.addAWTEventListener(this, AWTEvent.MOUSE_EVENT_MASK);
        requestFocus();
        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    } else {
        toolkit.removeAWTEventListener(this);
        if (lastFocusOwner != null) {
            lastFocusOwner.requestFocus();
            lastFocusOwner = null;
        }
        setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    }
    super.setVisible(b);
}

@SuppressWarnings("unchecked")
public void eventDispatched(AWTEvent e) {
    Object source = e.getSource();
    if (e instanceof EventObject && source instanceof Component) {
        Component src = (Component) source;
        EventObject ev = e;
        if (SwingUtilities.windowForComponent(src) == parentWindow) {
            try {
                Class[] cls = {};
                Object[] args = {};
                ev.getClass().getMethod("consume", cls).invoke(ev, args);
            } catch (Exception ex) {
                // ex.printStackTrace();
            }
        }
    }
}
+1  A: 

Just at a glance, I see several problems here, mostly in and around the eventDispatched() method.

First, why are you implementing AWTEventListener at all, since you never add this object to anything as an AWTEventListener? Did you mean to add this object to itself as an event listener? Are you adding it as an event listener somewhere else in code that isn't shown here?

Second, why do you test e instanceof EventObject? I cut-and-pasted your code into Eclipse, which immediately warned me that all AWTEvent objects are instances of EventObject. So, you can get rid of that test - It will always be true.

Third, why on earth are you resorting to reflection? It looks as if you are trying to use a Swing-only method on AWT events that don't have it. That approach won't work - Trying to reflectively call a nonexistent method will just throw an exception, which this code will silently catch and ignore.

Finally, why are you reinventing the wheel? Some quick Googling reveals some simpler examples and some more complicated examples that you could use as a starting point for your work and which would likely get you much closer to what you really want here.

Joe Carnahan
I don't like the tone and I was pretty clear that I'm not the author of this code, but thanks anyway for the links.
Skeptic