00001
00002
00003
00004
00005
00006 package edu.stanford.hci.r3.util;
00007
00008 import java.awt.*;
00009
00010 import javax.swing.JFrame;
00011 import javax.swing.UIManager;
00012
00025 public class WindowUtils {
00026
00027
00028 private static int cachedScreenHeight = 480;
00029
00030
00031 private static int cachedScreenWidth = 640;
00032
00033 public static final int DESKTOP_CENTER = 0;
00034
00035 public static final int DESKTOP_EAST = 2;
00036
00037 public static final int DESKTOP_NORTH = 3;
00038
00039 public static final int DESKTOP_NORTHEAST = 6;
00040
00041 public static final int DESKTOP_NORTHWEST = 5;
00042
00043 public static final int DESKTOP_SOUTH = 4;
00044
00045 public static final int DESKTOP_SOUTHEAST = 8;
00046
00047 public static final int DESKTOP_SOUTHWEST = 7;
00048
00049 public static final int DESKTOP_WEST = 1;
00050
00051
00052
00053 private static Rectangle desktopBounds = new Rectangle(0, 0, 640, 480);
00054
00055 private static Point fsFrameLocation;
00056
00057 private static boolean fsFrameResizable;
00058
00059 private static Dimension fsFrameSize;
00060
00061
00062 private static boolean fsFrameUndecorated;
00063
00064 public static final int INVALID_MAX = 18;
00065
00066
00067 public static final int INVALID_MIN = -1;
00068
00069 public static final int SCREEN_CENTER = 9;
00070
00071 public static final int SCREEN_EAST = 11;
00072
00073 public static final int SCREEN_NORTH = 12;
00074
00075 public static final int SCREEN_NORTHEAST = 15;
00076
00077 public static final int SCREEN_NORTHWEST = 14;
00078
00079 public static final int SCREEN_SOUTH = 13;
00080
00081 public static final int SCREEN_SOUTHEAST = 17;
00082
00083 public static final int SCREEN_SOUTHWEST = 16;
00084
00085 public static final int SCREEN_WEST = 10;
00086
00087
00088 static {
00089 initCachedWindowState();
00090 }
00091
00097 public static void centerWindow(JFrame frame) {
00098 frame.setLocation(getWindowOrigin(frame.getWidth(), frame.getHeight(),
00099 WindowUtils.DESKTOP_CENTER));
00100 }
00101
00106 public static void enterFullScreenIfPossible(JFrame mainAppFrame) {
00107 final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
00108 final GraphicsDevice defaultScreenDevice = ge.getDefaultScreenDevice();
00109
00110
00111
00112
00113
00114
00115
00116 if (defaultScreenDevice.isFullScreenSupported()) {
00117 try {
00118 mainAppFrame.dispose();
00119
00120 fsFrameSize = mainAppFrame.getSize();
00121 mainAppFrame.setSize(getScreenSize());
00122
00123 fsFrameUndecorated = mainAppFrame.isUndecorated();
00124 mainAppFrame.setUndecorated(true);
00125
00126 fsFrameResizable = mainAppFrame.isResizable();
00127 mainAppFrame.setResizable(false);
00128
00129 fsFrameLocation = mainAppFrame.getLocation();
00130 mainAppFrame.setLocation(0, 0);
00131
00132 defaultScreenDevice.setFullScreenWindow(mainAppFrame);
00133 } finally {
00134 defaultScreenDevice.setFullScreenWindow(null);
00135 }
00136 } else {
00137 System.err.println("Fullscreen mode is not supported on this monitor.");
00138 }
00139 }
00140
00144 public static void exitFullScreen(JFrame mainAppFrame) {
00145 mainAppFrame.dispose();
00146 mainAppFrame.setUndecorated(fsFrameUndecorated);
00147 mainAppFrame.setResizable(fsFrameResizable);
00148 mainAppFrame.setSize(fsFrameSize);
00149 mainAppFrame.setLocation(fsFrameLocation);
00150 GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
00151 .setFullScreenWindow(null);
00152 mainAppFrame.setVisible(true);
00153 }
00154
00163 public static Point getCachedWindowOrigin(int windowWidth, int windowHeight, int where) {
00164 Point p = new Point(0, 0);
00165 if ((where >= INVALID_MAX) || (where <= INVALID_MIN)) {
00166
00167 return p;
00168 } else if ((windowWidth < 0) || (windowHeight < 0)) {
00169
00170 return p;
00171 }
00172
00173 int x = 0;
00174 int y = 0;
00175
00176 switch (where) {
00177
00178 case DESKTOP_CENTER:
00179 x = desktopBounds.x + desktopBounds.width / 2 - windowWidth / 2;
00180 y = desktopBounds.y + desktopBounds.height / 2 - windowHeight / 2;
00181 break;
00182 case DESKTOP_NORTH:
00183 x = desktopBounds.x + desktopBounds.width / 2 - windowWidth / 2;
00184 y = desktopBounds.y;
00185 break;
00186 case DESKTOP_EAST:
00187 x = desktopBounds.x + desktopBounds.width - windowWidth;
00188 y = desktopBounds.y + desktopBounds.height / 2 - windowHeight / 2;
00189 break;
00190 case DESKTOP_WEST:
00191 x = desktopBounds.x;
00192 y = desktopBounds.y + desktopBounds.height / 2 - windowHeight / 2;
00193 break;
00194 case DESKTOP_SOUTH:
00195 x = desktopBounds.x + desktopBounds.width / 2 - windowWidth / 2;
00196 y = desktopBounds.y + desktopBounds.height - windowHeight;
00197 break;
00198 case DESKTOP_NORTHWEST:
00199 x = desktopBounds.x;
00200 y = desktopBounds.y;
00201 break;
00202 case DESKTOP_NORTHEAST:
00203 x = desktopBounds.x + desktopBounds.width - windowWidth;
00204 y = desktopBounds.y;
00205 break;
00206 case DESKTOP_SOUTHWEST:
00207 x = desktopBounds.x;
00208 y = desktopBounds.y + desktopBounds.height - windowHeight;
00209 break;
00210 case DESKTOP_SOUTHEAST:
00211 x = desktopBounds.x + desktopBounds.width - windowWidth;
00212 y = desktopBounds.y + desktopBounds.height - windowHeight;
00213 break;
00214
00215 case SCREEN_CENTER:
00216 x = cachedScreenWidth / 2 - windowWidth / 2;
00217 y = cachedScreenHeight / 2 - windowHeight / 2;
00218 break;
00219 case SCREEN_NORTH:
00220 x = cachedScreenWidth / 2 - windowWidth / 2;
00221 y = 0;
00222 break;
00223 case SCREEN_EAST:
00224 x = cachedScreenWidth - windowWidth;
00225 y = cachedScreenHeight / 2 - windowHeight / 2;
00226 break;
00227 case SCREEN_WEST:
00228 x = 0;
00229 y = cachedScreenHeight / 2 - windowHeight / 2;
00230 break;
00231 case SCREEN_SOUTH:
00232 x = cachedScreenWidth / 2 - windowWidth / 2;
00233 y = cachedScreenHeight - windowHeight;
00234 break;
00235 case SCREEN_NORTHWEST:
00236 x = 0;
00237 y = 0;
00238 break;
00239 case SCREEN_NORTHEAST:
00240 x = cachedScreenWidth - windowWidth;
00241 y = 0;
00242 break;
00243 case SCREEN_SOUTHWEST:
00244 x = 0;
00245 y = cachedScreenHeight - windowHeight;
00246 break;
00247 case SCREEN_SOUTHEAST:
00248 x = cachedScreenWidth - windowWidth;
00249 y = cachedScreenHeight - windowHeight;
00250 break;
00251 default:
00252 x = 0;
00253 y = 0;
00254 break;
00255 }
00256
00257
00258 if (windowHeight > cachedScreenHeight) {
00259 y = 0;
00260 }
00261 if (windowWidth > cachedScreenWidth) {
00262 x = 0;
00263 }
00264
00265 p.setLocation(x, y);
00266 return p;
00267 }
00268
00272 public static Rectangle getDesktopBounds() {
00273 return desktopBounds;
00274 }
00275
00279 public static Dimension getDesktopSize() {
00280 return desktopBounds.getSize();
00281 }
00282
00286 public static int getScreenHeight() {
00287 return cachedScreenHeight;
00288 }
00289
00293 public static Dimension getScreenSize() {
00294 return new Dimension(getScreenWidth(), getScreenHeight());
00295 }
00296
00300 public static int getScreenWidth() {
00301 return cachedScreenWidth;
00302 }
00303
00316 public static Point getWindowOrigin(int windowWidth, int windowHeight, int where) {
00317 initCachedWindowState();
00318 return getCachedWindowOrigin(windowWidth, windowHeight, where);
00319 }
00320
00326 public static Point getWindowOrigin(Frame frame, int where) {
00327 return getCachedWindowOrigin(frame.getWidth(), frame.getHeight(), where);
00328 }
00329
00335 public static void initCachedWindowState() {
00336
00337 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
00338 cachedScreenWidth = (int) screenSize.getWidth();
00339 cachedScreenHeight = (int) screenSize.getHeight();
00340
00341
00342 GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
00343 desktopBounds = env.getMaximumWindowBounds();
00344 }
00345
00352 public static void main(String[] args) throws InterruptedException {
00353 JFrame f = new JFrame("Bob");
00354 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
00355 f.setSize(800, 600);
00356 f.setLocation(10, 10);
00357 f.setVisible(true);
00358 Thread.sleep(3000);
00359 enterFullScreenIfPossible(f);
00360 Thread.sleep(3000);
00361 exitFullScreen(f);
00362 }
00363
00372 public static JFrame openInJFrame(Container content, int width, int height) {
00373 return openInJFrame(content, width, height, content.getClass().getName(), Color.white);
00374 }
00375
00385 public static JFrame openInJFrame(Container content, int width, int height, String title) {
00386 return (openInJFrame(content, width, height, title, Color.white));
00387 }
00388
00397 public static JFrame openInJFrame(Container content, int width, int height, String title,
00398 Color bgColor) {
00399 return (openInJFrame(content, width, height, title, bgColor, true));
00400 }
00401
00413 public static JFrame openInJFrame(Container content, int width, int height, String title,
00414 Color bgColor, boolean exitOnClose) {
00415 JFrame frame = new JFrame(title);
00416 frame.setBackground(bgColor);
00417 content.setBackground(bgColor);
00418 frame.setSize(width, height);
00419 frame.setContentPane(content);
00420 if (exitOnClose) {
00421 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
00422 }
00423 frame.setVisible(true);
00424 return (frame);
00425 }
00426
00430 public static void setJavaLookAndFeel() {
00431 try {
00432 UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
00433 } catch (Exception e) {
00434 System.out.println("Error setting Java LAF: " + e);
00435 }
00436 }
00437
00441 public static void setMotifLookAndFeel() {
00442 try {
00443 UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
00444 } catch (Exception e) {
00445 System.out.println("Error setting Motif LAF: " + e);
00446 }
00447 }
00448
00452 public static void setNativeLookAndFeel() {
00453 try {
00454 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
00455 } catch (Exception e) {
00456 System.out.println("Error setting native LAF: " + e);
00457 }
00458 }
00459
00466 public static void fitToDesktop(final JFrame frame) {
00467 final int minWidth = (int) Math.min(frame.getBounds().getWidth(), desktopBounds.getWidth());
00468 final int minHeight = (int) Math.min(frame.getBounds().getHeight(), desktopBounds
00469 .getHeight());
00470
00471 frame.setSize(minWidth, minHeight);
00472 }
00473 }