Login  Register

Re: Notification of File Save

Posted by Michael Schmid on Dec 23, 2020; 4:40pm
URL: http://imagej.273.s1.nabble.com/Notification-of-File-Save-tp5024300p5024302.html

Hi Michael,

ImageJ has a CommandListener interface. You could check whether the
command is "Jpeg..." and then do whatever you want.

The problem with it: I fear that the commandExecuting is called before
the user actually saves the file (the file save dialog), so this is too
early; you will have to remember the foreground image and wait until the
file is actually saved.
After saving is done, the FileInfo of the image will be updated with the
new filename and directory, and the 'changes' of the ImagePlus will be
set to false.
You also have to allow for the user pressing <cancel> and not saving it,
so you should stop waiting some time after the next command.
This will be somewhat a hack, not a very clean solution, however.

Unfortunately, I think that your suggestion of adding additional
callbacks to ImagePlus does not work: The ImageListener of ImagePlus
cannot be extended because its callback functions are not something like
imageOperationDone(image, whatOperation) but the notifyListeners has
separate
   imageOpened,
   imageClosed, and
   imageUpdated
calls to the listener. One can't add more without breaking compatibility
with existing plugins.

The RoiListener would be better in this respect, its callback
   roiModified(image, id)
would allow adding additional possibilities for the 'id', but saving an
image is not related to Rois.


Michael
________________________________________________________________
On 23.12.20 13:59, Michael Ellis wrote:

> Is it possible to get notified or hook into when an image is saved?
>
> The ImageListener interface allows notifications only for Open/Close and Updated.
>
> My reason for wanting this extra functionality is that I wish to save a thumbnail JPEG image whenever an image is saved.
>
> My current strategy is for my plugin to cache a BufferedImage when I receive an ImageListener Updated event through and then have Java File System watch service notice whenever a file is created or modified in a watched directory, recall the cached BufferedImage and write the thumbnail then. It's an inefficient strategy.
>
> If there is an existing means of doing this then I would be grateful to learn of it!
>
> If not, then, as I understand it, the "ImagePlus:: protected void notifyListeners(final int id)" is only called from ImagePlus and the derivative CompositeImage.
>
> Because notifyListeners() is also protected so not callable from the code that saves images such as in FileSaver.java
>
> Is there anyone else who would like to see a new interface added:
>   public interface ImageListenerNew {
>   public notifyListeners(final String id);
>   }
>
> Which could be used for some additional notifications (such as Save, and perhaps Closing - gets called just before the images is closed) and potentially could be used for user-extensible events (say with a reverse domain string with your own event name as the string ID parameter)
>
> My thinking is that some standard IDs could be added for the existing "OPENED", "CLOSED", "UPDATED" that get called in addition to the existing notifyListeners(final int id)" so backward compatibility is maintained for existing plugins but PluginDeveloppers could choose the old ImageListener or the ImageListenerNew interface if they needed the extra functionality.
>
> Thanking anyone in advance and seasons greetings to you all
>
> -- Michael Ellis
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html