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.utils.ui.workbench;
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;
21 public class FocusContextActivator {
23 private String contextId;
25 private IContextActivation activation;
27 public FocusContextActivator(String contextId, final Control c, final IWorkbenchWindow window) {
28 this.contextId = contextId;
30 c.addFocusListener(new FocusListener() {
31 private IContextService getService() {
32 return (IContextService) window.getService(IContextService.class);
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);
44 public void focusLost(FocusEvent e) {
45 IContextService contextService = getService();
46 if (contextService != null && activation != null)
47 contextService.deactivateContext(activation);