--- /dev/null
+/*******************************************************************************\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 java.util.function.Consumer;\r
+\r
+import org.eclipse.core.runtime.IAdaptable;\r
+import org.eclipse.jface.action.IMenuManager;\r
+import org.eclipse.jface.action.IToolBarManager;\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.eclipse.jface.viewers.ISelectionProvider;\r
+import org.eclipse.jface.viewers.StructuredSelection;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Control;\r
+import org.eclipse.swt.widgets.Event;\r
+import org.eclipse.swt.widgets.Listener;\r
+import org.eclipse.ui.IPartListener2;\r
+import org.eclipse.ui.IWorkbenchPart;\r
+import org.eclipse.ui.IWorkbenchPartReference;\r
+import org.eclipse.ui.IWorkbenchPartSite;\r
+import org.eclipse.ui.part.IPageSite;\r
+import org.simantics.Simantics;\r
+import org.simantics.browsing.ui.common.IPageBookViewPagePartInit;\r
+import org.simantics.browsing.ui.common.views.IFilterAreaProvider;\r
+import org.simantics.browsing.ui.swt.PartNameListener;\r
+import org.simantics.browsing.ui.swt.PropertyPageUtil;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.management.ISessionContext;\r
+import org.simantics.db.management.ISessionContextChangedListener;\r
+import org.simantics.db.management.ISessionContextProvider;\r
+import org.simantics.db.management.SessionContextChangedEvent;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.ui.workbench.IPropertyPage;\r
+\r
+/**\r
+ * <p>\r
+ * Subclasses may extend or reimplement the following methods as required:\r
+ * <ul>\r
+ * <li><code>createPageControls</code> - to create the page's controls</li>\r
+ * <li><code>fillToolBar</code> - to add actions to the page's toolbar</li>\r
+ * <li><code>fillDropDownMenu</code> - to add actions to the page's drop-down menu</li>\r
+ * <li><code>getControl</code> - to retrieve the page's control</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
+ * <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 for the view before any controls are created</li>\r
+ * <li><code>sessionContextChanged</code> - reimplement to take actions when the source database session changes</li>\r
+ * </ul>\r
+ * </p>\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class PropertyPage extends AbstractPropertyPage implements IPropertyPage, IAdaptable, IPartListener2, IPageBookViewPagePartInit {\r
+\r
+ protected static final int MAX_SELECTION_LENGTH_TO_SHOW = PropertyPageUtil.MAX_SELECTION_LENGTH_TO_SHOW;\r
+\r
+ protected ISessionContext sessionContext;\r
+\r
+ protected IPropertyTab tab;\r
+\r
+ protected ISelection latestSelection = StructuredSelection.EMPTY;\r
+\r
+ protected IWorkbenchPart propertyPageView;\r
+ \r
+ protected boolean visible = true;\r
+\r
+ /**\r
+ * @param site the workbench part site that contains 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 PropertyPage(IWorkbenchPartSite site) {\r
+ super(site);\r
+ }\r
+\r
+ @Override\r
+ public void init(IPageSite pageSite) {\r
+ \r
+ super.init(pageSite);\r
+ \r
+ if(getSite() != null)\r
+ getSite().getPage().addPartListener(this);\r
+ \r
+ }\r
+ \r
+ @Override\r
+ public void initPart(IWorkbenchPart part) {\r
+ propertyPageView = part;\r
+ }\r
+ \r
+ @Override\r
+ public void dispose() {\r
+\r
+ if(getSite() != null)\r
+ getSite().getPage().removePartListener(this);\r
+ \r
+ // Stop listening for title changes.\r
+ if (currentPartNameListener != null) {\r
+ currentPartNameListener.dispose();\r
+ currentPartNameListener = null;\r
+ }\r
+\r
+ ISessionContextProvider contextProvider = getSessionContextProvider();\r
+ if (contextProvider != null)\r
+ contextProvider.removeContextChangedListener(contextChangeListener);\r
+\r
+ //System.out.println("dispose " + this);\r
+ // Must invoke dispose before nullifying table since super will dispose the widget.\r
+ super.dispose();\r
+ tab = null;\r
+ sessionContext = null;\r
+ }\r
+\r
+ protected ISessionContextProvider getSessionContextProvider() {\r
+ return Simantics.getSessionContextProvider();\r
+ }\r
+\r
+ protected ISessionContext getSessionContext() {\r
+ return sessionContext;\r
+ }\r
+\r
+ protected Session getSession() {\r
+ if (sessionContext == null)\r
+ throw new IllegalStateException("null session context");\r
+ return sessionContext.getSession();\r
+ }\r
+\r
+ @Override\r
+ public final void createControl(Composite parent) {\r
+ createPageControls(parent);\r
+\r
+ // Attach to current session context provider to keep the UI intact even\r
+ // when the current UI session changes.\r
+ ISessionContextProvider contextProvider = getSessionContextProvider();\r
+ contextProvider.addContextChangedListener(contextChangeListener);\r
+ setSessionContext(contextProvider.getSessionContext());\r
+ }\r
+\r
+ /**\r
+ * Override to customize the UI component created on this page.\r
+ * \r
+ * @param parent\r
+ */\r
+ protected void createPageControls(Composite parent) {\r
+ PropertyTable pt = new PropertyTable(sourceSite, parent, SWT.NONE);\r
+ tab = pt;\r
+ tab.createControl(pt, getSessionContext());\r
+ tab.getControl().addListener(SWT.Dispose, new Listener() {\r
+ @Override\r
+ public void handleEvent(Event event) {\r
+ PropertyPage.this.dispose();\r
+ }\r
+ });\r
+ ISelectionProvider pv = pt.getSelectionProvider();\r
+ getSite().setSelectionProvider(pv);\r
+\r
+ fillToolBar(getSite().getActionBars().getToolBarManager());\r
+ fillDropDownMenu(getSite().getActionBars().getMenuManager());\r
+ }\r
+\r
+ protected void fillToolBar(IToolBarManager manager) {\r
+ }\r
+\r
+ protected void fillDropDownMenu(IMenuManager manager) {\r
+ }\r
+\r
+ /**\r
+ * @param newContext\r
+ */\r
+ protected final void setSessionContext(ISessionContext newContext) {\r
+// ISessionContext oldContext = this.sessionContext;\r
+ this.sessionContext = newContext;\r
+// System.out.println("PropertyPage.setSessionContext: " + oldContext + " -> " + newContext);\r
+ }\r
+\r
+ protected ISessionContextChangedListener contextChangeListener = new ISessionContextChangedListener() {\r
+ @Override\r
+ public void sessionContextChanged(SessionContextChangedEvent event) {\r
+ setSessionContext(event.getNewValue());\r
+ }\r
+ };\r
+\r
+ @Override\r
+ public Control getControl() {\r
+ return (tab != null) ? tab.getControl() : null;\r
+ }\r
+\r
+ @Override\r
+ public ISelection getSelection() {\r
+ if (tab != null && !tab.isDisposed()) {\r
+ ISelectionProvider provider = tab.getSelectionProvider();\r
+ if (provider != null)\r
+ return provider.getSelection();\r
+ }\r
+ return null;\r
+ }\r
+\r
+ /**\r
+ * Sets focus to a part in the page.\r
+ * @see org.eclipse.ui.part.Page#setFocus()\r
+ */\r
+ @Override\r
+ public void setFocus() {\r
+ if (tab != null && !tab.isDisposed()) {\r
+ tab.requestFocus();\r
+ }\r
+ }\r
+\r
+ @Override\r
+ protected void sourcePartClosed(IWorkbenchPart part) {\r
+ if (tab != null && !tab.isDisposed()) {\r
+ tab.setInput(getSessionContext(), StructuredSelection.EMPTY, false);\r
+ }\r
+ }\r
+\r
+ @Override\r
+ protected void sourceSelectionChanged(ISelection selection) {\r
+ if (tab != null && !tab.isDisposed()) {\r
+ latestSelection = selection;\r
+ if(visible)\r
+ tab.setInput(getSessionContext(), selection, false);\r
+ }\r
+ }\r
+\r
+ protected void refresh() {\r
+ if (tab != null && !tab.isDisposed()) {\r
+ tab.setInput(getSessionContext(), latestSelection, true);\r
+ }\r
+ }\r
+\r
+ protected PartNameListener currentPartNameListener = null;\r
+\r
+ @Override\r
+ public void updatePartName(final ISelection forSelection, Consumer<String> updateCallback) {\r
+ \r
+ if(!visible) {\r
+ updateCallback.accept("Selection");\r
+ return;\r
+ }\r
+ \r
+ PartNameListener oldListener = currentPartNameListener;\r
+ PartNameListener newListener = new PartNameListener(updateCallback);\r
+ currentPartNameListener = newListener;\r
+ if (oldListener != null)\r
+ oldListener.dispose();\r
+\r
+ if (sessionContext != null) {\r
+ sessionContext.getSession().asyncRequest(new Read<String>() {\r
+ @Override\r
+ public String perform(ReadGraph graph) throws DatabaseException {\r
+ return computeTitle(graph, forSelection);\r
+ }\r
+ }, newListener);\r
+ }\r
+ }\r
+\r
+ protected String computeTitle(ReadGraph graph, ISelection forSelection) throws DatabaseException {\r
+ return PropertyPageUtil.computeTitle(graph, forSelection);\r
+ }\r
+\r
+ @SuppressWarnings("unchecked")\r
+ @Override\r
+ public <T> T getAdapter(Class<T> adapter) {\r
+ if (adapter == IFilterAreaProvider.class)\r
+ return (T) tab;\r
+ return null;\r
+ }\r
+\r
+ @Override\r
+ public void partActivated(IWorkbenchPartReference partRef) {\r
+ }\r
+\r
+ @Override\r
+ public void partBroughtToTop(IWorkbenchPartReference partRef) {\r
+ }\r
+\r
+ @Override\r
+ public void partClosed(IWorkbenchPartReference partRef) {\r
+ }\r
+\r
+ @Override\r
+ public void partDeactivated(IWorkbenchPartReference partRef) {\r
+ }\r
+\r
+ @Override\r
+ public void partOpened(IWorkbenchPartReference partRef) {\r
+ }\r
+\r
+ @Override\r
+ public void partInputChanged(IWorkbenchPartReference partRef) {\r
+ }\r
+ \r
+ @Override\r
+ public void partHidden(IWorkbenchPartReference partRef) {\r
+ IWorkbenchPart part = partRef.getPart(false);\r
+ if (propertyPageView.equals(part)) {\r
+ visible = false;\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public void partVisible(IWorkbenchPartReference partRef) {\r
+ IWorkbenchPart part = partRef.getPart(false);\r
+ if (propertyPageView.equals(part)) {\r
+ visible = true;\r
+ sourceSelectionChanged(latestSelection);\r
+ //System.err.println("[" + this + "]selection view was shown");\r
+ }\r
+ }\r
+ \r
+}\r