]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/participant/SGFocusParticipant.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / participant / SGFocusParticipant.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2013 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.diagram.participant;
13
14 import java.awt.event.FocusEvent;
15 import java.awt.event.FocusListener;
16
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;
22
23 /**
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.
27  * 
28  * This participant depends on the {@link ContextUtil} canvas participant.
29  * 
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. 
34  * 
35  * @see ContextUtil
36  * 
37  * @author J-P Laine
38  * @author Tuukka Lehtonen
39  * @author Teemu Lempinen
40  */
41 public class SGFocusParticipant extends AbstractCanvasParticipant implements FocusListener {
42
43     @Reference
44     ContextUtil                 contextUtil;
45     
46     @Reference
47     org.simantics.diagram.participant.e4.ContextUtil e4ContextUtil;
48
49     final SWTChassis            chassis;
50     String                      contextId;
51     ContextState                originalContextState;
52
53     public SGFocusParticipant(SWTChassis chassis, String contextId) {
54         super();
55         this.chassis = chassis;
56         this.contextId = contextId;
57     }
58
59     @Override
60     public void addedToContext(ICanvasContext ctx) {
61         super.addedToContext(ctx);
62         chassis.getAWTComponent().addFocusListener(this);
63     }
64
65     @Override
66     public void removedFromContext(ICanvasContext ctx) {
67         if (!chassis.isDisposed()) {
68             AWTChassis component = chassis.getAWTComponent();
69             if (component != null)
70                 component.removeFocusListener(this);
71         }
72         super.removedFromContext(ctx);
73     }
74
75     @Override
76     public void focusGained(FocusEvent e) {
77         ContextUtil util = contextUtil;
78         if (util != null) {
79             DiagramCommandBindings.activateBindings(util, contextId);
80         } else {
81             org.simantics.diagram.participant.e4.ContextUtil e4Util = e4ContextUtil;
82             if (e4Util != null) {
83                 DiagramCommandBindings.activateBindings(e4Util, contextId);
84             }
85         }
86     }
87
88     @Override
89     public void focusLost(FocusEvent e) {
90         ContextUtil util = contextUtil;
91         if (util != null) {
92             DiagramCommandBindings.deactivateBindings(util, contextId);
93         } else {
94             org.simantics.diagram.participant.e4.ContextUtil e4Util = e4ContextUtil;
95             if (e4Util != null) {
96                 DiagramCommandBindings.deactivateBindings(e4Util, contextId);
97             }
98         }
99     }
100
101 }