Skip to content

Commit 2ebbdae

Browse files
committed
Merge branch 'headless-ui'
Closes #245. This gives us a dummy UserInterface that takes precedence when headless, and is used as a last resort non-headlessly if no other UI is available. Hence, uiService.getDefaultUI() should no longer ever return null. Thanks to Richard Domander for spearheading this!
2 parents 2bd51a3 + 57f764d commit 2ebbdae

File tree

10 files changed

+373
-210
lines changed

10 files changed

+373
-210
lines changed

.gitignore

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
*.swp
2+
3+
# Maven #
4+
/target/
5+
6+
# Eclipse #
27
/.classpath
38
/.project
49
/.settings/
5-
/target/
10+
11+
# IntelliJ #
12+
/*.iml
13+
/.idea/

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@
6666
<url>http://imagej.net/User:Dietzc</url>
6767
<properties><id>dietzc</id></properties>
6868
</contributor>
69+
<contributor>
70+
<name>Richard Domander</name>
71+
<url>http://imagej.net/User:Rdom</url>
72+
<properties><id>rimadoma</id></properties>
73+
</contributor>
6974
<contributor>
7075
<name>Gabriel Einsdorf</name>
7176
<url>http://imagej.net/User:Gab1one</url>

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

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
package org.scijava.ui;
3333

34-
import java.io.File;
3534
import java.util.List;
3635

3736
import org.scijava.app.StatusService;
@@ -45,10 +44,8 @@
4544
import org.scijava.plugin.PluginService;
4645
import org.scijava.prefs.PrefService;
4746
import org.scijava.thread.ThreadService;
48-
import org.scijava.ui.console.ConsolePane;
4947
import org.scijava.ui.viewer.DisplayViewer;
5048
import org.scijava.ui.viewer.DisplayWindow;
51-
import org.scijava.widget.FileWidget;
5249

5350
/**
5451
* Abstract superclass for {@link UserInterface} implementations.
@@ -62,9 +59,6 @@ public abstract class AbstractUserInterface extends AbstractRichPlugin
6259
private static final String LAST_X = "lastXLocation";
6360
private static final String LAST_Y = "lastYLocation";
6461

65-
@Parameter
66-
private CommandService commandService;
67-
6862
@Parameter
6963
private DisplayService displayService;
7064

@@ -74,9 +68,6 @@ public abstract class AbstractUserInterface extends AbstractRichPlugin
7468
@Parameter
7569
private PluginService pluginService;
7670

77-
@Parameter
78-
private StatusService statusService;
79-
8071
@Parameter
8172
private ThreadService threadService;
8273

@@ -102,11 +93,6 @@ public boolean isVisible() {
10293
return visible;
10394
}
10495

105-
@Override
106-
public void show(final Object o) {
107-
show(null, o);
108-
}
109-
11096
@Override
11197
public void show(final String name, final Object o) {
11298
final Display<?> display;
@@ -163,44 +149,6 @@ public void run() {
163149
});
164150
}
165151

166-
@Override
167-
public Desktop getDesktop() {
168-
return null;
169-
}
170-
171-
@Override
172-
public ApplicationFrame getApplicationFrame() {
173-
return null;
174-
}
175-
176-
@Override
177-
public ToolBar getToolBar() {
178-
return null;
179-
}
180-
181-
@Override
182-
public StatusBar getStatusBar() {
183-
return null;
184-
}
185-
186-
@Override
187-
public ConsolePane<?> getConsolePane() {
188-
return null;
189-
}
190-
191-
@Override
192-
public File chooseFile(final File file, final String style) {
193-
return chooseFile(fileChooserTitle(style), file, style);
194-
}
195-
196-
@Deprecated
197-
@Override
198-
public File chooseFile(final String title, final File file,
199-
final String style)
200-
{
201-
throw new UnsupportedOperationException("No default implementation.");
202-
}
203-
204152
@Override
205153
public void saveLocation() {
206154
final ApplicationFrame appFrame = getApplicationFrame();
@@ -230,13 +178,4 @@ public void restoreLocation() {
230178
protected void createUI() {
231179
restoreLocation();
232180
}
233-
234-
/** Gets a default file chooser title to use when none is given. */
235-
protected String fileChooserTitle(final String style) {
236-
if (style.equals(FileWidget.DIRECTORY_STYLE)) return "Choose a directory";
237-
if (style.equals(FileWidget.OPEN_STYLE)) return "Open";
238-
if (style.equals(FileWidget.SAVE_STYLE)) return "Save";
239-
return "Choose a file";
240-
}
241-
242181
}

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

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import org.scijava.app.AppService;
4242
import org.scijava.app.StatusService;
4343
import org.scijava.app.event.StatusEvent;
44-
import org.scijava.command.CommandService;
4544
import org.scijava.display.Display;
4645
import org.scijava.display.DisplayService;
4746
import org.scijava.display.event.DisplayActivatedEvent;
@@ -51,10 +50,6 @@
5150
import org.scijava.event.EventHandler;
5251
import org.scijava.event.EventService;
5352
import org.scijava.log.LogService;
54-
import org.scijava.menu.MenuService;
55-
import org.scijava.options.OptionsService;
56-
import org.scijava.platform.AppEventService;
57-
import org.scijava.platform.PlatformService;
5853
import org.scijava.platform.event.AppQuitEvent;
5954
import org.scijava.plugin.Parameter;
6055
import org.scijava.plugin.Plugin;
@@ -63,11 +58,11 @@
6358
import org.scijava.service.AbstractService;
6459
import org.scijava.service.Service;
6560
import org.scijava.thread.ThreadService;
66-
import org.scijava.tool.ToolService;
6761
import org.scijava.ui.DialogPrompt.MessageType;
6862
import org.scijava.ui.DialogPrompt.OptionType;
6963
import org.scijava.ui.DialogPrompt.Result;
7064
import org.scijava.ui.event.UIShownEvent;
65+
import org.scijava.ui.headlessUI.HeadlessUI;
7166
import org.scijava.ui.viewer.DisplayViewer;
7267

