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-2016 Wilhelm Burger, Mark J. Burge. All rights reserved. 
007 * Visit http://imagingbook.com for additional details.
008 *******************************************************************************/
009
010package imagingbook.lib.ij;
011
012import java.util.ArrayList;
013import java.util.List;
014
015import ij.IJ;
016import ij.ImagePlus;
017import ij.WindowManager;
018import ij.gui.GenericDialog;
019
020public class GuiTools {
021        
022        /**
023         * Queries the user to select one of the currently open images.
024         * String parameter 'title' can be used to specify the title
025         * of the dialog (if null, the default title is used).
026         * Parameter 'excludeIm' can be used to specify an image to exclude
027         * from being selected (typically the current image).
028         * Returns a reference to the chosen image (ImagePlus) or
029         * null, if the dialog was canceled.
030         * 
031         * @author W. Burger
032         * @version 2012/02
033         */
034        static String defaultTitle = "Choose image";
035        
036    public static ImagePlus chooseOpenImage(String title, ImagePlus excludeIm) {
037                if (title == null) {
038                        title = defaultTitle;
039                }
040        
041                int[] imgIdsAll = WindowManager.getIDList();
042                if (imgIdsAll==null) {
043                        IJ.error("No images are open.");
044                        return null;
045                }
046
047                List<Integer> imgIdList   = new ArrayList<Integer>(imgIdsAll.length);   // use a Map instead?
048                List<String>  imgNameList = new ArrayList<String>(imgIdsAll.length);
049                
050                for (int id : imgIdsAll) {
051                        ImagePlus img = WindowManager.getImage(id);
052                        if (img!=null && img != excludeIm && img.isProcessor()) {
053                                imgIdList.add(id);
054                                imgNameList.add(img.getShortTitle());
055                        }
056                }
057                
058                if (imgIdList.size() < 1) {
059                        IJ.error("No other images found.");
060                        return null;
061                }
062                
063                Integer[] imgIds   = imgIdList.toArray(new Integer[0]);
064                String[]  imgNames = imgNameList.toArray(new String[0]);
065                GenericDialog gd = new GenericDialog(title, null);
066                gd.addChoice("Image:", imgNames, imgNames[0]);  
067                gd.showDialog();
068                if (gd.wasCanceled())
069                        return null;
070                else {
071                        int idx = gd.getNextChoiceIndex();
072                        return WindowManager.getImage(imgIds[idx]);
073                }
074    }
075    
076    /* 
077     * Convenience methods for the above.
078     */
079    
080        public static ImagePlus chooseOpenImage(String title) {
081                return chooseOpenImage(title, null);
082        }
083        
084    public static ImagePlus chooseOpenImage() {
085        return chooseOpenImage(null, null);
086    }
087
088}