1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
/**
This plugin, written by Jon Harmon, implements the File/Open Next command.
It opens the "next" image in a directory, where "next" can be the
succeeding or preceeding image in the directory list.
Press shift-o to open the succeeding image or
alt-shift-o to open the preceeding image.
It can leave the previous file open, or close it.
You may contact the author at Jonathan_Harman at yahoo.com
This code was modified from Image_Browser by Albert Cardona
*/
package ij.plugin;
import ij.*;
import ij.io.*;
import ij.gui.*;
import java.io.File;
public class NextImageOpener implements PlugIn {
boolean forward = true; // default browse direction is forward
boolean closeCurrent = true; //default behavior is to close current window
ImagePlus imp0;
public void run(String arg) {
/* get changes to defaults */
if (arg.equals("backward") || IJ.altKeyDown()) forward = false;
if (arg.equals("backwardsc")) {
forward = false;
closeCurrent = false;
}
if (arg.equals("forwardsc")) {
forward = true;
closeCurrent = false;
}
// get current image; displays error and aborts if no image is open
imp0 = IJ.getImage();
// get current image directory
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;
}
// get the next name (full path)
//long start = System.currentTimeMillis();
String nextPath = getNext(currentPath, getName(imp0), forward);
//IJ.log("time: "+(System.currentTimeMillis()-start));
if (IJ.debugMode) IJ.log("OpenNext.nextPath:" + nextPath);
// open
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) {
ImagePlus imp2 = IJ.openImage(nextPath);
if (imp2==null) 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 (imp2.isComposite() || imp2.isHyperStack()) {
imp2.show();
imp0.close();
imp0 = imp2;
} else {
imp0.setStack(newTitle, imp2.getStack());
imp0.setCalibration(imp2.getCalibration());
imp0.setFileInfo(imp2.getOriginalFileInfo());
imp0.setProperty ("Info", imp2.getProperty ("Info"));
ImageWindow win = imp0.getWindow();
if (win!=null) win.repaint();
}
return "ok";
}
/** gets the next image name in a directory list */
String getNext(String path, String imageName, boolean forward) {
File dir = new File(path);
if (!dir.isDirectory()) return null;
String[] names = dir.list();
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;// can't find current image
// make candidate the index of the next file
int candidate = thisfile + 1;
if (!forward) candidate = thisfile - 1;
if (candidate<0) candidate = names.length - 1;
if (candidate==names.length) candidate = 0;
// keep on going until an image file is found or we get back to beginning
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.UNKNOWN || type==Opener.JAVA_OR_TEXT
|| type==Opener.ROI || type==Opener.TEXT)
canOpen = false;
}
if (canOpen)
return nextPath;
else {// increment again
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;
}
}
|