1 /*******************************************************************************
2 * Copyright (c) 2007, 2013 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.diagram.participant;
14 import java.awt.event.FocusEvent;
15 import java.awt.event.FocusListener;
17 import org.simantics.g2d.canvas.ICanvasContext;
18 import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;
19 import org.simantics.g2d.canvas.impl.DependencyReflection.Reference;
20 import org.simantics.g2d.chassis.AWTChassis;
21 import org.simantics.g2d.chassis.SWTChassis;
24 * The purpose of this participant is to properly handle contexts in Eclipse/SWT
25 * based deployments G2D canvases where it is possible that Eclipse eats input
26 * events based on UI contexts, key bindings, etc.
28 * This participant depends on the {@link ContextUtil} canvas participant.
30 * When some other scene graph component or UI Part, besides the root pane, gets focus,
31 * this participant will deactivate its {@link #contextId}. When the root pane gets the
32 * focus back, it will restore {@link #contextId} to allow Eclipse key bindings
33 * for that {@link #contextId} to work properly.
38 * @author Tuukka Lehtonen
39 * @author Teemu Lempinen
41 public class SGFocusParticipant extends AbstractCanvasParticipant implements FocusListener {
44 ContextUtil contextUtil;
47 org.simantics.diagram.participant.e4.ContextUtil e4ContextUtil;
49 final SWTChassis chassis;
51 ContextState originalContextState;
53 public SGFocusParticipant(SWTChassis chassis, String contextId) {
55 this.chassis = chassis;
56 this.contextId = contextId;
60 public void addedToContext(ICanvasContext ctx) {
61 super.addedToContext(ctx);
62 chassis.getAWTComponent().addFocusListener(this);
66 public void removedFromContext(ICanvasContext ctx) {
67 if (!chassis.isDisposed()) {
68 AWTChassis component = chassis.getAWTComponent();
69 if (component != null)
70 component.removeFocusListener(this);
72 super.removedFromContext(ctx);
76 public void focusGained(FocusEvent e) {
77 ContextUtil util = contextUtil;
79 DiagramCommandBindings.activateBindings(util, contextId);
81 org.simantics.diagram.participant.e4.ContextUtil e4Util = e4ContextUtil;
83 DiagramCommandBindings.activateBindings(e4Util, contextId);
89 public void focusLost(FocusEvent e) {
90 ContextUtil util = contextUtil;
92 DiagramCommandBindings.deactivateBindings(util, contextId);
94 org.simantics.diagram.participant.e4.ContextUtil e4Util = e4ContextUtil;
96 DiagramCommandBindings.deactivateBindings(e4Util, contextId);