]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/symbollibrary/ui/SymbolPageView.java
Remove usage of deprecated SimanticsUI-methods
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / symbollibrary / ui / SymbolPageView.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.diagram.symbollibrary.ui;
13
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.Simantics;
28 import org.simantics.db.management.ISessionContextProvider;
29 import org.simantics.diagram.symbolcontribution.EmptySymbolProviderFactory;
30 import org.simantics.diagram.symbolcontribution.SymbolProviderFactory;
31 import org.simantics.utils.datastructures.BijectionMap;
32
33 /**
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.
38  * 
39  * <p>
40  * To get a symbol palette for your editor, add the following to your
41  * {@link IEditorPart#getAdapter(Class)} implementation:
42  * 
43  * <pre>
44  *     if (c == ISymbolProviderFactory.class) {
45  *         try {
46  *             return DiagramTypeUtils.readSymbolProviderFactory(RequestProcessor, Resource diagram);
47  *         } catch (DatabaseException e) {
48  *             // TODO: Log error
49  *             return null;
50  *         }
51  *     }
52  * </pre>
53  * 
54  * @author Tuukka Lehtonen
55  */
56 public class SymbolPageView extends PageBookView {
57
58     private static final String                SYMBOL_VIEW_CONTEXT = "org.simantics.diagram.symbolLibrary";
59
60     private ISessionContextProvider            contextProvider;
61
62     private IMemento                           memento;
63
64     BijectionMap<SymbolProviderFactory, IPage> providerPageMap     = new BijectionMap<SymbolProviderFactory, IPage>();
65
66     @Override
67     public void createPartControl(Composite parent) {
68         super.createPartControl(parent);
69
70         IContextService cs = (IContextService) getSite().getService(IContextService.class);
71         cs.activateContext(SYMBOL_VIEW_CONTEXT);
72     }
73
74     @SuppressWarnings("rawtypes")
75     @Override
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() {
82                 @Override
83                 public IWorkbenchPart getContributingPart() {
84                     return getContributingEditor();
85                 }
86             };
87         }
88         if (adapter == ISessionContextProvider.class)
89             return contextProvider;
90         return super.getViewAdapter(adapter);
91     }
92
93     /**
94      * Returns the editor which contributed the current
95      * page to this view.
96      *
97      * @return the editor which contributed the current page
98      * or <code>null</code> if no editor contributed the current page
99      */
100     private IWorkbenchPart getContributingEditor() {
101         return getCurrentContributingPart();
102     }
103
104     /* (non-Javadoc)
105      * @see org.eclipse.ui.part.ViewPart#init(org.eclipse.ui.IViewSite, org.eclipse.ui.IMemento)
106      */
107     @Override
108     public void init(IViewSite site, IMemento memento) throws PartInitException {
109         this.memento = memento;
110         init(site);
111     }
112
113     /*
114      * (non-Javadoc)
115      * 
116      * @see org.eclipse.ui.part.PageBookView#init(org.eclipse.ui.IViewSite)
117      */
118     @Override
119     public void init(IViewSite site) throws PartInitException {
120         super.init(site);
121
122         // This prevents the view from providing a selection to other
123         // workbench parts, thus making them lose their selections which is not
124         // desirable.
125         site.setSelectionProvider(null);
126
127         contextProvider = Simantics.getSessionContextProvider();
128     }
129
130     @Override
131     public void saveState(IMemento memento) {
132         if (this.memento != null) {
133             memento.putMemento(this.memento);
134         }
135     }
136
137     /* (non-Javadoc)
138      * Method declared on IWorkbenchPart.
139      */
140     @Override
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
145         // disposal will.
146         super.dispose();
147
148         contextProvider = null;
149     }
150
151     @Override
152     protected IPage createDefaultPage(PageBook book) {
153         Page page = new SymbolLibraryPage(EmptySymbolProviderFactory.INSTANCE);
154         initPage(page);
155         page.createControl(getPageBook());
156         return page;
157 //        MessagePage page = new MessagePage();
158 //        initPage(page);
159 //        page.createControl(book);
160 //        page.setMessage(Messages.SymbolPageView_noSymbolsAvailable);
161 //        return page;
162     }
163
164     @Override
165     protected PageRec doCreatePage(IWorkbenchPart part) {
166         SymbolProviderFactory providerFactory = (SymbolProviderFactory) part.getAdapter(SymbolProviderFactory.class);
167         if (providerFactory == null)
168             return null;
169
170         IPage page = providerPageMap.getRight(providerFactory);
171         if (page == null) {
172             //System.out.println("create new symbol library page for part " + part + " and symbol provider factory " + providerFactory);
173             Page newPage = new SymbolLibraryPage(providerFactory);
174             initPage(newPage);
175             newPage.createControl(getPageBook());
176
177             providerPageMap.map(providerFactory, newPage);
178             page = newPage;
179         }
180         return new PageRec(part, page);
181     }
182
183     @Override
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);
189         page.dispose();
190         pageRecord.dispose();
191     }
192
193     @Override
194     protected IWorkbenchPart getBootstrapPart() {
195         IWorkbenchPage page = getSite().getPage();
196         if (page != null) {
197             IEditorPart editor = page.getActiveEditor();
198             if (editor != null)
199                 return editor;
200             return page.getActivePart();
201         }
202         return null;
203     }
204
205     @Override
206     protected boolean isImportant(IWorkbenchPart part) {
207         // Ignore all but editors.
208         return part instanceof IEditorPart;
209     }
210
211     /* (non-Javadoc)
212      * Method declared on IViewPart.
213      * Treat this the same as part activation.
214      */
215     @Override
216     public void partBroughtToTop(IWorkbenchPart part) {
217         partActivated(part);
218     }
219
220 //    @Override
221 //    public void partActivated(IWorkbenchPart part) {
222 //        IWorkbenchPart source = getSourcePart(part);
223 //        //System.out.println("part activated: " + part + ", " + source);
224 //        super.partActivated(source);
225 //    }
226 //
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)
232 //                return source;
233 //        }
234 //        return part;
235 //    }
236
237 }