| 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; |
| 17 | |
| 18 | import java.awt.event.ActionEvent; |
| 19 | import java.io.File; |
| 20 | import java.io.PrintStream; |
| 21 | |
| 22 | import javax.swing.SwingUtilities; |
| 23 | |
| 24 | import net.roydesign.event.ApplicationEvent; |
| 25 | import net.sf.jomic.comic.ComicFileFilter; |
| 26 | import net.sf.jomic.common.JomicStartup; |
| 27 | import net.sf.jomic.common.Settings; |
| 28 | import net.sf.jomic.common.SplashScreen; |
| 29 | import net.sf.jomic.common.StartupTools; |
| 30 | import net.sf.jomic.tools.LocaleTools; |
| 31 | import net.sf.jomic.tools.StandardConstants; |
| 32 | import net.sf.jomic.ui.JomicApplication; |
| 33 | |
| 34 | import org.apache.commons.logging.Log; |
| 35 | import org.apache.commons.logging.LogFactory; |
| 36 | |
| 37 | import com.apple.mrj.MRJApplicationUtils; |
| 38 | import com.apple.mrj.MRJOpenDocumentHandler; |
| 39 | |
| 40 | /** |
| 41 | * Jomic application listening to Mac OS X application events, for example drag and drop in Finder. |
| 42 | * |
| 43 | * @author Thomas Aglassinger |
| 44 | * @see Jomic |
| 45 | */ |
| 46 | public final class JomicMacOSX implements MRJOpenDocumentHandler, StandardConstants |
| 47 | { |
| 48 | private static final int INITIALIZATION_TIMEOUT = 30000; |
| 49 | private static final int WAITING_FOR_INITIALITATION_LOG_INTERVAL = 1000; |
| 50 | private static Log logger; |
| 51 | private boolean initailisationFinished; |
| 52 | private Jomic jomic; |
| 53 | private JomicStartup startup; |
| 54 | private StartupTools startupTools; |
| 55 | |
| 56 | private JomicMacOSX() { |
| 57 | startupTools = StartupTools.instance(); |
| 58 | startupTools.logDuration("entering JomicMacOSX()"); |
| 59 | |
| 60 | // Start listening to Mac OS X application events. We have to do this |
| 61 | // as early as possible in order to prevent the event from getting |
| 62 | // lost. Otherwise, we could just let JomicApplication let listen for |
| 63 | // the "open document" event. |
| 64 | // TODO: Find non-deprecated way to handle "OpenDocument" events. |
| 65 | MRJApplicationUtils.registerOpenDocumentHandler(this); |
| 66 | startupTools.logDuration("after registerOpenDocumentHandler()"); |
| 67 | startup = JomicStartup.instance(); |
| 68 | } |
| 69 | |
| 70 | private void setFileToBeOpened(File file) { |
| 71 | if (file != null) { |
| 72 | ComicFileFilter comicFileFilter = new ComicFileFilter(); |
| 73 | |
| 74 | if (comicFileFilter.accept(file, file.getName())) { |
| 75 | if (jomic.isReadyForOpen()) { |
| 76 | JomicApplication.instance().openInNewWindow(file); |
| 77 | } else { |
| 78 | if (logger.isInfoEnabled()) { |
| 79 | logger.info("scheduling file to be opened when ready: \"" + file + "\""); |
| 80 | } |
| 81 | // TODO: support multiple files |
| 82 | jomic.setComicFile(file); |
| 83 | } |
| 84 | } else { |
| 85 | logger.warn("rejecting file \"" + file + "\""); |
| 86 | } |
| 87 | } else { |
| 88 | if (logger.isDebugEnabled()) { |
| 89 | logger.debug("application event without file"); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | private void setInitialisationFinished(boolean value) { |
| 95 | initailisationFinished = value; |
| 96 | } |
| 97 | |
| 98 | private void setJomic(Jomic newJomic) { |
| 99 | assert newJomic != null; |
| 100 | jomic = newJomic; |
| 101 | } |
| 102 | |
| 103 | private Jomic getJomic() { |
| 104 | return jomic; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Launch Jomic under Mac OS X, ignoring <code>arguments</code>. |
| 109 | */ |
| 110 | public static void main(final String[] arguments) { |
| 111 | StartupTools startupTools = StartupTools.instance(); |
| 112 | |
| 113 | startupTools.logDuration("entering main"); |
| 114 | |
| 115 | JomicMacOSX launcher = new JomicMacOSX(); |
| 116 | |
| 117 | startupTools.logDuration("application initialised"); |
| 118 | |
| 119 | SplashScreen splashScreen = startupTools.openSplashScreen(); |
| 120 | |
| 121 | startupTools.logDuration("SplashScreen opened"); |
| 122 | try { |
| 123 | if (arguments.length > 0) { |
| 124 | throw new IllegalArgumentException("to pass command line arguments, " |
| 125 | + Jomic.class.getName() + " must be used"); |
| 126 | } |
| 127 | logger = LogFactory.getLog(JomicMacOSX.class); |
| 128 | |
| 129 | JomicStartup startup = JomicStartup.instance(); |
| 130 | |
| 131 | startup.startup(); |
| 132 | |
| 133 | Settings settings = Settings.instance(); |
| 134 | |
| 135 | settings.read(settings.getSettingsFile()); |
| 136 | launcher.setJomic(new Jomic(null)); |
| 137 | launcher.setInitialisationFinished(true); |
| 138 | startupTools.logDuration("Jomic initialised"); |
| 139 | SwingUtilities.invokeLater(new Jomic.JomicRunner(launcher.getJomic())); |
| 140 | startupTools.logDuration("JomicRunner invoked"); |
| 141 | } catch (Throwable error) { |
| 142 | Jomic.exitWithError(error); |
| 143 | } finally { |
| 144 | startupTools.disposeSplashScreen(splashScreen); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Handle Mac OS X application events. On other platforms, this method will never be called. |
| 150 | * |
| 151 | * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) |
| 152 | */ |
| 153 | public void actionPerformed(ActionEvent event) { |
| 154 | assert event instanceof ApplicationEvent : "event.class = " + event.getClass(); |
| 155 | |
| 156 | waitForInitialisationFinished(); |
| 157 | |
| 158 | ApplicationEvent appEvent = (ApplicationEvent) event; |
| 159 | // HACK: Silly name to prevent hiding the "startup" field inherited |
| 160 | // from net.sf.jomic.Jomic. |
| 161 | JomicStartup osxStartup = JomicStartup.instance(); |
| 162 | |
| 163 | if (logger.isInfoEnabled()) { |
| 164 | logger.info("handle application event: " + appEvent); |
| 165 | } |
| 166 | try { |
| 167 | int type = appEvent.getType(); |
| 168 | |
| 169 | if ((type == ApplicationEvent.OPEN_DOCUMENT) || (type == ApplicationEvent.REOPEN_APPLICATION)) { |
| 170 | File file = appEvent.getFile(); |
| 171 | |
| 172 | setFileToBeOpened(file); |
| 173 | } else { |
| 174 | logger.warn("unknown application event: " + appEvent); |
| 175 | } |
| 176 | } catch (Throwable error) { |
| 177 | String message = LocaleTools.instance().getMessage( |
| 178 | "errors.cannotProcessApplicationEvent", new Integer(appEvent.getType())); |
| 179 | |
| 180 | osxStartup.showError(message, error, false); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | public void handleOpenFile(File file) { |
| 185 | try { |
| 186 | waitForInitialisationFinished(); |
| 187 | setFileToBeOpened(file); |
| 188 | } catch (Throwable error) { |
| 189 | String message = LocaleTools.instance().getMessage( |
| 190 | "errors.cannotProcessApplicationEvent", new Integer(0)); |
| 191 | |
| 192 | startup.showError(message, error, false); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | private void waitForInitialisationFinished() { |
| 197 | int timeout = INITIALIZATION_TIMEOUT; |
| 198 | |
| 199 | assert WAITING_FOR_INITIALITATION_LOG_INTERVAL % TICK == 0; |
| 200 | |
| 201 | while (!initailisationFinished && (timeout > 0)) { |
| 202 | if ((timeout % WAITING_FOR_INITIALITATION_LOG_INTERVAL) == 0) { |
| 203 | startupTools.logDuration("waiting for initialization to be finished before processing event"); |
| 204 | } |
| 205 | try { |
| 206 | timeout -= TICK; |
| 207 | Thread.sleep(TICK); |
| 208 | } catch (InterruptedException interruption) { |
| 209 | PrintStream warnStream = System.err; |
| 210 | |
| 211 | warnStream.println("cannot wait for initialization to be finished: " + interruption); |
| 212 | } |
| 213 | } |
| 214 | if (timeout <= 0) { |
| 215 | throw new IllegalStateException("timeout while waiting to be ready for events"); |
| 216 | } |
| 217 | } |
| 218 | } |