]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/chassis/SWTChassis.java
Let the request processor handle the exceptions
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / chassis / SWTChassis.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.Component;
15
16 import org.eclipse.swt.graphics.Color;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Display;
19 import org.eclipse.ui.IEditorPart;
20 import org.eclipse.ui.IWorkbenchPage;
21 import org.eclipse.ui.IWorkbenchWindow;
22 import org.simantics.g2d.canvas.ICanvasContext;
23 import org.simantics.g2d.event.adapter.SWTFocusAdapter;
24 import org.simantics.g2d.event.adapter.SWTKeyEventAdapter;
25 import org.simantics.g2d.event.adapter.SWTMouseEventAdapter;
26 import org.simantics.utils.datastructures.hints.IHintContext;
27 import org.simantics.utils.datastructures.hints.IHintContext.Key;
28 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
29 import org.simantics.utils.threads.IThreadWorkQueue;
30 import org.simantics.utils.ui.SWTAWTComponent;
31
32 /**
33  * SWT Composite based chassis
34  * 
35  * User must invoke CompositeChassis.syncPopulate() after constructions.
36  * 
37  * @author Toni Kalajainen
38  */
39 public class SWTChassis extends SWTAWTComponent implements ICanvasChassis {
40
41     protected AWTChassis                                                        component;
42     protected SWTMouseEventAdapter                                      mouseAdapter;
43     protected SWTKeyEventAdapter                                        keyAdapter;
44     protected SWTFocusAdapter                                           focusAdapter;
45
46     /**
47      * Tells the Composite where the canvas is installed.
48      */
49     public static final Key KEY_SWT_COMPOSITE = new KeyOf(Composite.class, "SWT_COMPOSITE");
50
51     public static final Key KEY_SWT_DISPLAY = new KeyOf(Display.class, "SWT_DISPLAY");
52
53     public static final Key KEY_WORKBENCHWINDOW = new KeyOf(IWorkbenchWindow.class, "WORKBENCH_WINDOW");
54     public static final Key KEY_WORKBENCHPAGE = new KeyOf(IWorkbenchPage.class, "WORKBENCH_PAGE");
55     public static final Key KEY_EDITORPART = new KeyOf(IEditorPart.class, "EDITORPART");
56
57     public SWTChassis(Composite parent, int style) {
58         super(parent, style);
59     }
60
61     @Override
62     protected void doDispose() {
63         if (component!=null)
64             component.fireChassisClosed();
65         super.doDispose();
66     }
67
68     @Override
69     protected Component createSwingComponent() {
70         component = new AWTChassis(false);
71         return component;
72     }
73
74     /**
75      * Run in SWT thread.
76      * 
77      * @param canvasContext new ICanvasContext to add or null to remove
78      */
79     @Override
80     public void setCanvasContext(ICanvasContext canvasContext) {
81         assert component != null;
82
83         // Unhook old context
84         if (component.canvasContext!=null) {
85             if (component.hintCtx != null) {
86                 component.hintCtx.removeHint( KEY_SWT_COMPOSITE );
87                 component.hintCtx.removeHint( KEY_SWT_DISPLAY );
88             }
89             // keyAdapter, mouseAdapter and focusAdapter may be null if
90             // the underlying AWTChassis has already had its CanvasContext set
91             // through other means than this method.
92             if (keyAdapter != null) {
93                 removeKeyListener(keyAdapter);
94                 removeMouseWheelListener(mouseAdapter);
95                 removeMouseMoveListener(mouseAdapter);
96                 removeMouseTrackListener(mouseAdapter);
97                 removeMouseListener(mouseAdapter);
98                 removeFocusListener(focusAdapter);
99             }
100         }
101         component.setCanvasContext(canvasContext);
102         // Hook new context
103         if (canvasContext!=null) {
104             if (component.hintCtx != null) {
105                 component.hintCtx.setHint( KEY_SWT_COMPOSITE, this);
106                 component.hintCtx.setHint( KEY_SWT_DISPLAY, this.getDisplay());
107             }
108             mouseAdapter = new SWTMouseEventAdapter(canvasContext, canvasContext.getEventQueue());
109             keyAdapter = new SWTKeyEventAdapter(canvasContext, canvasContext.getEventQueue());
110             focusAdapter = new SWTFocusAdapter(canvasContext, canvasContext.getEventQueue());
111             addKeyListener(keyAdapter);
112             addMouseWheelListener(mouseAdapter);
113             addMouseMoveListener(mouseAdapter);
114             addMouseTrackListener(mouseAdapter);
115             addMouseListener(mouseAdapter);
116             addFocusListener(focusAdapter);
117         }
118     }
119
120     @Override
121     public AWTChassis getAWTComponent() {
122         return component;
123     }
124
125     @Override
126     public ICanvasContext getCanvasContext() {
127         return component.canvasContext;
128     }
129
130     @Override
131     public void addChassisListener(IThreadWorkQueue thread, IChassisListener listener) {
132         component.addChassisListener(thread, listener);
133     }
134
135     @Override
136     public void removeChassisListener(IThreadWorkQueue thread, IChassisListener listener) {
137         component.removeChassisListener(thread, listener);
138     }
139
140     @Override
141     public void addChassisListener(IChassisListener listener) {
142         component.addChassisListener(listener);
143     }
144
145     @Override
146     public void removeChassisListener(IChassisListener listener) {
147         component.removeChassisListener(listener);
148     }
149
150     @Override
151     public IHintContext getHintContext() {
152         return component.hintCtx;
153     }
154
155     @Override
156     public void setBackground(Color color) {
157         super.setBackground(color);
158         if (component != null) {
159             java.awt.Color awtColor = null;
160             if (color != null)
161                 awtColor = new java.awt.Color(color.getRed(), color.getGreen(), color.getBlue());
162             component.setBackground(awtColor);
163         }
164     }
165
166 }