]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.selectionview/src/org/simantics/selectionview/PropertyPage.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.selectionview / src / org / simantics / selectionview / PropertyPage.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 java.util.function.Consumer;
15
16 import org.eclipse.core.runtime.IAdaptable;
17 import org.eclipse.jface.action.IMenuManager;
18 import org.eclipse.jface.action.IToolBarManager;
19 import org.eclipse.jface.viewers.ISelection;
20 import org.eclipse.jface.viewers.ISelectionProvider;
21 import org.eclipse.jface.viewers.StructuredSelection;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.swt.widgets.Event;
26 import org.eclipse.swt.widgets.Listener;
27 import org.eclipse.ui.IPartListener2;
28 import org.eclipse.ui.IWorkbenchPart;
29 import org.eclipse.ui.IWorkbenchPartReference;
30 import org.eclipse.ui.IWorkbenchPartSite;
31 import org.eclipse.ui.part.IPageSite;
32 import org.simantics.Simantics;
33 import org.simantics.browsing.ui.common.IPageBookViewPagePartInit;
34 import org.simantics.browsing.ui.common.views.IFilterAreaProvider;
35 import org.simantics.browsing.ui.swt.PartNameListener;
36 import org.simantics.browsing.ui.swt.PropertyPageUtil;
37 import org.simantics.db.ReadGraph;
38 import org.simantics.db.Session;
39 import org.simantics.db.exception.DatabaseException;
40 import org.simantics.db.management.ISessionContext;
41 import org.simantics.db.management.ISessionContextChangedListener;
42 import org.simantics.db.management.ISessionContextProvider;
43 import org.simantics.db.management.SessionContextChangedEvent;
44 import org.simantics.db.request.Read;
45 import org.simantics.ui.workbench.IPropertyPage;
46
47 /**
48  * <p>
49  * Subclasses may extend or reimplement the following methods as required:
50  * <ul>
51  *   <li><code>createPageControls</code> - to create the page's controls</li>
52  *   <li><code>fillToolBar</code> - to add actions to the page's toolbar</li>
53  *   <li><code>fillDropDownMenu</code> - to add actions to the page's drop-down menu</li>
54  *   <li><code>getControl</code> - to retrieve the page's control</li>
55  *   <li><code>setFocus</code> - implement to accept focus</li>
56  *   <li><code>sourceSelectionChanged</code> - puts the incoming ISelection into use in this page</li>
57  *   <li><code>sourcePartClosed</code> - cleans up the page controls after a current selection source part has been closed</li>
58  *   <li><code>dispose</code> - extend to provide additional cleanup</li>
59  *   <li><code>setActionBars</code> - reimplement to make contributions</li>
60  *   <li><code>makeContributions</code> - this method exists to support previous versions</li>
61  *   <li><code>setActionBars</code> - this method exists to support previous versions</li>
62  *   <li><code>init</code> - extend to provide additional setup for the view before any controls are created</li>
63  *   <li><code>sessionContextChanged</code> - reimplement to take actions when the source database session changes</li>
64  * </ul>
65  * </p>
66  * 
67  * @author Tuukka Lehtonen
68  */
69 public class PropertyPage extends AbstractPropertyPage implements IPropertyPage, IAdaptable, IPartListener2, IPageBookViewPagePartInit {
70
71     protected static final int MAX_SELECTION_LENGTH_TO_SHOW = PropertyPageUtil.MAX_SELECTION_LENGTH_TO_SHOW;
72
73     protected ISessionContext  sessionContext;
74
75     protected IPropertyTab     tab;
76
77     protected ISelection       latestSelection = StructuredSelection.EMPTY;
78
79     protected IWorkbenchPart   propertyPageView;
80     
81     protected boolean visible = true;
82
83     /**
84      * @param site the workbench part site that contains this page or
85      *        <code>null</code> if there is no site, i.e. the page is within a
86      *        dialog or a plain shell.
87      */
88     public PropertyPage(IWorkbenchPartSite site) {
89         super(site);
90     }
91
92     @Override
93     public void init(IPageSite pageSite) {
94         
95         super.init(pageSite);
96         
97         if(getSite() != null)
98                 getSite().getPage().addPartListener(this);
99         
100     }
101     
102     @Override
103     public void initPart(IWorkbenchPart part) {
104         propertyPageView = part;
105     }
106     
107     @Override
108     public void dispose() {
109
110         if(getSite() != null)
111                 getSite().getPage().removePartListener(this);
112         
113         // Stop listening for title changes.
114         if (currentPartNameListener != null) {
115             currentPartNameListener.dispose();
116             currentPartNameListener = null;
117         }
118
119         ISessionContextProvider contextProvider = getSessionContextProvider();
120         if (contextProvider != null)
121             contextProvider.removeContextChangedListener(contextChangeListener);
122
123         //System.out.println("dispose " + this);
124         // Must invoke dispose before nullifying table since super will dispose the widget.
125         super.dispose();
126         tab = null;
127         sessionContext = null;
128     }
129
130     protected ISessionContextProvider getSessionContextProvider() {
131         return Simantics.getSessionContextProvider();
132     }
133
134     protected ISessionContext getSessionContext() {
135         return sessionContext;
136     }
137
138     protected Session getSession() {
139         if (sessionContext == null)
140             throw new IllegalStateException("null session context");
141         return sessionContext.getSession();
142     }
143
144     @Override
145     public final void createControl(Composite parent) {
146         createPageControls(parent);
147
148         // Attach to current session context provider to keep the UI intact even
149         // when the current UI session changes.
150         ISessionContextProvider contextProvider = getSessionContextProvider();
151         contextProvider.addContextChangedListener(contextChangeListener);
152         setSessionContext(contextProvider.getSessionContext());
153     }
154
155     /**
156      * Override to customize the UI component created on this page.
157      * 
158      * @param parent
159      */
160     protected void createPageControls(Composite parent) {
161         PropertyTable pt = new PropertyTable(sourceSite, parent, SWT.NONE);
162         tab = pt;
163         tab.createControl(pt, getSessionContext());
164         tab.getControl().addListener(SWT.Dispose, new Listener() {
165             @Override
166             public void handleEvent(Event event) {
167                 PropertyPage.this.dispose();
168             }
169         });
170         ISelectionProvider pv = pt.getSelectionProvider();
171         getSite().setSelectionProvider(pv);
172
173         fillToolBar(getSite().getActionBars().getToolBarManager());
174         fillDropDownMenu(getSite().getActionBars().getMenuManager());
175     }
176
177     protected void fillToolBar(IToolBarManager manager) {
178     }
179
180     protected void fillDropDownMenu(IMenuManager manager) {
181     }
182
183     /**
184      * @param newContext
185      */
186     protected final void setSessionContext(ISessionContext newContext) {
187 //        ISessionContext oldContext = this.sessionContext;
188         this.sessionContext = newContext;
189 //        System.out.println("PropertyPage.setSessionContext: " + oldContext + " -> " + newContext);
190     }
191
192     protected ISessionContextChangedListener contextChangeListener = new ISessionContextChangedListener() {
193         @Override
194         public void sessionContextChanged(SessionContextChangedEvent event) {
195             setSessionContext(event.getNewValue());
196         }
197     };
198
199     @Override
200     public Control getControl() {
201         return (tab != null) ? tab.getControl() : null;
202     }
203
204     @Override
205     public ISelection getSelection() {
206         if (tab != null && !tab.isDisposed()) {
207             ISelectionProvider provider = tab.getSelectionProvider();
208             if (provider != null)
209                 return provider.getSelection();
210         }
211         return null;
212     }
213
214     /**
215      * Sets focus to a part in the page.
216      * @see org.eclipse.ui.part.Page#setFocus()
217      */
218     @Override
219     public void setFocus() {
220         if (tab != null && !tab.isDisposed()) {
221             tab.requestFocus();
222         }
223     }
224
225     @Override
226     protected void sourcePartClosed(IWorkbenchPart part) {
227         if (tab != null && !tab.isDisposed()) {
228             tab.setInput(getSessionContext(), StructuredSelection.EMPTY, false);
229         }
230     }
231
232     @Override
233     protected void sourceSelectionChanged(ISelection selection) {
234         if (tab != null && !tab.isDisposed()) {
235             latestSelection = selection;
236             if(visible)
237                 tab.setInput(getSessionContext(), selection, false);
238         }
239     }
240
241     protected void refresh() {
242         if (tab != null && !tab.isDisposed()) {
243             tab.setInput(getSessionContext(), latestSelection, true);
244         }
245     }
246
247     protected PartNameListener currentPartNameListener = null;
248
249     @Override
250     public void updatePartName(final ISelection forSelection, Consumer<String> updateCallback) {
251         
252         if(!visible) {
253                 updateCallback.accept("Selection");
254                 return;
255         }
256         
257         PartNameListener oldListener = currentPartNameListener;
258         PartNameListener newListener = new PartNameListener(updateCallback);
259         currentPartNameListener = newListener;
260         if (oldListener != null)
261             oldListener.dispose();
262
263         if (sessionContext != null) {
264             sessionContext.getSession().asyncRequest(new Read<String>() {
265                 @Override
266                 public String perform(ReadGraph graph) throws DatabaseException {
267                     return computeTitle(graph, forSelection);
268                 }
269             }, newListener);
270         }
271     }
272
273     protected String computeTitle(ReadGraph graph, ISelection forSelection) throws DatabaseException {
274         return PropertyPageUtil.computeTitle(graph, forSelection);
275     }
276
277     @SuppressWarnings("unchecked")
278     @Override
279     public <T> T getAdapter(Class<T> adapter) {
280         if (adapter == IFilterAreaProvider.class)
281             return (T) tab;
282         return null;
283     }
284
285         @Override
286         public void partActivated(IWorkbenchPartReference partRef) {
287         }
288
289         @Override
290         public void partBroughtToTop(IWorkbenchPartReference partRef) {
291         }
292
293         @Override
294         public void partClosed(IWorkbenchPartReference partRef) {
295         }
296
297         @Override
298         public void partDeactivated(IWorkbenchPartReference partRef) {
299         }
300
301         @Override
302         public void partOpened(IWorkbenchPartReference partRef) {
303         }
304
305         @Override
306         public void partInputChanged(IWorkbenchPartReference partRef) {
307         }
308         
309     @Override
310     public void partHidden(IWorkbenchPartReference partRef) {
311         IWorkbenchPart part = partRef.getPart(false);
312         if (propertyPageView.equals(part)) {
313                 visible = false;
314         }
315     }
316
317     @Override
318     public void partVisible(IWorkbenchPartReference partRef) {
319         IWorkbenchPart part = partRef.getPart(false);
320         if (propertyPageView.equals(part)) {
321                 visible = true;
322                 sourceSelectionChanged(latestSelection);
323                 //System.err.println("[" + this + "]selection view was shown");
324         }
325     }
326         
327 }