package ij.plugin;
import ij.*;
import ij.io.*;
import ij.gui.*;
import java.io.File;
public class NextImageOpener implements PlugIn {
boolean forward = true; boolean closeCurrent = true; ImagePlus imp0;
public void run(String arg) {
if (arg.equals("backward") || IJ.altKeyDown()) forward = false;
if (arg.equals("backwardsc")) {
forward = false;
closeCurrent = false;
}
if (arg.equals("forwardsc")) {
forward = true;
closeCurrent = false;
}
imp0 = IJ.getImage();
String currentPath = getDirectory(imp0);
if (IJ.debugMode) IJ.log("OpenNext.currentPath:" + currentPath);
if (currentPath==null) {
IJ.error("Next Image", "Directory information for \""+imp0.getTitle()+"\" not found.");
return;
}
String nextPath = getNext(currentPath, getName(imp0), forward);
if (IJ.debugMode) IJ.log("OpenNext.nextPath:" + nextPath);
if (nextPath != null) {
String rtn = open(nextPath);
if (rtn==null)
open(getNext(currentPath, (new File(nextPath)).getName(), forward));
}
}
String getDirectory(ImagePlus imp) {
FileInfo fi = imp.getOriginalFileInfo();
if (fi==null) return null;
String dir = fi.openNextDir;
if (dir==null) dir = fi.directory;
return dir;
}
String getName(ImagePlus imp) {
String name = imp.getTitle();
FileInfo fi = imp.getOriginalFileInfo();
if (fi!=null) {
if (fi.openNextName!=null)
name = fi.openNextName;
else if (fi.fileName!=null)
name = fi.fileName;
}
return name;
}
String open(String nextPath) {
int nImages = WindowManager.getImageCount();
ImagePlus imp2 = IJ.openImage(nextPath);
if (imp2==null) {
if (WindowManager.getImageCount()>nImages)
return "ok";
else
return null;
}
String newTitle = imp2.getTitle();
if (imp0.changes) {
String msg;
String name = imp0.getTitle();
if (name.length()>22)
msg = "Save changes to\n" + "\"" + name + "\"?";
else
msg = "Save changes to \"" + name + "\"?";
YesNoCancelDialog d = new YesNoCancelDialog(imp0.getWindow(), "ImageJ", msg);
if (d.cancelPressed())
return "Canceled";
else if (d.yesPressed()) {
FileSaver fs = new FileSaver(imp0);
if (!fs.save())
return "Canceled";
}
imp0.changes = false;
}
if (!(imp0 instanceof CompositeImage) && (imp2.isComposite() || imp2.isHyperStack())) {
imp2.show();
imp0.close();
imp0 = imp2;
} else
imp0.setImage(imp2);
return "ok";
}
String getNext(String path, String imageName, boolean forward) {
File dir = new File(path);
if (!dir.isDirectory())
return null;
String[] names = dir.list();
if (names==null) {
IJ.log("getNext directory empty: "+path);
return null;
}
ij.util.StringSorter.sort(names);
int thisfile = -1;
for (int i=0; i<names.length; i++) {
if (names[i].equals(imageName)) {
thisfile = i;
break;
}
}
if (IJ.debugMode) IJ.log("OpenNext.thisfile:" + thisfile);
if(thisfile == -1) return null;
int candidate = thisfile + 1;
if (!forward) candidate = thisfile - 1;
if (candidate<0) candidate = names.length - 1;
if (candidate==names.length) candidate = 0;
while (candidate!=thisfile) {
String nextPath = path + names[candidate];
if (IJ.debugMode) IJ.log("OpenNext: "+ candidate + " " + names[candidate]);
File nextFile = new File(nextPath);
boolean canOpen = true;
if (names[candidate].startsWith(".") || nextFile.isDirectory())
canOpen = false;
if (canOpen) {
Opener o = new Opener();
int type = o.getFileType(nextPath);
if (type==Opener.JAVA_OR_TEXT
|| type==Opener.ROI || type==Opener.TEXT)
canOpen = false;
}
if (canOpen)
return nextPath;
else { if (forward)
candidate = candidate + 1;
else
candidate = candidate - 1;
if (candidate<0) candidate = names.length - 1;
if (candidate == names.length) candidate = 0;
}
}
if (IJ.debugMode) IJ.log("OpenNext: Search failed");
return null;
}
}