1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.g2d.chassis;
14 import java.awt.BorderLayout;
15 import java.awt.DisplayMode;
16 import java.awt.Frame;
17 import java.awt.GraphicsDevice;
18 import java.awt.GraphicsEnvironment;
19 import java.awt.event.KeyAdapter;
20 import java.awt.event.KeyEvent;
22 import javax.swing.JFrame;
24 import org.eclipse.jface.viewers.ArrayContentProvider;
25 import org.eclipse.jface.viewers.LabelProvider;
26 import org.eclipse.jface.window.Window;
27 import org.eclipse.swt.graphics.Rectangle;
28 import org.eclipse.swt.widgets.Display;
29 import org.eclipse.swt.widgets.Monitor;
30 import org.eclipse.swt.widgets.Shell;
31 import org.eclipse.ui.dialogs.ListDialog;
32 import org.simantics.g2d.canvas.ICanvasContext;
33 import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;
34 import org.simantics.g2d.participant.KeyToCommand;
35 import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;
36 import org.simantics.scenegraph.g2d.events.command.Command;
37 import org.simantics.scenegraph.g2d.events.command.CommandEvent;
38 import org.simantics.scenegraph.g2d.events.command.CommandKeyBinding;
39 import org.simantics.scenegraph.g2d.events.command.Commands;
40 import org.simantics.utils.datastructures.cache.IProvider;
42 public class FullscreenUtils {
47 * Views a canvas in full screen mode
48 * @param monitor display device
49 * @param ctx canvas context
52 public static Frame viewFullScreen(GraphicsDevice monitor, ICanvasContext ctx)
54 DisplayMode dm = monitor.getDisplayMode();
55 final JFrame frame = new JFrame("Fullscreen mode");
56 frame.setSize(dm.getWidth(), dm.getHeight());
57 frame.setUndecorated(true);
59 // This is an empty content area in the frame
60 AWTChassis chassis = new AWTChassis();
61 // There is a background painter in canvas context (==it is opaque)
62 chassis.setOpaque(true);
64 chassis.addKeyListener(new KeyAdapter() {
66 public void keyPressed(KeyEvent e) {
67 if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
71 frame.getContentPane().add(chassis, BorderLayout.CENTER);
75 java.awt.Rectangle awtBounds = monitor.getDefaultConfiguration().getBounds();
76 frame.setBounds(awtBounds);
77 frame.setAlwaysOnTop(true);
78 frame.setVisible(true);
80 chassis.setCanvasContext(ctx);
85 * Adds Alt-Enter full screen handler to a canvas context
88 public static void addFullScreenHandler(ICanvasContext ctx, final Shell shell, final IProvider<ICanvasContext> fullscreenProvider)
91 KeyToCommand commands = ctx.getAtMostOneItemOfClass(KeyToCommand.class);
93 ctx.add( commands = new KeyToCommand() );
94 commands.addBinding( new CommandKeyBinding(Commands.FULL_SCREEN, "View in full screen mode", KeyEvent.VK_ENTER, KeyEvent.VK_CONTROL) );
95 ctx.add( new AbstractCanvasParticipant() {
96 @SuppressWarnings("unused")
97 @EventHandler(priority = 0)
98 public boolean handleEvent(CommandEvent e) {
100 Command c = e.command;
102 if (c.equals(Commands.FULL_SCREEN))
104 shell.getDisplay().asyncExec(new Runnable() {
107 GraphicsDevice gd = getOrQueryDisplay(shell);
109 viewFullScreen(gd, fullscreenProvider.get());
119 * Gets a single display or dialogs
121 * @return null or a display
123 public static GraphicsDevice getOrQueryDisplay(Shell shell)
125 GraphicsDevice list[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
126 if (list.length==0) return null;
127 if (list.length==1) return list[0];
128 return doDisplayDialog(shell);
132 * Dialog for choosing display device
135 public static GraphicsDevice doDisplayDialog(Shell shell)
137 ListDialog ld = new ListDialog(shell);
138 ld.setInput(GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices());
139 ld.setTitle("Select display device");
140 ld.setInitialSelections(new Object[] {GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()});
141 ld.setMessage("Select display device");
142 ld.setContentProvider(new ArrayContentProvider());
143 ld.setLabelProvider(new LabelProvider() {
145 public String getText(Object element) {
146 GraphicsDevice gd = (GraphicsDevice) element;
147 return gd.getIDstring()+" ("+gd.getDisplayMode().getWidth()+"*"+gd.getDisplayMode().getHeight()+")";
150 ld.setBlockOnOpen(true);
151 if (ld.open() != Window.OK) return null;
152 return (GraphicsDevice)ld.getResult()[0];
156 * Adds esc to close full screen view
159 @SuppressWarnings("unused")
160 private static void addCloseHandler(ICanvasContext ctx, final Frame frame)
163 KeyToCommand commands = ctx.getAtMostOneItemOfClass(KeyToCommand.class);
165 ctx.add( commands = new KeyToCommand() );
166 commands.addBinding( new CommandKeyBinding(Commands.CLOSE, "Close Canvas", KeyEvent.VK_ESCAPE) );
167 ctx.add( new AbstractCanvasParticipant() {
168 @EventHandler(priority = 0)
169 public boolean handleEvent(CommandEvent e) {
170 assertDependencies();
178 * Get the monitor the shell is mostly in
182 public static Monitor getMonitor(Shell shell)
184 Monitor result = null;
185 long largestArea = 0;
187 Rectangle shellRectangle = shell.getBounds();
188 for (Monitor monitor : Display.getCurrent().getMonitors())
190 Rectangle monitorBounds = monitor.getBounds();
191 Rectangle intersection = monitorBounds.intersection(shellRectangle);
192 long area = intersection.width * intersection.height;
193 if (area>largestArea) {
202 * Gets corresponding awt monitor for a swt monitor
206 public static GraphicsDevice getMonitorCorrespondence(Monitor swtMonitor)
208 Rectangle swtBounds = swtMonitor.getBounds();
210 for (GraphicsDevice gd : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices())
212 if (gd.getType() != GraphicsDevice.TYPE_RASTER_SCREEN) continue;
213 java.awt.Rectangle awtBounds = gd.getDefaultConfiguration().getBounds();
214 if (awtBounds.x == swtBounds.x && awtBounds.y == swtBounds.y && awtBounds.width == swtBounds.width && awtBounds.height == swtBounds.height)