]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.selectionview/src/org/simantics/selectionview/PropertyTabAdapter.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.selectionview / src / org / simantics / selectionview / PropertyTabAdapter.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.concurrent.atomic.AtomicBoolean;
15
16 import org.eclipse.jface.viewers.ISelectionProvider;
17 import org.eclipse.swt.events.DisposeEvent;
18 import org.eclipse.swt.events.DisposeListener;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Control;
21 import org.eclipse.ui.IWorkbenchSite;
22 import org.simantics.browsing.ui.common.ErrorLogger;
23 import org.simantics.db.common.procedure.adapter.ListenerSupport;
24 import org.simantics.db.management.ISessionContext;
25
26 /**
27  * Override {@link #createControls(Composite, ISessionContext)} to create
28  * controls for your own tab.
29  * 
30  * <p>
31  * This class implements {@link ListenerSupport} to help in dealing with graph
32  * request listeners.
33  * 
34  * <p>
35  * This adapter has the following default implementations for IPropertyTab:
36  * <ul>
37  * <li>{@link #getSelectionProvider()} - returns null, i.e. does not provide any
38  * selection to other UI/Workbench parts.</li>
39  * <li>{@link #requestFocus()} - calls {@link Control#setFocus()} for
40  * {@link #getControl()} if a control exists</li>
41  * <li>{@link #isDisposed()} - returns <code>false</code> by default and
42  * <code>true</code> if {@link #setDisposed()} has been called</li>
43  * </ul>
44  * 
45  * @author Tuukka Lehtonen
46  * 
47  * @see PropertyTabContributorImpl
48  */
49 public abstract class PropertyTabAdapter implements IPropertyTab2, ListenerSupport {
50
51     protected IWorkbenchSite    site;
52
53     private final AtomicBoolean disposed = new AtomicBoolean();
54
55     public PropertyTabAdapter(IWorkbenchSite site) {
56         this.site = site;
57     }
58
59
60     /**
61      * Override this implementation and call super.createControl(parent) as the
62      * last thing in your property table.
63      * 
64      * @see org.simantics.selectionview.IPropertyTab#createControl(org.eclipse.swt.widgets.Composite)
65      */
66     @Override
67     public final void createControl(Composite parent, ISessionContext context) {
68         createControls(parent, context);
69         Control control = getControl();
70
71         if (control == null || control.isDisposed())
72             return;
73
74         control.addDisposeListener(new DisposeListener() {
75             @Override
76             public void widgetDisposed(DisposeEvent e) {
77                 setDisposed();
78             }
79         });
80     }
81
82     /**
83      * Override this implementation to create the tab's controls.
84      * 
85      * @see #createControl(Composite, ISessionContext)
86      */
87     public void createControls(Composite parent, ISessionContext context) {
88         Control control = getControl();
89         if (control == null || control.isDisposed())
90             return;
91
92         control.addDisposeListener(new DisposeListener() {
93             @Override
94             public void widgetDisposed(DisposeEvent e) {
95                 setDisposed();
96             }
97         });
98     }
99
100     @Override
101     public void dispose() {
102     }
103
104     /**
105      * @return <code>true</code> if tab was marked disposed, <code>false</code>
106      *         if it was already marked disposed
107      */
108     protected boolean setDisposed() {
109         return disposed.compareAndSet(false, true);
110     }
111
112     @Override
113     public boolean isDisposed() {
114         return disposed.get();
115     }
116
117     @Override
118     public ISelectionProvider getSelectionProvider() {
119         return null;
120     }
121
122     @Override
123     public void requestFocus() {
124         Control control = getControl();
125         if (control == null || control.isDisposed())
126             return;
127
128         control.setFocus();
129     }
130
131     @Override
132     public void exception(Throwable t) {
133         ErrorLogger.defaultLogError("PropertyTabAdapter received unexpected exception.", t);
134     }
135
136 }