Skip to content

Commit 8b56cb2

Browse files
authored
Merge pull request #465 from scijava/active-ui
Manage the active UI in a better way
2 parents de330f3 + a32977f commit 8b56cb2

File tree

1 file changed

+28
-43
lines changed

1 file changed

+28
-43
lines changed

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

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

120+
/** The last UI used when performing UI operations via the service. */
121+
private UserInterface activeUI;
122+
120123
/**
121124
* When true, {@link #isHeadless()} will return true regardless of the value
122125
* of the {@code java.awt.headless} system property. When false, {@link
@@ -143,18 +146,12 @@ public void addUI(final String name, final UserInterface ui) {
143146
@Override
144147
public void showUI() {
145148
if (disposed) return;
146-
final UserInterface ui = getDefaultUI();
147-
if (ui == null) {
148-
throw new IllegalStateException("No UIs available. " +
149-
"Please add a component containing a UIPlugin " +
150-
"(e.g., scijava-ui-swing) to your class-path.");
151-
}
152-
showUI(ui);
149+
showUI(activeUI());
153150
}
154151

155152
@Override
156153
public void showUI(final String name) {
157-
final UserInterface ui = uiMap().get(name);
154+
final UserInterface ui = getUI(name);
158155
if (ui == null) {
159156
throw new IllegalArgumentException("No such user interface: " + name);
160157
}
@@ -174,14 +171,12 @@ public void showUI(final UserInterface ui) {
174171

175172
@Override
176173
public boolean isVisible() {
177-
final UserInterface ui = getDefaultUI();
178-
if (ui == null) return false;
179-
return ui.isVisible();
174+
return activeUI().isVisible();
180175
}
181176

182177
@Override
183178
public boolean isVisible(final String name) {
184-
final UserInterface ui = uiMap().get(name);
179+
final UserInterface ui = getUI(name);
185180
return ui != null && ui.isVisible();
186181
}
187182

@@ -200,7 +195,7 @@ public boolean isHeadless() {
200195
@Override
201196
public UserInterface getDefaultUI() {
202197
if (!initialized) discoverUIs();
203-
if (isHeadless()) return uiMap().get(HeadlessUI.NAME);
198+
if (isHeadless()) return getUI(HeadlessUI.NAME);
204199
if (defaultUI != null) return defaultUI;
205200
return uiList().isEmpty() ? null : uiList().get(0);
206201
}
@@ -244,17 +239,17 @@ public List<PluginInfo<DisplayViewer<?>>> getViewerPlugins() {
244239

245240
@Override
246241
public void show(final Object o) {
247-
getVisibleUI(true).show(o);
242+
activeUI().show(o);
248243
}
249244

250245
@Override
251246
public void show(final String name, final Object o) {
252-
getVisibleUI(true).show(name, o);
247+
activeUI().show(name, o);
253248
}
254249

255250
@Override
256251
public void show(final Display<?> display) {
257-
getVisibleUI(true).show(display);
252+
activeUI().show(display);
258253
}
259254

260255
@Override
@@ -309,44 +304,38 @@ public DialogPrompt.Result showDialog(final String message,
309304
final String title, final DialogPrompt.MessageType messageType,
310305
final DialogPrompt.OptionType optionType)
311306
{
312-
UserInterface ui = getVisibleUI(false);
313-
if (ui == null) return null;
314-
final DialogPrompt dialogPrompt = ui.dialogPrompt(message, title, messageType, optionType);
307+
final DialogPrompt dialogPrompt = //
308+
activeUI().dialogPrompt(message, title, messageType, optionType);
315309
return dialogPrompt == null ? null : dialogPrompt.prompt();
316310
}
317311

318312
@Override
319313
public File chooseFile(final File file, final String style) {
320-
final UserInterface ui = getVisibleUI(true);
321-
return ui == null ? null : ui.chooseFile(file, style);
314+
return activeUI().chooseFile(file, style);
322315
}
323316

324317
@Override
325318
public File
326319
chooseFile(final String title, final File file, final String style)
327320
{
328-
final UserInterface ui = getVisibleUI(true);
329-
return ui == null ? null : ui.chooseFile(title, file, style);
321+
return activeUI().chooseFile(title, file, style);
330322
}
331323

332324
@Override
333325
public File[] chooseFiles(File parent, File[] files, FileFilter filter, String style) {
334-
final UserInterface ui = getVisibleUI(true);
335-
return ui == null ? null : ui.chooseFiles(parent, files, filter, style);
326+
return activeUI().chooseFiles(parent, files, filter, style);
336327
}
337328

338329
@Override
339330
public List<File> chooseFiles(File parent, List<File> fileList, FileFilter filter, String style) {
340-
final UserInterface ui = getVisibleUI(true);
341-
return ui == null ? null : ui.chooseFiles(parent, fileList, filter, style);
331+
return activeUI().chooseFiles(parent, fileList, filter, style);
342332
}
343333

344334
@Override
345335
public void showContextMenu(final String menuRoot, final Display<?> display,
346336
final int x, final int y)
347337
{
348-
final UserInterface ui = getVisibleUI(true);
349-
if (ui != null) ui.showContextMenu(menuRoot, display, x, y);
338+
activeUI().showContextMenu(menuRoot, display, x, y);
350339
}
351340

352341
@Override
@@ -542,20 +531,16 @@ private String getTitle() {
542531
return appService.getApp().getTitle();
543532
}
544533

545-
private UserInterface getVisibleUI(final boolean forceShow) {
546-
// finds the first (highest priority) VISIBLE UserInterface
547-
// if none are visible, then we show default UI if the caller indicated so.
548-
UserInterface defaultUI = getDefaultUI();
549-
if (defaultUI == null) return null;
550-
if (defaultUI.isVisible()) return defaultUI;
551-
else if(getVisibleUIs().size() > 0) {
552-
return getVisibleUIs().get(0);
553-
}
534+
/** Gets the UI to use when performing UI operations via the service. */
535+
private UserInterface activeUI() {
536+
// If a particular UI is already active and still visible, use that one.
537+
if (activeUI != null && activeUI.isVisible()) return activeUI;
554538

555-
if (forceShow) {
556-
showUI(defaultUI);
557-
return defaultUI;
558-
}
559-
return null;
539+
// If a UI is visible, use it.
540+
final List<UserInterface> visibleUIs = getVisibleUIs();
541+
if (visibleUIs.size() > 0) return activeUI = visibleUIs.get(0);
542+
543+
// No UI is visible, so use the default one.
544+
return activeUI = getDefaultUI();
560545
}
561546
}

0 commit comments

Comments
 (0)