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 java.awt.*;
  29 import java.util.*;
  30 import java.util.List;
  31 
  32 import javax.swing.RootPaneContainer;
  33 
  34 import com.apple.eawt.AppEvent.FullScreenEvent;
  35 
  36 import java.lang.annotation.Native;
  37 
  38 final class FullScreenHandler {
  39     private static final String CLIENT_PROPERTY = "com.apple.eawt.event.internalFullScreenHandler";
  40 
  41     @Native static final int FULLSCREEN_WILL_ENTER = 1;
  42     @Native static final int FULLSCREEN_DID_ENTER = 2;
  43     @Native static final int FULLSCREEN_WILL_EXIT = 3;
  44     @Native static final int FULLSCREEN_DID_EXIT = 4;
  45 
  46     // installs a private instance of the handler, if necessary
  47     static void addFullScreenListenerTo(final RootPaneContainer window, final FullScreenListener listener) {
  48         final Object value = window.getRootPane().getClientProperty(CLIENT_PROPERTY);
  49         if (value instanceof FullScreenHandler) {
  50             ((FullScreenHandler)value).addListener(listener);
  51             return;
  52         }
  53 
  54         if (value != null) return; // some other garbage is in our client property
  55 
  56         final FullScreenHandler newHandler = new FullScreenHandler();
  57         newHandler.addListener(listener);
  58         window.getRootPane().putClientProperty(CLIENT_PROPERTY, newHandler);
  59     }
  60 
  61     // asks the installed FullScreenHandler to remove it's listener (does not uninstall the FullScreenHandler)
  62     static void removeFullScreenListenerFrom(final RootPaneContainer window, final FullScreenListener listener) {
  63         final Object value = window.getRootPane().getClientProperty(CLIENT_PROPERTY);
  64         if (!(value instanceof FullScreenHandler)) return;
  65         ((FullScreenHandler)value).removeListener(listener);
  66     }
  67 
  68     static FullScreenHandler getHandlerFor(final RootPaneContainer window) {
  69         final Object value = window.getRootPane().getClientProperty(CLIENT_PROPERTY);
  70         if (value instanceof FullScreenHandler) return (FullScreenHandler)value;
  71         return null;
  72     }
  73 
  74     // called from native
  75     static void handleFullScreenEventFromNative(final Window window, final int type) {
  76         if (!(window instanceof RootPaneContainer)) return; // handles null
  77 
  78         EventQueue.invokeLater(new Runnable() {
  79             public void run() {
  80                 final FullScreenHandler handler = getHandlerFor((RootPaneContainer)window);
  81                 if (handler != null) handler.notifyListener(new FullScreenEvent(window), type);
  82             }
  83         });
  84     }
  85 
  86 
  87     final List<FullScreenListener> listeners = new LinkedList<FullScreenListener>();
  88 
  89     FullScreenHandler() { }
  90 
  91     void addListener(final FullScreenListener listener) {
  92         listeners.add(listener);
  93     }
  94 
  95     void removeListener(final FullScreenListener listener) {
  96         listeners.remove(listener);
  97     }
  98 
  99     void notifyListener(final FullScreenEvent e, final int op) {
 100         for (final FullScreenListener listener : listeners) {
 101                 switch (op) {
 102                 case FULLSCREEN_WILL_ENTER:
 103                         listener.windowEnteringFullScreen(e);
 104                     return;
 105                 case FULLSCREEN_DID_ENTER:
 106                         listener.windowEnteredFullScreen(e);
 107                     return;
 108                 case FULLSCREEN_WILL_EXIT:
 109                         listener.windowExitingFullScreen(e);
 110                     return;
 111                 case FULLSCREEN_DID_EXIT:
 112                         listener.windowExitedFullScreen(e);
 113                     return;
 114             }
 115         }
 116     }
 117 }