Skip to content

Commit 0ec7dee

Browse files
committed
Create WelcomeService
Created a new WelcomeSerivce (a port and refactoring of the ImageJ2 ReadmeService). This service, on display of a UI, determines if it's the first time the current app has run. If so, a welcome message is shown, and a WelcomeEvent is published for other interested parties to consume.
1 parent bb024a1 commit 0ec7dee

File tree

3 files changed

+235
-0
lines changed

3 files changed

+235
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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.welcome;
33+
34+
import java.io.File;
35+
import java.io.IOException;
36+
37+
import org.scijava.app.AppService;
38+
import org.scijava.command.CommandService;
39+
import org.scijava.display.Display;
40+
import org.scijava.display.DisplayService;
41+
import org.scijava.event.EventHandler;
42+
import org.scijava.event.EventService;
43+
import org.scijava.log.LogService;
44+
import org.scijava.plugin.Parameter;
45+
import org.scijava.plugin.Plugin;
46+
import org.scijava.service.AbstractService;
47+
import org.scijava.service.Service;
48+
import org.scijava.text.TextService;
49+
import org.scijava.ui.event.UIShownEvent;
50+
import org.scijava.util.Prefs;
51+
import org.scijava.welcome.event.WelcomeEvent;
52+
53+
/**
54+
* Default service for displaying the welcome greeting.
55+
*
56+
* @author Curtis Rueden
57+
* @author Mark Hiner
58+
*/
59+
@Plugin(type = Service.class)
60+
public class DefaultWelcomeService extends AbstractService implements
61+
WelcomeService
62+
{
63+
64+
private String WELCOME_FILE = "WELCOME.md";
65+
66+
@Parameter
67+
private LogService log;
68+
69+
@Parameter
70+
private AppService appService;
71+
72+
@Parameter
73+
DisplayService displayService;
74+
75+
@Parameter
76+
private CommandService commandService;
77+
78+
@Parameter
79+
private TextService textService;
80+
81+
@Parameter
82+
private EventService eventService;
83+
84+
// -- ReadmeService methods --
85+
86+
@Override
87+
public void displayWelcome() {
88+
final File baseDir = appService.getApp().getBaseDirectory();
89+
final File welcomeFile = new File(baseDir, WELCOME_FILE);
90+
try {
91+
if (welcomeFile.exists()) {
92+
final String welcomeText = textService.asHTML(welcomeFile);
93+
Display<?> display = displayService.createDisplay(welcomeText);
94+
if (display != null) display.display(welcomeText);
95+
}
96+
}
97+
catch (final IOException e) {
98+
throw new IllegalStateException(e.getMessage());
99+
}
100+
}
101+
102+
@Override
103+
public boolean isFirstRun() {
104+
final String firstRun = Prefs.get(getClass(), firstRunPrefKey());
105+
return firstRun == null || Boolean.parseBoolean(firstRun);
106+
}
107+
108+
@Override
109+
public void setFirstRun(final boolean firstRun) {
110+
Prefs.put(getClass(), firstRunPrefKey(), firstRun);
111+
}
112+
113+
// -- Event handlers --
114+
115+
/** Displays the welcome text when a UI is shown for the first time. */
116+
@EventHandler
117+
protected void onEvent(@SuppressWarnings("unused") final UIShownEvent evt) {
118+
if (!isFirstRun()) return;
119+
eventService.publish(new WelcomeEvent());
120+
setFirstRun(false);
121+
displayWelcome();
122+
}
123+
124+
// -- Helper methods --
125+
126+
/** Gets the preference key for ImageJ's first run. */
127+
private String firstRunPrefKey() {
128+
return "firstRun-" + appService.getApp().getVersion();
129+
}
130+
131+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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.welcome;
33+
34+
import org.scijava.service.SciJavaService;
35+
36+
/**
37+
* Interface for services to display the welcome greeting when the UI is shown
38+
* for the first time.
39+
*
40+
* @author Curtis Rueden
41+
* @author Mark Hiner
42+
*/
43+
public interface WelcomeService extends SciJavaService {
44+
45+
/** Displays the welcome greeting. */
46+
void displayWelcome();
47+
48+
/**
49+
* Returns true iff this version of the running SJC application has not run
50+
* before.
51+
*/
52+
boolean isFirstRun();
53+
54+
/**
55+
* Sets a preference recording whether this version of the dominant SJC
56+
* application has run.
57+
*/
58+
void setFirstRun(final boolean firstRun);
59+
60+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
package org.scijava.welcome.event;
32+
33+
import org.scijava.event.SciJavaEvent;
34+
35+
36+
/**
37+
* Marker {@link SciJavaEvent} indicating the first time an application is
38+
* run.
39+
*
40+
* @author Mark Hiner
41+
*/
42+
public class WelcomeEvent extends SciJavaEvent {
43+
// marker event: nothing to do
44+
}

0 commit comments

Comments
 (0)