|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * Core platform plugins for SciJava applications. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2010 - 2015 Board of Regents of the University of |
| 6 | + * Wisconsin-Madison. |
| 7 | + * %% |
| 8 | + * Redistribution and use in source and binary forms, with or without |
| 9 | + * modification, are permitted provided that the following conditions are met: |
| 10 | + * |
| 11 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer. |
| 13 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 14 | + * this list of conditions and the following disclaimer in the documentation |
| 15 | + * and/or other materials provided with the distribution. |
| 16 | + * |
| 17 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 21 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 25 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 26 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 27 | + * POSSIBILITY OF SUCH DAMAGE. |
| 28 | + * #L% |
| 29 | + */ |
| 30 | + |
| 31 | +package org.scijava.plugins.platforms.macos; |
| 32 | + |
| 33 | +import com.apple.eawt.AboutHandler; |
| 34 | +import com.apple.eawt.AppEvent.AboutEvent; |
| 35 | +import com.apple.eawt.AppEvent.AppForegroundEvent; |
| 36 | +import com.apple.eawt.AppEvent.AppHiddenEvent; |
| 37 | +import com.apple.eawt.AppEvent.AppReOpenedEvent; |
| 38 | +import com.apple.eawt.AppEvent.OpenFilesEvent; |
| 39 | +import com.apple.eawt.AppEvent.PreferencesEvent; |
| 40 | +import com.apple.eawt.AppEvent.PrintFilesEvent; |
| 41 | +import com.apple.eawt.AppEvent.QuitEvent; |
| 42 | +import com.apple.eawt.AppEvent.ScreenSleepEvent; |
| 43 | +import com.apple.eawt.AppEvent.SystemSleepEvent; |
| 44 | +import com.apple.eawt.AppEvent.UserSessionEvent; |
| 45 | +import com.apple.eawt.AppForegroundListener; |
| 46 | +import com.apple.eawt.AppHiddenListener; |
| 47 | +import com.apple.eawt.AppReOpenedListener; |
| 48 | +import com.apple.eawt.Application; |
| 49 | +import com.apple.eawt.OpenFilesHandler; |
| 50 | +import com.apple.eawt.PreferencesHandler; |
| 51 | +import com.apple.eawt.PrintFilesHandler; |
| 52 | +import com.apple.eawt.QuitHandler; |
| 53 | +import com.apple.eawt.QuitResponse; |
| 54 | +import com.apple.eawt.ScreenSleepListener; |
| 55 | +import com.apple.eawt.SystemSleepListener; |
| 56 | +import com.apple.eawt.UserSessionListener; |
| 57 | + |
| 58 | +import org.scijava.event.EventService; |
| 59 | +import org.scijava.platform.event.AppAboutEvent; |
| 60 | +import org.scijava.platform.event.AppFocusEvent; |
| 61 | +import org.scijava.platform.event.AppOpenFilesEvent; |
| 62 | +import org.scijava.platform.event.AppPreferencesEvent; |
| 63 | +import org.scijava.platform.event.AppPrintEvent; |
| 64 | +import org.scijava.platform.event.AppQuitEvent; |
| 65 | +import org.scijava.platform.event.AppReOpenEvent; |
| 66 | +import org.scijava.platform.event.AppScreenSleepEvent; |
| 67 | +import org.scijava.platform.event.AppSystemSleepEvent; |
| 68 | +import org.scijava.platform.event.AppUserSessionEvent; |
| 69 | +import org.scijava.platform.event.AppVisibleEvent; |
| 70 | + |
| 71 | +/** |
| 72 | + * Rebroadcasts macOS application events as ImageJ events. |
| 73 | + * |
| 74 | + * @author Curtis Rueden |
| 75 | + */ |
| 76 | +public class MacOSAppEventDispatcher implements AboutHandler, |
| 77 | + AppForegroundListener, AppHiddenListener, AppReOpenedListener, |
| 78 | + PreferencesHandler, PrintFilesHandler, QuitHandler, ScreenSleepListener, |
| 79 | + SystemSleepListener, UserSessionListener, OpenFilesHandler |
| 80 | +{ |
| 81 | + |
| 82 | + private final EventService eventService; |
| 83 | + |
| 84 | + public MacOSAppEventDispatcher(final EventService eventService) { |
| 85 | + this(Application.getApplication(), eventService); |
| 86 | + } |
| 87 | + |
| 88 | + public MacOSAppEventDispatcher(final Application app, |
| 89 | + final EventService eventService) |
| 90 | + { |
| 91 | + this.eventService = eventService; |
| 92 | + app.setAboutHandler(this); |
| 93 | + app.setPreferencesHandler(this); |
| 94 | + app.setPrintFileHandler(this); |
| 95 | + app.setQuitHandler(this); |
| 96 | + app.addAppEventListener(this); |
| 97 | + app.setOpenFileHandler(this); |
| 98 | + } |
| 99 | + |
| 100 | + // -- AboutHandler methods -- |
| 101 | + |
| 102 | + @Override |
| 103 | + public void handleAbout(final AboutEvent e) { |
| 104 | + eventService.publish(new AppAboutEvent()); |
| 105 | + } |
| 106 | + |
| 107 | + // -- PreferencesHandler methods -- |
| 108 | + |
| 109 | + @Override |
| 110 | + public void handlePreferences(final PreferencesEvent e) { |
| 111 | + eventService.publish(new AppPreferencesEvent()); |
| 112 | + } |
| 113 | + |
| 114 | + // -- PrintFilesHandler -- |
| 115 | + |
| 116 | + @Override |
| 117 | + public void printFiles(final PrintFilesEvent e) { |
| 118 | + eventService.publish(new AppPrintEvent()); |
| 119 | + } |
| 120 | + |
| 121 | + // -- QuitHandler methods -- |
| 122 | + |
| 123 | + @Override |
| 124 | + public void handleQuitRequestWith(final QuitEvent e, final QuitResponse r) { |
| 125 | + eventService.publish(new AppQuitEvent()); |
| 126 | + r.cancelQuit(); |
| 127 | + } |
| 128 | + |
| 129 | + // -- UserSessionListener methods -- |
| 130 | + |
| 131 | + @Override |
| 132 | + public void userSessionActivated(final UserSessionEvent e) { |
| 133 | + eventService.publish(new AppUserSessionEvent(true)); |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public void userSessionDeactivated(final UserSessionEvent e) { |
| 138 | + eventService.publish(new AppUserSessionEvent(false)); |
| 139 | + } |
| 140 | + |
| 141 | + // -- SystemSleepListener methods -- |
| 142 | + |
| 143 | + @Override |
| 144 | + public void systemAboutToSleep(final SystemSleepEvent e) { |
| 145 | + eventService.publish(new AppSystemSleepEvent(true)); |
| 146 | + } |
| 147 | + |
| 148 | + //@Override |
| 149 | + public void systemAwoke(final SystemSleepEvent e) { |
| 150 | + eventService.publish(new AppSystemSleepEvent(false)); |
| 151 | + } |
| 152 | + |
| 153 | + public void systemAweoke(final SystemSleepEvent e) { |
| 154 | + // HACK: To make com.yuvimasory:orange-extensions:1.3 happy. |
| 155 | + // See: https://github.com/ymasory/OrangeExtensions/pull/10 |
| 156 | + } |
| 157 | + |
| 158 | + // -- ScreenSleepListener methods -- |
| 159 | + |
| 160 | + @Override |
| 161 | + public void screenAboutToSleep(final ScreenSleepEvent e) { |
| 162 | + eventService.publish(new AppScreenSleepEvent(true)); |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public void screenAwoke(final ScreenSleepEvent e) { |
| 167 | + eventService.publish(new AppScreenSleepEvent(false)); |
| 168 | + } |
| 169 | + |
| 170 | + // -- AppHiddenListener methods -- |
| 171 | + |
| 172 | + @Override |
| 173 | + public void appHidden(final AppHiddenEvent e) { |
| 174 | + eventService.publish(new AppVisibleEvent(false)); |
| 175 | + } |
| 176 | + |
| 177 | + @Override |
| 178 | + public void appUnhidden(final AppHiddenEvent e) { |
| 179 | + eventService.publish(new AppVisibleEvent(true)); |
| 180 | + } |
| 181 | + |
| 182 | + // -- AppForegroundListener methods -- |
| 183 | + |
| 184 | + @Override |
| 185 | + public void appMovedToBackground(final AppForegroundEvent e) { |
| 186 | + eventService.publish(new AppFocusEvent(false)); |
| 187 | + } |
| 188 | + |
| 189 | + @Override |
| 190 | + public void appRaisedToForeground(final AppForegroundEvent e) { |
| 191 | + eventService.publish(new AppFocusEvent(true)); |
| 192 | + } |
| 193 | + |
| 194 | + // -- AppReOpenedListener methods -- |
| 195 | + |
| 196 | + @Override |
| 197 | + public void appReOpened(final AppReOpenedEvent e) { |
| 198 | + eventService.publish(new AppReOpenEvent()); |
| 199 | + } |
| 200 | + |
| 201 | + // -- OpenFilesHandler methods -- |
| 202 | + |
| 203 | + @Override |
| 204 | + public void openFiles(final OpenFilesEvent event) { |
| 205 | + eventService.publish(new AppOpenFilesEvent(event.getFiles())); |
| 206 | + } |
| 207 | + |
| 208 | +} |
0 commit comments