001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.content.slideshow;
031
032import java.awt.GraphicsDevice;
033import java.awt.GraphicsEnvironment;
034
035import javax.swing.JFrame;
036
037import org.apache.commons.lang.SystemUtils;
038
039/**
040 * Utility class for dealing with fullscreen Swing applications.
041 * 
042 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
043 * 
044 */
045public class FullscreenUtility {
046        protected JFrame window;
047        protected boolean fullscreen = false;
048
049        /**
050         * Construct with the given JFrame. The utility will allow the frame to be
051         * toggled between windowed and fullscreen mode.
052         * 
053         * @param frame
054         *            The frame.
055         */
056        public FullscreenUtility(JFrame frame) {
057                this.window = frame;
058        }
059
060        /**
061         * Method allows changing whether this window is displayed in fullscreen or
062         * windowed mode.
063         * 
064         * @param fullscreen
065         *            true = change to fullscreen, false = change to windowed
066         */
067        public void setFullscreen(boolean fullscreen)
068        {
069                // get a reference to the device.
070                final GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
071
072                if (this.fullscreen != fullscreen)
073                { // are we actually changing modes.
074                        // change modes.
075                        this.fullscreen = fullscreen;
076                        // toggle fullscreen mode
077                        if (!fullscreen)
078                        {
079                                // hide the frame so we can change it.
080                                window.setVisible(false);
081                                // remove the frame from being displayable.
082                                window.dispose();
083                                // put the borders back on the frame.
084                                window.setUndecorated(false);
085                                // needed to unset this window as the fullscreen window.
086                                device.setFullScreenWindow(null);
087                                // recenter window
088                                window.setLocationRelativeTo(null);
089                                window.setResizable(true);
090
091                                // reset the display mode to what it was before
092                                // we changed it.
093                                window.setVisible(true);
094                        }
095                        else
096                        { // change to fullscreen.
097                                // hide everything
098                                window.setVisible(false);
099                                // remove the frame from being displayable.
100                                window.dispose();
101                                // remove borders around the frame
102                                window.setUndecorated(true);
103                                // attempt to change the screen resolution.
104                                window.setResizable(false);
105                                window.setAlwaysOnTop(false);
106                                // make the window fullscreen.
107                                device.setFullScreenWindow(window);
108
109                                if (SystemUtils.IS_JAVA_1_7 && SystemUtils.IS_OS_MAC_OSX) {
110                                        System.err.println("Applying first responder fix");
111                                        // OSX first responder bug:
112                                        // http://mail.openjdk.java.net/pipermail/macosx-port-dev/2012-November/005109.html
113                                        // unfortunately this might not be a complete fix...
114                                        window.setVisible(false);
115                                }
116                                // show the frame
117                                window.setVisible(true);
118                        }
119                        // make sure that the screen is refreshed.
120                        window.repaint();
121                }
122        }
123}