INTViewer APIs for Selections

Through the INTViewer GlobalSelection and the NetBeans LookupListener APIs, you can detect which window and layer are currently selected by the end-user within the INTViewer desktop. You can also find out when these selections change.

Retrieving the selected objects

IWindowManager manager = IWindowManager.Factory.getInstance();
IViewerWindow selectedWindow = manager.getSelectedViewerWindow();
IVisual selectedVisual = selectedWindow.getSelectedVisual();
IData selectedData = selectedWindow.getData();

Listening to layer selection changes

INTViewer has a utility class GlobalSelection for notification of layer and window selection. It extends NetBeans Lookup mechanism by simplifying the registration and by returning unique instances, to make sure the same object is never selected twice.

    import com.interactive.intviewerapi.GlobalSelection;
    ...
    GlobalSelection<ILayer2D> layerSelection = new GlobalSelection<>(ILayer2D.class);

Then you need to implement a NetBeans LookupListener, which has one method called resultChanged:

    import org.openide.util.LookupListener;
    import org.openide.util.LookupEvent;
    ...
    public void resultChanged(LookupEvent evt) {
        Collection<? extends ILayer2D> selections = layerSelection.allInstances();
        if (!selections.isEmpty()) {
            System.out.println("Layer " + selections.iterator().next().getDisplayName() + " has been selected.";
        }
    }

Finally, you will need to register the listener:

    layerSelection.addLookupListener(this);

Assuming this is the class that implements LookupListener. To cleanup a listener you can do the following:

    layerSelection.removeLookupListener(this);

Or, if you don't want to have to deal with the cleanup, you can register a weak listener as follows:

    import org.openide.util.WeakListeners;
    ...
    layerSelection.addLookupListener(WeakListeners.create(LookupListener.class, this, layerSelection));

Listening to window selection changes

You can use the same mechanism to listen to window selection by create a GlobalSelection object with type ILayeredWindow instead of ILayer2D.

    GlobalSelection<ILayeredWindow> windowSelection = new GlobalSelection<>(ILayeredWindow.class);

Listening to a specific layer type

If you want to be notified only when a specific layer type is selected (ISeismicLayer for example), create a GlobalSelection with type ISeismicLayer instead of ILayer2D.

Making your Actions context sensitive using the Lookup

The following code shows the entire implemention of the Print action in INTViewer. It gets activated only when a window component implementing the IPrintable interface is selected.

package com.interactive.intviewer.control;
import java.awt.event.*;
import com.interactive.intviewerapi.GlobalSelection;
import com.interactive.intviewerapi.windows.IPrintable;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.util.HelpCtx;
import org.openide.util.LookupEvent;
import org.openide.util.LookupListener;
import org.openide.util.actions.SystemAction;
public class PrintAction extends SystemAction implements LookupListener{
    private final static long serialVersionUID = 1L;
    GlobalSelection<IPrintable> result;
    public PrintAction() {
        super();
        putValue(SHORT_DESCRIPTION, "Print the content of the selected window");
        result =  new GlobalSelection<>(IPrintable.class);
        result.addLookupListener(this);
        setEnabled(result.allInstances().size() >0);
    }
    public void actionPerformed(ActionEvent e) {
        if( result.allInstances().isEmpty())
            return;
        try {
            result.allInstances().iterator().next().print();
        } catch (Exception ex) {
            DialogDisplayer.getDefault().notify(new NotifyDescriptor.Exception(ex));
        }
    }
    @Override
    public String getName() {
        return "Print ...";
    }
        // Activate the printing option if a window implementing interface
    // IPrintable is selected.
    public void resultChanged(LookupEvent arg0) {
        setEnabled(result.allInstances().size() >0);
    }
    @Override
    protected String iconResource() {
        return "com/interactive/intviewer/util/icon/Print.png";
    }
}