]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/WorkbenchSelectionInputSource.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / WorkbenchSelectionInputSource.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 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.browsing.ui.graph.impl;
13
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;
26
27 /**
28  * @author Antti Villberg
29  */
30 public class WorkbenchSelectionInputSource implements WorkbenchSessionContextInputSource, ObservableInputSource,
31         ISelectionListener, Disposable {
32
33     protected ISelectionService   service;
34     protected Object              selection;
35     protected IWorkbenchPart      part;
36     protected IWorkbenchPart      ownPart;
37     protected InputSourceListener listener;
38
39     @Override
40     public void init(IWorkbenchSite site, IWorkbenchPart ownPart) {
41         this.ownPart = ownPart;
42         attachToWorkbench();
43     }
44
45     /**
46      * @thread SWT
47      */
48     protected void attachToWorkbench() {
49         IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
50         if (window == null)
51             return;
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();
59         }
60         selectionChanged(activePart, initialSelection);
61         service.addPostSelectionListener(this);
62     }
63
64     /**
65      * @thread SWT
66      */
67     @Override
68     public void dispose() {
69         if (service != null)
70             service.removePostSelectionListener(this);
71         service = null;
72         selection = null;
73         listener = null;
74     }
75
76     /**
77      * @thread SWT
78      */
79     @Override
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;
85         this.part = part;
86         //System.err.println("WorkbenchSelectionInputSource.selectionChanged(" + part + ", " + this.selection + ")");
87         fireIfInputChanged(old, selection);
88     }
89
90     /**
91      * @param oldSelection
92      * @param newSelection
93      * @thread SWT
94      */
95     protected void fireIfInputChanged(Object oldSelection, Object newSelection) {
96         InputSourceListener l = listener;
97         if (l != null)
98             if (!ObjectUtils.objectEquals(oldSelection, newSelection))
99                 l.inputChanged(this);
100     }
101
102     @Override
103     public Object get(ISessionContext ctx) {
104         return selection != null ? selection : GraphExplorer.EMPTY_INPUT;
105     }
106     
107     @Override
108     public IWorkbenchPart getProvider() {
109         return this.part;
110     }
111
112     @Override
113     public void setListener(InputSourceListener listener) {
114         this.listener = listener;
115     }
116
117 }