| 1 | // Jomic - a viewer for comic book archives. |
| 2 | // Copyright (C) 2004-2011 Thomas Aglassinger |
| 3 | // |
| 4 | // This program is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | package net.sf.jomic.common; |
| 17 | |
| 18 | import java.io.File; |
| 19 | import java.io.FileNotFoundException; |
| 20 | import java.io.PrintStream; |
| 21 | import java.text.SimpleDateFormat; |
| 22 | import java.util.Date; |
| 23 | |
| 24 | import javax.swing.SwingUtilities; |
| 25 | |
| 26 | import net.roydesign.mac.MRJAdapter; |
| 27 | import net.roydesign.mac.MRJFolderConstants; |
| 28 | |
| 29 | /** |
| 30 | * Utility methods used during the early startup. At that the time the following things are not |
| 31 | * available yet: |
| 32 | * <ul> |
| 33 | * <li> Logging</li> |
| 34 | * <li> Localization</li> |
| 35 | * <li> All other classes in the net.sf.jomic.tools package</li> |
| 36 | * </ul> |
| 37 | * |
| 38 | * |
| 39 | * @author Thomas Aglassinger |
| 40 | */ |
| 41 | public final class StartupTools |
| 42 | { |
| 43 | /** |
| 44 | * Image resource for the splash screen. |
| 45 | * |
| 46 | * @see SplashScreen |
| 47 | */ |
| 48 | public static final String SPLASH_RESOURCE = "/net/sf/jomic/images/splash.jpg"; |
| 49 | |
| 50 | private static StartupTools instance; |
| 51 | private boolean isMacOsX; |
| 52 | private boolean isWindows; |
| 53 | private SimpleDateFormat logDateFormat; |
| 54 | |
| 55 | /** |
| 56 | * Stores the time when StartupTools was instantiated the first time. |
| 57 | */ |
| 58 | private long startupTime; |
| 59 | |
| 60 | private StartupTools() { |
| 61 | String osName = System.getProperty("os.name").toLowerCase(); |
| 62 | |
| 63 | if (Boolean.getBoolean(PropertyConstants.SYSTEM_PROPERTY_PREFIX + PropertyConstants.IGNORE_OSX)) { |
| 64 | isMacOsX = false; |
| 65 | } else { |
| 66 | isMacOsX = osName.startsWith("mac os x"); |
| 67 | } |
| 68 | isWindows = osName.startsWith("windows"); |
| 69 | startupTime = System.currentTimeMillis(); |
| 70 | logDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Get file where the settings are stored. |
| 75 | */ |
| 76 | public File getSettingsDir() { |
| 77 | File result = null; |
| 78 | |
| 79 | // Use Mac OS X standard settings dir? |
| 80 | if (System.getProperty("mrj.version") != null) { |
| 81 | try { |
| 82 | result = MRJAdapter.findFolder( |
| 83 | MRJFolderConstants.kUserDomain, |
| 84 | MRJFolderConstants.kPreferencesFolderType, |
| 85 | true); |
| 86 | } catch (FileNotFoundException ignore) { |
| 87 | logDuration("cannot get settings directory (" + result + "); using default"); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // Possibly use settings dir specified in system properties (typically for testing purpose). |
| 92 | String testSettingsPath = System.getProperty( |
| 93 | PropertyConstants.SYSTEM_PROPERTY_PREFIX + PropertyConstants.SETTINGS_DIR); |
| 94 | |
| 95 | if (testSettingsPath != null) { |
| 96 | result = new File(testSettingsPath); |
| 97 | logDuration("using test directory for settings: " + result); |
| 98 | } |
| 99 | |
| 100 | // As a last resort, use the users home directory. |
| 101 | if (result == null) { |
| 102 | logDuration("using home directory for settings"); |
| 103 | result = new File(System.getProperty("user.home")); |
| 104 | } |
| 105 | return result; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Get file where the settings are stored. |
| 110 | */ |
| 111 | public File getSettingsFile(String baseName) { |
| 112 | assert baseName != null; |
| 113 | |
| 114 | File result; |
| 115 | String applicationSettingsDirName = baseName; |
| 116 | File applicationSettingsDir; |
| 117 | |
| 118 | if (isMacOsX) { |
| 119 | applicationSettingsDir = getSettingsDir(); |
| 120 | } else { |
| 121 | if (!isWindows) { |
| 122 | applicationSettingsDirName = "." + applicationSettingsDirName; |
| 123 | } |
| 124 | applicationSettingsDir = new File(getSettingsDir(), applicationSettingsDirName); |
| 125 | } |
| 126 | result = new File(applicationSettingsDir, baseName + ".properties"); |
| 127 | |
| 128 | return result; |
| 129 | } |
| 130 | |
| 131 | public static synchronized StartupTools instance() { |
| 132 | if (instance == null) { |
| 133 | instance = new StartupTools(); |
| 134 | } |
| 135 | return instance; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Dispose the splash screen in the event handling thread. |
| 140 | */ |
| 141 | public void disposeSplashScreen(SplashScreen splashScreen) { |
| 142 | SwingUtilities.invokeLater(new SplashScreenDisposer(splashScreen)); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Log <code>message</code> with a timestamp <code>time</code> to <code>System.out</code>. |
| 147 | */ |
| 148 | public void log(String message, long time) { |
| 149 | if (Boolean.getBoolean(PropertyConstants.SYSTEM_PROPERTY_PREFIX + PropertyConstants.LOG_STARTUP)) { |
| 150 | PrintStream out = System.out; |
| 151 | String timeText = logDateFormat.format(new Date(time)); |
| 152 | |
| 153 | out.println(timeText + " [" + Thread.currentThread().getName() + "] START " |
| 154 | + StartupTools.class.getName() + " - " + message); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Log <code>message</code> to <code>System.out</code> and append the duration since startup. |
| 160 | */ |
| 161 | public void logDuration(String message) { |
| 162 | assert message != null; |
| 163 | long time = System.currentTimeMillis(); |
| 164 | |
| 165 | log("[after " + (time - startupTime) + " ms] " + message, time); |
| 166 | } |
| 167 | |
| 168 | public SplashScreen openSplashScreen() { |
| 169 | SplashScreen result = new SplashScreen(SPLASH_RESOURCE); |
| 170 | |
| 171 | result.splash(); |
| 172 | return result; |
| 173 | } |
| 174 | } |