Skip to content

Commit 1ed6e95

Browse files
committed
Code cleanup...
SwingSafeResult is a 10 year old relic that is no longer needed
1 parent 29df1f2 commit 1ed6e95

9 files changed

Lines changed: 100 additions & 168 deletions

File tree

‎src/main/java/sc/fiji/snt/PathManagerUI.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@ public Map<String, Tree> getSelectedPathsOrganizedByTrees(final boolean ifNoneSe
859859
* if unselected.
860860
*/
861861
public List<Path> getSelectedPaths(final boolean ifNoneSelectedGetAll) {
862-
return SwingSafeResult.getResult(() -> {
862+
return GuiUtils.callOnEDT(() -> {
863863
if (ifNoneSelectedGetAll && tree.getSelectionCount() == 0) {
864864
// If the view is filtered (e.g., "Hide others is toggled in navigation bar"), prefer the paths actually
865865
// listed in the JTree. Detect filtering by comparing against the full model captured during last rebuild.

‎src/main/java/sc/fiji/snt/gui/GuiUtils.java‎

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,12 @@
101101
import java.util.function.Supplier;
102102
import java.util.regex.Matcher;
103103
import java.util.regex.Pattern;
104+
import java.util.concurrent.Callable;
104105
import java.util.concurrent.CancellationException;
105106
import java.util.concurrent.CopyOnWriteArrayList;
106107
import java.util.concurrent.ExecutionException;
107108
import java.util.concurrent.Future;
109+
import java.util.concurrent.FutureTask;
108110
import java.util.stream.Collectors;
109111
import java.util.stream.IntStream;
110112

@@ -1585,25 +1587,58 @@ private Object showOpenDialog(final String title, final int selectionMode, final
15851587
return getOpenFileChooserResult(fileChooser);
15861588
}
15871589

1590+
/**
1591+
* Runs {@code c} on the Event Dispatch Thread (EDT) and returns its result.
1592+
* Runs immediately if already on the EDT, otherwise blocks until done.
1593+
*
1594+
* @param <T> the result type
1595+
* @param c the task to run
1596+
* @return the result of {@code c}
1597+
* @throws IllegalStateException if the calling thread was interrupted while
1598+
* waiting (its interrupt status is restored)
1599+
* @throws RuntimeException the exception thrown by {@code c}, if any
1600+
* (checked exceptions are wrapped)
1601+
*/
1602+
public static <T> T callOnEDT(final Callable<T> c) {
1603+
final FutureTask<T> ft = new FutureTask<>(c);
1604+
try {
1605+
if (SwingUtilities.isEventDispatchThread()) ft.run();
1606+
else SwingUtilities.invokeAndWait(ft);
1607+
return ft.get();
1608+
} catch (final InterruptedException e) {
1609+
Thread.currentThread().interrupt();
1610+
throw new IllegalStateException("Interrupted while waiting for the EDT", e);
1611+
} catch (final ExecutionException | java.lang.reflect.InvocationTargetException e) {
1612+
final Throwable cause = (e.getCause() == null) ? e : e.getCause();
1613+
if (cause instanceof RuntimeException) throw (RuntimeException) cause;
1614+
if (cause instanceof Error) throw (Error) cause;
1615+
throw new RuntimeException(cause);
1616+
}
1617+
}
1618+
1619+
/**
1620+
* As {@link #callOnEDT(Callable)}, for tasks that do not return a value
1621+
*
1622+
* @param r the task to run
1623+
*/
1624+
public static void runOnEDT(final Runnable r) {
1625+
callOnEDT(() -> {
1626+
r.run();
1627+
return null;
1628+
});
1629+
}
1630+
15881631
private Object showNativeOpenDialog(final String title, final int selectionMode, final boolean multiSelection,
15891632
final File preset, final boolean presetIsDirectory, final FileNameExtensionFilter filter) {
15901633
// Unlike JFileChooser.showOpenDialog(), SystemFileChooser.showOpenDialog() throws
15911634
// IllegalStateException if not called on the EDT
1592-
if (!SwingUtilities.isEventDispatchThread()) {
1593-
final Object[] result = new Object[1];
1594-
try {
1595-
SwingUtilities.invokeAndWait(() -> result[0] = showNativeOpenDialogOnEDT(
1596-
title, selectionMode, multiSelection, preset, presetIsDirectory, filter));
1597-
} catch (final InterruptedException e) {
1598-
Thread.currentThread().interrupt();
1599-
return null;
1600-
} catch (final java.lang.reflect.InvocationTargetException e) {
1601-
SNTUtils.error("Native file dialog failed", e.getCause());
1602-
return null;
1603-
}
1604-
return result[0];
1635+
try {
1636+
return callOnEDT(() -> showNativeOpenDialogOnEDT(title, selectionMode, multiSelection, preset,
1637+
presetIsDirectory, filter));
1638+
} catch (final RuntimeException e) {
1639+
SNTUtils.error("Native file dialog failed", e);
1640+
return null;
16051641
}
1606-
return showNativeOpenDialogOnEDT(title, selectionMode, multiSelection, preset, presetIsDirectory, filter);
16071642
}
16081643

16091644
private Object showNativeOpenDialogOnEDT(final String title, final int selectionMode,
@@ -1643,20 +1678,12 @@ private Object showNativeOpenDialogOnEDT(final String title, final int selection
16431678
private File showNativeSaveDialog(final String title, final File preset, final FileNameExtensionFilter filter) {
16441679
// Unlike JFileChooser.showSaveDialog(), SystemFileChooser.showSaveDialog() throws
16451680
// IllegalStateException if not called on the EDT
1646-
if (!SwingUtilities.isEventDispatchThread()) {
1647-
final File[] result = new File[1];
1648-
try {
1649-
SwingUtilities.invokeAndWait(() -> result[0] = showNativeSaveDialogOnEDT(title, preset, filter));
1650-
} catch (final InterruptedException e) {
1651-
Thread.currentThread().interrupt();
1652-
return null;
1653-
} catch (final java.lang.reflect.InvocationTargetException e) {
1654-
SNTUtils.error("Native file dialog failed", e.getCause());
1655-
return null;
1656-
}
1657-
return result[0];
1681+
try {
1682+
return callOnEDT(() -> showNativeSaveDialogOnEDT(title, preset, filter));
1683+
} catch (final RuntimeException e) {
1684+
SNTUtils.error("Native file dialog failed", e);
1685+
return null;
16581686
}
1659-
return showNativeSaveDialogOnEDT(title, preset, filter);
16601687
}
16611688

16621689
private File showNativeSaveDialogOnEDT(final String title, final File preset,

‎src/main/java/sc/fiji/snt/gui/SwingSafeResult.java‎

Lines changed: 0 additions & 57 deletions
This file was deleted.

‎src/main/java/sc/fiji/snt/gui/cmds/CommonDynamicCmd.java‎

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import java.awt.*;
4343
import java.awt.event.WindowAdapter;
4444
import java.awt.event.WindowEvent;
45-
import java.lang.reflect.InvocationTargetException;
4645
import java.util.ArrayList;
4746
import java.util.List;
4847

@@ -149,18 +148,12 @@ protected void error(final String msg) {
149148
* @return the user's choice; {@code false} if the EDT could not be reached (e.g. interrupted while waiting)
150149
*/
151150
protected boolean getConfirmationEdtSafe(final String msg, final String title) {
152-
if (SwingUtilities.isEventDispatchThread()) {
153-
return new GuiUtils().getConfirmation(msg, title);
154-
}
155-
final boolean[] result = { false };
156151
try {
157-
SwingUtilities.invokeAndWait(() -> result[0] = new GuiUtils().getConfirmation(msg, title));
158-
} catch (final InterruptedException e) {
159-
Thread.currentThread().interrupt();
160-
} catch (final InvocationTargetException e) {
161-
SNTUtils.log("getConfirmationEdtSafe failed: " + e.getCause());
152+
return GuiUtils.callOnEDT(() -> new GuiUtils().getConfirmation(msg, title));
153+
} catch (final RuntimeException e) {
154+
SNTUtils.log("getConfirmationEdtSafe failed: " + e);
155+
return false;
162156
}
163-
return result[0];
164157
}
165158

166159
protected void msg(final String msg, final String title) {

‎src/main/java/sc/fiji/snt/plugin/TWSLoaderCmd.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import sc.fiji.snt.SNTService;
3535
import sc.fiji.snt.analysis.RoiConverter;
3636
import sc.fiji.snt.gui.GuiUtils;
37-
import sc.fiji.snt.gui.SwingSafeResult;
3837
import trainableSegmentation.Weka_Segmentation;
3938

4039
import java.lang.reflect.Field;
@@ -99,7 +98,7 @@ private void initializeTWS(final ImagePlus trainingImage) {
9998
ij.IJ.run("Channels Tool...");
10099
}
101100
run((trainingImage.getNSlices() == 1) ? "" : "3D");
102-
twsWin = SwingSafeResult.getResult(this::getTWSgui);
101+
twsWin = GuiUtils.callOnEDT(this::getTWSgui);
103102
}
104103

105104
private ImagePlus getTWSdisplayImage() {

‎src/main/java/sc/fiji/snt/viewer/AbstractBigViewer.java‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,13 +1114,9 @@ private boolean restoreSavedSettings(final bdv.viewer.ViewerState state, final b
11141114
}
11151115

11161116
private static void runOnEdt(final Runnable r) {
1117-
if (SwingUtilities.isEventDispatchThread()) {
1118-
r.run();
1119-
return;
1120-
}
11211117
try {
1122-
SwingUtilities.invokeAndWait(r);
1123-
} catch (final InterruptedException | java.lang.reflect.InvocationTargetException ex) {
1118+
GuiUtils.runOnEDT(r);
1119+
} catch (final RuntimeException ex) {
11241120
SNTUtils.log("Settings prompt failed: " + ex);
11251121
}
11261122
}

‎src/main/java/sc/fiji/snt/viewer/Bvv.java‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2092,23 +2092,22 @@ public ImagePlus snapshot(final String viewMode) {
20922092
final AffineTransform3D target = computeAlignTransform(vMode);
20932093
if (target != null) {
20942094
try {
2095-
SwingUtilities.invokeAndWait(
2096-
() -> viewerPanel.state().setViewerTransform(target));
2097-
} catch (final Exception ex) {
2098-
Thread.currentThread().interrupt();
2095+
GuiUtils.runOnEDT(() -> viewerPanel.state().setViewerTransform(target));
2096+
} catch (final RuntimeException ex) {
2097+
SNTUtils.log("Could not apply alignment transform: " + ex);
20992098
}
21002099
}
21012100
}
21022101

21032102
final ImagePlus result = screenshotImp();
21042103
if (!"current".equals(vMode)) {
21052104
try {
2106-
SwingUtilities.invokeAndWait(() -> {
2105+
GuiUtils.runOnEDT(() -> {
21072106
viewerPanel.state().setViewerTransform(savedTransform);
21082107
viewerPanel.requestRepaint();
21092108
});
2110-
} catch (final Exception ex) {
2111-
Thread.currentThread().interrupt();
2109+
} catch (final RuntimeException ex) {
2110+
SNTUtils.log("Could not restore viewer transform: " + ex);
21122111
}
21132112
}
21142113
if (result != null) {
@@ -2178,9 +2177,9 @@ private ImagePlus screenshotImp() {
21782177
final java.awt.image.BufferedImage bi =
21792178
new java.awt.image.BufferedImage(w, h, java.awt.image.BufferedImage.TYPE_INT_RGB);
21802179
try {
2181-
SwingUtilities.invokeAndWait(() -> canvas.paint(bi.getGraphics()));
2182-
} catch (final java.lang.reflect.InvocationTargetException | InterruptedException ex) {
2183-
Thread.currentThread().interrupt();
2180+
GuiUtils.runOnEDT(() -> canvas.paint(bi.getGraphics()));
2181+
} catch (final RuntimeException ex) {
2182+
SNTUtils.log("Snapshot failed: " + ex);
21842183
return null;
21852184
}
21862185
return new ImagePlus("BVV Snapshot", new ij.process.ColorProcessor(bi));

‎src/main/java/sc/fiji/snt/viewer/KeyframeRecorder.java‎

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
import net.imglib2.realtransform.AffineTransform3D;
3232
import net.imglib2.type.numeric.ARGBType;
3333
import sc.fiji.snt.SNTUtils;
34+
import sc.fiji.snt.gui.GuiUtils;
3435

3536
import javax.imageio.ImageIO;
36-
import javax.swing.SwingUtilities;
3737
import java.awt.Component;
3838
import java.awt.image.BufferedImage;
3939
import java.io.File;
@@ -162,17 +162,13 @@ private void applyDisplay(final Map<Integer, Keyframe.SourceDisplay> display) {
162162
private void showAndWait(final AffineTransform3D t, final int timepoint,
163163
final Map<Integer, Keyframe.SourceDisplay> display) throws InterruptedException {
164164
viewer.awaitRender(() -> {
165-
try {
166-
SwingUtilities.invokeAndWait(() -> {
167-
viewer.getViewerState().setViewerTransform(t);
168-
if (display != null) applyDisplay(display);
169-
if (timepoint > 0 && timepoint != viewer.getCurrentTimepoint())
170-
viewer.setCurrentTimepoint(timepoint);
171-
viewer.repaint();
172-
});
173-
} catch (final Exception e) {
174-
throw new IllegalStateException(e);
175-
}
165+
GuiUtils.runOnEDT(() -> {
166+
viewer.getViewerState().setViewerTransform(t);
167+
if (display != null) applyDisplay(display);
168+
if (timepoint > 0 && timepoint != viewer.getCurrentTimepoint())
169+
viewer.setCurrentTimepoint(timepoint);
170+
viewer.repaint();
171+
});
176172
}, settleMillis);
177173
}
178174

@@ -212,8 +208,8 @@ private static boolean saveFrame(final Component canvas, final File dir, final i
212208
final BufferedImage bi = new BufferedImage(canvas.getWidth(), canvas.getHeight(),
213209
BufferedImage.TYPE_INT_RGB);
214210
try {
215-
SwingUtilities.invokeAndWait(() -> canvas.paint(bi.getGraphics()));
216-
} catch (final Exception e) {
211+
GuiUtils.runOnEDT(() -> canvas.paint(bi.getGraphics()));
212+
} catch (final RuntimeException e) {
217213
SNTUtils.log("Screenshot failed at frame " + index);
218214
return false;
219215
}

0 commit comments

Comments
 (0)