|
| 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 | +} |
0 commit comments