Skip to content

Commit 89a1efd

Browse files
committed
AbstractSwingUI: try different Look+Feels
If constructing the UI with the default L&F fails, try again with some other L&Fs in descending order of preference. And if none of them work, throw an exception.
1 parent 546a716 commit 89a1efd

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

src/main/java/org/scijava/ui/swing/AbstractSwingUI.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,36 @@ protected void createUI() {
255255
// catch and throw a RuntimeException in response, thereby
256256
// avoiding any subsequent output shenanigans past this point.
257257
if (!Boolean.getBoolean(ConsolePane.NO_CONSOLE_PROPERTY)) {
258-
consolePane = new SwingConsolePane(getContext());
258+
final String[] lafsToTry = {
259+
null, // the default look and feel, as set by initLookAndFeel()
260+
// e.g. Linux=com.sun.java.swing.plaf.gtk.GTKLookAndFeel
261+
javax.swing.UIManager.getSystemLookAndFeelClassName(),
262+
"Nimbus", // because it's nicer than Metal
263+
// javax.swing.plaf.metal.MetalLookAndFeel
264+
javax.swing.UIManager.getCrossPlatformLookAndFeelClassName()
265+
};
266+
// NB: Ideally we would try these multiple LAFs regardless of if the
267+
// console is enabled or not. However, when a LAF explodes due to missing
268+
// ComponentUI, Java does printStackTrace() internally and does *not*
269+
// throw an exception! So we can't try/catch any `new SwingComponent()`;
270+
// we specifically need to do it with the ConsolePane, which has special
271+
// logic for detecting infinite console output loops that throws
272+
// RuntimeException when this sort of nonsense happens.
273+
for (final String laf : lafsToTry) {
274+
if (laf != null && lafService != null) lafService.setLookAndFeel(laf);
275+
try {
276+
consolePane = new SwingConsolePane(getContext());
277+
break;
278+
}
279+
catch (final RuntimeException exc) {
280+
log.error("Failed to configure UI with " + //
281+
(laf == null ? "default" : laf) + " Look+Feel", exc);
282+
if (lafService == null) break;
283+
}
284+
}
285+
if (consolePane == null) {
286+
throw new IllegalStateException("Failed to configure UI Look+Feel!");
287+
}
259288
}
260289

261290
final JMenuBar menuBar = createMenus();

0 commit comments

Comments
 (0)