]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/chassis/FullscreenUtils.java
isImmutable can NPE
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / chassis / FullscreenUtils.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.g2d.chassis;
13
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;
21
22 import javax.swing.JFrame;
23
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;
41
42 public class FullscreenUtils {
43
44  
45         
46     /**
47      * Views a canvas in full screen mode
48      * @param monitor display device
49      * @param ctx canvas context
50      * @return
51      */
52         public static Frame viewFullScreen(GraphicsDevice monitor, ICanvasContext ctx)
53         {
54         DisplayMode dm = monitor.getDisplayMode();
55         final JFrame frame = new JFrame("Fullscreen mode");
56         frame.setSize(dm.getWidth(), dm.getHeight());
57         frame.setUndecorated(true);
58                 
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);
63                 // Esc listener
64                 chassis.addKeyListener(new KeyAdapter() {
65                         @Override
66                         public void keyPressed(KeyEvent e) {
67                                 if (e.getKeyCode() == KeyEvent.VK_ESCAPE)                                       
68                                         frame.dispose();
69                         }});
70                 
71                 frame.getContentPane().add(chassis, BorderLayout.CENTER);
72                 frame.pack();           
73
74         frame.invalidate();
75         java.awt.Rectangle awtBounds         = monitor.getDefaultConfiguration().getBounds();
76         frame.setBounds(awtBounds);
77         frame.setAlwaysOnTop(true);
78                 frame.setVisible(true);         
79                 
80                 chassis.setCanvasContext(ctx);
81                 return frame;
82         }
83         
84         /**
85          * Adds Alt-Enter full screen handler to a canvas context
86          * @param ctx
87          */
88     public static void addFullScreenHandler(ICanvasContext ctx, final Shell shell, final IProvider<ICanvasContext> fullscreenProvider)
89     {
90         // Key bindings
91         KeyToCommand commands = ctx.getAtMostOneItemOfClass(KeyToCommand.class);
92         if (commands==null)
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) {
99                         assertDependencies();
100                         Command c = e.command;
101                         // Arrow key panning
102                         if (c.equals(Commands.FULL_SCREEN))
103                         {
104                                 shell.getDisplay().asyncExec(new Runnable() {
105                                                 @Override
106                                                 public void run() {
107                                                 GraphicsDevice gd = getOrQueryDisplay(shell);
108                                                 if (gd!=null) {
109                                                         viewFullScreen(gd, fullscreenProvider.get());
110                                                 }}});
111                                 return true;
112                         }
113                         return false;
114                 }       
115         });
116     }   
117
118     /**
119      * Gets a single display or dialogs
120      * @param shell
121      * @return null or a display
122      */
123     public static GraphicsDevice getOrQueryDisplay(Shell shell)
124     {
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);
129     }
130     
131     /**
132      * Dialog for choosing display device
133      * @return
134      */
135     public static GraphicsDevice doDisplayDialog(Shell shell)
136     {
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() {
144                 @Override
145                 public String getText(Object element) {
146                         GraphicsDevice gd = (GraphicsDevice) element;
147                         return gd.getIDstring()+" ("+gd.getDisplayMode().getWidth()+"*"+gd.getDisplayMode().getHeight()+")";
148                 }               
149         });
150         ld.setBlockOnOpen(true);
151         if (ld.open() != Window.OK) return null;
152         return (GraphicsDevice)ld.getResult()[0];
153     }
154         
155         /**
156          * Adds esc to close full screen view
157          * @param ctx
158          */
159     @SuppressWarnings("unused")
160     private static void addCloseHandler(ICanvasContext ctx, final Frame frame)
161     {
162         // Key bindings
163         KeyToCommand commands = ctx.getAtMostOneItemOfClass(KeyToCommand.class);
164         if (commands==null)
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();
171                         frame.dispose();
172                         return true;
173                 }       
174         });
175     }       
176     
177     /**
178      * Get the monitor the shell is mostly in
179      * @param shell
180      * @return
181      */
182     public static Monitor getMonitor(Shell shell)
183     {
184         Monitor result = null;
185         long largestArea = 0;
186                
187         Rectangle shellRectangle = shell.getBounds();
188         for (Monitor monitor : Display.getCurrent().getMonitors())
189         {
190             Rectangle monitorBounds = monitor.getBounds();
191             Rectangle intersection = monitorBounds.intersection(shellRectangle);
192             long area = intersection.width * intersection.height;
193             if (area>largestArea) {
194                 largestArea = area;
195                 result = monitor;
196             }
197         }
198         return result;        
199     }   
200         
201     /**
202      * Gets corresponding awt monitor for a swt monitor 
203      * @param swtMonitor
204      * @return
205      */
206     public static GraphicsDevice getMonitorCorrespondence(Monitor swtMonitor)
207     {
208         Rectangle swtBounds = swtMonitor.getBounds();
209
210         for (GraphicsDevice gd : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices())
211         {
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)
215                 return gd;
216         }
217         return null;
218     }
219     
220 }