All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ij.WindowManager Maven / Gradle / Ivy

Go to download

ImageJ is an open source Java image processing program inspired by NIH Image for the Macintosh.

There is a newer version: 1.54p
Show newest version
package ij;
import ij.plugin.Converter;
import ij.plugin.frame.Recorder;
import ij.plugin.frame.Editor; 
import ij.macro.Interpreter;
import ij.text.TextWindow;
import ij.plugin.frame.PlugInFrame;
import ij.util.Tools;
import java.awt.*;
import java.util.*;
import ij.gui.*;

/** This class consists of static methods used to manage ImageJ's windows. */
public class WindowManager {

	public static boolean checkForDuplicateName;
	private static Vector imageList = new Vector();		 // list of image windows
	private static Vector nonImageList = new Vector();	// list of non-image windows (Frames and Dialogs)
	private static ImageWindow currentWindow;			 // active image window
	private static Window frontWindow;
	private static Frame frontFrame;
	private static Hashtable tempImageTable = new Hashtable();
	
	private WindowManager() {
	}

	/** Makes the image contained in the specified window the active image. */
	public static void setCurrentWindow(ImageWindow win) {
		if (win==null || win.isClosed() || win.getImagePlus()==null) // deadlock-"wait to lock"
			return;
		//IJ.log("setCurrentWindow: "+win.getImagePlus().getTitle()+" ("+(currentWindow!=null?currentWindow.getImagePlus().getTitle():"null") + ")");
		setWindow(win);
		tempImageTable.remove(Thread.currentThread());
		if (win==currentWindow || imageList.size()==0)
			return;
		if (currentWindow!=null) {
			// free up pixel buffers used by current window
			ImagePlus imp = currentWindow.getImagePlus();
			if (imp!=null ) {
				if (!Prefs.keepUndoBuffers)
					imp.trimProcessor();
				imp.saveRoi();
			}
		}
		Undo.reset();
		currentWindow = win;
		Menus.updateMenus();
		if (Recorder.record && !IJ.isMacro())
			Recorder.record("selectWindow", win.getImagePlus().getTitle());
	}
	
	/** Returns the active ImageWindow. */
	public static ImageWindow getCurrentWindow() {
		//if (IJ.debugMode) IJ.write("ImageWindow.getCurrentWindow");
		return currentWindow;
	}

	static int getCurrentIndex() {
		return imageList.indexOf(currentWindow);
	}

	/** Returns a reference to the active image or null if there isn't one.
	 * @see ij.IJ#getImage
	 */
	public static ImagePlus getCurrentImage() {
		ImagePlus img = (ImagePlus)tempImageTable.get(Thread.currentThread());
		//String str = (img==null)?" null":"";
		if (img==null)
			img = getActiveImage();
		//if (img!=null) IJ.log("getCurrentImage: "+img.getTitle()+" "+Thread.currentThread().hashCode()+str);
		return img;
	}

	/** Makes the specified image temporarily the active 
		image for this thread. Call again with a null
		argument to revert to the previous active image. */
	public static void setTempCurrentImage(ImagePlus img) {
		//IJ.log("setTempImage: "+(img!=null?img.getTitle():"null")+" "+Thread.currentThread().hashCode());
		if (img==null)
			tempImageTable.remove(Thread.currentThread());
		else
			tempImageTable.put(Thread.currentThread(), img);
	}
	
	/** Sets a temporary image for the specified thread. */
	public static void setTempCurrentImage(Thread thread, ImagePlus img) {
		if (thread==null)
			throw new RuntimeException("thread==null");
		if (img==null)
			tempImageTable.remove(thread);
		else
			tempImageTable.put(thread, img);
	}

	/** Returns the active ImagePlus. */
	private static ImagePlus getActiveImage() {
		if (currentWindow!=null)
			return currentWindow.getImagePlus();
		else if (frontWindow!=null && (frontWindow instanceof ImageWindow))
			return ((ImageWindow)frontWindow).getImagePlus();
		else 	if (imageList.size()>0) {	
			ImageWindow win = (ImageWindow)imageList.elementAt(imageList.size()-1);
			return win.getImagePlus();
		} else
			return Interpreter.getLastBatchModeImage(); 
	}

	/** Returns the number of open image windows. */
	public static int getWindowCount() {
		int count = imageList.size();
		return count;
	}

