18 Nisan 2018 Çarşamba

NetBeans Platform Lookup Sınıfı

Giriş
Şu satırı dahil ederiz.
import org.openide.util.Lookup;
import org.openide.util.lookup.AbstractLookup;
import org.openide.util.lookup.InstanceContent;
Global Lookup
Açıklaması şöyle.
The global Lookup is initialized when the application is starting. It will parse all the @ServiceProvider annotations in your class files. The ServiceProvider Annotation is used to register an object in the global Lookup.

When you try to retrieve an instance of one of those objects, it will be instantiated by it’s default constructor at first retrieval – so make sure the class has one!! Once instantiated you will retrieve a singleton on each following fetch.

The global Lookup is a bit inflexible for certain tasks – its contents can’t be changed at runtime.
Local Lookup
Açıklaması şöyle.
Local Lookups are made of two Objects: InstanceContent, AbstractLookup:

InstanceContent is like an ArrayList which can be modified dynamically.

AbstractLookup will be initialized with the InstanceContent. This way you can give a Lookup to any Object. To let NetBeans know about this, let the Object implement LookupProvider, then implement the getLookup() method and you’re fine:
Node
Açıklaması şöyle.
NetBeans has another important Class called Node. A Node is a tree element. Wherever NetBeans uses trees to display data (project explorer, navigator, etc), Node objects will be used. These have the advantage that by clicking on them, NetBeans recognizes that and makes all objects stored in the node available in the local Lookup. The local Lookup is changing each time you click on another node or TopComponent (basically wrapper for a JPanel).

This way you have a context sensitive lookup that always represents the contents of the selected element.e.g. when a click is made on a file node in the project tree, NetBeans will look into the selected Node’s local lookup and check for special NetBeans objects like OpenCookie. When OpenCookie is found, NetBeans knows that it can load the file and show a TopComponent displaying the loaded data.

And so there are more special Classes typically residing in Nodes: DataObject(wrapper for the file „under“ the node), Savable (indicates that the file needs to be saved), …

You can see what is in the lookup of the currently selected Node/TopComponent by using Utilities.actionsGlobalContext().lookupAll(Object.class) which gives you a List of Objects contained in the lookup.
constructor
Örnek
Açıklaması şöyle.
Utilities.actionsGlobalContext() is related to currently selected (active) TopCompoment.
Şöyle yaparız.
Lookup context = Utilities.actionsGlobalContext();
lookupAll metodu
Elimizde şöyle bir arayüz olsun.
public interface Beverage {
   ...
}
Bu arayüzü gerçekleştiren bir sınıf olsun.
@ServiceProvider(service = Beverage.class)
public class SomeBeer implements Beverage {
   ...
}
Şöyle yaparız.
Collection list = Lookup.getDefault().lookupAll(Beverage.class);
Diğer
ProxyLookup sınıfı sınıfı Lookup sınıfından kalıtır. Farklı Lookup listelerini birleştirmek için kullanılır.
Lookups sınıfı fixed() ve singleton() metodları kendi Lookup nesnelerimi yaratmak için kullanılır.
Lookup.Template ve Lookup.Result sınıfları PreparedStatement ve ResultSet gibi düşünülebilir.
AbstractLookup sınıfı Lookup sınıfından kalıtır.

Hiç yorum yok:

Yorum Gönder