]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.selectionview/src/org/simantics/selectionview/AbstractPropertyPage.java b/bundles/org.simantics.selectionview/src/org/simantics/selectionview/AbstractPropertyPage.java
new file mode 100644 (file)
index 0000000..7d7c015
--- /dev/null
@@ -0,0 +1,143 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.selectionview;\r
+\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.eclipse.ui.IPartListener;\r
+import org.eclipse.ui.IWorkbenchPage;\r
+import org.eclipse.ui.IWorkbenchPart;\r
+import org.eclipse.ui.IWorkbenchPartSite;\r
+import org.eclipse.ui.part.Page;\r
+import org.simantics.ui.workbench.IPropertyPage;\r
+\r
+/**\r
+ * <p>\r
+ * Subclasses must implement the following methods:\r
+ * <ul>\r
+ *   <li><code>createControl</code> - to create the page's control</li>\r
+ *   <li><code>getControl</code> - to retrieve the page's control</li>\r
+ *   <li><code>getSelection</code> - to retrieve the page's current selection</li>\r
+ *   <li><code>setFocus</code> - implement to accept focus</li>\r
+ *   <li><code>sourceSelectionChanged</code> - puts the incoming ISelection into use in this page</li>\r
+ *   <li><code>sourcePartClosed</code> - cleans up the page controls after a current selection source part has been closed</li>\r
+ * </ul>\r
+ * </p>\r
+ * <p>\r
+ * Subclasses may extend or reimplement the following methods as required:\r
+ * <ul>\r
+ *   <li><code>dispose</code> - extend to provide additional cleanup</li>\r
+ *   <li><code>setActionBars</code> - reimplement to make contributions</li>\r
+ *   <li><code>makeContributions</code> - this method exists to support previous versions</li>\r
+ *   <li><code>setActionBars</code> - this method exists to support previous versions</li>\r
+ *   <li><code>init</code> - extend to provide additional setup</li>\r
+ * </ul>\r
+ * </p>\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public abstract class AbstractPropertyPage extends Page implements IPropertyPage {\r
+\r
+    /**\r
+     * Part listener which cleans up this page when the source part is closed.\r
+     * This is hooked only when there is a source part.\r
+     */\r
+    private class PartListener implements IPartListener {\r
+        public void partActivated(IWorkbenchPart part) {\r
+        }\r
+\r
+        public void partBroughtToTop(IWorkbenchPart part) {\r
+        }\r
+\r
+        public void partClosed(IWorkbenchPart part) {\r
+            if (sourcePart == part) {\r
+                sourcePart = null;\r
+                sourcePartClosed(part);\r
+            }\r
+        }\r
+\r
+        public void partDeactivated(IWorkbenchPart part) {\r
+        }\r
+\r
+        public void partOpened(IWorkbenchPart part) {\r
+        }\r
+    }\r
+\r
+    private final PartListener   partListener = new PartListener();\r
+\r
+    protected IWorkbenchPartSite sourceSite;\r
+\r
+    protected IWorkbenchPart     sourcePart;\r
+\r
+    /**\r
+     * @param sourceSite the workbench part site that produces this page or\r
+     *        <code>null</code> if there is no site, i.e. the page is within a\r
+     *        dialog or a plain shell.\r
+     */\r
+    public AbstractPropertyPage(IWorkbenchPartSite sourceSite) {\r
+        super();\r
+        this.sourceSite = sourceSite;\r
+    }\r
+\r
+    @Override\r
+    public void dispose() {\r
+        super.dispose();\r
+\r
+        if (sourcePart != null) {\r
+            sourcePart.getSite().getPage().removePartListener(partListener);\r
+            sourcePart = null;\r
+        }\r
+\r
+        sourceSite = null;\r
+\r
+    }\r
+\r
+    @Override\r
+    public void selectionChanged(IWorkbenchPart part, ISelection selection) {\r
+        if (getControl() == null) {\r
+            return;\r
+        }\r
+\r
+        if (sourcePart != null) {\r
+            sourcePart.getSite().getPage().removePartListener(partListener);\r
+            sourcePart = null;\r
+        }\r
+\r
+        // change the viewer input since the workbench selection has changed.\r
+        sourcePart = part;\r
+        sourceSelectionChanged(selection);\r
+\r
+        if (sourcePart != null) {\r
+            IWorkbenchPartSite site = sourcePart.getSite();\r
+            if(site == null) {\r
+                new Exception("null site").printStackTrace();\r
+                return;\r
+            }\r
+            IWorkbenchPage page = site.getPage();\r
+            if(page == null) {\r
+                new Exception("null page").printStackTrace();\r
+                return;\r
+            }\r
+            page.addPartListener(partListener);\r
+        }\r
+    }\r
+\r
+    /**\r
+     * @param selection\r
+     */\r
+    protected abstract void sourceSelectionChanged(ISelection selection);\r
+\r
+    /**\r
+     * @param part\r
+     */\r
+    protected abstract void sourcePartClosed(IWorkbenchPart part);\r
+\r
+}\r