]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.g2d/src/org/simantics/g2d/chassis/SWTChassis.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / chassis / SWTChassis.java
diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/chassis/SWTChassis.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/chassis/SWTChassis.java
new file mode 100644 (file)
index 0000000..2286131
--- /dev/null
@@ -0,0 +1,166 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.g2d.chassis;\r
+\r
+import java.awt.Component;\r
+\r
+import org.eclipse.swt.graphics.Color;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Display;\r
+import org.eclipse.ui.IEditorPart;\r
+import org.eclipse.ui.IWorkbenchPage;\r
+import org.eclipse.ui.IWorkbenchWindow;\r
+import org.simantics.g2d.canvas.ICanvasContext;\r
+import org.simantics.g2d.event.adapter.SWTFocusAdapter;\r
+import org.simantics.g2d.event.adapter.SWTKeyEventAdapter;\r
+import org.simantics.g2d.event.adapter.SWTMouseEventAdapter;\r
+import org.simantics.utils.datastructures.hints.IHintContext;\r
+import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
+import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;\r
+import org.simantics.utils.threads.IThreadWorkQueue;\r
+import org.simantics.utils.ui.SWTAWTComponent;\r
+\r
+/**\r
+ * SWT Composite based chassis\r
+ * \r
+ * User must invoke CompositeChassis.syncPopulate() after constructions.\r
+ * \r
+ * @author Toni Kalajainen\r
+ */\r
+public class SWTChassis extends SWTAWTComponent implements ICanvasChassis {\r
+\r
+    protected AWTChassis                                                       component;\r
+    protected SWTMouseEventAdapter                                     mouseAdapter;\r
+    protected SWTKeyEventAdapter                                       keyAdapter;\r
+    protected SWTFocusAdapter                                          focusAdapter;\r
+\r
+    /**\r
+     * Tells the Composite where the canvas is installed.\r
+     */\r
+    public static final Key KEY_SWT_COMPOSITE = new KeyOf(Composite.class, "SWT_COMPOSITE");\r
+\r
+    public static final Key KEY_SWT_DISPLAY = new KeyOf(Display.class, "SWT_DISPLAY");\r
+\r
+    public static final Key KEY_WORKBENCHWINDOW = new KeyOf(IWorkbenchWindow.class, "WORKBENCH_WINDOW");\r
+    public static final Key KEY_WORKBENCHPAGE = new KeyOf(IWorkbenchPage.class, "WORKBENCH_PAGE");\r
+    public static final Key KEY_EDITORPART = new KeyOf(IEditorPart.class, "EDITORPART");\r
+\r
+    public SWTChassis(Composite parent, int style) {\r
+        super(parent, style);\r
+    }\r
+\r
+    @Override\r
+    protected void doDispose() {\r
+        if (component!=null)\r
+            component.fireChassisClosed();\r
+        super.doDispose();\r
+    }\r
+\r
+    @Override\r
+    protected Component createSwingComponent() {\r
+        component = new AWTChassis(false);\r
+        return component;\r
+    }\r
+\r
+    /**\r
+     * Run in SWT thread.\r
+     * \r
+     * @param canvasContext new ICanvasContext to add or null to remove\r
+     */\r
+    @Override\r
+    public void setCanvasContext(ICanvasContext canvasContext) {\r
+        assert component != null;\r
+\r
+        // Unhook old context\r
+        if (component.canvasContext!=null) {\r
+            if (component.hintCtx != null) {\r
+                component.hintCtx.removeHint( KEY_SWT_COMPOSITE );\r
+                component.hintCtx.removeHint( KEY_SWT_DISPLAY );\r
+            }\r
+            // keyAdapter, mouseAdapter and focusAdapter may be null if\r
+            // the underlying AWTChassis has already had its CanvasContext set\r
+            // through other means than this method.\r
+            if (keyAdapter != null) {\r
+                removeKeyListener(keyAdapter);\r
+                removeMouseWheelListener(mouseAdapter);\r
+                removeMouseMoveListener(mouseAdapter);\r
+                removeMouseTrackListener(mouseAdapter);\r
+                removeMouseListener(mouseAdapter);\r
+                removeFocusListener(focusAdapter);\r
+            }\r
+        }\r
+        component.setCanvasContext(canvasContext);\r
+        // Hook new context\r
+        if (canvasContext!=null) {\r
+            if (component.hintCtx != null) {\r
+                component.hintCtx.setHint( KEY_SWT_COMPOSITE, this);\r
+                component.hintCtx.setHint( KEY_SWT_DISPLAY, this.getDisplay());\r
+            }\r
+            mouseAdapter = new SWTMouseEventAdapter(canvasContext, canvasContext.getEventQueue());\r
+            keyAdapter = new SWTKeyEventAdapter(canvasContext, canvasContext.getEventQueue());\r
+            focusAdapter = new SWTFocusAdapter(canvasContext, canvasContext.getEventQueue());\r
+            addKeyListener(keyAdapter);\r
+            addMouseWheelListener(mouseAdapter);\r
+            addMouseMoveListener(mouseAdapter);\r
+            addMouseTrackListener(mouseAdapter);\r
+            addMouseListener(mouseAdapter);\r
+            addFocusListener(focusAdapter);\r
+        }\r
+    }\r
+\r
+    @Override\r
+    public AWTChassis getAWTComponent() {\r
+        return component;\r
+    }\r
+\r
+    @Override\r
+    public ICanvasContext getCanvasContext() {\r
+        return component.canvasContext;\r
+    }\r
+\r
+    @Override\r
+    public void addChassisListener(IThreadWorkQueue thread, IChassisListener listener) {\r
+        component.addChassisListener(thread, listener);\r
+    }\r
+\r
+    @Override\r
+    public void removeChassisListener(IThreadWorkQueue thread, IChassisListener listener) {\r
+        component.removeChassisListener(thread, listener);\r
+    }\r
+\r
+    @Override\r
+    public void addChassisListener(IChassisListener listener) {\r
+        component.addChassisListener(listener);\r
+    }\r
+\r
+    @Override\r
+    public void removeChassisListener(IChassisListener listener) {\r
+        component.removeChassisListener(listener);\r
+    }\r
+\r
+    @Override\r
+    public IHintContext getHintContext() {\r
+        return component.hintCtx;\r
+    }\r
+\r
+    @Override\r
+    public void setBackground(Color color) {\r
+        super.setBackground(color);\r
+        if (component != null) {\r
+            java.awt.Color awtColor = null;\r
+            if (color != null)\r
+                awtColor = new java.awt.Color(color.getRed(), color.getGreen(), color.getBlue());\r
+            component.setBackground(awtColor);\r
+        }\r
+    }\r
+\r
+}\r