如何将 MouseListener 添加到 EditorPart 内的每个元素(一个元素除外)?

How can I add a MouseListener to every element (except one) inside an EditorPart?

当用户单击文本小部件外部的某处时,我想停用其滚动条。

它是这样工作的:

public class RttProjectActionEditor extends EditorPart {
    ...
    MouseListener exitCommandText = new MouseListener() {
        @Override
        public void mouseDoubleClick(MouseEvent e) {}
        @Override
        public void mouseDown(MouseEvent e) {}
        @Override
        public void mouseUp(MouseEvent e) {
            if (e.widget != textArea) {
                textArea.getVerticalBar().setEnabled(false);
            } 
        }
    };

    labelGroup.addMouseListener(exitCommandText);
    myComposite.addMouseListener(exitCommandText);
    ... // all widgets in my EditorPart

但是这样我必须将它们一一添加到此 EditorPart 拥有的所有小部件中。有没有更好更简单的方法?

您可以递归地将侦听器添加到控件和任何子控件(如果它是复合控件),使用如下所示:

private void hookRecursively(Control c, MouseListener listener) {
    c.addMouseListener(listener);

    if (c instanceof Composite) {
        Control[] children = ((Composite) c).getChildren();
        for (Control element : children) {
            hookRecursively(element, listener);
        }
    }
}