Skip to content

Commit 0889da2

Browse files
committed
UIService: fix behavior of isHeadless/setHeadless
When setHeadless is called, it should toggle a flag local to the context, not override the java.awt.headless system property. That way, each context can be headless or not independently. Closes #424.
1 parent 780acad commit 0889da2

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/main/java/org/scijava/ui/DefaultUIService.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,14 @@ public final class DefaultUIService extends AbstractService implements
116116
/** The default user interface to use, if one is not explicitly specified. */
117117
private UserInterface defaultUI;
118118

119+
/**
120+
* When true, {@link #isHeadless()} will return true regardless of the value
121+
* of the {@code java.awt.headless} system property. When false, {@link
122+
* #isHeadless()} matches the global JVM headless state defined by {@code
123+
* java.awt.headless}.
124+
*/
125+
private boolean forceHeadless;
126+
119127
private boolean activationInvocationPending = false;
120128

121129
// -- UIService methods --
@@ -178,12 +186,14 @@ public boolean isVisible(final String name) {
178186

179187
@Override
180188
public void setHeadless(final boolean headless) {
181-
System.setProperty("java.awt.headless", String.valueOf(headless));
189+
forceHeadless = headless;
182190
}
183191

184192
@Override
185193
public boolean isHeadless() {
186-
return Boolean.getBoolean("java.awt.headless");
194+
// NB: We do not use java.awt.GraphicsConfiguration.isHeadless()
195+
// because scijava-common eschews java.awt.* classes when possible.
196+
return forceHeadless || Boolean.getBoolean("java.awt.headless");
187197
}
188198

189199
@Override

src/main/java/org/scijava/ui/UIService.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,22 @@ public interface UIService extends SciJavaService {
101101
/** Gets whether the UI with the given name or class name is visible. */
102102
boolean isVisible(String name);
103103

104-
/** Sets whether the application is running in headless mode (no UI). */
104+
/**
105+
* Sets whether the application should run in headless mode (no UI).
106+
* <p>
107+
* Note that if the system itself is headless&mdash;which can be detected via
108+
* the {@code java.awt.headless} system property or by calling
109+
* {@code java.awt.GraphicsEnvironment.isHeadless()}&mdash;then calling
110+
* {@code setHeadless(false)} will have no effect; the system will still be
111+
* headless, and {@link #isHeadless()} will still return true.
112+
* </p>
113+
* <p>
114+
* But if the system itself is <em>not</em> headless, calling
115+
* {@code setHeadless(true)} will force {@link #isHeadless()} to return true,
116+
* instructing the application to behave in a headless manner insofar as it
117+
* can.
118+
* </p>
119+
*/
105120
void setHeadless(boolean isHeadless);
106121

107122
/** Gets whether the UI is running in headless mode (no UI). */

0 commit comments

Comments
 (0)