7368
/**
@@ -95,30 +90,12 @@ public final class DefaultUIService extends AbstractService implements
9590
@Parameter
9691
private AppService appService;
9792

98-
@Parameter
99-
private PlatformService platformService;
100-
10193
@Parameter
10294
private PluginService pluginService;
10395

104-
@Parameter
105-
private CommandService commandService;
106-
10796
@Parameter
10897
private DisplayService displayService;
10998

110-
@Parameter
111-
private MenuService menuService;
112-
113-
@Parameter
114-
private ToolService toolService;
115-
116-
@Parameter
117-
private OptionsService optionsService;
118-
119-
@Parameter
120-
private AppEventService appEventService;
121-
12299
/**
123100
* A list of extant display viewers. It's needed in order to find the viewer
124101
* associated with a display.
@@ -212,10 +189,9 @@ public boolean isHeadless() {
212189

213190
@Override
214191
public UserInterface getDefaultUI() {
192+
if (isHeadless()) return uiMap().get(HeadlessUI.NAME);
215193
if (defaultUI != null) return defaultUI;
216-
if (uiList().isEmpty()) return null;
217-
if (defaultUI != null) return defaultUI;
218-
return uiList().get(0);
194+
return uiList().isEmpty() ? null : uiList().get(0);
219195
}
220196

221197
@Override
@@ -326,32 +302,29 @@ public DialogPrompt.Result showDialog(final String message,
326302
if (ui == null) return null;
327303
final DialogPrompt dialogPrompt =
328304
ui.dialogPrompt(message, title, messageType, optionType);
329-
return dialogPrompt.prompt();
305+
return dialogPrompt == null ? null : dialogPrompt.prompt();
330306
}
331307

332308
@Override
333309
public File chooseFile(final File file, final String style) {
334310
final UserInterface ui = getDefaultUI();
335-
if (ui == null) return null;
336-
return ui.chooseFile(file, style);
311+
return ui == null ? null : ui.chooseFile(file, style);
337312
}
338313

339314
@Override
340315
public File
341316
chooseFile(final String title, final File file, final String style)
342317
{
343318
final UserInterface ui = getDefaultUI();
344-
if (ui == null) return null;
345-
return ui.chooseFile(title, file, style);
319+
return ui == null ? null : ui.chooseFile(title, file, style);
346320
}
347321

348322
@Override
349323
public void showContextMenu(final String menuRoot, final Display<?> display,
350324
final int x, final int y)
351325
{
352326
final UserInterface ui = getDefaultUI();
353-
if (ui == null) return;
354-
ui.showContextMenu(menuRoot, display, x, y);
327+
if (ui != null) ui.showContextMenu(menuRoot, display, x, y);
355328
}
356329

357330
@Override

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

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
* implementing this interface, it is encouraged to instead extend
5151
* {@link AbstractUserInterface}, for convenience.
5252
* </p>
53-
*
53+
*
5454
* @author Curtis Rueden
5555
* @see Plugin
5656
* @see UIService
@@ -70,11 +70,13 @@ public interface UserInterface extends RichPlugin, Disposable {
7070
boolean isVisible();
7171

7272
/** Shows the object onscreen using an appropriate UI widget. */
73-
void show(Object o);
73+
default void show(final Object o) {
74+
show(null, o);
75+
}
7476

