]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/DefaultExplorerSelectionListener.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / DefaultExplorerSelectionListener.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.browsing.ui.swt;
13
14 import org.eclipse.jface.viewers.IPostSelectionProvider;
15 import org.eclipse.jface.viewers.ISelection;
16 import org.eclipse.ui.ISelectionListener;
17 import org.eclipse.ui.IWorkbenchPart;
18 import org.simantics.browsing.ui.GraphExplorer;
19
20 public class DefaultExplorerSelectionListener implements ISelectionListener {
21
22     private static final boolean DEBUG = false;
23
24     IWorkbenchPart           owner;
25     GraphExplorer            explorer;
26     WorkbenchSelectionFilter filter;
27
28     public DefaultExplorerSelectionListener(IWorkbenchPart owner, GraphExplorer explorer) {
29         this(owner, explorer, ResourceSelectionFilter.FILTER);
30     }
31
32     public DefaultExplorerSelectionListener(IWorkbenchPart owner, GraphExplorer explorer, WorkbenchSelectionFilter filter) {
33         this.owner = owner;
34         this.explorer = explorer;
35         this.filter = filter;
36     }
37
38     @Override
39     public void selectionChanged(IWorkbenchPart part, ISelection selection) {
40         // Always disregard own selections.
41         if (part == owner)
42             return;
43
44         if (DEBUG)
45             System.out.println("[" + owner + "] workbench selection changed: part=" + part + ", selection=" + selection);
46         ISelection s = selection;
47         if (filter != null)
48             s = filter.filterSelection(part, selection);
49
50         if (s != null) {
51             if (DEBUG) {
52                 System.out.println("** [" + owner + "] workbench selection changed: part=" + part + ", selection=" + selection);
53                 System.out.println("    SETTING NEW SELECTION");
54             }
55             if(!explorer.isDisposed()) {
56                 IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class);
57                 selectionProvider.setSelection(selection);
58             }
59         } else {
60             if (DEBUG)
61                 System.out.println("[" + owner + "] discarding selection: part=" + part + ", selection=" + selection);
62         }
63     }
64 }