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

如何将java编辑器中的文本作为参数传递给我的netbeans模块?,java,netbeans-platform,Java,Netbeans Platform,我正在创建一个Netbeans模块。如何将java编辑器中选定的文本作为参数传递给我的ActionListener类,在处理它之后,如何用java编辑器中新处理的文本替换这个旧文本(作为参数传递) @ActionID(category = "Edit", id = "com.beg.regextester.RegexTesterListener") @ActionRegistration(displayName = "Regex Tester") @ActionReference(path =

我正在创建一个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
    }

经过长时间的研究,我得到了它

@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
@ActionID(category=“Edit”,id=“com.beg.regextester.RegexTesterListener”)
@操作注册(displayName=“Regex Tester”)
@ActionReference(path=“Editors/text/x-java/Popup”)
公共类RegextestListener实现ActionListener{
私有最终数据对象上下文;
公共RegextestListener(数据对象上下文){
this.context=上下文;
}
@凌驾
已执行的公共无效操作(操作事件e){
//在上下文中标识java对象
FileObject=context.getPrimaryFile();
JavaSource javaSrc=JavaSource.forFileObject(fileObject);
if(javaSrc==null){
StatusDisplayer.getDefault().setStatusText(fileObject.getPath()+“不是java文件”);
}否则{
试一试{
javaSrc.runUserActionTask(new org.netbeans.api.java.source.Task(){
@凌驾
公共无效运行(CompliationController p)引发异常{
p、 toPhase(JavaSource.Phase.ELEMENTS_解析);
Document doc=p.getDocument();
如果(doc==null){
StatusDisplayer.getDefault().setStatusText(“Java文件已关闭”);
}否则{
新建MemberVisitor(p.scan)(p.getCompilationUnit(),null);
}
}
},对);
}捕获(IOEX异常){
例外情况。printStackTrace(ex);
}
}//结束其他
}//已执行的结束操作
私有静态类MemberVisitor扩展TreePathScanner{
私人编辑信息;
公共成员访问者(编译信息){
this.info=info;
}
@凌驾
公共Void visitClass(类树t、Void v){
试一试{
JTextComponent editor=EditorRegistry.lastFocusedComponent();
if(editor.getDocument()==info.getDocument()){
int dot=editor.getCaret().getDot();
TreePath tp=info.getTreeUtilities().pathFor(点);
Element el=info.getTrees().getElement(tp);
如果(el!=null){
StatusDisplayer.getDefault().setStatusText(“请选择一个字符串”);
}否则{
//获取所选文本
String str=editor.getSelectedText();
//处理字符串并将其传递到剪贴板
...
//用新的替换旧的str
editor.paste();
}
}
}捕获(IOEX异常){
例外情况。printStackTrace(ex);
}
返回null;
}
}
}//末级