よくここまで組んだ

この本をJavaOne会場で買って、今まで手探りでやってたことが全部書いてあるのですごく楽になった。やっとLookupとExplorerManagerについて意味がわかってきました。

Rich Client Programming: Plugging into the NetBeans™ Platform

Rich Client Programming: Plugging into the NetBeans™ Platform


NetBeans Platformを使って作ったプログラムを改修してるんですけど、なんか、よく試行錯誤でここまで組んだなぁという感じ。

ノードの選択イベントを取得する

LookupListenerインタフェースを実装してresultChangedでイベント処理

    public void resultChanged(LookupEvent lookupEvent) {
        Lookup.Result res = (Lookup.Result)lookupEvent.getSource();
        for(Object o : res.allInstances()){
            //選択の処理
        }
    }


TopComponentのaddNotifyをオーバーライドしてリスナーの登録

    Lookup.Result selectedNode;
    public void addNotify() {
        super.addNotify();

       //MyNodeは、監視対象のノードクラス        
       selectedNode = Utilities.actionsGlobalContext().lookup(
               new Lookup.Template(MyNode.class));
       selectedNode.addLookupListener(this);
    }


TopComponentのremoveNotifyをオーバーライドしてリスナーの解除

    public void removeNotify() {
        super.removeNotify();
        selectedNode.removeLookupListener(this);
    }