	/** Returns the number of open images. */
	public static int getImageCount() {
		int count = imageList.size();
		count += Interpreter.getBatchModeImageCount();
		if (count==0 && getCurrentImage()!=null)
			count = 1;
		return count;
	}

	/** Returns the front most window or null. */
	public static Window getActiveWindow() {
		return frontWindow;
	}

	/** Obsolete; replaced by getActiveWindow. */
	public static Frame getFrontWindow() {
		return frontFrame;
	}

	/** Returns a list of the IDs of open images. Returns
		null if no windows are open. */
	public synchronized static int[] getIDList() {
		int nWindows = imageList.size();
		int[] batchModeImages = Interpreter.getBatchModeImageIDs();
		int nBatchImages = batchModeImages.length;
		if ((nWindows+nBatchImages)==0)
			return null;
		int[] list = new int[nWindows+nBatchImages];
		for (int i=0; i0)
			imageID = getNthImageID(imageID);
		if (imageID==0 || getImageCount()==0)
			return null;
		ImagePlus imp2 = Interpreter.getBatchModeImage(imageID);
		if (imp2!=null)
			return imp2;
		ImagePlus imp = null;
		for (int i=0; ilist.length)
                	return 0;
                else
                	return list[n-1];
            } else {
            	if (n>imageList.size()) return 0;
                ImageWindow win = (ImageWindow)imageList.elementAt(n-1);
                if (win!=null)
                    return win.getImagePlus().getID();
                else
                    return 0;
            }
	}

	
	/** Returns the first image that has the specified title or null if it is not found. */
	public synchronized static ImagePlus getImage(String title) {
		int[] wList = getIDList();
		if (wList==null) return null;
		for (int i=0; i=0) {
			 	//if (ij!=null && !ij.quitting())
				Menus.removeWindowMenuItem(index);
				nonImageList.removeElement(win);
			}
		}
		setWindow(null);
	}

	/** Removes the specified Frame from the Window menu. */
	public static void removeWindow(Frame win) {
		removeWindow((Window)win);
	}

	private static void removeImageWindow(ImageWindow win) {
		int index = imageList.indexOf(win);
		if (index==-1)
			return;  // not on the window list
		if (imageList.size()>1 && IJ.isMacro()) {
			int newIndex = index-1;
			if (newIndex<0)
				newIndex = imageList.size()-1;
			setCurrentWindow((ImageWindow)imageList.elementAt(newIndex));
		} else
			currentWindow = null;
		imageList.removeElementAt(index);
		setTempCurrentImage(null);  //???
		int nonImageCount = nonImageList.size();
		if (nonImageCount>0)
			nonImageCount++;
		Menus.removeWindowMenuItem(nonImageCount+index);
		Menus.updateMenus();
		Undo.reset();
	}

	/** The specified Window becomes the front window. */
	public static void setWindow(Window win) {
		frontWindow = win;
		if (win instanceof Frame)
			frontFrame = (Frame)win;
    }

	/** The specified frame becomes the front window, the one returnd by getFrontWindow(). */
	public static void setWindow(Frame win) {
		frontWindow = win;
		frontFrame = win;
		//IJ.log("Set window: "+(win!=null?win.getTitle():"null"));
    }

	/** Closes all windows. Stops and returns false if an image or Editor "save changes" dialog is canceled. */
	public synchronized static boolean closeAllWindows() {
		while (imageList.size()>0) {
			if (!((ImageWindow)imageList.elementAt(0)).close())
				return false;
			IJ.wait(100);
		}
		Frame[] nonImages = getNonImageWindows();
		for (int i=0; i0) // remove image size (e.g., " 90K")
		menuItemLabel = menuItemLabel.substring(0, lastSpace);
		String idString = item.getActionCommand();
		int id = (int)Tools.parseDouble(idString, 0);
		ImagePlus imp = WindowManager.getImage(id);
		if (imp==null) return;
		ImageWindow win1 = imp.getWindow();
		if (win1==null) return;
		setCurrentWindow(win1);
		toFront(win1);
		int index = imageList.indexOf(win1);
		int n = Menus.window.getItemCount();
		int start = Menus.WINDOW_MENU_ITEMS+Menus.windowMenuItems2;
		for (int j=start; j




© 2015 - 2025 Weber Informatics LLC | Privacy Policy