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