File: FullScreenWindow.java

package info (click to toggle)
libpdfrenderer-java 0~20080829-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,432 kB
  • ctags: 2,198
  • sloc: java: 15,383; xml: 1,065; makefile: 16
file content (223 lines) | stat: -rw-r--r-- 6,673 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
 * $Id: FullScreenWindow.java,v 1.3 2007/12/20 18:33:33 rbair Exp $
 *
 * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
 * Santa Clara, California 95054, U.S.A. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

package com.sun.pdfview;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.DisplayMode;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;

/**
 * A window that takes over the full screen.  You can put exactly one
 * JComponent into the window.  If there are multiple screens attached
 * to the computer, this class will display buttons on each screen so
 * that the user can select which one receives the full-screen window.
 */
public class FullScreenWindow
{
    /**
     * The screen that the user last chose for displaying a
     * FullScreenWindow
     */
    private static GraphicsDevice defaultScreen;

    /** The current screen for the FullScreenWindow */
    private GraphicsDevice screen;

    /** The JFrame filling the screen */
    private JFrame jf;

    /**
     * Whether this FullScreenWindow has been used.  Each FullScreenWindow
     * can only be displayed once.
     */
    private boolean dead= false;

    /**
     * Create a full screen window containing a JComponent, and ask the
     * user which screen they'd like to use if more than one is present.
     * @param part the JComponent to display
     * @param forcechoice true if you want force the display of the screen
     * choice buttons.  If false, buttons will only display if the user
     * hasn't previously picked a screen.
     */
    public FullScreenWindow(JComponent part, boolean forcechoice) {
	//	super();
	init(part, forcechoice);
    }

    /**
     * Create a full screen window containing a JComponent.  The user
     * will only be asked which screen to display on if there are multiple
     * monitors attached and the user hasn't already made a choice.
     * @param part the JComponent to display
     */
    public FullScreenWindow(JComponent part) {
	//	super();
	init(part, false);
    }

    /**
     * Close the full screen window.  This particular FullScreenWindow
     * object cannot be used again.
     */
    public void close() {
	dead= true;
	flag.set();
	screen.setFullScreenWindow(null);
	if (jf!=null) {
	    jf.dispose();
	}
    }

    /**
     * Create the window, asking for which screen to use if there are
     * multiple monitors and either forcechoice is true, or the user
     * hasn't already picked a screen.
     * @param part the JComponent to display
     * @param forcechoice false if user shouldn't be asked twice which
     * of several monitors to use.
     */
    private void init(JComponent part, boolean forcechoice) {
	if (forcechoice) {
	    defaultScreen= null;
	}
	screen= null;

	GraphicsEnvironment ge=
	    GraphicsEnvironment.getLocalGraphicsEnvironment();
	GraphicsDevice screens[]= ge.getScreenDevices();
	if (defaultScreen!=null) {
	    for (int i=0; i<screens.length; i++) {
		if (screens[i]== defaultScreen) {
		    screen= defaultScreen;
		}
	    }
	}

	if (screens.length==1) {
	    screen= screens[0];
	}
	if (screen==null) {
	    screen= pickScreen(screens);
	}
	if (dead) {
	    return;
	}
	defaultScreen= screen;
	DisplayMode dm= screen.getDisplayMode();
	GraphicsConfiguration gc= screen.getDefaultConfiguration();
	jf= new JFrame(gc);
	jf.setUndecorated(true);
	jf.setBounds(gc.getBounds());
	jf.getContentPane().add(part);
	jf.show();
	screen.setFullScreenWindow(jf);
    }

    /**
     * A button that appears on a particular graphics device, asking
     * whether that device should be used for multiple-monitor choices.
     */
    class PickMe extends JFrame {
	GraphicsDevice mygd;

	/**
	 * Creates the PickMe button on a particular display.
	 * @param gd the GraphicsDevice (display) to use for this button
	 */
	public PickMe(GraphicsDevice gd) {
	    super(gd.getDefaultConfiguration());
	    //	    super((java.awt.Frame)null, false);
	    setUndecorated(true);
	    mygd= gd;
	    JButton jb= new JButton("Click here to use this screen");
	    jb.setBackground(Color.yellow);
	    jb.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent evt) {
			pickDevice(mygd);
		    }
		});
	    Dimension sz= jb.getPreferredSize();
	    sz.width+= 30;
	    sz.height= 200;
	    jb.setPreferredSize(sz);
	    getContentPane().add(jb);
	    pack();
	    Rectangle bounds= gd.getDefaultConfiguration().getBounds();
	    int x= bounds.width/2-sz.width/2+bounds.x;
	    int y= bounds.height/2-sz.height/2+bounds.y;
//	    System.out.println("Opening picker at "+x+","+y);
	    setLocation(x,y);	    
	    show();
	}
    }

    /**
     * Flag indicating whether the user has selected a screen or not.
     */
    private Flag flag= new Flag();
    private GraphicsDevice pickedDevice;

    /**
     * Select a particular screen for display of this window, and set
     * the flag.
     */
    private void pickDevice(GraphicsDevice gd) {
	pickedDevice= gd;
	flag.set();
    }

    /**
     * Displays a button on each attached monitor, and returns the
     * GraphicsDevice object associated with that monitor.
     * @param scrns a list of GraphicsDevices on which to display buttons
     * @return the GraphicsDevice selected.
     */
    private GraphicsDevice pickScreen(GraphicsDevice scrns[]) {
	flag.clear();
	int count=0;
	PickMe pickers[]= new PickMe[scrns.length];
	for (int i=0; i<scrns.length; i++) {
	    if (scrns[i].isFullScreenSupported()) {
		count++;
	    }
	    pickers[i]= new PickMe(scrns[i]);
	}
	flag.waitForFlag();
	for (int i=0; i<pickers.length; i++) {
	    if (pickers[i]!=null) {
		pickers[i].dispose();
	    }
	}
	return pickedDevice;
    }
}