]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.debug.ui/src/org/simantics/debug/ui/ShowInBrowser.java
Re-enabled CTRL+SHIFT+R resource search dialog in org.simantics.debug.ui
[simantics/platform.git] / bundles / org.simantics.debug.ui / src / org / simantics / debug / ui / ShowInBrowser.java
diff --git a/bundles/org.simantics.debug.ui/src/org/simantics/debug/ui/ShowInBrowser.java b/bundles/org.simantics.debug.ui/src/org/simantics/debug/ui/ShowInBrowser.java
new file mode 100644 (file)
index 0000000..54acdd6
--- /dev/null
@@ -0,0 +1,126 @@
+/*******************************************************************************\r
+ * Copyright (c) 2016 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
+ *     Semantum Oy - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.debug.ui;\r
+\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+import java.util.Deque;\r
+import java.util.LinkedList;\r
+\r
+import org.eclipse.core.commands.AbstractHandler;\r
+import org.eclipse.core.commands.ExecutionEvent;\r
+import org.eclipse.core.commands.ExecutionException;\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.eclipse.ui.IViewPart;\r
+import org.eclipse.ui.IWorkbenchPage;\r
+import org.eclipse.ui.PartInitException;\r
+import org.eclipse.ui.handlers.HandlerUtil;\r
+import org.simantics.Simantics;\r
+import org.simantics.browsing.ui.GraphExplorer;\r
+import org.simantics.browsing.ui.NodeContext;\r
+import org.simantics.browsing.ui.common.NodeContextBuilder;\r
+import org.simantics.browsing.ui.model.browsecontexts.BrowseContext;\r
+import org.simantics.browsing.ui.model.nodetypes.EntityNodeType;\r
+import org.simantics.browsing.ui.model.nodetypes.NodeType;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.UniqueRead;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.ui.selection.WorkbenchSelectionUtils;\r
+import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
+\r
+/**\r
+ * @author Antti Villberg\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class ShowInBrowser extends AbstractHandler {\r
+\r
+       private static final String DEFAULT_BROWSER_VIEW_ID = "org.simantics.modeling.ui.browser"; //$NON-NLS-1$\r
+\r
+       private static Read<Collection<NodeContext>> getParentsRequest(BrowseContext bc, NodeContext context) {\r
+               return new UniqueRead<Collection<NodeContext>>() {\r
+                       @Override\r
+                       public Collection<NodeContext> perform(ReadGraph graph) throws DatabaseException {\r
+                               return bc.getParents(graph, context);\r
+                       }\r
+               };\r
+       }\r
+\r
+       private static Read<NodeType> getNodeTypeRequest(Resource element) {\r
+               return new UniqueRead<NodeType>() {\r
+                       @Override\r
+                       public NodeType perform(ReadGraph graph) throws DatabaseException {\r
+                               return EntityNodeType.getNodeTypeFor(graph, element);\r
+                       }\r
+               };\r
+       }\r
+\r
+       private static Collection<NodeContext> tryGetParents(BrowseContext bc, NodeContext context) {\r
+               try {\r
+                       return Simantics.getSession().syncRequest(getParentsRequest(bc, context));\r
+               } catch (DatabaseException e) {\r
+                       return Collections.emptyList();\r
+               }\r
+       }\r
+\r
+       private static boolean show(GraphExplorer explorer, BrowseContext browseContext, NodeContext context, Deque<NodeContext> path) {\r
+               if (explorer.isVisible(context))\r
+                       return explorer.selectPath(path);\r
+               else {\r
+                       for (NodeContext parent : tryGetParents(browseContext, context)) {\r
+                               path.addFirst(parent);\r
+                               if (show(explorer, browseContext, parent, path))\r
+                                       return true;\r
+                               path.removeFirst();\r
+                       }\r
+               }\r
+               return false;\r
+       }\r
+\r
+       public static Object defaultExecute(ISelection selection, String browserViewId) {\r
+               IViewPart browser = (IViewPart) WorkbenchUtils.findView(browserViewId);\r
+               if (browser == null)\r
+                       return null;\r
+\r
+               GraphExplorer explorer = (GraphExplorer) browser.getAdapter(GraphExplorer.class);\r
+               BrowseContext browseContext = (BrowseContext) browser.getAdapter(BrowseContext.class);\r
+               if (explorer == null || browseContext == null)\r
+                       return null;\r
+\r
+               try {\r
+                       final Resource element = WorkbenchSelectionUtils.getPossibleResource(selection);\r
+                       WorkbenchUtils.showView(browserViewId, IWorkbenchPage.VIEW_VISIBLE);\r
+                       NodeType nodeType = Simantics.getSession().syncRequest(getNodeTypeRequest(element));;\r
+                       NodeContext context = NodeContextBuilder.buildWithData(NodeType.KEY_SEQUENCE, new Object[] { element, nodeType });\r
+                       Deque<NodeContext> path = new LinkedList<>();\r
+                       path.add(context);\r
+                       if (show(explorer, browseContext, context, path)) {\r
+                               WorkbenchUtils.activateView(browserViewId);\r
+                       }\r
+               } catch (DatabaseException e) {\r
+               } catch (PartInitException e) {\r
+               }\r
+\r
+               return null;\r
+       }\r
+\r
+       public static Object defaultExecute(ISelection selection) {\r
+               return defaultExecute(selection, DEFAULT_BROWSER_VIEW_ID);\r
+       }\r
+\r
+       @Override\r
+       public Object execute(ExecutionEvent event) throws ExecutionException {\r
+               return defaultExecute(HandlerUtil.getCurrentSelectionChecked(event));\r
+       }\r
+\r
+}
\ No newline at end of file