]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/DefaultExplorerSelectionListener.java
Fixed multiple issues causing dangling references to discarded queries
[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.eclipse.ui.services.IDisposable;
19 import org.simantics.browsing.ui.GraphExplorer;
20
21 public class DefaultExplorerSelectionListener implements ISelectionListener, IDisposable {
22
23     private static final boolean DEBUG = false;
24
25     IWorkbenchPart           owner;
26     GraphExplorer            explorer;
27     WorkbenchSelectionFilter filter;
28
29     public DefaultExplorerSelectionListener(IWorkbenchPart owner, GraphExplorer explorer) {
30         this(owner, explorer, ResourceSelectionFilter.FILTER);
31     }
32
33     public DefaultExplorerSelectionListener(IWorkbenchPart owner, GraphExplorer explorer, WorkbenchSelectionFilter filter) {
34         this.owner = owner;
35         this.explorer = explorer;
36         this.filter = filter;
37     }
38
39     @Override
40     public void dispose() {
41         owner = null;
42         explorer = null;
43         filter = null;
44     }
45
46     @Override
47     public void selectionChanged(IWorkbenchPart part, ISelection selection) {
48         // Always disregard own selections.
49         if (part == owner)
50             return;
51
52         if (DEBUG)
53             System.out.println("[" + owner + "] workbench selection changed: part=" + part + ", selection=" + selection);
54         ISelection s = selection;
55         WorkbenchSelectionFilter filter = this.filter;
56         if (filter != null)
57             s = filter.filterSelection(part, selection);
58
59         if (s != null) {
60             if (DEBUG) {
61                 System.out.println("** [" + owner + "] workbench selection changed: part=" + part + ", selection=" + selection);
62                 System.out.println("    SETTING NEW SELECTION");
63             }
64             GraphExplorer explorer = this.explorer;
65             if(explorer != null && !explorer.isDisposed()) {
66                 IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class);
67                 if (selectionProvider != null) {
68                     selectionProvider.setSelection(selection);
69                 }
70             }
71         } else {
72             if (DEBUG)
73                 System.out.println("[" + owner + "] discarding selection: part=" + part + ", selection=" + selection);
74         }
75     }
76 }