Volodymyr B
Volodymyr B

Reputation: 37

How do I handle file saves properly in NetBeans platform project (plugin)

I try to create a new language support for NetBeans 7.4 and higher.

When files are being saved locally I need to deploy them to a server. So I need to handle the save event. I did this implementing 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.

Upvotes: 3

Views: 651

Answers (1)

Milan Nosáľ
Milan Nosáľ

Reputation: 19737

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);
        }

    }
}

Upvotes: 2

Related Questions