]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.selectionview/src/org/simantics/selectionview/PropertyTabAdapter.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.selectionview / src / org / simantics / selectionview / PropertyTabAdapter.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.selectionview;\r
13 \r
14 import java.util.concurrent.atomic.AtomicBoolean;\r
15 \r
16 import org.eclipse.jface.viewers.ISelectionProvider;\r
17 import org.eclipse.swt.events.DisposeEvent;\r
18 import org.eclipse.swt.events.DisposeListener;\r
19 import org.eclipse.swt.widgets.Composite;\r
20 import org.eclipse.swt.widgets.Control;\r
21 import org.eclipse.ui.IWorkbenchSite;\r
22 import org.simantics.browsing.ui.common.ErrorLogger;\r
23 import org.simantics.db.common.procedure.adapter.ListenerSupport;\r
24 import org.simantics.db.management.ISessionContext;\r
25 \r
26 /**\r
27  * Override {@link #createControls(Composite, ISessionContext)} to create\r
28  * controls for your own tab.\r
29  * \r
30  * <p>\r
31  * This class implements {@link ListenerSupport} to help in dealing with graph\r
32  * request listeners.\r
33  * \r
34  * <p>\r
35  * This adapter has the following default implementations for IPropertyTab:\r
36  * <ul>\r
37  * <li>{@link #getSelectionProvider()} - returns null, i.e. does not provide any\r
38  * selection to other UI/Workbench parts.</li>\r
39  * <li>{@link #requestFocus()} - calls {@link Control#setFocus()} for\r
40  * {@link #getControl()} if a control exists</li>\r
41  * <li>{@link #isDisposed()} - returns <code>false</code> by default and\r
42  * <code>true</code> if {@link #setDisposed()} has been called</li>\r
43  * </ul>\r
44  * \r
45  * @author Tuukka Lehtonen\r
46  * \r
47  * @see PropertyTabContributorImpl\r
48  */\r
49 public abstract class PropertyTabAdapter implements IPropertyTab2, ListenerSupport {\r
50 \r
51     protected IWorkbenchSite    site;\r
52 \r
53     private final AtomicBoolean disposed = new AtomicBoolean();\r
54 \r
55     public PropertyTabAdapter(IWorkbenchSite site) {\r
56         this.site = site;\r
57     }\r
58 \r
59 \r
60     /**\r
61      * Override this implementation and call super.createControl(parent) as the\r
62      * last thing in your property table.\r
63      * \r
64      * @see org.simantics.selectionview.IPropertyTab#createControl(org.eclipse.swt.widgets.Composite)\r
65      */\r
66     @Override\r
67     public final void createControl(Composite parent, ISessionContext context) {\r
68         createControls(parent, context);\r
69         Control control = getControl();\r
70 \r
71         if (control == null || control.isDisposed())\r
72             return;\r
73 \r
74         control.addDisposeListener(new DisposeListener() {\r
75             @Override\r
76             public void widgetDisposed(DisposeEvent e) {\r
77                 setDisposed();\r
78             }\r
79         });\r
80     }\r
81 \r
82     /**\r
83      * Override this implementation to create the tab's controls.\r
84      * \r
85      * @see #createControl(Composite, ISessionContext)\r
86      */\r
87     public void createControls(Composite parent, ISessionContext context) {\r
88         Control control = getControl();\r
89         if (control == null || control.isDisposed())\r
90             return;\r
91 \r
92         control.addDisposeListener(new DisposeListener() {\r
93             @Override\r
94             public void widgetDisposed(DisposeEvent e) {\r
95                 setDisposed();\r
96             }\r
97         });\r
98     }\r
99 \r
100     @Override\r
101     public void dispose() {\r
102     }\r
103 \r
104     /**\r
105      * @return <code>true</code> if tab was marked disposed, <code>false</code>\r
106      *         if it was already marked disposed\r
107      */\r
108     protected boolean setDisposed() {\r
109         return disposed.compareAndSet(false, true);\r
110     }\r
111 \r
112     @Override\r
113     public boolean isDisposed() {\r
114         return disposed.get();\r
115     }\r
116 \r
117     @Override\r
118     public ISelectionProvider getSelectionProvider() {\r
119         return null;\r
120     }\r
121 \r
122     @Override\r
123     public void requestFocus() {\r
124         Control control = getControl();\r
125         if (control == null || control.isDisposed())\r
126             return;\r
127 \r
128         control.setFocus();\r
129     }\r
130 \r
131     @Override\r
132     public void exception(Throwable t) {\r
133         ErrorLogger.defaultLogError("PropertyTabAdapter received unexpected exception.", t);\r
134     }\r
135 \r
136 }\r