站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > NetBeans API Javadoc 5.5.1

Overview (NetBeans Editor MIME Lookup) - NetBeans API Javadoc 5.5.1

org.netbeans.modules.editor.mimelookup/1 1.3.31

NetBeans Editor MIME Lookup

It consists of API in org.netbeans.api.editor.mimelookup SPI in org.netbeans.spi.editor.mimelookup

See:
          Description

Editor MIME Lookup
org.netbeans.api.editor.mimelookup  
org.netbeans.spi.editor.mimelookup  

 

It consists of

MIME Lookup defines MimeLookupAPI.

API contains only one class MimeLookup with public methods:
MimeLookup is represented via ProxyLookup that collects registered lookups. Particular lookups, responsible for looking up their objects can be registered using interface MimeLookupInitializer into default lookup by META-INF/services registration.

In addition to this basic registration, xml layer folder registration is also available. It is provided by registering implemented interface Class2LayerFolder into default lookup via META-INF/services registration. This approach provides a mapping of class to specific subfolder. Using this mapping one can achieve the convenient way of using MimeLookup i.e.

MimeLookup.getMimeLookup("text/x-java").lookup(FoldManagerFactory.class);

Using this, an instance of FoldManagerFactory is retrieved from the folder with path "Editors/text/x-java/FoldManager" provided that FoldManagerFactory.class is registered to a subfolder "FoldManager" via Class2LayerFolder registration.

There InstanceProvider can be used if there are files of various types in the layer folder that need additional handling before becoming and instance of certain class. For more details look at use case of PopupActions creation.

The Javadoc documentation can be generated by using

    cd /cvs/editor/mimelookup
    ant javadoc

What is New (see all changes)?

Use Cases

Per mime-type operation
Operation of the editor module must be parametrized by the type of the file being edited. In the past the operation was parametrized by the class of the editor kit but that did not show up as being useful enough.
It is more practical to use a string-based parametrization concretely the mime-type. Anyone can then easily register an additional functionality for the editor because it's just enough to know the right mime-type and the type of the functionality class to be implemented and the xml layer folder where the class should be registered.
For example the editor/fold module expects to find the registered fold manager factories (org.netbeans.spi.editor.fold.FoldManagerFactory classes) in the Editors/<mime-type>/FoldManager layer folder.
The editor/completion module expects to find the registered completion providers (org.netbeans.spi.editor.completion.CompletionProvider classes) in the Editors/<mime-type>/CompletionProviders layer folder.
Provide list of instances as lookup result
On the modules' implementation side the registered functionality must be retrieved somehow. It's necessary to instantiate the registered objects and react to module enablding/disabling which can affect validity of the registered objects.
As the most convenient solution appears to use org.openide.util.Lookup allowing to provide the registered instances as a Lookup.Result allowing to listen for changes (e.g. caused by the module enabling/disabling).
This resulted into creation of class MimeLookup extends Lookup containing static MimeLookup getMimeLookup(String mimeType).
Nested mime-types
On the lexical level the document can contain nested languages.
For example JSP document can contain pieces of java code which can further contain javadoc comment tokens with nested javadoc language.
The nested languages should allow for special settings such as fonts and colors of nested syntax coloring but even things like actions that would be active in the nested document section.
This resulted into creation of MimeLookup childLookup(String mimeType) method in MimeLookup.

API Use Cases

Find class instances for the given mime-type
An API method

MimeLookup lookup = MimeLookup.getMimeLookup("text/x-java");

can be used for getting the mime specific lookup. Having this we can lookup class or template:

Object obj = lookup.lookup(LookedUpClass.class);

or

Lookup.Result result = lookup.lookup(new Lookup.Template(LookedUpClass.class));
Getting mime-type specific child (embeded) MimeLookup
Child mime content can be embeded into parent mime content in various embeded languages. In this case mime lookup child is specified as subelement of parent lookup i.e.: MimeLookup("text/x-jsp") can have a child MimeLookup("text/x-java") in a case of a jsp scriplet.

