001 package com.croftsoft.core.gui;
002
003 import java.awt.*;
004 import java.awt.event.*;
005 import java.security.AccessControlException;
006 import javax.swing.*;
007
008 import com.croftsoft.core.lang.NullArgumentException;
009
010 /*********************************************************************
011 * Toggles full-screen mode using ALT-ENTER.
012 *
013 * @version
014 * $Date: 2006/01/13 18:47:08 $
015 * @since
016 * 2003-02-19
017 * @author
018 * <a href="http://www.CroftSoft.com/">David Wallace Croft</a>
019 *********************************************************************/
020
021 public final class FullScreenToggler
022 extends AbstractAction
023 //////////////////////////////////////////////////////////////////////
024 //////////////////////////////////////////////////////////////////////
025 {
026
027 private static final long serialVersionUID = 0L;
028
029 //
030
031 public static final String ACTION_KEY_TOGGLE_FULLSCREEN
032 = "com.croftsoft.core.gui.FullScreenToggler";
033
034 //
035
036 private final Window window;
037
038 //////////////////////////////////////////////////////////////////////
039 // static methods
040 //////////////////////////////////////////////////////////////////////
041
042 public static void main ( String [ ] args )
043 //////////////////////////////////////////////////////////////////////
044 {
045 JFrame jFrame = new JFrame ( "Press ALT-ENTER to toggle" );
046
047 jFrame.setDefaultCloseOperation (
048 WindowConstants.DO_NOTHING_ON_CLOSE );
049
050 jFrame.addWindowListener ( new ShutdownWindowAdapter ( ) );
051
052 WindowLib.centerOnScreen ( jFrame, 0.8 );
053
054 toggle ( jFrame, true );
055
056 monitor ( jFrame );
057
058 jFrame.setVisible ( true );
059 }
060
061 public static boolean monitor ( JComponent component )
062 //////////////////////////////////////////////////////////////////////
063 {
064 NullArgumentException.check ( component );
065
066 Window window = WindowLib.getParentWindow ( component );
067
068 if ( window == null )
069 {
070 return false;
071 }
072
073 KeyStroke keyStroke = KeyStroke.getKeyStroke (
074 KeyEvent.VK_ENTER, InputEvent.ALT_MASK, false );
075
076 InputMap inputMap = component.getInputMap (
077 JComponent.WHEN_IN_FOCUSED_WINDOW );
078
079 inputMap.put ( keyStroke, ACTION_KEY_TOGGLE_FULLSCREEN );
080
081 inputMap = component.getInputMap ( JComponent.WHEN_FOCUSED );
082
083 inputMap.put ( keyStroke, ACTION_KEY_TOGGLE_FULLSCREEN );
084
085 inputMap = component.getInputMap (
086 JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT );
087
088 inputMap.put ( keyStroke, ACTION_KEY_TOGGLE_FULLSCREEN );
089
090 component.getActionMap ( ).put (
091 ACTION_KEY_TOGGLE_FULLSCREEN,
092 new FullScreenToggler ( window ) );
093
094 return true;
095 }
096
097 public static void monitor ( JFrame jFrame )
098 //////////////////////////////////////////////////////////////////////
099 {
100 NullArgumentException.check ( jFrame );
101
102 monitor ( jFrame.getRootPane ( ) );
103 }
104
105 public static void toggle (
106 Window window,
107 boolean fullScreen )
108 //////////////////////////////////////////////////////////////////////
109 {
110 NullArgumentException.check ( window );
111
112 GraphicsConfiguration graphicsConfiguration
113 = window.getGraphicsConfiguration ( );
114
115 GraphicsDevice graphicsDevice
116 = graphicsConfiguration.getDevice ( );
117
118 if ( fullScreen )
119 {
120 try
121 {
122 graphicsDevice.setFullScreenWindow ( window );
123
124 window.validate ( );
125
126 window.repaint ( );
127 }
128 catch ( AccessControlException ex )
129 {
130 }
131 }
132 else
133 {
134 try
135 {
136 graphicsDevice.setFullScreenWindow ( null );
137
138 window.validate ( );
139
140 window.repaint ( );
141 }
142 catch ( AccessControlException ex )
143 {
144 }
145 }
146 }
147
148 public static void toggle ( Window window )
149 //////////////////////////////////////////////////////////////////////
150 {
151 NullArgumentException.check ( window );
152
153 GraphicsConfiguration graphicsConfiguration
154 = window.getGraphicsConfiguration ( );
155
156 GraphicsDevice graphicsDevice
157 = graphicsConfiguration.getDevice ( );
158
159 toggle ( window, graphicsDevice.getFullScreenWindow ( ) != window );
160 }
161
162 //////////////////////////////////////////////////////////////////////
163 // constructor methods
164 //////////////////////////////////////////////////////////////////////
165
166 public FullScreenToggler ( Window window )
167 //////////////////////////////////////////////////////////////////////
168 {
169 NullArgumentException.check ( this.window = window );
170 }
171
172 //////////////////////////////////////////////////////////////////////
173 //////////////////////////////////////////////////////////////////////
174
175 public void toggle ( )
176 //////////////////////////////////////////////////////////////////////
177 {
178 toggle ( window );
179 }
180
181 public void toggle ( boolean fullScreen )
182 //////////////////////////////////////////////////////////////////////
183 {
184 toggle ( window, fullScreen );
185 }
186
187 public void actionPerformed ( ActionEvent actionEvent )
188 //////////////////////////////////////////////////////////////////////
189 {
190 toggle ( window );
191 }
192
193 //////////////////////////////////////////////////////////////////////
194 //////////////////////////////////////////////////////////////////////
195 }