]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.selectionview/src/org/simantics/selectionview/AbstractPropertyPage.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.selectionview / src / org / simantics / selectionview / AbstractPropertyPage.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.selectionview;
13
14 import org.eclipse.jface.viewers.ISelection;
15 import org.eclipse.ui.IPartListener;
16 import org.eclipse.ui.IWorkbenchPage;
17 import org.eclipse.ui.IWorkbenchPart;
18 import org.eclipse.ui.IWorkbenchPartSite;
19 import org.eclipse.ui.part.Page;
20 import org.simantics.ui.workbench.IPropertyPage;
21
22 /**
23  * <p>
24  * Subclasses must implement the following methods:
25  * <ul>
26  *   <li><code>createControl</code> - to create the page's control</li>
27  *   <li><code>getControl</code> - to retrieve the page's control</li>
28  *   <li><code>getSelection</code> - to retrieve the page's current selection</li>
29  *   <li><code>setFocus</code> - implement to accept focus</li>
30  *   <li><code>sourceSelectionChanged</code> - puts the incoming ISelection into use in this page</li>
31  *   <li><code>sourcePartClosed</code> - cleans up the page controls after a current selection source part has been closed</li>
32  * </ul>
33  * </p>
34  * <p>
35  * Subclasses may extend or reimplement the following methods as required:
36  * <ul>
37  *   <li><code>dispose</code> - extend to provide additional cleanup</li>
38  *   <li><code>setActionBars</code> - reimplement to make contributions</li>
39  *   <li><code>makeContributions</code> - this method exists to support previous versions</li>
40  *   <li><code>setActionBars</code> - this method exists to support previous versions</li>
41  *   <li><code>init</code> - extend to provide additional setup</li>
42  * </ul>
43  * </p>
44  * 
45  * @author Tuukka Lehtonen
46  */
47 public abstract class AbstractPropertyPage extends Page implements IPropertyPage {
48
49     /**
50      * Part listener which cleans up this page when the source part is closed.
51      * This is hooked only when there is a source part.
52      */
53     private class PartListener implements IPartListener {
54         public void partActivated(IWorkbenchPart part) {
55         }
56
57         public void partBroughtToTop(IWorkbenchPart part) {
58         }
59
60         public void partClosed(IWorkbenchPart part) {
61             if (sourcePart == part) {
62                 sourcePart = null;
63                 sourcePartClosed(part);
64             }
65         }
66
67         public void partDeactivated(IWorkbenchPart part) {
68         }
69
70         public void partOpened(IWorkbenchPart part) {
71         }
72     }
73
74     private final PartListener   partListener = new PartListener();
75
76     protected IWorkbenchPartSite sourceSite;
77
78     protected IWorkbenchPart     sourcePart;
79
80     /**
81      * @param sourceSite the workbench part site that produces this page or
82      *        <code>null</code> if there is no site, i.e. the page is within a
83      *        dialog or a plain shell.
84      */
85     public AbstractPropertyPage(IWorkbenchPartSite sourceSite) {
86         super();
87         this.sourceSite = sourceSite;
88     }
89
90     @Override
91     public void dispose() {
92         super.dispose();
93
94         if (sourcePart != null) {
95             sourcePart.getSite().getPage().removePartListener(partListener);
96             sourcePart = null;
97         }
98
99         sourceSite = null;
100
101     }
102
103     @Override
104     public void selectionChanged(IWorkbenchPart part, ISelection selection) {
105         if (getControl() == null) {
106             return;
107         }
108
109         if (sourcePart != null) {
110             sourcePart.getSite().getPage().removePartListener(partListener);
111             sourcePart = null;
112         }
113
114         // change the viewer input since the workbench selection has changed.
115         sourcePart = part;
116         sourceSelectionChanged(selection);
117
118         if (sourcePart != null) {
119             IWorkbenchPartSite site = sourcePart.getSite();
120             if(site == null) {
121                 new Exception("null site").printStackTrace();
122                 return;
123             }
124             IWorkbenchPage page = site.getPage();
125             if(page == null) {
126                 new Exception("null page").printStackTrace();
127                 return;
128             }
129             page.addPartListener(partListener);
130         }
131     }
132
133     /**
134      * @param selection
135      */
136     protected abstract void sourceSelectionChanged(ISelection selection);
137
138     /**
139      * @param part
140      */
141     protected abstract void sourcePartClosed(IWorkbenchPart part);
142
143 }