MimeLookup lookup = MimeLookup.getMimeLookup("text/x-jsp").childLookup("text/x-java");

SPI Use Cases

Declare the mime specific object via xml layer
This is the simpliest way of declaring, it is suitable for objects stored directly in mime specific folders:
<filesystem>
    <folder name="Editors">
        <folder name="text">
            <folder name="x-java">
                <file name="org-netbeans-modules-editor-mimelookuptest-MimeSpecificObject.instance"/>
            </folder>
        </folder>
    </folder>
</filesystem>
Lookup of this object will look like:
MimeLookup lookup = MimeLookup.getMimeLookup("text/x-java");
MimeSpecificObject mso = (MimeSpecificObject) lookup.lookup(MimeSpecificObject.class);
Declare the object via xml layer in some specific folder using Class2LayerFolder.

Some objects are connected to some specific folder in mime specific layer folders, i.e. FoldManagerFactory.class is placed in FoldManager subfolder in the Editors/<mime-type>/. Such objects can be registered to the specific folder using interface Class2LayerFolder. Let's register FoldManagerFactory.class to FoldManager folder. First we need to implement the interface:
public class FoldManagerClass2LayerFolder implements Class2LayerFolder{
    
    public FoldManagerClass2LayerFolder {
    }
    
    /* declaring the class */
    public Class getClazz(){
        return FoldManagerFactory.class;
    }
    
    /* assigning the declared class to folder */
    public String getLayerFolderName(){
        return "FoldManager";
    }

    /* we will not support InstanceProvider */
    public org.netbeans.spi.editor.mimelookup.InstanceProvider getInstanceProvider() {
        return null;
    }
    
}

Then we need to register it to default lookup via META-INF/services registration. We need to create a folder structure META-INF/services and place there a file org.netbeans.spi.editor.mimelookup.Class2LayerFolder with the content FoldManagerClass2LayerFolder

having this we can register appropriate object to specific folder:
<filesystem>
    <folder name="Editors">
        <folder name="text">
            <folder name="x-java">
                <folder name="FoldManager">
                    <file name="org-netbeans-modules-editor-MyFoldManagerFactory.instance"/>
                </folder>
            </folder>
        </folder>
    </folder>
</filesystem>
Lookup of this object will look like:
MimeLookup lookup = MimeLookup.getMimeLookup("text/x-java");
FoldManagerFactory foldManagerFactory =  (FoldManagerFactory) lookup.lookup(FoldManagerFactory.class);
or, if there should be more instances of the FoldManagerFactory:
MimeLookup lookup = MimeLookup.getMimeLookup("text/x-java");
Collection foldManagerFactories = lookup.lookup(new Lookup.Template(FoldManagerFactory.class)).allInstances();
Notice, that the FoldManagerFactory object is found in "FoldManager" folder. It is not necessary for client of the API to know about some folder structure.

Providing implemented MimeLookupInitializer
It is the general way of adding mime specific object into the MimeLookup. Implementation of MimeLookupInitializer should be created and registered to default lookup via META-INF/services registration. For details, please look at the simplified TestMimeLookupInitializer in mimelookup/test/unit or LayerMimeLookupInitializer.
Using InstanceProvider for declaration of compound folder objects and inheritance
Demonstration of InstanceProvider and inheritance will be used together in one use case. Example of editor context menu construction will be used. Each module can register its actions to editor context menu via xml layer. As context menu is mime type sensitive (java editor has different menu items than plain editor) the actions are registered into mime specific layer folders to subfolder "Popup". When the context menu is constructing, the xml layer is scanned for the action items located in the "Popup" subfolder of specific mime folder. In addition to this there is inheritance mechanism used to share global actions (like Cut, Copy, Paste) over all mime types context menus, thus not only the action items from actual mime type are considered. All underlaying mime types are scanned also and the result is context menu with merged action items.

For example JSP scriplet context menu should be merged from action items gathered over:
  1. Editors/Popup - this is base level for global actions like Cut, Copy, Paste
  2. Editors/text/x-jsp/Popup - items from jsp mime type
  3. Editors/text/x-jsp/text/x-java/Popup - items specific to java scriplet in JSP document

