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.

Comments:

Geertjan - Where can we find the CodeGenerator class? I added the library dependencies as you mentioned, but the CodeGenerator class with the above mentioned package can't seem to be found. Am I missing something simple?

Posted by Adam Myatt on April 23, 2008 at 03:41 AM PDT #

Adam, please read the last sentence again. In other words, you need to set an implementation dependency on "Java Editor". The CodeGenerator class is not public yet. So, in the Libraries panel, right-click the "Java Editor" entry and then set "Implementation Version". Then the CodeGenerator class will be exposed. Hope to see you at JavaOne (are you doing a book signing, if so when)?

Posted by Geertjan on April 23, 2008 at 04:17 AM PDT #

Ah ha! Guess I read over that too quickly. Thanks! works now.

Posted by Adam Myatt on April 23, 2008 at 08:03 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
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 2014
SunMonTueWedThuFriSat
  
12
13
14
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today