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