For now, this construction mechanism is implemented in editor module in NbEditorKit class. The code responsible for construction is quite large, and it is not very generic, now it supports only base level + mime specific level merging. Embeded languages like example in JSP scriplet are not supported. MimeLookup will solve also this embeding and the gathering of the context menu items will looks such simply as:

MimeLookup lookup = MimeLookup.getMimeLookup("text/x-jsp").childLookup("text/x-java");
PopupActions actions = (PopupActions) lookup.lookup(PopupActions.class);
List popupActions = actions.getPopupActions();
where PopupActions is implementation of InstanceProvider and PopupActions.class needs to be registered to "Popup" subfolder using Class2LayerFolder implementation. Let's register this step by step.

Because action items are instances of various objects like:
  1. javax.swing.Action
  2. javax.swing.JSeparator
  3. org.openide.util.actions.SystemAction
  4. java.lang.String
the InstanceProvider needs to be created for this:
public class PopupActions implements InstanceProvider{
    
    List ordered;

    public PopupActions(){
    }
    
    public PopupActions(List ordered){
        this.ordered = ordered;
    }

    public List getPopupActions(){
        List retList = new ArrayList();
        for (int i = 0; i<ordered.size(); i++){
            DataObject dob = (DataObject) ordered.get(i);
            InstanceCookie ic = (InstanceCookie)dob.getCookie(InstanceCookie.class);
            if (ic!=null){
                try{
                    if (String.class.isAssignableFrom(ic.instanceClass()) ||
                        Action.class.isAssignableFrom(ic.instanceClass()) ||
                        SystemAction.class.isAssignableFrom(ic.instanceClass()) ||
                        JSeparator.class.isAssignableFrom(ic.instanceClass())){
                        Object instance = ic.instanceCreate();
                        retList.add(instance);
                    }
                }catch(IOException ioe){
                    ioe.printStackTrace();
                }catch(ClassNotFoundException cnfe){
                    cnfe.printStackTrace();
                }
            } else{
                retList.add(dob.getName());
            }
        }
        return retList;
    }

    public Object createInstance(List ordered) {
        return new PopupActions(ordered);
    }
}

This InstanceProvider needs to be declared in Class2LayerFolder implementation:
public class PopupInitializer implements Class2LayerFolder{
    
    public PopupInitializer() {
    }
    
    public Class getClazz(){
        return PopupActions.class;
    }
    
    public String getLayerFolderName(){
        return "Popup"; //NOI18N
    }

    public InstanceProvider getInstanceProvider() {
        return new PopupActions();
    }

}

Now, we just need to register PopupInitializer into default lookup via META-INF/services registration and the initialization is done.

Exported Interfaces

This table lists all of the module exported APIs with defined stability classifications. It is generated based on answers to questions about the architecture of the module. Read them all...
Group of java interfaces
Interface NameIn/OutStabilitySpecified in What Document?
MimeLookupAPIExportedOfficial

Group of lookup interfaces
Interface NameIn/OutStabilitySpecified in What Document?
org.netbeans.spi.editor.mimelookup.MimeLookupInitializerExportedOfficial

is registered via META-INF/services and is looked up by MimeLookup.

org.netbeans.spi.editor.mimelookup.Class2LayerFolderExportedOfficial

is registered via META-INF/services and is looked up by LayerMimeLookupInitializer.

Group of layer interfaces
Interface NameIn/OutStabilitySpecified in What Document?
EditorsExportedOfficial

MimeLookup module creates folder "Editors" in default layer filesystem since all mime types folders are expected to be located in this folder.

Implementation Details

What do other modules need to do to declare a dependency on this one?
OpenIDE-Module-Module-Dependencies: org.netbeans.modules.editor.mimelookup/1 > 1.3.31

Read more about the implementation in the answers to architecture questions.


org.netbeans.modules.editor.mimelookup/1 1.3.31

Built on March 26 2007.  |  Portions Copyright 1997-2005 Sun Microsystems, Inc. All rights reserved.