3

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.

1 Answers1

2

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

    }
}
Milan Nosáľ
  • 19,169
  • 4
  • 55
  • 90
  • I cannot find much info on the OnSaveTask. What if I would like to handle multiple mime types? Do I need to create new class to handle it or I somehow able to handle saves for all the files I need in one place? How do I make it work for NetBeans plugin app, just creating the class? Thanks again. – Volodymyr B Nov 03 '14 at 20:31
  • I am not very experienced in NetBeans API either, this `OnSaveTask` was just a thing I ran upon when implementing one plugin and even in the end I had to solve my problem using another API. Since `@MimeRegistration` annotation cannot be used multiple times, you have to either create multiple classes for multiple mime types, or you can use a single class and register it to multiple mime types manually in `layer.xml` (I haven't tried it, but as I know NB APi this should be working). The only problem with that can be in case you don't know all mime types up front. Is that your case? – Milan Nosáľ Nov 05 '14 at 07:03
  • 1
    In case you don't know the mime types up front, you can take a look at [ListeningOnSaveEvents](http://wiki.netbeans.org/DevFaqListenForSaveEvents) where is briefly shown how to follow saving on all open files in NB editor. However, this is not the Save All event only, you will get notified for all save events. In my plugin I needed to follow only single one dataobject of a given mime type, so I tried `OnSaveTask`, and then for some practical reasons I used just `PropertyChangeListener` on a given data object (I created the data object so I had a reference to it). – Milan Nosáľ Nov 05 '14 at 07:08
  • Tried this and it worked perfectly, for the save and saveAll events. – Volodymyr B Dec 03 '14 at 16:07
  • [https://www.youtube.com/watch?v=8IIrf_JSuQk](https://www.youtube.com/watch?v=8IIrf_JSuQk) – Milan Nosáľ Dec 04 '14 at 17:16