1 /*******************************************************************************
2 * Copyright (c) 2007, 2011 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.browsing.ui.graph.impl;
14 import org.eclipse.jface.viewers.ISelection;
15 import org.eclipse.ui.ISelectionListener;
16 import org.eclipse.ui.ISelectionService;
17 import org.eclipse.ui.IWorkbenchPage;
18 import org.eclipse.ui.IWorkbenchPart;
19 import org.eclipse.ui.IWorkbenchSite;
20 import org.eclipse.ui.IWorkbenchWindow;
21 import org.eclipse.ui.PlatformUI;
22 import org.simantics.browsing.ui.GraphExplorer;
23 import org.simantics.db.Disposable;
24 import org.simantics.db.management.ISessionContext;
25 import org.simantics.utils.ObjectUtils;
28 * @author Antti Villberg
30 public class WorkbenchSelectionInputSource implements WorkbenchSessionContextInputSource, ObservableInputSource,
31 ISelectionListener, Disposable {
33 protected ISelectionService service;
34 protected Object selection;
35 protected IWorkbenchPart part;
36 protected IWorkbenchPart ownPart;
37 protected InputSourceListener listener;
40 public void init(IWorkbenchSite site, IWorkbenchPart ownPart) {
41 this.ownPart = ownPart;
48 protected void attachToWorkbench() {
49 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
52 service = window.getSelectionService();
53 ISelection initialSelection = service.getSelection();
54 IWorkbenchPart activePart = null;
55 if (initialSelection != null) {
56 IWorkbenchPage activePage = window.getActivePage();
57 if (activePage != null)
58 activePart = activePage.getActivePart();
60 selectionChanged(activePart, initialSelection);
61 service.addPostSelectionListener(this);
68 public void dispose() {
70 service.removePostSelectionListener(this);
80 public void selectionChanged(IWorkbenchPart part, ISelection selection) {
81 // Do not process selections from self
82 if(ObjectUtils.objectEquals(ownPart, part)) return;
83 Object old = this.selection;
84 this.selection = selection;
86 //System.err.println("WorkbenchSelectionInputSource.selectionChanged(" + part + ", " + this.selection + ")");
87 fireIfInputChanged(old, selection);
95 protected void fireIfInputChanged(Object oldSelection, Object newSelection) {
96 InputSourceListener l = listener;
98 if (!ObjectUtils.objectEquals(oldSelection, newSelection))
103 public Object get(ISessionContext ctx) {
104 return selection != null ? selection : GraphExplorer.EMPTY_INPUT;
108 public IWorkbenchPart getProvider() {
113 public void setListener(InputSourceListener listener) {
114 this.listener = listener;