Zelig
Zelig

Reputation: 1808

Adding an object to the lookup

I want to create an object in a module and then have it available to other modules via the lookup. I know how to retrieve object from the lookup but I don't know how to put objets in it.

Can anyone tell me the procedure?

Let me be more specific about what I really want to do :

I already know how to select (through the use of a "Favorite" module) and edit (in "Multiviews") a file in a Netbeans platform application. But rather than selecting the file to edit in an Explorer object, I would like to be able to select it through a "File/Open" menu item. This menu item would call an open file dialog and the user selected file would be added to the lookup for use by the views. Is it possible and, if yes, how?

Thanks in advance for the time you will spend trying to help me.

Upvotes: 1

Views: 214

Answers (1)

obataku
obataku

Reputation: 29656

You can add to your own AbstractLookup with InstanceContent, you can merely do...

final InstanceContent content = ...;
content.add(instance);
final Lookup lookup = new AbstractLookup(content);

Generally, when using AbstractLookup, I think you can implement your own AbstractLookup.Pair for adding after creation time.

final AbstractLookup lookup = ...;
lookup.addPair(new AbstractLookup.Pair<MyObject>() {

  final MyObject inst = ...;

  protected boolean creatorOf(final Object inst) {
    return this.inst == inst;
  }

  protected boolean instanceOf(final Class<?> cls) {
    return cls == MyObject.class;
  }

});

Upvotes: 1

Related Questions