]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui.workbench/src/org/simantics/utils/ui/workbench/FocusContextActivator.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.utils.ui.workbench / src / org / simantics / utils / ui / workbench / FocusContextActivator.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.utils.ui.workbench;
13
14 import org.eclipse.swt.events.FocusEvent;
15 import org.eclipse.swt.events.FocusListener;
16 import org.eclipse.swt.widgets.Control;
17 import org.eclipse.ui.IWorkbenchWindow;
18 import org.eclipse.ui.contexts.IContextActivation;
19 import org.eclipse.ui.contexts.IContextService;
20
21 public class FocusContextActivator {
22
23     private String             contextId;
24
25     private IContextActivation activation;
26
27     public FocusContextActivator(String contextId, final Control c, final IWorkbenchWindow window) {
28         this.contextId = contextId;
29
30         c.addFocusListener(new FocusListener() {
31             private IContextService getService() {
32                 return (IContextService) window.getService(IContextService.class);
33             }
34
35             @Override
36             public void focusGained(FocusEvent e) {
37                 IContextService contextService = getService();
38                 String ctx = FocusContextActivator.this.contextId;
39                 if (contextService != null && ctx != null)
40                     activation = contextService.activateContext(ctx);
41             }
42
43             @Override
44             public void focusLost(FocusEvent e) {
45                 IContextService contextService = getService();
46                 if (contextService != null && activation != null)
47                     contextService.deactivateContext(activation);
48             }
49         });
50     }
51
52 }