Skip to content

Commit c05b691

Browse files
committed
Makes BdvHandle helper more robust to it being potentially a BdvHandlePanel instead of a BdvHandleFrame
1 parent 5eed571 commit c05b691

File tree

2 files changed

+48
-28
lines changed

2 files changed

+48
-28
lines changed

src/main/java/sc/fiji/bdvpg/bdv/BdvHandleHelper.java

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@
3030
package sc.fiji.bdvpg.bdv;
3131

3232
import bdv.tools.brightness.ConverterSetup;
33-
import bdv.ui.CardPanel;
3433
import bdv.util.BdvFunctions;
3534
import bdv.util.BdvHandle;
35+
import bdv.util.BdvHandleFrame;
36+
import bdv.util.BdvHandlePanel;
3637
import bdv.util.BdvOptions;
3738
import bdv.util.BdvOverlay;
3839
import bdv.viewer.Source;
@@ -336,13 +337,19 @@ public static double[] getDisplayRange(ConverterSetup converterSetup) {
336337
}
337338

338339
public static JFrame getJFrame(BdvHandle bdvh) {
340+
if (!(bdvh instanceof BdvHandleFrame)) {
341+
throw new RuntimeException("The BdvHandle is a Panel, not a Frame");
342+
}
339343
return (JFrame) SwingUtilities.getWindowAncestor(bdvh.getViewerPanel());
340344
}
341345

342346
public static void setBdvHandleCloseOperation(BdvHandle bdvh, CacheService cs,
343347
SourceAndConverterBdvDisplayService bdvsds, boolean putWindowOnTop,
344348
Runnable runnable)
345349
{
350+
if (bdvh instanceof BdvHandlePanel) {
351+
throw new RuntimeException("The BdvHandle is a Panel, not a Frame");
352+
}
346353
JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(bdvh
347354
.getViewerPanel());
348355
WindowAdapter wa;
@@ -375,38 +382,50 @@ public void windowActivated(WindowEvent e) {
375382
topFrame.addWindowListener(wa);
376383

377384
if (putWindowOnTop) {
378-
cs.put("LAST_ACTIVE_BDVH", new WeakReference<>(bdvh));// why a weak
379-
// reference ?
380-
// because we want
381-
// to dispose the
382-
// bdvhandle if it
383-
// is closed
385+
cs.put("LAST_ACTIVE_BDVH", new WeakReference<>(bdvh));// why a wea kreference ?
386+
// because we want to dispose the bdvhandle if it is closed
384387
}
385388
}
386389

387390
public static void activateWindow(BdvHandle bdvh) {
388391
JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(bdvh
389392
.getViewerPanel());
390-
topFrame.toFront();
391-
topFrame.requestFocus();
393+
if (topFrame != null) {
394+
topFrame.toFront();
395+
topFrame.requestFocus();
396+
} else {
397+
bdvh.getViewerPanel().requestFocus();
398+
}
392399
}
393400

394401
public static void closeWindow(BdvHandle bdvh) {
395402
JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(bdvh
396403
.getViewerPanel());
397-
topFrame.dispatchEvent(new WindowEvent(topFrame,
398-
WindowEvent.WINDOW_CLOSING));
404+
if (topFrame!=null) {
405+
topFrame.dispatchEvent(new WindowEvent(topFrame,
406+
WindowEvent.WINDOW_CLOSING));
407+
} else {
408+
System.err.println("Can't close the bdv handle because it is of class "+bdvh.getClass().getName());
409+
}
399410
}
400411

401412
public static void setWindowTitle(BdvHandle bdvh, String title) {
402413
JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(bdvh
403414
.getViewerPanel());
404-
topFrame.setTitle(title);
415+
if (topFrame!=null) {
416+
topFrame.setTitle(title);
417+
} else {
418+
System.err.println("Can't set the bdv handle window title because it is of class "+bdvh.getClass().getName());
419+
}
405420
}
406421

407422
public static String getWindowTitle(BdvHandle bdvh) {
408423
JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(bdvh
409-
.getViewerPanel());
424+
.getViewerPanel());
425+
if (topFrame == null) {
426+
System.err.println("Can't set the window title since the bdv handle because it is of class "+bdvh.getClass().getName());
427+
return bdvh.toString();
428+
}
410429
return topFrame.getTitle();
411430
}
412431

src/main/java/sc/fiji/bdvpg/scijava/command/bdv/BdvOrthoCreatorCommand.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
package sc.fiji.bdvpg.scijava.command.bdv;
3131

3232
import bdv.util.BdvHandle;
33+
import bdv.util.BdvHandleFrame;
3334
import org.scijava.ItemIO;
3435
import org.scijava.plugin.Parameter;
3536
import org.scijava.plugin.Plugin;
@@ -138,21 +139,21 @@ BdvHandle createBdv(String suffix, double locX, double locY) {
138139

139140
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
140141
GraphicsDevice[] gd = ge.getScreenDevices();
141-
JFrame frame = BdvHandleHelper.getJFrame(bdvh);
142-
SwingUtilities.invokeLater(() -> {
143-
if (screen > -1 && screen < gd.length) {
144-
frame.setLocation(gd[screen].getDefaultConfiguration().getBounds().x +
145-
(int) locX, (int) locY);
146-
}
147-
else if (gd.length > 0) {
148-
frame.setLocation(gd[0].getDefaultConfiguration().getBounds().x +
149-
(int) locX, (int) locY);
150-
}
151-
else {
152-
throw new RuntimeException("No Screens Found");
153-
}
154-
frame.setSize(sizex, sizey);
155-
});
142+
if (bdvh instanceof BdvHandleFrame) {
143+
JFrame frame = BdvHandleHelper.getJFrame(bdvh);
144+
SwingUtilities.invokeLater(() -> {
145+
if (screen > -1 && screen < gd.length) {
146+
frame.setLocation(gd[screen].getDefaultConfiguration().getBounds().x +
147+
(int) locX, (int) locY);
148+
} else if (gd.length > 0) {
149+
frame.setLocation(gd[0].getDefaultConfiguration().getBounds().x +
150+
(int) locX, (int) locY);
151+
} else {
152+
throw new RuntimeException("No Screens Found");
153+
}
154+
frame.setSize(sizex, sizey);
155+
});
156+
}
156157

157158
return bdvh;
158159
}

0 commit comments

Comments
 (0)