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.Component;
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;
33 * SWT Composite based chassis
35 * User must invoke CompositeChassis.syncPopulate() after constructions.
37 * @author Toni Kalajainen
39 public class SWTChassis extends SWTAWTComponent implements ICanvasChassis {
41 protected AWTChassis component;
42 protected SWTMouseEventAdapter mouseAdapter;
43 protected SWTKeyEventAdapter keyAdapter;
44 protected SWTFocusAdapter focusAdapter;
47 * Tells the Composite where the canvas is installed.
49 public static final Key KEY_SWT_COMPOSITE = new KeyOf(Composite.class, "SWT_COMPOSITE");
51 public static final Key KEY_SWT_DISPLAY = new KeyOf(Display.class, "SWT_DISPLAY");
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");
57 public SWTChassis(Composite parent, int style) {
62 protected void doDispose() {
64 component.fireChassisClosed();
69 protected Component createSwingComponent() {
70 component = new AWTChassis(false);
77 * @param canvasContext new ICanvasContext to add or null to remove
80 public void setCanvasContext(ICanvasContext canvasContext) {
81 assert component != null;
84 if (component.canvasContext!=null) {
85 if (component.hintCtx != null) {
86 component.hintCtx.removeHint( KEY_SWT_COMPOSITE );
87 component.hintCtx.removeHint( KEY_SWT_DISPLAY );
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);
101 component.setCanvasContext(canvasContext);
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());
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);
121 public AWTChassis getAWTComponent() {
126 public ICanvasContext getCanvasContext() {
127 return component.canvasContext;
131 public void addChassisListener(IThreadWorkQueue thread, IChassisListener listener) {
132 component.addChassisListener(thread, listener);
136 public void removeChassisListener(IThreadWorkQueue thread, IChassisListener listener) {
137 component.removeChassisListener(thread, listener);
141 public void addChassisListener(IChassisListener listener) {
142 component.addChassisListener(listener);
146 public void removeChassisListener(IChassisListener listener) {
147 component.removeChassisListener(listener);
151 public IHintContext getHintContext() {
152 return component.hintCtx;
156 public void setBackground(Color color) {
157 super.setBackground(color);
158 if (component != null) {
159 java.awt.Color awtColor = null;
161 awtColor = new java.awt.Color(color.getRed(), color.getGreen(), color.getBlue());
162 component.setBackground(awtColor);