如何将java编辑器中的文本作为参数传递给我的netbeans模块?

时间:2015-10-08 19:32:08

标签: java netbeans-platform

我正在创建一个Netbeans模块。如何将java编辑器中的选定文本作为参数传递给我的ActionListener类,并且在处理之后,如何用java编辑器中的新处理文本替换这个旧文本(作为参数传递)?

@ActionID(category = "Edit", id = "com.beg.regextester.RegexTesterListener")
@ActionRegistration(displayName = "Regex Tester")
@ActionReference(path = "Editors/text/x-java/Popup")
public class RegexTesterListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        //the code here
    }

1 个答案:

答案 0 :(得分:0)

经过长时间的研究,我明白了。

@ActionID(category = "Edit", id = "com.beg.regextester.RegexTesterListener")
@ActionRegistration(displayName = "Regex Tester")
@ActionReference(path = "Editors/text/x-java/Popup")
public class RegexTesterListener implements ActionListener {

    private final DataObject context;

    public RegexTesterListener(DataObject context) {
        this.context = context;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        //Identify java object in the context
        FileObject fileObject = context.getPrimaryFile();
        JavaSource javaSrc = JavaSource.forFileObject(fileObject);
        if (javaSrc == null) {
           StatusDisplayer.getDefault().setStatusText(fileObject.getPath() + " is not a java file");
    } else {
        try {
            javaSrc.runUserActionTask(new org.netbeans.api.java.source.Task<CompilationController>() {

                @Override
                public void run(CompilationController p) throws Exception {
                    p.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
                    Document doc = p.getDocument();
                    if (doc == null) {
                        StatusDisplayer.getDefault().setStatusText("Java file is closed");
                    } else {
                        new MemberVisitor(p).scan(p.getCompilationUnit(), null);
                    }
                }
            }, true);
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        }
    }//end else
    }//end actionPerformed

    private static class MemberVisitor extends TreePathScanner<Void, Void> {

    private CompilationInfo info;

    public MemberVisitor(CompilationInfo info) {
        this.info = info;
    }

    @Override
    public Void visitClass(ClassTree t, Void v) {
        try {
            JTextComponent editor = EditorRegistry.lastFocusedComponent();

            if (editor.getDocument() == info.getDocument()) {
                int dot = editor.getCaret().getDot();

                TreePath tp = info.getTreeUtilities().pathFor(dot);
                Element el = info.getTrees().getElement(tp);
                if (el != null) {
                    StatusDisplayer.getDefault().setStatusText("Please, select a string");
                } else {
                        //get the selected text
                        String str = editor.getSelectedText();
                        //process the string and pass it to the clipboard
                        ...
                        //replacing the old str by the new one
                        editor.paste(); 


                }
            }
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        }
        return null;
    }

}
}//end class