Search in sources :

Example 1 with StackWindow

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;
        }
    });
}
Also used : StackWindow(ij.gui.StackWindow) MouseEvent(java.awt.event.MouseEvent) MouseAdapter(java.awt.event.MouseAdapter) ImagePlus(ij.ImagePlus) Point(java.awt.Point) NoninvertibleTransformException(java.awt.geom.NoninvertibleTransformException) MouseListener(java.awt.event.MouseListener) File(java.io.File) Opener(ij.io.Opener)

Aggregations

ImagePlus (ij.ImagePlus)1 StackWindow (ij.gui.StackWindow)1 Opener (ij.io.Opener)1 Point (java.awt.Point)1 MouseAdapter (java.awt.event.MouseAdapter)1 MouseEvent (java.awt.event.MouseEvent)1 MouseListener (java.awt.event.MouseListener)1 NoninvertibleTransformException (java.awt.geom.NoninvertibleTransformException)1 File (java.io.File)1