3636import org .scijava .plugin .Plugin ;
3737import org .scijava .plugin .RichPlugin ;
3838import org .scijava .plugin .SingletonPlugin ;
39+ import org .scijava .util .AppUtils ;
3940import org .scijava .util .Manifest ;
4041import org .scijava .util .POM ;
4142
5657public interface App extends RichPlugin , SingletonPlugin {
5758
5859 /** Gets the title of the application. */
59- String getTitle ();
60+ default String getTitle () {
61+ return getInfo ().getName ();
62+ }
63+
6064
6165 /** The Maven {@code groupId} of the application. */
6266 String getGroupId ();
@@ -80,20 +84,40 @@ public interface App extends RichPlugin, SingletonPlugin {
8084 *
8185 * @param mem If true, memory usage information is included.
8286 */
83- String getInfo (boolean mem );
87+ default String getInfo (final boolean mem ) {
88+ final String appTitle = getTitle ();
89+ final String appVersion = getVersion ();
90+ final String javaVersion = System .getProperty ("java.version" );
91+ final String osArch = System .getProperty ("os.arch" );
92+ final long maxMem = Runtime .getRuntime ().maxMemory ();
93+ final long totalMem = Runtime .getRuntime ().totalMemory ();
94+ final long freeMem = Runtime .getRuntime ().freeMemory ();
95+ final long usedMem = totalMem - freeMem ;
96+ final long usedMB = usedMem / 1048576 ;
97+ final long maxMB = maxMem / 1048576 ;
98+ final StringBuilder sb = new StringBuilder ();
99+ sb .append (appTitle + " " + appVersion );
100+ sb .append ("; Java " + javaVersion + " [" + osArch + "]" );
101+ if (mem ) sb .append ("; " + usedMB + "MB of " + maxMB + "MB" );
102+ return sb .toString ();
103+ }
84104
85105 /**
86106 * A system property which, if set, overrides the base directory of the
87107 * application.
88108 */
89- String getSystemProperty ();
109+ default String getSystemProperty () {
110+ return getInfo ().getName ().toLowerCase () + ".dir" ;
111+ }
90112
91113 /**
92114 * Gets the application's root directory. If the application's system property
93115 * is set, it is used. Otherwise, we scan up the tree from this class for a
94116 * suitable directory.
95117 */
96- File getBaseDirectory ();
118+ default File getBaseDirectory () {
119+ return AppUtils .getBaseDirectory (getSystemProperty (), getClass (), null );
120+ }
97121
98122 /**
99123 * Displays information about the application. Typically this action
@@ -105,13 +129,17 @@ public interface App extends RichPlugin, SingletonPlugin {
105129 * Displays application preferences. The behavior is application-specific, but
106130 * typically a preferences dialog will be shown onscreen.
107131 */
108- void prefs ();
132+ default void prefs () {
133+ // NB: Do nothing.
134+ }
109135
110136 /**
111137 * Quits the application. Typically this action will prompt the user about any
112138 * unsaved work first.
113139 */
114- void quit ();
140+ default void quit () {
141+ getContext ().dispose ();
142+ }
115143
116144 // -- Versioned methods --
117145
@@ -125,6 +153,24 @@ public interface App extends RichPlugin, SingletonPlugin {
125153 * @return The application version, in {@code major.minor.micro} format.
126154 */
127155 @ Override
128- String getVersion ();
129-
156+ default String getVersion () {
157+ // NB: We do not use VersionUtils.getVersion(c, groupId, artifactId)
158+ // because that method does not cache the parsed Manifest and/or POM.
159+ // We might have them already parsed here, and if not, we want to
160+ // parse then cache locally, rather than discarding them afterwards.
161+
162+ // try the manifest first, since it might know its build number
163+ final Manifest m = getManifest ();
164+ if (m != null ) {
165+ final String v = m .getVersion ();
166+ if (v != null ) return v ;
167+ }
168+ // try the POM
169+ final POM p = getPOM ();
170+ if (p != null ) {
171+ final String v = p .getVersion ();
172+ if (v != null ) return v ;
173+ }
174+ return "Unknown" ;
175+ }
130176}
0 commit comments