]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/chassis/FullscreenUtils.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / chassis / FullscreenUtils.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.g2d.chassis;\r
13 \r
14 import java.awt.BorderLayout;\r
15 import java.awt.DisplayMode;\r
16 import java.awt.Frame;\r
17 import java.awt.GraphicsDevice;\r
18 import java.awt.GraphicsEnvironment;\r
19 import java.awt.event.KeyAdapter;\r
20 import java.awt.event.KeyEvent;\r
21 \r
22 import javax.swing.JFrame;\r
23 \r
24 import org.eclipse.jface.viewers.ArrayContentProvider;\r
25 import org.eclipse.jface.viewers.LabelProvider;\r
26 import org.eclipse.jface.window.Window;\r
27 import org.eclipse.swt.graphics.Rectangle;\r
28 import org.eclipse.swt.widgets.Display;\r
29 import org.eclipse.swt.widgets.Monitor;\r
30 import org.eclipse.swt.widgets.Shell;\r
31 import org.eclipse.ui.dialogs.ListDialog;\r
32 import org.simantics.g2d.canvas.ICanvasContext;\r
33 import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;\r
34 import org.simantics.g2d.participant.KeyToCommand;\r
35 import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;\r
36 import org.simantics.scenegraph.g2d.events.command.Command;\r
37 import org.simantics.scenegraph.g2d.events.command.CommandEvent;\r
38 import org.simantics.scenegraph.g2d.events.command.CommandKeyBinding;\r
39 import org.simantics.scenegraph.g2d.events.command.Commands;\r
40 import org.simantics.utils.datastructures.cache.IProvider;\r
41 \r
42 public class FullscreenUtils {\r
43 \r
44  \r
45         \r
46     /**\r
47      * Views a canvas in full screen mode\r
48      * @param monitor display device\r
49      * @param ctx canvas context\r
50      * @return\r
51      */\r
52         public static Frame viewFullScreen(GraphicsDevice monitor, ICanvasContext ctx)\r
53         {\r
54         DisplayMode dm = monitor.getDisplayMode();\r
55         final JFrame frame = new JFrame("Fullscreen mode");\r
56         frame.setSize(dm.getWidth(), dm.getHeight());\r
57         frame.setUndecorated(true);\r
58                 \r
59                 // This is an empty content area in the frame\r
60                 AWTChassis chassis = new AWTChassis();          \r
61                 // There is a background painter in canvas context (==it is opaque)\r
62                 chassis.setOpaque(true);\r
63                 // Esc listener\r
64                 chassis.addKeyListener(new KeyAdapter() {\r
65                         @Override\r
66                         public void keyPressed(KeyEvent e) {\r
67                                 if (e.getKeyCode() == KeyEvent.VK_ESCAPE)                                       \r
68                                         frame.dispose();\r
69                         }});\r
70                 \r
71                 frame.getContentPane().add(chassis, BorderLayout.CENTER);\r
72                 frame.pack();           \r
73 \r
74         frame.invalidate();\r
75         java.awt.Rectangle awtBounds         = monitor.getDefaultConfiguration().getBounds();\r
76         frame.setBounds(awtBounds);\r
77         frame.setAlwaysOnTop(true);\r
78                 frame.setVisible(true);         \r
79                 \r
80                 chassis.setCanvasContext(ctx);\r
81                 return frame;\r
82         }\r
83         \r
84         /**\r
85          * Adds Alt-Enter full screen handler to a canvas context\r
86          * @param ctx\r
87          */\r
88     public static void addFullScreenHandler(ICanvasContext ctx, final Shell shell, final IProvider<ICanvasContext> fullscreenProvider)\r
89     {\r
90         // Key bindings\r
91         KeyToCommand commands = ctx.getAtMostOneItemOfClass(KeyToCommand.class);\r
92         if (commands==null)\r
93                 ctx.add( commands = new KeyToCommand() );\r
94         commands.addBinding( new CommandKeyBinding(Commands.FULL_SCREEN, "View in full screen mode", KeyEvent.VK_ENTER, KeyEvent.VK_CONTROL) );\r
95         ctx.add( new AbstractCanvasParticipant() {\r
96                 @SuppressWarnings("unused")\r
97             @EventHandler(priority = 0)\r
98                 public boolean handleEvent(CommandEvent e) {\r
99                         assertDependencies();\r
100                         Command c = e.command;\r
101                         // Arrow key panning\r
102                         if (c.equals(Commands.FULL_SCREEN))\r
103                         {\r
104                                 shell.getDisplay().asyncExec(new Runnable() {\r
105                                                 @Override\r
106                                                 public void run() {\r
107                                                 GraphicsDevice gd = getOrQueryDisplay(shell);\r
108                                                 if (gd!=null) {\r
109                                                         viewFullScreen(gd, fullscreenProvider.get());\r
110                                                 }}});\r
111                                 return true;\r
112                         }\r
113                         return false;\r
114                 }       \r
115         });\r
116     }   \r
117 \r
118     /**\r
119      * Gets a single display or dialogs\r
120      * @param shell\r
121      * @return null or a display\r
122      */\r
123     public static GraphicsDevice getOrQueryDisplay(Shell shell)\r
124     {\r
125         GraphicsDevice list[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();\r
126         if (list.length==0) return null;\r
127         if (list.length==1) return list[0];\r
128         return doDisplayDialog(shell);\r
129     }\r
130     \r
131     /**\r
132      * Dialog for choosing display device\r
133      * @return\r
134      */\r
135     public static GraphicsDevice doDisplayDialog(Shell shell)\r
136     {\r
137         ListDialog ld = new ListDialog(shell);\r
138         ld.setInput(GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices());\r
139         ld.setTitle("Select display device");\r
140         ld.setInitialSelections(new Object[] {GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()});\r
141         ld.setMessage("Select display device");\r
142         ld.setContentProvider(new ArrayContentProvider());\r
143         ld.setLabelProvider(new LabelProvider() {\r
144                 @Override\r
145                 public String getText(Object element) {\r
146                         GraphicsDevice gd = (GraphicsDevice) element;\r
147                         return gd.getIDstring()+" ("+gd.getDisplayMode().getWidth()+"*"+gd.getDisplayMode().getHeight()+")";\r
148                 }               \r
149         });\r
150         ld.setBlockOnOpen(true);\r
151         if (ld.open() != Window.OK) return null;\r
152         return (GraphicsDevice)ld.getResult()[0];\r
153     }\r
154         \r
155         /**\r
156          * Adds esc to close full screen view\r
157          * @param ctx\r
158          */\r
159     @SuppressWarnings("unused")\r
160     private static void addCloseHandler(ICanvasContext ctx, final Frame frame)\r
161     {\r
162         // Key bindings\r
163         KeyToCommand commands = ctx.getAtMostOneItemOfClass(KeyToCommand.class);\r
164         if (commands==null)\r
165                 ctx.add( commands = new KeyToCommand() );\r
166         commands.addBinding( new CommandKeyBinding(Commands.CLOSE, "Close Canvas", KeyEvent.VK_ESCAPE) );\r
167         ctx.add( new AbstractCanvasParticipant() {\r
168                 @EventHandler(priority = 0)\r
169                 public boolean handleEvent(CommandEvent e) {\r
170                         assertDependencies();\r
171                         frame.dispose();\r
172                         return true;\r
173                 }       \r
174         });\r
175     }       \r
176     \r
177     /**\r
178      * Get the monitor the shell is mostly in\r
179      * @param shell\r
180      * @return\r
181      */\r
182     public static Monitor getMonitor(Shell shell)\r
183     {\r
184         Monitor result = null;\r
185         long largestArea = 0;\r
186                \r
187         Rectangle shellRectangle = shell.getBounds();\r
188         for (Monitor monitor : Display.getCurrent().getMonitors())\r
189         {\r
190             Rectangle monitorBounds = monitor.getBounds();\r
191             Rectangle intersection = monitorBounds.intersection(shellRectangle);\r
192             long area = intersection.width * intersection.height;\r
193             if (area>largestArea) {\r
194                 largestArea = area;\r
195                 result = monitor;\r
196             }\r
197         }\r
198         return result;        \r
199     }   \r
200         \r
201     /**\r
202      * Gets corresponding awt monitor for a swt monitor \r
203      * @param swtMonitor\r
204      * @return\r
205      */\r
206     public static GraphicsDevice getMonitorCorrespondence(Monitor swtMonitor)\r
207     {\r
208         Rectangle swtBounds = swtMonitor.getBounds();\r
209 \r
210         for (GraphicsDevice gd : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices())\r
211         {\r
212             if (gd.getType() != GraphicsDevice.TYPE_RASTER_SCREEN) continue;\r
213             java.awt.Rectangle awtBounds         = gd.getDefaultConfiguration().getBounds();\r
214             if (awtBounds.x == swtBounds.x && awtBounds.y == swtBounds.y && awtBounds.width == swtBounds.width && awtBounds.height == swtBounds.height)\r
215                 return gd;\r
216         }\r
217         return null;\r
218     }\r
219     \r
220 }\r