1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.diagram.symbollibrary.ui;
14 import org.eclipse.swt.widgets.Composite;
15 import org.eclipse.ui.IEditorPart;
16 import org.eclipse.ui.IMemento;
17 import org.eclipse.ui.IViewSite;
18 import org.eclipse.ui.IWorkbenchPage;
19 import org.eclipse.ui.IWorkbenchPart;
20 import org.eclipse.ui.PartInitException;
21 import org.eclipse.ui.contexts.IContextService;
22 import org.eclipse.ui.part.IContributedContentsView;
23 import org.eclipse.ui.part.IPage;
24 import org.eclipse.ui.part.Page;
25 import org.eclipse.ui.part.PageBook;
26 import org.eclipse.ui.part.PageBookView;
27 import org.simantics.db.management.ISessionContextProvider;
28 import org.simantics.diagram.symbolcontribution.EmptySymbolProviderFactory;
29 import org.simantics.diagram.symbolcontribution.SymbolProviderFactory;
30 import org.simantics.ui.SimanticsUI;
31 import org.simantics.utils.datastructures.BijectionMap;
34 * This paged view shows {@link SymbolLibraryPage}s for editors based on the
35 * {@link ISymbolProviderFactory} the editors adapt to. ISymbolProvider equality is
36 * used for deciding what pages to create/assign for activated workbench parts.
37 * Only editors are considered in page creation.
40 * To get a symbol palette for your editor, add the following to your
41 * {@link IEditorPart#getAdapter(Class)} implementation:
44 * if (c == ISymbolProviderFactory.class) {
46 * return DiagramTypeUtils.readSymbolProviderFactory(RequestProcessor, Resource diagram);
47 * } catch (DatabaseException e) {
54 * @author Tuukka Lehtonen
56 public class SymbolPageView extends PageBookView {
58 private static final String SYMBOL_VIEW_CONTEXT = "org.simantics.diagram.symbolLibrary";
60 private ISessionContextProvider contextProvider;
62 private IMemento memento;
64 BijectionMap<SymbolProviderFactory, IPage> providerPageMap = new BijectionMap<SymbolProviderFactory, IPage>();
67 public void createPartControl(Composite parent) {
68 super.createPartControl(parent);
70 IContextService cs = (IContextService) getSite().getService(IContextService.class);
71 cs.activateContext(SYMBOL_VIEW_CONTEXT);
74 @SuppressWarnings("rawtypes")
76 protected Object getViewAdapter(Class adapter) {
77 if (adapter == IContributedContentsView.class) {
78 // This makes it possible to duplicate a PropertyPageView with another
79 // secondary ID and make it show the same property page that was showing
80 // in the original property page view.
81 return new IContributedContentsView() {
83 public IWorkbenchPart getContributingPart() {
84 return getContributingEditor();
88 if (adapter == ISessionContextProvider.class)
89 return contextProvider;
90 return super.getViewAdapter(adapter);
94 * Returns the editor which contributed the current
97 * @return the editor which contributed the current page
98 * or <code>null</code> if no editor contributed the current page
100 private IWorkbenchPart getContributingEditor() {
101 return getCurrentContributingPart();
105 * @see org.eclipse.ui.part.ViewPart#init(org.eclipse.ui.IViewSite, org.eclipse.ui.IMemento)
108 public void init(IViewSite site, IMemento memento) throws PartInitException {
109 this.memento = memento;
116 * @see org.eclipse.ui.part.PageBookView#init(org.eclipse.ui.IViewSite)
119 public void init(IViewSite site) throws PartInitException {
122 // This prevents the view from providing a selection to other
123 // workbench parts, thus making them lose their selections which is not
125 site.setSelectionProvider(null);
127 contextProvider = SimanticsUI.getSessionContextProvider();
131 public void saveState(IMemento memento) {
132 if (this.memento != null) {
133 memento.putMemento(this.memento);
138 * Method declared on IWorkbenchPart.
141 public void dispose() {
142 // System.out.println("dispose: " + this);
143 // Dispose of this before nullifying contextProvider because this
144 // dispose may just need the context provider - at least PropertyPage
148 contextProvider = null;
152 protected IPage createDefaultPage(PageBook book) {
153 Page page = new SymbolLibraryPage(EmptySymbolProviderFactory.INSTANCE);
155 page.createControl(getPageBook());
157 // MessagePage page = new MessagePage();
159 // page.createControl(book);
160 // page.setMessage(Messages.SymbolPageView_noSymbolsAvailable);
165 protected PageRec doCreatePage(IWorkbenchPart part) {
166 SymbolProviderFactory providerFactory = (SymbolProviderFactory) part.getAdapter(SymbolProviderFactory.class);
167 if (providerFactory == null)
170 IPage page = providerPageMap.getRight(providerFactory);
172 //System.out.println("create new symbol library page for part " + part + " and symbol provider factory " + providerFactory);
173 Page newPage = new SymbolLibraryPage(providerFactory);
175 newPage.createControl(getPageBook());
177 providerPageMap.map(providerFactory, newPage);
180 return new PageRec(part, page);
184 protected void doDestroyPage(IWorkbenchPart part, PageRec pageRecord) {
185 IPage page = pageRecord.page;
186 @SuppressWarnings("unused")
187 SymbolProviderFactory provider = providerPageMap.removeWithRight(page);
188 //System.out.println("destroy page " + page + " for symbol provider " + provider + " by closing part " + part);
190 pageRecord.dispose();
194 protected IWorkbenchPart getBootstrapPart() {
195 IWorkbenchPage page = getSite().getPage();
197 IEditorPart editor = page.getActiveEditor();
200 return page.getActivePart();
206 protected boolean isImportant(IWorkbenchPart part) {
207 // Ignore all but editors.
208 return part instanceof IEditorPart;
212 * Method declared on IViewPart.
213 * Treat this the same as part activation.
216 public void partBroughtToTop(IWorkbenchPart part) {
221 // public void partActivated(IWorkbenchPart part) {
222 // IWorkbenchPart source = getSourcePart(part);
223 // //System.out.println("part activated: " + part + ", " + source);
224 // super.partActivated(source);
227 // private IWorkbenchPart getSourcePart(IWorkbenchPart part) {
228 // IContributedContentsView view = (IContributedContentsView) part.getAdapter(IContributedContentsView.class);
229 // if (view != null) {
230 // IWorkbenchPart source = view.getContributingPart();
231 // if (source != null)