001: /*
002: * $Id: FullScreenWindow.java,v 1.3 2007/12/20 18:33:33 rbair Exp $
003: *
004: * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005: * Santa Clara, California 95054, U.S.A. All rights reserved.
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
020: */
021:
022: package com.sun.pdfview;
023:
024: import java.awt.Color;
025: import java.awt.Dimension;
026: import java.awt.DisplayMode;
027: import java.awt.GraphicsConfiguration;
028: import java.awt.GraphicsDevice;
029: import java.awt.GraphicsEnvironment;
030: import java.awt.Rectangle;
031: import java.awt.event.ActionEvent;
032: import java.awt.event.ActionListener;
033:
034: import javax.swing.JButton;
035: import javax.swing.JComponent;
036: import javax.swing.JFrame;
037:
038: /**
039: * A window that takes over the full screen. You can put exactly one
040: * JComponent into the window. If there are multiple screens attached
041: * to the computer, this class will display buttons on each screen so
042: * that the user can select which one receives the full-screen window.
043: */
044: public class FullScreenWindow {
045: /**
046: * The screen that the user last chose for displaying a
047: * FullScreenWindow
048: */
049: private static GraphicsDevice defaultScreen;
050:
051: /** The current screen for the FullScreenWindow */
052: private GraphicsDevice screen;
053:
054: /** The JFrame filling the screen */
055: private JFrame jf;
056:
057: /**
058: * Whether this FullScreenWindow has been used. Each FullScreenWindow
059: * can only be displayed once.
060: */
061: private boolean dead = false;
062:
063: /**
064: * Create a full screen window containing a JComponent, and ask the
065: * user which screen they'd like to use if more than one is present.
066: * @param part the JComponent to display
067: * @param forcechoice true if you want force the display of the screen
068: * choice buttons. If false, buttons will only display if the user
069: * hasn't previously picked a screen.
070: */
071: public FullScreenWindow(JComponent part, boolean forcechoice) {
072: // super();
073: init(part, forcechoice);
074: }
075:
076: /**
077: * Create a full screen window containing a JComponent. The user
078: * will only be asked which screen to display on if there are multiple
079: * monitors attached and the user hasn't already made a choice.
080: * @param part the JComponent to display
081: */
082: public FullScreenWindow(JComponent part) {
083: // super();
084: init(part, false);
085: }
086:
087: /**
088: * Close the full screen window. This particular FullScreenWindow
089: * object cannot be used again.
090: */
091: public void close() {
092: dead = true;
093: flag.set();
094: screen.setFullScreenWindow(null);
095: if (jf != null) {
096: jf.dispose();
097: }
098: }
099:
100: /**
101: * Create the window, asking for which screen to use if there are
102: * multiple monitors and either forcechoice is true, or the user
103: * hasn't already picked a screen.
104: * @param part the JComponent to display
105: * @param forcechoice false if user shouldn't be asked twice which
106: * of several monitors to use.
107: */
108: private void init(JComponent part, boolean forcechoice) {
109: if (forcechoice) {
110: defaultScreen = null;
111: }
112: screen = null;
113:
114: GraphicsEnvironment ge = GraphicsEnvironment
115: .getLocalGraphicsEnvironment();
116: GraphicsDevice screens[] = ge.getScreenDevices();
117: if (defaultScreen != null) {
118: for (int i = 0; i < screens.length; i++) {
119: if (screens[i] == defaultScreen) {
120: screen = defaultScreen;
121: }
122: }
123: }
124:
125: if (screens.length == 1) {
126: screen = screens[0];
127: }
128: if (screen == null) {
129: screen = pickScreen(screens);
130: }
131: if (dead) {
132: return;
133: }
134: defaultScreen = screen;
135: DisplayMode dm = screen.getDisplayMode();
136: GraphicsConfiguration gc = screen.getDefaultConfiguration();
137: jf = new JFrame(gc);
138: jf.setUndecorated(true);
139: jf.setBounds(gc.getBounds());
140: jf.getContentPane().add(part);
141: jf.show();
142: screen.setFullScreenWindow(jf);
143: }
144:
145: /**
146: * A button that appears on a particular graphics device, asking
147: * whether that device should be used for multiple-monitor choices.
148: */
149: class PickMe extends JFrame {
150: GraphicsDevice mygd;
151:
152: /**
153: * Creates the PickMe button on a particular display.
154: * @param gd the GraphicsDevice (display) to use for this button
155: */
156: public PickMe(GraphicsDevice gd) {
157: super (gd.getDefaultConfiguration());
158: // super((java.awt.Frame)null, false);
159: setUndecorated(true);
160: mygd = gd;
161: JButton jb = new JButton("Click here to use this screen");
162: jb.setBackground(Color.yellow);
163: jb.addActionListener(new ActionListener() {
164: public void actionPerformed(ActionEvent evt) {
165: pickDevice(mygd);
166: }
167: });
168: Dimension sz = jb.getPreferredSize();
169: sz.width += 30;
170: sz.height = 200;
171: jb.setPreferredSize(sz);
172: getContentPane().add(jb);
173: pack();
174: Rectangle bounds = gd.getDefaultConfiguration().getBounds();
175: int x = bounds.width / 2 - sz.width / 2 + bounds.x;
176: int y = bounds.height / 2 - sz.height / 2 + bounds.y;
177: // System.out.println("Opening picker at "+x+","+y);
178: setLocation(x, y);
179: show();
180: }
181: }
182:
183: /**
184: * Flag indicating whether the user has selected a screen or not.
185: */
186: private Flag flag = new Flag();
187: private GraphicsDevice pickedDevice;
188:
189: /**
190: * Select a particular screen for display of this window, and set
191: * the flag.
192: */
193: private void pickDevice(GraphicsDevice gd) {
194: pickedDevice = gd;
195: flag.set();
196: }
197:
198: /**
199: * Displays a button on each attached monitor, and returns the
200: * GraphicsDevice object associated with that monitor.
201: * @param scrns a list of GraphicsDevices on which to display buttons
202: * @return the GraphicsDevice selected.
203: */
204: private GraphicsDevice pickScreen(GraphicsDevice scrns[]) {
205: flag.clear();
206: int count = 0;
207: PickMe pickers[] = new PickMe[scrns.length];
208: for (int i = 0; i < scrns.length; i++) {
209: if (scrns[i].isFullScreenSupported()) {
210: count++;
211: }
212: pickers[i] = new PickMe(scrns[i]);
213: }
214: flag.waitForFlag();
215: for (int i = 0; i < pickers.length; i++) {
216: if (pickers[i] != null) {
217: pickers[i].dispose();
218: }
219: }
220: return pickedDevice;
221: }
222: }
|