Skip to content

Commit

Permalink
Makes BdvHandle helper more robust to it being potentially a BdvHandl…
Browse files Browse the repository at this point in the history
…ePanel instead of a BdvHandleFrame
  • Loading branch information
NicoKiaru committed Dec 10, 2024
1 parent 5eed571 commit c05b691
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 28 deletions.
45 changes: 32 additions & 13 deletions src/main/java/sc/fiji/bdvpg/bdv/BdvHandleHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@
package sc.fiji.bdvpg.bdv;

import bdv.tools.brightness.ConverterSetup;
import bdv.ui.CardPanel;
import bdv.util.BdvFunctions;
import bdv.util.BdvHandle;
import bdv.util.BdvHandleFrame;
import bdv.util.BdvHandlePanel;
import bdv.util.BdvOptions;
import bdv.util.BdvOverlay;
import bdv.viewer.Source;
Expand Down Expand Up @@ -336,13 +337,19 @@ public static double[] getDisplayRange(ConverterSetup converterSetup) {
}

public static JFrame getJFrame(BdvHandle bdvh) {
if (!(bdvh instanceof BdvHandleFrame)) {
throw new RuntimeException("The BdvHandle is a Panel, not a Frame");
}
return (JFrame) SwingUtilities.getWindowAncestor(bdvh.getViewerPanel());
}

public static void setBdvHandleCloseOperation(BdvHandle bdvh, CacheService cs,
SourceAndConverterBdvDisplayService bdvsds, boolean putWindowOnTop,
Runnable runnable)
{
if (bdvh instanceof BdvHandlePanel) {
throw new RuntimeException("The BdvHandle is a Panel, not a Frame");
}
JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(bdvh
.getViewerPanel());
WindowAdapter wa;
Expand Down Expand Up @@ -375,38 +382,50 @@ public void windowActivated(WindowEvent e) {
topFrame.addWindowListener(wa);

if (putWindowOnTop) {
cs.put("LAST_ACTIVE_BDVH", new WeakReference<>(bdvh));// why a weak
// reference ?
// because we want
// to dispose the
// bdvhandle if it
// is closed
cs.put("LAST_ACTIVE_BDVH", new WeakReference<>(bdvh));// why a wea kreference ?
// because we want to dispose the bdvhandle if it is closed
}
}

public static void activateWindow(BdvHandle bdvh) {
JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(bdvh
.getViewerPanel());
topFrame.toFront();
topFrame.requestFocus();
if (topFrame != null) {
topFrame.toFront();
topFrame.requestFocus();
} else {
bdvh.getViewerPanel().requestFocus();
}
}

public static void closeWindow(BdvHandle bdvh) {
JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(bdvh
.getViewerPanel());
topFrame.dispatchEvent(new WindowEvent(topFrame,
WindowEvent.WINDOW_CLOSING));
if (topFrame!=null) {
topFrame.dispatchEvent(new WindowEvent(topFrame,
WindowEvent.WINDOW_CLOSING));
} else {
System.err.println("Can't close the bdv handle because it is of class "+bdvh.getClass().getName());
}
}

public static void setWindowTitle(BdvHandle bdvh, String title) {
JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(bdvh
.getViewerPanel());
topFrame.setTitle(title);
if (topFrame!=null) {
topFrame.setTitle(title);
} else {
System.err.println("Can't set the bdv handle window title because it is of class "+bdvh.getClass().getName());
}
}

public static String getWindowTitle(BdvHandle bdvh) {
JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(bdvh
.getViewerPanel());
.getViewerPanel());
if (topFrame == null) {
System.err.println("Can't set the window title since the bdv handle because it is of class "+bdvh.getClass().getName());
return bdvh.toString();
}
return topFrame.getTitle();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
package sc.fiji.bdvpg.scijava.command.bdv;

import bdv.util.BdvHandle;
import bdv.util.BdvHandleFrame;
import org.scijava.ItemIO;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
Expand Down Expand Up @@ -138,21 +139,21 @@ BdvHandle createBdv(String suffix, double locX, double locY) {

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gd = ge.getScreenDevices();
JFrame frame = BdvHandleHelper.getJFrame(bdvh);
SwingUtilities.invokeLater(() -> {
if (screen > -1 && screen < gd.length) {
frame.setLocation(gd[screen].getDefaultConfiguration().getBounds().x +
(int) locX, (int) locY);
}
else if (gd.length > 0) {
frame.setLocation(gd[0].getDefaultConfiguration().getBounds().x +
(int) locX, (int) locY);
}
else {
throw new RuntimeException("No Screens Found");
}
frame.setSize(sizex, sizey);
});
if (bdvh instanceof BdvHandleFrame) {
JFrame frame = BdvHandleHelper.getJFrame(bdvh);
SwingUtilities.invokeLater(() -> {
if (screen > -1 && screen < gd.length) {
frame.setLocation(gd[screen].getDefaultConfiguration().getBounds().x +
(int) locX, (int) locY);
} else if (gd.length > 0) {
frame.setLocation(gd[0].getDefaultConfiguration().getBounds().x +
(int) locX, (int) locY);
} else {
throw new RuntimeException("No Screens Found");
}
frame.setSize(sizex, sizey);
});
}

return bdvh;
}
Expand Down

0 comments on commit c05b691

Please sign in to comment.