1 /*
   2  * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.apple.eawt;
  27 
  28 import com.apple.eawt.event.FullScreenEvent;
  29 import java.awt.*;
  30 import java.util.*;
  31 import java.util.List;
  32 
  33 import javax.swing.RootPaneContainer;
  34 
  35 import sun.awt.SunToolkit;
  36 
  37 import java.lang.annotation.Native;
  38 
  39 final class FullScreenHandler {
  40     private static final String CLIENT_PROPERTY = "com.apple.eawt.event.internalFullScreenHandler";
  41 
  42     @Native static final int FULLSCREEN_WILL_ENTER = 1;
  43     @Native static final int FULLSCREEN_DID_ENTER = 2;
  44     @Native static final int FULLSCREEN_WILL_EXIT = 3;
  45     @Native static final int FULLSCREEN_DID_EXIT = 4;
  46 
  47     // installs a private instance of the handler, if necessary
  48     static void addFullScreenListenerTo(final RootPaneContainer window, final FullScreenListener listener) {
  49         final Object value = window.getRootPane().getClientProperty(CLIENT_PROPERTY);
  50         if (value instanceof FullScreenHandler) {
  51             ((FullScreenHandler)value).addListener(listener);
  52             return;
  53         }
  54 
  55         if (value != null) return; // some other garbage is in our client property
  56 
  57         final FullScreenHandler newHandler = new FullScreenHandler();
  58         newHandler.addListener(listener);
  59         window.getRootPane().putClientProperty(CLIENT_PROPERTY, newHandler);
  60     }
  61 
  62     // asks the installed FullScreenHandler to remove it's listener (does not uninstall the FullScreenHandler)
  63     static void removeFullScreenListenerFrom(final RootPaneContainer window, final FullScreenListener listener) {
  64         final Object value = window.getRootPane().getClientProperty(CLIENT_PROPERTY);
  65         if (!(value instanceof FullScreenHandler)) return;
  66         ((FullScreenHandler)value).removeListener(listener);
  67     }
  68 
  69     static FullScreenHandler getHandlerFor(final RootPaneContainer window) {
  70         final Object value = window.getRootPane().getClientProperty(CLIENT_PROPERTY);
  71         if (value instanceof FullScreenHandler) return (FullScreenHandler)value;
  72         return null;
  73     }
  74 
  75     // called from native
  76     static void handleFullScreenEventFromNative(final Window window, final int type) {
  77         if (!(window instanceof RootPaneContainer)) return; // handles null
  78 
  79         SunToolkit.executeOnEventHandlerThread(window, new Runnable() {
  80             public void run() {
  81                 final FullScreenHandler handler = getHandlerFor((RootPaneContainer)window);
  82                 if (handler != null) handler.notifyListener(new FullScreenEvent(window), type);
  83             }
  84         });
  85     }
  86 
  87 
  88     final List<FullScreenListener> listeners = new LinkedList<FullScreenListener>();
  89 
  90     FullScreenHandler() { }
  91 
  92     void addListener(final FullScreenListener listener) {
  93         listeners.add(listener);
  94     }
  95 
  96     void removeListener(final FullScreenListener listener) {
  97         listeners.remove(listener);
  98     }
  99 
 100     void notifyListener(final FullScreenEvent e, final int op) {
 101         for (final FullScreenListener listener : listeners) {
 102                 switch (op) {
 103                 case FULLSCREEN_WILL_ENTER:
 104                         listener.windowEnteringFullScreen(e);
 105                     return;
 106                 case FULLSCREEN_DID_ENTER:
 107                         listener.windowEnteredFullScreen(e);
 108                     return;
 109                 case FULLSCREEN_WILL_EXIT:
 110                         listener.windowExitingFullScreen(e);
 111                     return;
 112                 case FULLSCREEN_DID_EXIT:
 113                         listener.windowExitedFullScreen(e);
 114                     return;
 115             }
 116         }
 117     }
 118 }