BookNBPlatformCookbookCH0304

Contents

NetBeans Platform Cookbook Chapter 03 04

Create Lookup, Provide Lookup

Many components in NetBeans Platform provide the Lookup instance - most known are TopComponent, Node, DataObject. Other objects or modules can use stored object instances.. An advantage of this approach is that it is not strictly prescribed which instances to store. The second advantage is that retrieving method for e.g. getSpecObject() is not part of the interface whereas other modules do not need the SpecObject for their work. Store objects into the Lookup and expose the Lookup instance. If some object knows some object and needs it then let it ask the Lookup for it.

.... into the central registry - they are called service providers.


How to

Empty Lookup

Lookups.EMPTY

Lookup containing one object

 
Lookup singleObjLookup = Lookups.singleton( new SupportingObject() );

Stable Lookup with several objects

stableObjLookup = Lookups.fixed( obj1, interfaceImplementation, obj2);

ProxyLookup delegates searching to other containing lookups

 
Lookup compound = new ProxyLookup(singleObjLookup, stableObjLookup);

Lookup delegating searches to Lookup of some object

(it must be Lookup.Provider). Lookup returned by given object can change from time to time, e.g. under some conditions – active tool, ActionMap. Created Lookup checks if returned objects is the same and fires change events and updates all existing and referenced results:

delegating = Lookups.proxy( new ObjWithLookup() );

Lookup with mutable content

When mutable content is needed create AbstractLookup with AbstractLookup.InstanceContent object which manages stored instances. We show an template source:

public class MyObject implements Lookup.Provider {

    private boolean modified = false;

    private Lookup lookup = null;
    private InstanceContent lookupContent = new InstanceContent();
 
    private SaveCookie saveCookie;

    public MuttableLookupProvider() {
        this.saveCookie = new MySaveCookie();
    }

    public Lookup getLookup() {
        if (lookup == null) {
            lookupContent.add(this);
            lookupContent.add( new SupportingClass(this) );
            lookup = new AbstractLookup( lookupContent );
        }
        return lookup;
    }

    private boolean isModified() {
        return modified;
    }

    public void notifyChanged() {
        this.modified = true;
        lookupContent.add( saveCookie );
    }

    public void notifyUnChanged() {
        this.modified = false;
        lookupContent.remove( saveCookie );
    }

    public class MySaveCookie implements SaveCookie {
        public void save() throws IOException {
            // save object
            notifyUnChanged();
        }
    } // MySaveCookie
    
    private static class SupportingClass 
                                        implements EditorCookie , OpenCookie {

        // in most cases it is corresponding DataObject
        MuttableLookupProvider par;

        public SupportingClass(MuttableLookupProvider par) {
            this.par = par;
        }

        public void open() {
            // open editor
        }

        public boolean close() {
            // close
        }

        public boolean isModified() {
            return par.isModified();
        }
        ...

    } // SupportingClass
    
}

InstanceContent object keeps stored instances. You can add and remove objects dynamically by add() and remove() methods. This approach is used for example by implementation of DataObjects. If the object was changed then set modified flag and add implementation of SaveCookie into lookup. If the SaveCookie is presented in the object lookup then the SaveAction is enabled. Clear the modified flag and remove the SaveCookie from the lookup content when object was saved.

We describe some other methods of the Lookups documentation. You do not need dip into the documentation.

Exclude given classes

Lookup filtered = Lookups.exclude( originalLookup
                                 , SomeObj.class, ActionMap.class);

Files from some folder

Create Lookup containing instance files from some folder:

 
Lookup actions = Lookups.forPath(“MyApp/Actions”);

It scans the Myapp/Actions folder recursively and stores all instance files.

And More

Find source of SaveAllAction in the NetBeans sources. It looks very brief. It listens to DataObject.getRegistry() collection changes. If that collection is not empty it enables itself. Performing of the action is delegated to LifecycleManager.saveAll() method.


Navigation

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo