Skip to content

Commit 0d4b014

Browse files
committed
Merge branch 'ui-split'
This branch migrates the UI subsystem from ImageJ core. This system has nothing specifically to do with images, and requires no additional dependencies. See: https://github.com/imagej/imagej/tree/imagej-2.0.0-beta-7.9/core/ui
2 parents b883130 + e852f51 commit 0d4b014

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+4575
-33
lines changed

src/main/java/org/scijava/app/AbstractApp.java

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

3232
package org.scijava.app;
3333

34+
import java.io.File;
35+
3436
import org.scijava.plugin.AbstractRichPlugin;
37+
import org.scijava.util.AppUtils;
3538
import org.scijava.util.Manifest;
3639
import org.scijava.util.POM;
3740

@@ -48,6 +51,11 @@ public abstract class AbstractApp extends AbstractRichPlugin implements App {
4851
/** JAR manifest with metadata about the application. */
4952
private Manifest manifest;
5053

54+
@Override
55+
public String getTitle() {
56+
return getInfo().getName();
57+
}
58+
5159
@Override
5260
public String getVersion() {
5361
return getPOM() == null ? "Unknown" : getPOM().getVersion();
@@ -90,4 +98,14 @@ public String getInfo(boolean mem) {
9098
return sb.toString();
9199
}
92100

101+
@Override
102+
public String getSystemProperty() {
103+
return getInfo().getName().toLowerCase() + ".dir";
104+
}
105+
106+
@Override
107+
public File getBaseDirectory() {
108+
return AppUtils.getBaseDirectory(getSystemProperty(), getClass(), null);
109+
}
110+
93111
}

src/main/java/org/scijava/app/App.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@
3131

3232
package org.scijava.app;
3333

34+
import java.io.File;
35+
3436
import org.scijava.plugin.Plugin;
3537
import org.scijava.plugin.RichPlugin;
38+
import org.scijava.plugin.SingletonPlugin;
3639
import org.scijava.util.Manifest;
3740
import org.scijava.util.POM;
3841

@@ -50,7 +53,7 @@
5053
* @see Plugin
5154
* @see AppService
5255
*/
53-
public interface App extends RichPlugin {
56+
public interface App extends RichPlugin, SingletonPlugin {
5457

5558
/** Gets the title of the application. */
5659
String getTitle();
@@ -90,4 +93,17 @@ public interface App extends RichPlugin {
9093
*/
9194
String getInfo(boolean mem);
9295

96+
/**
97+
* A system property which, if set, overrides the base directory of the
98+
* application.
99+
*/
100+
String getSystemProperty();
101+
102+
/**
103+
* Gets the application's root directory. If the application's system property
104+
* is set, it is used. Otherwise, we scan up the tree from this class for a
105+
* suitable directory.
106+
*/
107+
File getBaseDirectory();
108+
93109
}

src/main/java/org/scijava/app/AppService.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,17 @@
3333

3434
import java.util.Map;
3535

36-
import org.scijava.service.SciJavaService;
36+
import org.scijava.plugin.SingletonService;
3737

3838
/**
3939
* Interface for application-level functionality.
4040
*
4141
* @author Curtis Rueden
4242
*/
43-
public interface AppService extends SciJavaService {
43+
public interface AppService extends SingletonService<App> {
44+
45+
/** Gets the foremost application (the one with the highest priority). */
46+
App getApp();
4447

4548
/** Gets an application by name. */
4649
App getApp(final String name);

src/main/java/org/scijava/app/DefaultAppService.java

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,10 @@
3737
import java.util.Map;
3838

3939
import org.scijava.log.LogService;
40+
import org.scijava.plugin.AbstractSingletonService;
4041
import org.scijava.plugin.Parameter;
4142
import org.scijava.plugin.Plugin;
42-
import org.scijava.plugin.PluginInfo;
4343
import org.scijava.plugin.PluginService;
44-
import org.scijava.service.AbstractService;
4544
import org.scijava.service.Service;
4645

4746
/**
@@ -50,7 +49,7 @@
5049
* @author Curtis Rueden
5150
*/
5251
@Plugin(type = Service.class)
53-
public class DefaultAppService extends AbstractService implements AppService {
52+
public class DefaultAppService extends AbstractSingletonService<App> implements AppService {
5453

5554
@Parameter
5655
private LogService log;
@@ -63,43 +62,51 @@ public class DefaultAppService extends AbstractService implements AppService {
6362

6463
// -- AppService methods --
6564

65+
@Override
66+
public App getApp() {
67+
final List<App> appList = getInstances();
68+
if (appList == null || appList.isEmpty()) return null;
69+
return appList.get(0);
70+
}
71+
6672
@Override
6773
public App getApp(final String name) {
68-
return apps.get(name);
74+
return apps().get(name);
6975
}
7076

7177
@Override
7278
public Map<String, App> getApps() {
73-
return apps;
79+
return apps();
7480
}
7581

76-
// -- Service methods --
82+
// -- SingletonService methods --
7783

7884
@Override
79-
public void initialize() {
80-
apps = Collections.unmodifiableMap(discoverApps());
81-
log.info("Found " + apps.size() + " applications.");
82-
super.initialize();
85+
public Class<App> getPluginType() {
86+
return App.class;
8387
}
8488

85-
// -- Helper methods --
89+
// -- Helper methods - lazy initialization --
90+
91+
/** Gets {@link #apps}, initializing if necessary. */
92+
private Map<String, App> apps() {
93+
if (apps == null) initApps();
94+
return apps;
95+
}
8696

87-
/** Discovers applications. */
88-
private HashMap<String, App> discoverApps() {
97+
/** Initializes {@link #apps}. */
98+
private synchronized void initApps() {
99+
if (apps != null) return; // already initialized
89100
final HashMap<String, App> map = new HashMap<String, App>();
90101

91-
final List<PluginInfo<App>> infos =
92-
pluginService.getPluginsOfType(App.class);
93-
for (final PluginInfo<App> info : infos) {
94-
final App app = pluginService.createInstance(info);
95-
if (app == null) continue;
96-
final String name = info.getName();
102+
for (final App app : getInstances()) {
103+
final String name = app.getInfo().getName();
97104
if (!map.containsKey(name)) {
98105
// no (higher-priority) app with the same name exists
99106
map.put(name, app);
100107
}
101108
}
102-
return map;
109+
apps = Collections.unmodifiableMap(map);
103110
}
104111

105112
}

src/main/java/org/scijava/app/SciJavaApp.java

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

3232
package org.scijava.app;
3333

34+
import org.scijava.Priority;
3435
import org.scijava.plugin.Plugin;
3536

3637
/**
@@ -39,16 +40,12 @@
3940
* @author Curtis Rueden
4041
* @see AppService
4142
*/
42-
@Plugin(type = App.class, name = SciJavaApp.NAME)
43+
@Plugin(type = App.class, name = SciJavaApp.NAME,
44+
priority = Priority.LOW_PRIORITY)
4345
public class SciJavaApp extends AbstractApp {
4446

4547
public static final String NAME = "SciJava";
4648

47-
@Override
48-
public String getTitle() {
49-
return NAME;
50-
}
51-
5249
@Override
5350
public String getGroupId() {
5451
return "org.scijava";
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2014 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.io.console;
33+
34+
import java.io.IOException;
35+
import java.util.LinkedList;
36+
37+
import org.scijava.console.AbstractConsoleArgument;
38+
import org.scijava.console.ConsoleArgument;
39+
import org.scijava.display.DisplayService;
40+
import org.scijava.io.IOService;
41+
import org.scijava.log.LogService;
42+
import org.scijava.plugin.Parameter;
43+
import org.scijava.plugin.Plugin;
44+
45+
/**
46+
* Handles the {@code --open} command line argument.
47+
*
48+
* @author Curtis Rueden
49+
*/
50+
@Plugin(type = ConsoleArgument.class)
51+
public class OpenArgument extends AbstractConsoleArgument {
52+
53+
@Parameter
54+
private IOService ioService;
55+
56+
@Parameter
57+
private DisplayService displayService;
58+
59+
@Parameter
60+
private LogService log;
61+
62+
// -- ConsoleArgument methods --
63+
64+
@Override
65+
public void handle(final LinkedList<String> args) {
66+
if (!supports(args)) return;
67+
68+
args.removeFirst(); // --open
69+
final String source = args.removeFirst();
70+
71+
try {
72+
final Object o = ioService.open(source);
73+
displayService.createDisplay(o);
74+
}
75+
catch (IOException exc) {
76+
log.error(exc);
77+
}
78+
}
79+
80+
// -- Typed methods --
81+
82+
@Override
83+
public boolean supports(final LinkedList<String> args) {
84+
return args != null && args.size() >= 2 && args.getFirst().equals("--open");
85+
}
86+
87+
}

0 commit comments

Comments
 (0)