sandip chitale's blog    Sandip Chitale's blog (scblog)
NOTE: I have moved many of my modules to NetBeans Plugin Portal . Please check there for latest versions of modules described on this blog.
20060131 Tuesday January 31, 2006

Define your own code template parameters in NetBeans editors (using a module)

In a past entry about Code Template Tools I promised that someday I will blog about how to define your own code template parameters. This blog entry is about that.

First, let me explain what are code template parameters. The code template parameters are place holders in the definition of a code template. The code template parameters have a syntax that looks like this: ${code-template-parameter-name attribute="attribute value" ....} After the code template is inserted into the editor, the caret is positioned inside the first place holder. The user can type text in the location of place holders. As the user types the text, all occurrences of the place holder are modified simulteneouly. The initial value of the place holder is it's name unless some code template processor, which understands that name is installed. In which case the processor gets to decide to the initial value of code template parameter. There is a special code template parameter ${cursor}. If used in the code template - that is where the caret ends up after the code template insertion has completed.

The code template parameter processors are based on the mime type of the file. The following example shows a code template processor that handles a parameter named ${selection}. It uses the selection in the text editor as the initial value of ${selection} code template parameter.

To install your own code template parameter processor you need to do three things:

  1. Register the factory for the processor in your module's layer file like this:
        <folder name="Editors">
            <folder name="text">
                <folder name="base">
                    <folder name="CodeTemplateProcessorFactories">
                        <file name="org-netbeans-modules-codetemplatetools-SelectionCodeTemplateProcessor$Factory.instance"/>
                    </folder>
                </folder>
            </folder>
        </folder>
    
  2. Define a factory class SelectionCodeTemplateProcessor$Factory that implements CodeTemplateProcessorFactory interface. This should create and return instance of class that extends CodeTemplateProcessor class.
  3. Extend the CodeTemplateProcessor class and process the template parameters that it wants to recognize in the public void updateDefaultValues(). See the example below. In it the code template processor processes the parameter named selection and sets it's initial value to the value of text selected in the editor before the code template insertion starts. If desired it is possible to adjust the values of the parameter after every key typed by the user. Attributes can be used to fine tune the behavior of the processor.
    package org.netbeans.modules.codetemplatetools;
    
    import java.lang.reflect.Modifier;
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Iterator;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Map;
    import javax.swing.text.JTextComponent;
    import org.netbeans.lib.editor.codetemplates.spi.CodeTemplateInsertRequest;
    import org.netbeans.lib.editor.codetemplates.spi.CodeTemplateParameter;
    import org.netbeans.lib.editor.codetemplates.spi.CodeTemplateProcessor;
    import org.netbeans.lib.editor.codetemplates.spi.CodeTemplateProcessorFactory;
    import org.openide.ErrorManager;
    
    /**
     *
     * @author Sandip V. Chitale (Sandip.Chitale@Sun.Com)
     */
    public class SelectionCodeTemplateProcessor implements CodeTemplateProcessor {
        
        public static final String SELECTION_PARAMETER         = "selection"; // NOI18N
        public static final String CLIPBOARD_CONTENT_PARAMETER = "clipboard-content"; // NOI18N
        
        private CodeTemplateInsertRequest request;
        
        SelectionCodeTemplateProcessor(CodeTemplateInsertRequest request) {
            this.request = request;
        }
        
        public void parameterValueChanged(CodeTemplateParameter masterParameter, boolean typingChange) {
            if (!typingChange) {
                
            }
        }
        
        public void updateDefaultValues() {
            JTextComponent component = request.getComponent();
            int offset = component.getCaretPosition();
            List typeHints = new ArrayList();
            for (Iterator masterParamsIt = request.getMasterParameters().iterator(); masterParamsIt.hasNext();) {
                CodeTemplateParameter master = (CodeTemplateParameter)masterParamsIt.next();
                if (master.getName().equals(SELECTION_PARAMETER)) {
                    String selectedText = component.getSelectedText();
                    if (selectedText == null) {
                        master.setValue("");
                    } else {
                        master.setValue(selectedText);
                    }
                } else if (master.getName().equals(CLIPBOARD_CONTENT_PARAMETER)) {
                    // use the contents of the clipboard - which may come from other apps
                }
            } // for
        }
        
        public void release() {
            
        }
        
        public static final class Factory implements CodeTemplateProcessorFactory {
            public CodeTemplateProcessor createProcessor(CodeTemplateInsertRequest request) {
                return new SelectionCodeTemplateProcessor(request);
            }
        }
    }
    

Here are some ideas for code template parameters:

  • ${date format=""} - date with date formats
  • ${file} - current files name. Variuations - full path, just the name,
  • ${method} - enclosing method name (Java)
  • ${type} - enclosing class or type name (Java)

For more details go here.

Posted by sandipchitale ( Jan 31 2006, 06:47:38 PM PST ) Permalink Comments [4]










January 2006 »
SunMonTueWedThuFriSat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
20
28
    
       
Today

Get NetBeans 5.5

Locations of visitors to this page

Today's Page Hits: 18


XML
All
/Creator
/General
/Hobby
/Java
/JavaScript
/Mozilla
/NetBeans
/Ubuntu
/VisualWeb
/VisualWebPack
/Web 2.0

XML
All
/Creator
/General
/Hobby
/Java
/JavaScript
/Mozilla
/NetBeans
/Ubuntu
/VisualWeb
/VisualWebPack
/Web 2.0

scblog
scblog