use of ij.gui.StackWindow in project TrakEM2 by trakem2.
the class Tree method openImage.
/**
* Open an image in a separate thread and returns the thread. Frees up to 1 Gb for it.
*/
private Future<ImagePlus> openImage(final String path, final Node<T> last) {
return project.getLoader().doLater(new Callable<ImagePlus>() {
@Override
public ImagePlus call() {
try {
if (!new File(path).exists()) {
Utils.log("Could not find file " + path);
return null;
}
// 1 Gb : can't tell for jpegs and tif-jpg, TODO would have to read the header.
project.getLoader().releaseToFit(1000000000L);
final Opener op = new Opener();
op.setSilentMode(true);
// TODO WARNING should go via the Loader
final ImagePlus imp = op.openImage(path);
if (null == imp) {
Utils.log("ERROR: could not open " + path);
} else {
final StackWindow stack = new StackWindow(imp);
final MouseListener[] ml = stack.getCanvas().getMouseListeners();
for (final MouseListener m : ml) stack.getCanvas().removeMouseListener(m);
stack.getCanvas().addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(final MouseEvent me) {
if (2 == me.getClickCount()) {
me.consume();
// Go to the node
// Slices are 1-based: 1<=i<=N
final int slice = imp.getCurrentSlice();
if (slice == imp.getNSlices()) {
Display.centerAt(createCoordinate(last));
} else {
Node<T> parent = last.getParent();
int count = imp.getNSlices() - 1;
while (null != parent) {
if (count == slice) {
Display.centerAt(createCoordinate(parent));
break;
}
// next cycle
count--;
parent = parent.getParent();
}
;
}
}
}
@Override
public void mouseDragged(final MouseEvent me) {
if (2 == me.getClickCount()) {
me.consume();
}
}
@Override
public void mouseReleased(final MouseEvent me) {
if (2 == me.getClickCount()) {
me.consume();
}
}
});
for (final MouseListener m : ml) stack.getCanvas().addMouseListener(m);
}
return imp;
} catch (final Exception e) {
IJError.print(e);
}
return null;
}
});
}
Aggregations