]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.browsing.ui.platform/src/org/simantics/browsing/ui/platform/VariablesPage.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.platform / src / org / simantics / browsing / ui / platform / VariablesPage.java
index 394859d095a40d957ec43e592a0a7b46e7415edd..cc0bee04fa8b90d362c234946dc74f11df545338 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.browsing.ui.platform;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Collection;\r
-import java.util.HashMap;\r
-import java.util.HashSet;\r
-import java.util.Map;\r
-import java.util.Set;\r
-import java.util.function.Consumer;\r
-\r
-import org.eclipse.core.runtime.IAdaptable;\r
-import org.eclipse.jface.viewers.ISelection;\r
-import org.eclipse.swt.SWT;\r
-import org.eclipse.swt.widgets.Composite;\r
-import org.eclipse.swt.widgets.Control;\r
-import org.eclipse.ui.IPartListener;\r
-import org.eclipse.ui.IWorkbenchPart;\r
-import org.eclipse.ui.IWorkbenchPartSite;\r
-import org.eclipse.ui.part.IPageBookViewPage;\r
-import org.eclipse.ui.part.Page;\r
-import org.simantics.browsing.ui.GraphExplorer;\r
-import org.simantics.browsing.ui.swt.IVariablesPage;\r
-import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite;\r
-import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite.InputSource;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.common.ResourceArray;\r
-import org.simantics.db.common.utils.NameUtils;\r
-import org.simantics.db.exception.AdaptionException;\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.procedure.Listener;\r
-import org.simantics.db.request.Read;\r
-import org.simantics.ui.utils.ResourceAdaptionUtils;\r
-import org.simantics.utils.ui.AdaptionUtils;\r
-import org.simantics.utils.ui.ErrorLogger;\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>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</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 VariablesPage extends Page implements IPageBookViewPage, IVariablesPage {\r
-\r
-    protected static final int MAX_SELECTION_LENGTH_TO_SHOW = 5;\r
-\r
-    protected ISessionContext sessionContext;\r
-\r
-    protected GraphExplorerComposite explorer;\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 VariablesPage(IWorkbenchPartSite site) {\r
-       this.site = site;\r
-    }\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
-     * @param adapter must provide an adapter for\r
-     *        <code>ISessionContextProvider.class</code>\r
-     */\r
-    public VariablesPage(IWorkbenchPartSite site, IAdaptable adapter) {\r
-        this(site);\r
-        setAdapter(adapter);\r
-    }\r
-\r
-    @Override\r
-    public void dispose() {\r
-       \r
-        // Stop listening for title changes.\r
-        if (currentPartNameListener != null)\r
-            currentPartNameListener.dispose();\r
-\r
-        if (adapter != null) {\r
-            ISessionContextProvider contextProvider = getSessionContextProvider();\r
-            contextProvider.removeContextChangedListener(contextChangeListener);\r
-        }\r
-\r
-        if (sourcePart != null) {\r
-            sourcePart.getSite().getPage().removePartListener(partListener);\r
-            sourcePart = null;\r
-        }\r
-\r
-        site = null;\r
-        adapter = null;\r
-        explorer = null;\r
-        sessionContext = null;\r
-        \r
-    }\r
-\r
-    protected ISessionContextProvider getSessionContextProvider() {\r
-        return (ISessionContextProvider) getAdapter().getAdapter(ISessionContextProvider.class);\r
-    }\r
-\r
-    protected ISessionContext getSessionContext() {\r
-        return sessionContext;\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
-\r
-       Map<String, Object> args = new HashMap<String, Object>();\r
-       Set<String> browseContexts = new HashSet<String>();\r
-       \r
-       browseContexts.add("org.simantics.browsing.ui.graph.variablesView");\r
-       \r
-       args.put("browseContexts", browseContexts);\r
-       \r
-       explorer = new GraphExplorerComposite(args, site, parent, SWT.NONE);\r
-       \r
-       explorer.setInputSource(new InputSource() {\r
-\r
-            @Override\r
-            public Object get(ISessionContext ctx, Object selection) {\r
-\r
-               final Resource input = AdaptionUtils.adaptToSingle(selection, Resource.class);\r
-               if(input == null) {\r
-                    return GraphExplorer.EMPTY_INPUT;\r
-               }\r
-               \r
-               final VariablePrefix prefix =  AdaptionUtils.adaptToSingle(selection, VariablePrefix.class);\r
-               if(prefix == null) {\r
-                       return new VariablesInput(null, input);\r
-               } else {\r
-                       return new VariablesInput(prefix.getPrefix(), input);\r
-               }\r
-\r
-            }\r
-               \r
-       });\r
-       \r
-       }\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("AbstractPropertyPage.setSessionContext: " + oldContext + " -> " + newContext);\r
-\r
-        sessionContextChanged(oldContext, newContext);\r
-    }\r
-\r
-    /**\r
-     * @param oldContext\r
-     * @param newContext\r
-     */\r
-    protected void sessionContextChanged(ISessionContext oldContext, ISessionContext newContext) {\r
-       explorer.applySessionContext(newContext);\r
-//        explorer.setSessionContext(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 (explorer != null) ? explorer : null;\r
-    }\r
-\r
-    @Override\r
-    public ISelection getSelection() {\r
-//        if (!explorer.isDisposed()) {\r
-//            return explorer.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 (explorer != null && !explorer.isDisposed()) {\r
-//             explorer.requestFocus();\r
-//        }\r
-    }\r
-\r
-    protected void sourcePartClosed(IWorkbenchPart part) {\r
-//        if (!explorer.isDisposed()) {\r
-//             explorer.setInput(StructuredSelection.EMPTY, false);\r
-//        }\r
-    }\r
-\r
-    protected void sourceSelectionChanged(ISelection selection) {\r
-        if (!explorer.isDisposed()) {\r
-               explorer.setInput(selection, false);\r
-        }\r
-    }\r
-\r
-    static class PartNameListener implements Listener<String> {\r
-        private boolean disposed = false;\r
-        private final Consumer<String> updateCallback;\r
-\r
-        public PartNameListener(Consumer<String> updateCallback) {\r
-            assert updateCallback != null;\r
-            this.updateCallback = updateCallback;\r
-        }\r
-\r
-        public void dispose() {\r
-            disposed = true;\r
-        }\r
-\r
-        @Override\r
-        public boolean isDisposed() {\r
-            return disposed;\r
-        }\r
-\r
-        @Override\r
-        public void execute(String result) {\r
-            //System.out.println("part name changed: " + result);\r
-            updateCallback.accept(result);\r
-        }\r
-\r
-        @Override\r
-        public void exception(Throwable t) {\r
-            ErrorLogger.defaultLogError(t);\r
-        }\r
-    }\r
-\r
-    PartNameListener currentPartNameListener = null;\r
-\r
-    @Override\r
-    public void updatePartName(final ISelection forSelection, Consumer<String> updateCallback) {\r
-        PartNameListener oldListener = currentPartNameListener;\r
-        PartNameListener newListener = new PartNameListener(updateCallback);\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 static String safeGetName(ReadGraph g, Resource r) throws DatabaseException {\r
-        try {\r
-            return g.adapt(r, String.class);\r
-        } catch (AdaptionException e) {\r
-            return NameUtils.getSafeName(g, r);\r
-        }\r
-    }\r
-\r
-    protected String computeTitle(ReadGraph graph, ISelection selection) throws DatabaseException {\r
-        boolean sameTypes = true;\r
-\r
-        try {\r
-\r
-            final ResourceArray[] ras = ResourceAdaptionUtils.toResourceArrays(selection);\r
-            if (ras.length == 0)\r
-                return null;\r
-\r
-            // Check if all the input resource are of the same type.\r
-            Collection<Resource> types = null;\r
-            for (ResourceArray ra : ras) {\r
-                if (ra.isEmpty()) {\r
-                    return null;\r
-                }\r
-                if (types == null) {\r
-                    types = graph.getPrincipalTypes(ra.resources[0]);\r
-                } else {\r
-                    Collection<Resource> ts = graph.getPrincipalTypes(ra.resources[0]);\r
-                    ts.removeAll(types);\r
-                    if (!ts.isEmpty()) {\r
-                        sameTypes = false;\r
-                        break;\r
-                    }\r
-                }\r
-            }\r
-            if (sameTypes) {\r
-                // If the resource no longer exists, provide default name only.\r
-                if (!graph.hasStatement(ras[0].resources[0])) {\r
-                    return null;\r
-                }\r
-\r
-                String name = safeGetName(graph, ras[0].resources[0]);\r
-                if (ras.length > 1)\r
-                    name += " [" + ras.length +"]";\r
-                return name;\r
-            } else {\r
-                Collection<String> names = new ArrayList<String>(ras.length);\r
-                boolean truncate = ras.length > MAX_SELECTION_LENGTH_TO_SHOW;\r
-                int end = Math.min(ras.length, MAX_SELECTION_LENGTH_TO_SHOW);\r
-                int missing = ras.length - end;\r
-                for (int i = 0; i < end; ++i) {\r
-                    // If the resource no longer exists, provide default name only.\r
-                    if (!graph.hasStatement(ras[i].resources[0]))\r
-                        continue;\r
-\r
-                    names.add(safeGetName(graph, ras[i].resources[0]));\r
-                }\r
-                if (names.isEmpty()) {\r
-                    return null;\r
-                }\r
-\r
-                if (truncate)\r
-                    names.add("+ " + missing + " more...");\r
-\r
-                String name = names.toString();\r
-                return name;\r
-            }\r
-\r
-        } catch (Throwable t) {\r
-            t.printStackTrace();\r
-            return null;\r
-        }\r
-\r
-    }\r
-\r
-    /**\r
-     * Part listener which cleans up this page when the source part is closed.\r
-     * This is hooked only when there is a source part.\r
-     */\r
-    private class PartListener implements IPartListener {\r
-        public void partActivated(IWorkbenchPart part) {\r
-        }\r
-\r
-        public void partBroughtToTop(IWorkbenchPart part) {\r
-        }\r
-\r
-        public void partClosed(IWorkbenchPart part) {\r
-            if (sourcePart == part) {\r
-                sourcePart = null;\r
-                sourcePartClosed(part);\r
-            }\r
-        }\r
-\r
-        public void partDeactivated(IWorkbenchPart part) {\r
-        }\r
-\r
-        public void partOpened(IWorkbenchPart part) {\r
-        }\r
-    }\r
-\r
-    private PartListener         partListener = new PartListener();\r
-\r
-    protected IWorkbenchPartSite site;\r
-\r
-    private IWorkbenchPart       sourcePart;\r
-\r
-    protected IAdaptable         adapter;\r
-\r
-    @Override\r
-    public void setAdapter(IAdaptable adapter) {\r
-        assert adapter != null;\r
-        this.adapter = adapter;\r
-    }\r
-\r
-    public IAdaptable getAdapter() {\r
-        assert adapter != null;\r
-        return adapter;\r
-    }\r
-\r
-    @Override\r
-    public void selectionChanged(IWorkbenchPart part, ISelection selection) {\r
-        if (getControl() == null) {\r
-            return;\r
-        }\r
-\r
-        if (sourcePart != null) {\r
-            sourcePart.getSite().getPage().removePartListener(partListener);\r
-            sourcePart = null;\r
-        }\r
-\r
-        // change the viewer input since the workbench selection has changed.\r
-        sourceSelectionChanged(selection);\r
-        sourcePart = part;\r
-\r
-        if (sourcePart != null) {\r
-            sourcePart.getSite().getPage().addPartListener(partListener);\r
-        }\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.browsing.ui.platform;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Consumer;
+
+import org.eclipse.core.runtime.IAdaptable;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.ui.IPartListener;
+import org.eclipse.ui.IWorkbenchPart;
+import org.eclipse.ui.IWorkbenchPartSite;
+import org.eclipse.ui.part.IPageBookViewPage;
+import org.eclipse.ui.part.Page;
+import org.simantics.browsing.ui.GraphExplorer;
+import org.simantics.browsing.ui.swt.IVariablesPage;
+import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite;
+import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite.InputSource;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.ResourceArray;
+import org.simantics.db.common.utils.NameUtils;
+import org.simantics.db.exception.AdaptionException;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.management.ISessionContext;
+import org.simantics.db.management.ISessionContextChangedListener;
+import org.simantics.db.management.ISessionContextProvider;
+import org.simantics.db.management.SessionContextChangedEvent;
+import org.simantics.db.procedure.Listener;
+import org.simantics.db.request.Read;
+import org.simantics.ui.utils.ResourceAdaptionUtils;
+import org.simantics.utils.ui.AdaptionUtils;
+import org.simantics.utils.ui.ErrorLogger;
+
+/**
+ * <p>
+ * Subclasses may extend or reimplement the following methods as required:
+ * <ul>
+ *   <li><code>createPageControls</code> - to create the page's controls</li>
+ *   <li><code>getControl</code> - to retrieve the page's control</li>
+ *   <li><code>setFocus</code> - implement to accept focus</li>
+ *   <li><code>sourceSelectionChanged</code> - puts the incoming ISelection into use in this page</li>
+ *   <li><code>sourcePartClosed</code> - cleans up the page controls after a current selection source part has been closed</li>
+ *   <li><code>dispose</code> - extend to provide additional cleanup</li>
+ *   <li><code>setActionBars</code> - reimplement to make contributions</li>
+ *   <li><code>makeContributions</code> - this method exists to support previous versions</li>
+ *   <li><code>setActionBars</code> - this method exists to support previous versions</li>
+ *   <li><code>init</code> - extend to provide additional setup</li>
+ *   <li><code>sessionContextChanged</code> - reimplement to take actions when the source database session changes</li>
+ * </ul>
+ * </p>
+ * 
+ * @author Tuukka Lehtonen
+ */
+public class VariablesPage extends Page implements IPageBookViewPage, IVariablesPage {
+
+    protected static final int MAX_SELECTION_LENGTH_TO_SHOW = 5;
+
+    protected ISessionContext sessionContext;
+
+    protected GraphExplorerComposite explorer;
+
+    /**
+     * @param site the workbench part site that contains this page or
+     *        <code>null</code> if there is no site, i.e. the page is within a
+     *        dialog or a plain shell.
+     */
+    public VariablesPage(IWorkbenchPartSite site) {
+       this.site = site;
+    }
+
+    /**
+     * @param site the workbench part site that contains this page or
+     *        <code>null</code> if there is no site, i.e. the page is within a
+     *        dialog or a plain shell.
+     * @param adapter must provide an adapter for
+     *        <code>ISessionContextProvider.class</code>
+     */
+    public VariablesPage(IWorkbenchPartSite site, IAdaptable adapter) {
+        this(site);
+        setAdapter(adapter);
+    }
+
+    @Override
+    public void dispose() {
+       
+        // Stop listening for title changes.
+        if (currentPartNameListener != null)
+            currentPartNameListener.dispose();
+
+        if (adapter != null) {
+            ISessionContextProvider contextProvider = getSessionContextProvider();
+            contextProvider.removeContextChangedListener(contextChangeListener);
+        }
+
+        if (sourcePart != null) {
+            sourcePart.getSite().getPage().removePartListener(partListener);
+            sourcePart = null;
+        }
+
+        site = null;
+        adapter = null;
+        explorer = null;
+        sessionContext = null;
+        
+    }
+
+    protected ISessionContextProvider getSessionContextProvider() {
+        return (ISessionContextProvider) getAdapter().getAdapter(ISessionContextProvider.class);
+    }
+
+    protected ISessionContext getSessionContext() {
+        return sessionContext;
+    }
+
+    @Override
+    public final void createControl(Composite parent) {
+        createPageControls(parent);
+
+        // Attach to current session context provider to keep the UI intact even
+        // when the current UI session changes.
+        ISessionContextProvider contextProvider = getSessionContextProvider();
+        contextProvider.addContextChangedListener(contextChangeListener);
+        setSessionContext(contextProvider.getSessionContext());
+    }
+
+    /**
+     * Override to customize the UI component created on this page.
+     * 
+     * @param parent
+     */
+    protected void createPageControls(Composite parent) {
+
+       Map<String, Object> args = new HashMap<String, Object>();
+       Set<String> browseContexts = new HashSet<String>();
+       
+       browseContexts.add("org.simantics.browsing.ui.graph.variablesView");
+       
+       args.put("browseContexts", browseContexts);
+       
+       explorer = new GraphExplorerComposite(args, site, parent, SWT.NONE);
+       
+       explorer.setInputSource(new InputSource() {
+
+            @Override
+            public Object get(ISessionContext ctx, Object selection) {
+
+               final Resource input = AdaptionUtils.adaptToSingle(selection, Resource.class);
+               if(input == null) {
+                    return GraphExplorer.EMPTY_INPUT;
+               }
+               
+               final VariablePrefix prefix =  AdaptionUtils.adaptToSingle(selection, VariablePrefix.class);
+               if(prefix == null) {
+                       return new VariablesInput(null, input);
+               } else {
+                       return new VariablesInput(prefix.getPrefix(), input);
+               }
+
+            }
+               
+       });
+       
+       }
+       
+
+    /**
+     * @param newContext
+     */
+    protected final void setSessionContext(ISessionContext newContext) {
+        ISessionContext oldContext = this.sessionContext;
+        this.sessionContext = newContext;
+//        System.out.println("AbstractPropertyPage.setSessionContext: " + oldContext + " -> " + newContext);
+
+        sessionContextChanged(oldContext, newContext);
+    }
+
+    /**
+     * @param oldContext
+     * @param newContext
+     */
+    protected void sessionContextChanged(ISessionContext oldContext, ISessionContext newContext) {
+       explorer.applySessionContext(newContext);
+//        explorer.setSessionContext(newContext);
+    }
+
+    protected ISessionContextChangedListener contextChangeListener = new ISessionContextChangedListener() {
+        @Override
+        public void sessionContextChanged(SessionContextChangedEvent event) {
+            setSessionContext(event.getNewValue());
+        }
+    };
+
+    @Override
+    public Control getControl() {
+        return (explorer != null) ? explorer : null;
+    }
+
+    @Override
+    public ISelection getSelection() {
+//        if (!explorer.isDisposed()) {
+//            return explorer.getSelection();
+//        }
+        return null;
+    }
+
+    /**
+     * Sets focus to a part in the page.
+     * @see org.eclipse.ui.part.Page#setFocus()
+     */
+    @Override
+    public void setFocus() {
+//        if (explorer != null && !explorer.isDisposed()) {
+//             explorer.requestFocus();
+//        }
+    }
+
+    protected void sourcePartClosed(IWorkbenchPart part) {
+//        if (!explorer.isDisposed()) {
+//             explorer.setInput(StructuredSelection.EMPTY, false);
+//        }
+    }
+
+    protected void sourceSelectionChanged(ISelection selection) {
+        if (!explorer.isDisposed()) {
+               explorer.setInput(selection, false);
+        }
+    }
+
+    static class PartNameListener implements Listener<String> {
+        private boolean disposed = false;
+        private final Consumer<String> updateCallback;
+
+        public PartNameListener(Consumer<String> updateCallback) {
+            assert updateCallback != null;
+            this.updateCallback = updateCallback;
+        }
+
+        public void dispose() {
+            disposed = true;
+        }
+
+        @Override
+        public boolean isDisposed() {
+            return disposed;
+        }
+
+        @Override
+        public void execute(String result) {
+            //System.out.println("part name changed: " + result);
+            updateCallback.accept(result);
+        }
+
+        @Override
+        public void exception(Throwable t) {
+            ErrorLogger.defaultLogError(t);
+        }
+    }
+
+    PartNameListener currentPartNameListener = null;
+
+    @Override
+    public void updatePartName(final ISelection forSelection, Consumer<String> updateCallback) {
+        PartNameListener oldListener = currentPartNameListener;
+        PartNameListener newListener = new PartNameListener(updateCallback);
+        if (oldListener != null)
+            oldListener.dispose();
+
+        if (sessionContext != null) {
+            sessionContext.getSession().asyncRequest(new Read<String>() {
+                @Override
+                public String perform(ReadGraph graph) throws DatabaseException {
+                    return computeTitle(graph, forSelection);
+                }
+            }, newListener);
+        }
+    }
+
+    protected static String safeGetName(ReadGraph g, Resource r) throws DatabaseException {
+        try {
+            return g.adapt(r, String.class);
+        } catch (AdaptionException e) {
+            return NameUtils.getSafeName(g, r);
+        }
+    }
+
+    protected String computeTitle(ReadGraph graph, ISelection selection) throws DatabaseException {
+        boolean sameTypes = true;
+
+        try {
+
+            final ResourceArray[] ras = ResourceAdaptionUtils.toResourceArrays(selection);
+            if (ras.length == 0)
+                return null;
+
+            // Check if all the input resource are of the same type.
+            Collection<Resource> types = null;
+            for (ResourceArray ra : ras) {
+                if (ra.isEmpty()) {
+                    return null;
+                }
+                if (types == null) {
+                    types = graph.getPrincipalTypes(ra.resources[0]);
+                } else {
+                    Collection<Resource> ts = graph.getPrincipalTypes(ra.resources[0]);
+                    ts.removeAll(types);
+                    if (!ts.isEmpty()) {
+                        sameTypes = false;
+                        break;
+                    }
+                }
+            }
+            if (sameTypes) {
+                // If the resource no longer exists, provide default name only.
+                if (!graph.hasStatement(ras[0].resources[0])) {
+                    return null;
+                }
+
+                String name = safeGetName(graph, ras[0].resources[0]);
+                if (ras.length > 1)
+                    name += " [" + ras.length +"]";
+                return name;
+            } else {
+                Collection<String> names = new ArrayList<String>(ras.length);
+                boolean truncate = ras.length > MAX_SELECTION_LENGTH_TO_SHOW;
+                int end = Math.min(ras.length, MAX_SELECTION_LENGTH_TO_SHOW);
+                int missing = ras.length - end;
+                for (int i = 0; i < end; ++i) {
+                    // If the resource no longer exists, provide default name only.
+                    if (!graph.hasStatement(ras[i].resources[0]))
+                        continue;
+
+                    names.add(safeGetName(graph, ras[i].resources[0]));
+                }
+                if (names.isEmpty()) {
+                    return null;
+                }
+
+                if (truncate)
+                    names.add("+ " + missing + " more...");
+
+                String name = names.toString();
+                return name;
+            }
+
+        } catch (Throwable t) {
+            t.printStackTrace();
+            return null;
+        }
+
+    }
+
+    /**
+     * Part listener which cleans up this page when the source part is closed.
+     * This is hooked only when there is a source part.
+     */
+    private class PartListener implements IPartListener {
+        public void partActivated(IWorkbenchPart part) {
+        }
+
+        public void partBroughtToTop(IWorkbenchPart part) {
+        }
+
+        public void partClosed(IWorkbenchPart part) {
+            if (sourcePart == part) {
+                sourcePart = null;
+                sourcePartClosed(part);
+            }
+        }
+
+        public void partDeactivated(IWorkbenchPart part) {
+        }
+
+        public void partOpened(IWorkbenchPart part) {
+        }
+    }
+
+    private PartListener         partListener = new PartListener();
+
+    protected IWorkbenchPartSite site;
+
+    private IWorkbenchPart       sourcePart;
+
+    protected IAdaptable         adapter;
+
+    @Override
+    public void setAdapter(IAdaptable adapter) {
+        assert adapter != null;
+        this.adapter = adapter;
+    }
+
+    public IAdaptable getAdapter() {
+        assert adapter != null;
+        return adapter;
+    }
+
+    @Override
+    public void selectionChanged(IWorkbenchPart part, ISelection selection) {
+        if (getControl() == null) {
+            return;
+        }
+
+        if (sourcePart != null) {
+            sourcePart.getSite().getPage().removePartListener(partListener);
+            sourcePart = null;
+        }
+
+        // change the viewer input since the workbench selection has changed.
+        sourceSelectionChanged(selection);
+        sourcePart = part;
+
+        if (sourcePart != null) {
+            sourcePart.getSite().getPage().addPartListener(partListener);
+        }
+    }
+    
+}