]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.browsing.ui.platform/src/org/simantics/browsing/ui/platform/VariablesPage.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.platform / src / org / simantics / browsing / ui / platform / VariablesPage.java
diff --git a/bundles/org.simantics.browsing.ui.platform/src/org/simantics/browsing/ui/platform/VariablesPage.java b/bundles/org.simantics.browsing.ui.platform/src/org/simantics/browsing/ui/platform/VariablesPage.java
new file mode 100644 (file)
index 0000000..394859d
--- /dev/null
@@ -0,0 +1,434 @@
+/*******************************************************************************\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