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

ij.plugin.Commands 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.54m
Show newest version
package ij.plugin;
import ij.*;
import ij.process.*;
import ij.gui.*;
import ij.io.*;
import ij.plugin.frame.*;
import ij.text.TextWindow;
import ij.macro.Interpreter;
import ij.plugin.Compiler;
import java.awt.Window;
import java.io.File;
import java.applet.Applet;
	
/**	Runs miscellaneous File and Window menu commands. */
public class Commands implements PlugIn {
	
	public void run(String cmd) {
		if (cmd.equals("new")) {
			if (IJ.altKeyDown())
				IJ.runPlugIn("ij.plugin.HyperStackMaker", "");
			else
				new NewImage();
		} else if (cmd.equals("open")) {
			if (Prefs.useJFileChooser && !IJ.macroRunning())
				new Opener().openMultiple();
			else
				new Opener().open();
		} else if (cmd.equals("close"))
			close();
		else if (cmd.equals("close-all"))
			closeAll();
		else if (cmd.equals("save"))
			save();
		else if (cmd.equals("revert"))
			revert();
		else if (cmd.equals("undo"))
			undo();
		else if (cmd.equals("ij")) {
			ImageJ ij = IJ.getInstance();
			if (ij!=null) ij.toFront();
		} else if (cmd.equals("tab"))
			WindowManager.putBehind();
		else if (cmd.equals("quit")) {
			ImageJ ij = IJ.getInstance();
			if (ij!=null) ij.quit();
		} else if (cmd.equals("startup"))
			openStartupMacros();
    }
    
    void revert() {
    	ImagePlus imp = WindowManager.getCurrentImage();
		if (imp!=null)
			imp.revert();
		else
			IJ.noImage();
	}

    void save() {
    	ImagePlus imp = WindowManager.getCurrentImage();
		if (imp!=null) {
			if (imp.getStackSize()>1) {
				imp.setIgnoreFlush(true);
				new FileSaver(imp).save();
				imp.setIgnoreFlush(false);
			} else
				new FileSaver(imp).save();
		} else
			IJ.noImage();
	}
	
    void undo() {
    	ImagePlus imp = WindowManager.getCurrentImage();
		if (imp!=null)
			Undo.undo();
		else
			IJ.noImage();
	}

	void close() {
    	ImagePlus imp = WindowManager.getCurrentImage();
		Window win = WindowManager.getActiveWindow();
		if (win==null || (Interpreter.isBatchMode() && win instanceof ImageWindow))
			closeImage(imp);
		else if (win instanceof PlugInFrame && !"Commands".equals(((PlugInFrame)win).getTitle()))
			((PlugInFrame)win).close();
		else if (win instanceof PlugInDialog)
			((PlugInDialog)win).close();
		else if (win instanceof TextWindow)
			((TextWindow)win).close();
		else
			closeImage(imp);
	}

	/** Closes all image windows, or returns 'false' if the user cancels the unsaved changes dialog box. */
	public static boolean closeAll() {
    	int[] list = WindowManager.getIDList();
    	if (list!=null) {
    		int imagesWithChanges = 0;
			for (int i=0; i0 && !IJ.macroRunning()) {
				GenericDialog gd = new GenericDialog("Close All");
				String msg = null;
				String pronoun = null;
				if (imagesWithChanges==1) {
					msg = "There is one image";
					pronoun = "It";
				} else {
					msg = "There are "+imagesWithChanges+" images";
					pronoun = "They";
				}
				gd.addMessage(msg+" with unsaved changes. "+pronoun
					+" will\nbe closed without being saved if you click \"OK\".");
				gd.showDialog();
				if (gd.wasCanceled())	
					return false;
			}
			Prefs.closingAll = true;
			for (int i=0; iMacros>Open Startup Macros command
	void openStartupMacros() {
		Applet applet = IJ.getApplet();
		if (applet!=null)
			IJ.run("URL...", "url="+IJ.URL+"/applet/StartupMacros.txt");
		else {
			String path = IJ.getDirectory("macros")+"StartupMacros.txt";
			File f = new File(path);
			if (!f.exists()) {
				path = IJ.getDirectory("macros")+"StartupMacros.ijm";
				f = new File(path);
			}
			if (!f.exists()) {
				path = IJ.getDirectory("macros")+"StartupMacros.fiji.ijm";
				f = new File(path);
			}
			if (!f.exists())
				IJ.error("\"StartupMacros.txt\" not found in ImageJ/macros/");
			else
				IJ.open(path);
		}
	}
		
}







© 2015 - 2024 Weber Informatics LLC | Privacy Policy