Lose Kopplung der Open Action (Beispiel von Geertjan Wielenga)

Heute habe ich versucht anhand von Geertjan Wielengas Blogbeitrag eine lose Kopplung der Open Action aus meinem Baum heraus hinzubekommen.

CacheNode

public class CacheNode extends BeanNode {

    public CacheNode(Cache cache) throws IntrospectionException {
        this(cache, new InstanceContent());
    }

    private CacheNode(final Cache cache, final InstanceContent ic) throws IntrospectionException {
        super(cache, Children.LEAF, new AbstractLookup(ic));
        this.setName(cache.getName());
        ic.add(new Openable() {
            @Override
            public void open() {
                ic.add(cache);
                ic.remove(cache);
            }
        });
    }  

    @Override
    public Action[] getActions(boolean context) {
        List cacheActions = (List)Utilities.actionsForPath("Actions/Cache");
        return cacheActions.toArray(new Action[cacheActions.size()]);
    }
}

OpenCacheActionListener

@ActionID(category = "Cache",  
id = "de.billmann.geocaching.frogger.actions.OpenCacheActionListener")  
@ActionRegistration(iconBase = "de/billmann/geocaching/frogger/actions/box_out.png",
        displayName = "#CTL_OpenCacheActionListener")
@Messages("CTL_OpenCacheActionListener=Open Cache")
@ActionReferences({
    @ActionReference(path = "Menu/File", position = 1450),
    @ActionReference(path = "Toolbars/File", position = 350)
})
public class OpenCacheActionListener implements ActionListener {

    private final Openable context;

    public OpenCacheActionListener(Openable context) {
        this.context = context;
    }

    @Override
    public void actionPerformed(ActionEvent ev) {
        context.open();
    }
}

Installer

public class Installer extends ModuleInstall implements LookupListener {

    private Result cacheResults;
    private Set uniqueCaches = new HashSet();

    @Override
    public void restored() {
        cacheResults = Utilities.actionsGlobalContext().lookupResult(Cache.class);
        cacheResults.addLookupListener(this);
    }

    @Override
    public void resultChanged(LookupEvent le) {
        if (cacheResults.allInstances().iterator().hasNext()) {
            Cache c = (Cache)cacheResults.allInstances().iterator().next();
            if (uniqueCaches.add(c)) {
                CacheTopComponent cetc = new CacheTopComponent(c);
                cetc.open();
                cetc.requestActive();
            } else { //In this case, the TopComponent is already open, but needs to become active:
                for (TopComponent tc : WindowManager.getDefault().findMode("editor").getTopComponents()){
                    if (tc.getName().contains(c.getName())){
                        tc.requestActive();
                    }
                }
            }
        }
    }
}

Screenshot

Comments