7577
/**
7678
* Shows the object onscreen using an appropriate UI widget.
77-
*
79+
*
7880
* @param name The name to use when displaying the object.
7981
* @param o The object to be displayed.
8082
*/
@@ -83,30 +85,51 @@ public interface UserInterface extends RichPlugin, Disposable {
8385
/** Shows the display onscreen using an appropriate UI widget. */
8486
void show(Display<?> display);
8587

86-
/** Gets the desktop, for use with multi-document interfaces (MDI). */
87-
Desktop getDesktop();
88+
/**
89+
* Gets the desktop, for use with multi-document interfaces (MDI), or null if
90+
* not applicable.
91+
*/
92+
default Desktop getDesktop() {
93+
return null;
94+
}
8895

8996
/** Gets the main SciJava application frame, or null if not applicable. */
90-
ApplicationFrame getApplicationFrame();
97+
default ApplicationFrame getApplicationFrame() {
98+
return null;
99+
}
91100

92101
/** Gets the main SciJava toolbar, or null if not applicable. */
93-
ToolBar getToolBar();
102+
default ToolBar getToolBar() {
103+
return null;
104+
}
94105

95106
/** Gets the main SciJava status bar, or null if not applicable. */
96-
StatusBar getStatusBar();
107+
default StatusBar getStatusBar() {
108+
return null;
109+
}
97110

98111
/** Gets the main SciJava console pane, or null if not applicable. */
99-
ConsolePane<?> getConsolePane();
112+
default ConsolePane<?> getConsolePane() {
113+
return null;
114+
}
100115

101-
/** Gets the system clipboard associated with this UI. */
102-
SystemClipboard getSystemClipboard();
116+
/**
117+
* Gets the system clipboard associated with this UI, or null if not
118+
* applicable.
119+
*/
120+
default SystemClipboard getSystemClipboard() {
121+
return null;
122+
}
103123

104-
/** Creates a new display window housing the given display. */
124+
/**
125+
* Creates a new display window housing the given display, or null if not
126+
* applicable.
127+
*/
105128
DisplayWindow createDisplayWindow(Display<?> display);
106129

107130
/**
108131
* Creates a dialog prompter.
109-
*
132+
*
110133
* @param message The message in the dialog itself.
111134
* @param title The title of the dialog.
112135
* @param messageType The type of message. This typically is rendered as an
@@ -115,27 +138,37 @@ public interface UserInterface extends RichPlugin, Disposable {
115138
* as an exclamation point.
116139
* @param optionType The choices available when dismissing the dialog. These
117140
* choices are typically rendered as buttons for the user to click.
118-
* @return The newly created DialogPrompt object.
141+
* @return The newly created DialogPrompt object, or null if not applicable.
119142
*/
120143
DialogPrompt dialogPrompt(String message, String title,
121144
DialogPrompt.MessageType messageType, DialogPrompt.OptionType optionType);
122145

123146
/**
124147
* Prompts the user to choose a file.
125-
*
148+
*
126149
* @param file The initial value displayed in the file chooser prompt.
127150
* @param style The style of chooser to use:
128151
* <ul>
129152
* <li>{@link FileWidget#OPEN_STYLE}</li>
130153
* <li>{@link FileWidget#SAVE_STYLE}</li>
131154
* <li>{@link FileWidget#DIRECTORY_STYLE}</li>
132155
* </ul>
156+
* @return The {@link File} chosen by the user, or null if prompt is not
157+
* available
133158
*/
134-
File chooseFile(File file, String style);
159+
default File chooseFile(final File file, final String style) {
160+
final String title;
161+
if (style.equals(FileWidget.DIRECTORY_STYLE)) title = "Choose a directory";
162+
else if (style.equals(FileWidget.OPEN_STYLE)) title = "Open";
163+
else if (style.equals(FileWidget.SAVE_STYLE)) title = "Save";
164+
else title = "Choose a file";
165+
166+
return chooseFile(title, file, style);
167+
}
135168

136169
/**
137170
* Prompts the user to choose a file.
138-
*
171+
*
139172
* @param title Title to use in the file chooser dialog.
140173
* @param file The initial value displayed in the file chooser prompt.
141174
* @param style The style of chooser to use:
@@ -144,8 +177,12 @@ DialogPrompt dialogPrompt(String message, String title,
144177
* <li>{@link FileWidget#SAVE_STYLE}</li>
145178
* <li>{@link FileWidget#DIRECTORY_STYLE}</li>
146179
* </ul>
180+
* @return The {@link File} chosen by the user, or null if prompt is not
181+
* available
147182
*/
148-
File chooseFile(String title, File file, String style);
183+
default File chooseFile(String title, File file, String style) {
184+
throw new UnsupportedOperationException();
185+
}
149186

150187
/**
151188
* Displays a popup context menu for the given display at the specified

0 commit comments

Comments
 (0)