Tuesday Apr 22, 2008

Hello Code Generator

I click Alt-Insert and I see this:

Here's the code:

import com.sun.source.util.TreePath;
import java.io.IOException;
import java.util.Collections;
import javax.swing.JOptionPane;
import javax.swing.text.JTextComponent;
import org.netbeans.api.java.source.CompilationController;
import org.netbeans.modules.java.editor.codegen.CodeGenerator;

public class HelloGenerator implements CodeGenerator {

    public static class Factory implements CodeGenerator.Factory {

        public Factory() {
        }

        @Override
        public Iterable<? extends CodeGenerator> create
                (CompilationController controller, TreePath path) throws IOException {
            return Collections.singleton(new HelloGenerator());
        }
    }

    @Override
    public String getDisplayName() {
        return "Hello World";
    }

    @Override
    public void invoke(JTextComponent arg0) {
        JOptionPane.showMessageDialog(null, "Hello World!");
    }

}

Doesn't do anything yet, but gives you a starting point. Register it like this:

<filesystem>
    <folder name="Editors">
        <folder name="text">
            <folder name="x-java">
                <folder name="codegenerators">
                    <file name="org-netbeans-modules-my-codegen-HelloGenerator$Factory.instance">
                        <attr name="position" intvalue="10"/>
                    </file>
                </folder>
            </folder>
        </folder>
    </folder>
</filesystem>

Dependencies: Javac API Wrapper, Java Editor (implementation dependency, because the above is not a public API yet), and Java Source.

About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2008 »
SunMonTueWedThuFriSat
  
2
4
5
13
18
19
23
30
   
       
Today