如何在NetBeans平台项目(插件)中正确处理文件保存



我尝试为NetBeans 7.4及更高版本创建一个新的语言支持。

当文件保存在本地时,我需要将它们部署到服务器上。我需要处理保存事件。我这样做是为了实现Savable:


     public class VFDataObject extends MultiDataObject implements Savable {
       .......
       @Override
       public void save() throws IOException {
         .......
       }
     }

And it worked perfectly for the Save event. But then I realized I need to extend HtmlDataObject instead of MultiDataObject:


    public class VFDataObject extends HtmlDataObject implements Savable {
       .......
       @Override
       public void save() throws IOException {
         .......
       }
    }

And now the save() doesn't get executed. Why? Since HtmlDataObject extends MultiDataObject. What should be done to make that work?

Also is there a way to catch Save All event in NetBeans as well? Do you have any info on if anything changed in 8.0 in this regards?

Thanks a lot.

Have you tried OnSaveTask SPI (https://netbeans.org/bugzilla/show_bug.cgi?id=140719)? The API can be used to perform tasks when files of a given type are saved.

Something like this can be used to listen to all the save events on a given MIME type (in this case "text/x-sieve-java"):

public static class CustomOnSaveTask implements OnSaveTask {
    private final Context context;
    public CustomOnSaveTask(Context ctx) {
        context = ctx;
    }
    @Override
    public void performTask() {
        System.out.println(">>> Save performed on " + 
                NbEditorUtilities.getDataObject(context.getDocument()).toString());
    }
    @Override
    public void runLocked(Runnable r) {
        r.run();
    }
    @Override
    public boolean cancel() {
        return true;
    }
    @MimeRegistration(mimeType = "text/x-sieve-java", service = OnSaveTask.Factory.class, position = 1600)
    public static class CustomOnSaveTaskFactory implements OnSaveTask.Factory {
        @Override
        public OnSaveTask createTask(Context cntxt) {
            return new CustomOnSaveTask(cntxt);
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新