--- /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.diagram.symbollibrary.ui;\r
+\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.ui.IEditorPart;\r
+import org.eclipse.ui.IMemento;\r
+import org.eclipse.ui.IViewSite;\r
+import org.eclipse.ui.IWorkbenchPage;\r
+import org.eclipse.ui.IWorkbenchPart;\r
+import org.eclipse.ui.PartInitException;\r
+import org.eclipse.ui.contexts.IContextService;\r
+import org.eclipse.ui.part.IContributedContentsView;\r
+import org.eclipse.ui.part.IPage;\r
+import org.eclipse.ui.part.Page;\r
+import org.eclipse.ui.part.PageBook;\r
+import org.eclipse.ui.part.PageBookView;\r
+import org.simantics.db.management.ISessionContextProvider;\r
+import org.simantics.diagram.symbolcontribution.EmptySymbolProviderFactory;\r
+import org.simantics.diagram.symbolcontribution.SymbolProviderFactory;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.utils.datastructures.BijectionMap;\r
+\r
+/**\r
+ * This paged view shows {@link SymbolLibraryPage}s for editors based on the\r
+ * {@link ISymbolProviderFactory} the editors adapt to. ISymbolProvider equality is\r
+ * used for deciding what pages to create/assign for activated workbench parts.\r
+ * Only editors are considered in page creation.\r
+ * \r
+ * <p>\r
+ * To get a symbol palette for your editor, add the following to your\r
+ * {@link IEditorPart#getAdapter(Class)} implementation:\r
+ * \r
+ * <pre>\r
+ * if (c == ISymbolProviderFactory.class) {\r
+ * try {\r
+ * return DiagramTypeUtils.readSymbolProviderFactory(RequestProcessor, Resource diagram);\r
+ * } catch (DatabaseException e) {\r
+ * // TODO: Log error\r
+ * return null;\r
+ * }\r
+ * }\r
+ * </pre>\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class SymbolPageView extends PageBookView {\r
+\r
+ private static final String SYMBOL_VIEW_CONTEXT = "org.simantics.diagram.symbolLibrary";\r
+\r
+ private ISessionContextProvider contextProvider;\r
+\r
+ private IMemento memento;\r
+\r
+ BijectionMap<SymbolProviderFactory, IPage> providerPageMap = new BijectionMap<SymbolProviderFactory, IPage>();\r
+\r
+ @Override\r
+ public void createPartControl(Composite parent) {\r
+ super.createPartControl(parent);\r
+\r
+ IContextService cs = (IContextService) getSite().getService(IContextService.class);\r
+ cs.activateContext(SYMBOL_VIEW_CONTEXT);\r
+ }\r
+\r
+ @SuppressWarnings("rawtypes")\r
+ @Override\r
+ protected Object getViewAdapter(Class adapter) {\r
+ if (adapter == IContributedContentsView.class) {\r
+ // This makes it possible to duplicate a PropertyPageView with another\r
+ // secondary ID and make it show the same property page that was showing\r
+ // in the original property page view.\r
+ return new IContributedContentsView() {\r
+ @Override\r
+ public IWorkbenchPart getContributingPart() {\r
+ return getContributingEditor();\r
+ }\r
+ };\r
+ }\r
+ if (adapter == ISessionContextProvider.class)\r
+ return contextProvider;\r
+ return super.getViewAdapter(adapter);\r
+ }\r
+\r
+ /**\r
+ * Returns the editor which contributed the current\r
+ * page to this view.\r
+ *\r
+ * @return the editor which contributed the current page\r
+ * or <code>null</code> if no editor contributed the current page\r
+ */\r
+ private IWorkbenchPart getContributingEditor() {\r
+ return getCurrentContributingPart();\r
+ }\r
+\r
+ /* (non-Javadoc)\r
+ * @see org.eclipse.ui.part.ViewPart#init(org.eclipse.ui.IViewSite, org.eclipse.ui.IMemento)\r
+ */\r
+ @Override\r
+ public void init(IViewSite site, IMemento memento) throws PartInitException {\r
+ this.memento = memento;\r
+ init(site);\r
+ }\r
+\r
+ /*\r
+ * (non-Javadoc)\r
+ * \r
+ * @see org.eclipse.ui.part.PageBookView#init(org.eclipse.ui.IViewSite)\r
+ */\r
+ @Override\r
+ public void init(IViewSite site) throws PartInitException {\r
+ super.init(site);\r
+\r
+ // This prevents the view from providing a selection to other\r
+ // workbench parts, thus making them lose their selections which is not\r
+ // desirable.\r
+ site.setSelectionProvider(null);\r
+\r
+ contextProvider = SimanticsUI.getSessionContextProvider();\r
+ }\r
+\r
+ @Override\r
+ public void saveState(IMemento memento) {\r
+ if (this.memento != null) {\r
+ memento.putMemento(this.memento);\r
+ }\r
+ }\r
+\r
+ /* (non-Javadoc)\r
+ * Method declared on IWorkbenchPart.\r
+ */\r
+ @Override\r
+ public void dispose() {\r
+// System.out.println("dispose: " + this);\r
+ // Dispose of this before nullifying contextProvider because this\r
+ // dispose may just need the context provider - at least PropertyPage\r
+ // disposal will.\r
+ super.dispose();\r
+\r
+ contextProvider = null;\r
+ }\r
+\r
+ @Override\r
+ protected IPage createDefaultPage(PageBook book) {\r
+ Page page = new SymbolLibraryPage(EmptySymbolProviderFactory.INSTANCE);\r
+ initPage(page);\r
+ page.createControl(getPageBook());\r
+ return page;\r
+// MessagePage page = new MessagePage();\r
+// initPage(page);\r
+// page.createControl(book);\r
+// page.setMessage(Messages.SymbolPageView_noSymbolsAvailable);\r
+// return page;\r
+ }\r
+\r
+ @Override\r
+ protected PageRec doCreatePage(IWorkbenchPart part) {\r
+ SymbolProviderFactory providerFactory = (SymbolProviderFactory) part.getAdapter(SymbolProviderFactory.class);\r
+ if (providerFactory == null)\r
+ return null;\r
+\r
+ IPage page = providerPageMap.getRight(providerFactory);\r
+ if (page == null) {\r
+ //System.out.println("create new symbol library page for part " + part + " and symbol provider factory " + providerFactory);\r
+ Page newPage = new SymbolLibraryPage(providerFactory);\r
+ initPage(newPage);\r
+ newPage.createControl(getPageBook());\r
+\r
+ providerPageMap.map(providerFactory, newPage);\r
+ page = newPage;\r
+ }\r
+ return new PageRec(part, page);\r
+ }\r
+\r
+ @Override\r
+ protected void doDestroyPage(IWorkbenchPart part, PageRec pageRecord) {\r
+ IPage page = pageRecord.page;\r
+ @SuppressWarnings("unused")\r
+ SymbolProviderFactory provider = providerPageMap.removeWithRight(page);\r
+ //System.out.println("destroy page " + page + " for symbol provider " + provider + " by closing part " + part);\r
+ page.dispose();\r
+ pageRecord.dispose();\r
+ }\r
+\r
+ @Override\r
+ protected IWorkbenchPart getBootstrapPart() {\r
+ IWorkbenchPage page = getSite().getPage();\r
+ if (page != null) {\r
+ IEditorPart editor = page.getActiveEditor();\r
+ if (editor != null)\r
+ return editor;\r
+ return page.getActivePart();\r
+ }\r
+ return null;\r
+ }\r
+\r
+ @Override\r
+ protected boolean isImportant(IWorkbenchPart part) {\r
+ // Ignore all but editors.\r
+ return part instanceof IEditorPart;\r
+ }\r
+\r
+ /* (non-Javadoc)\r
+ * Method declared on IViewPart.\r
+ * Treat this the same as part activation.\r
+ */\r
+ @Override\r
+ public void partBroughtToTop(IWorkbenchPart part) {\r
+ partActivated(part);\r
+ }\r
+\r
+// @Override\r
+// public void partActivated(IWorkbenchPart part) {\r
+// IWorkbenchPart source = getSourcePart(part);\r
+// //System.out.println("part activated: " + part + ", " + source);\r
+// super.partActivated(source);\r
+// }\r
+//\r
+// private IWorkbenchPart getSourcePart(IWorkbenchPart part) {\r
+// IContributedContentsView view = (IContributedContentsView) part.getAdapter(IContributedContentsView.class);\r
+// if (view != null) {\r
+// IWorkbenchPart source = view.getContributingPart();\r
+// if (source != null)\r
+// return source;\r
+// }\r
+// return part;\r
+// }\r
+\r
+}\r