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