001/*******************************************************************************
002 * This software is provided as a supplement to the authors' textbooks on digital
003 * image processing published by Springer-Verlag in various languages and editions.
004 * Permission to use and distribute this software is granted under the BSD 2-Clause
005 * "Simplified" License (see http://opensource.org/licenses/BSD-2-Clause).
006 * Copyright (c) 2006-2023 Wilhelm Burger, Mark J. Burge. All rights reserved.
007 * Visit https://imagingbook.com for additional details.
008 ******************************************************************************/
009package Tools_;
010
011/**
012 * ImageJ plugin, closes all open images except the currently active image. Much copied from from
013 * ij.plugin.WindowOrganizer.java
014 *
015 * @author WB
016 * @version 2012/02/15
017 */
018import ij.ImagePlus;
019import ij.WindowManager;
020import ij.gui.GenericDialog;
021import ij.plugin.filter.PlugInFilter;
022import ij.process.ImageProcessor;
023import imagingbook.core.jdoc.JavaDocHelp;
024
025public class Close_Other_Images implements PlugInFilter, JavaDocHelp {
026        
027        private ImagePlus im;
028        
029        @Override
030        public int setup(String arg0, ImagePlus im) {
031                this.im = im;
032                return DOES_ALL + NO_CHANGES;
033        }
034        
035        @Override
036        public void run(ImageProcessor ip) {
037                                int[] winIds = WindowManager.getIDList();
038                if (winIds == null || winIds.length < 1) {
039                        return;
040                }
041                
042                GenericDialog gd = new GenericDialog("Close all images");
043                gd.addHelp(getJavaDocUrl());
044                gd.addCheckbox("Close current image too?", false);
045                gd.showDialog();
046                if (gd.wasCanceled()) {
047                        return;
048                }
049                
050                boolean closeCurrentImage = gd.getNextBoolean();
051                int thisId = this.im.getID();
052                
053                for (int id : winIds) {
054                        if (id != thisId || closeCurrentImage) {
055                                ImagePlus imp = WindowManager.getImage(id);
056                                if (imp != null) {
057                                        imp.close();
058                                }
059                        }
060                }
061        }
062}
063