Skip to content

Commit 838af0a

Browse files
committed
UserInterface: push default behavior to interface
There is quite a bit of reasonable default behavior which can now, thanks to Java 8, be expressed in default interface methods.
1 parent 6464d96 commit 838af0a

File tree

2 files changed

+33
-64
lines changed

2 files changed

+33
-64
lines changed

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

Lines changed: 0 additions & 55 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.
@@ -102,11 +99,6 @@ public boolean isVisible() {
10299
return visible;
103100
}
104101

105-
@Override
106-
public void show(final Object o) {
107-
show(null, o);
108-
}
109-
110102
@Override
111103
public void show(final String name, final Object o) {
112104
final Display<?> display;
@@ -163,44 +155,6 @@ public void run() {
163155
});
164156
}
165157

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-
204158
@Override
205159
public void saveLocation() {
206160
final ApplicationFrame appFrame = getApplicationFrame();
@@ -230,13 +184,4 @@ public void restoreLocation() {
230184
protected void createUI() {
231185
restoreLocation();
232186
}
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-
242187
}

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

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ 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.
@@ -87,25 +89,37 @@ public interface UserInterface extends RichPlugin, Disposable {
8789
* Gets the desktop, for use with multi-document interfaces (MDI), or null if
8890
* not applicable.
8991
*/
90-
Desktop getDesktop();
92+
default Desktop getDesktop() {
93+
return null;
94+
}
9195

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

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

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

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

104116
/**
105117
* Gets the system clipboard associated with this UI, or null if not
106118
* applicable.
107119
*/
108-
SystemClipboard getSystemClipboard();
120+
default SystemClipboard getSystemClipboard() {
121+
return null;
122+
}
109123

110124
/**
111125
* Creates a new display window housing the given display, or null if not
@@ -142,7 +156,15 @@ DialogPrompt dialogPrompt(String message, String title,
142156
* @return The {@link File} chosen by the user, or null if prompt is not
143157
* available
144158
*/
145-
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+
}
146168

147169
/**
148170
* Prompts the user to choose a file.
@@ -158,7 +180,9 @@ DialogPrompt dialogPrompt(String message, String title,
158180
* @return The {@link File} chosen by the user, or null if prompt is not
159181
* available
160182
*/
161-
File chooseFile(String title, File file, String style);
183+
default File chooseFile(String title, File file, String style) {
184+
throw new UnsupportedOperationException();
185+
}
162186

163187
/**
164188
* Displays a popup context menu for the given display at the specified

0 commit comments

Comments
 (0)