]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2016 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     Semantum Oy - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.debug.ui;\r
13 \r
14 import java.util.Collection;\r
15 import java.util.Collections;\r
16 import java.util.Deque;\r
17 import java.util.LinkedList;\r
18 \r
19 import org.eclipse.core.commands.AbstractHandler;\r
20 import org.eclipse.core.commands.ExecutionEvent;\r
21 import org.eclipse.core.commands.ExecutionException;\r
22 import org.eclipse.jface.viewers.ISelection;\r
23 import org.eclipse.ui.IViewPart;\r
24 import org.eclipse.ui.IWorkbenchPage;\r
25 import org.eclipse.ui.PartInitException;\r
26 import org.eclipse.ui.handlers.HandlerUtil;\r
27 import org.simantics.Simantics;\r
28 import org.simantics.browsing.ui.GraphExplorer;\r
29 import org.simantics.browsing.ui.NodeContext;\r
30 import org.simantics.browsing.ui.common.NodeContextBuilder;\r
31 import org.simantics.browsing.ui.model.browsecontexts.BrowseContext;\r
32 import org.simantics.browsing.ui.model.nodetypes.EntityNodeType;\r
33 import org.simantics.browsing.ui.model.nodetypes.NodeType;\r
34 import org.simantics.db.ReadGraph;\r
35 import org.simantics.db.Resource;\r
36 import org.simantics.db.common.request.UniqueRead;\r
37 import org.simantics.db.exception.DatabaseException;\r
38 import org.simantics.db.request.Read;\r
39 import org.simantics.ui.selection.WorkbenchSelectionUtils;\r
40 import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
41 \r
42 /**\r
43  * @author Antti Villberg\r
44  * @author Tuukka Lehtonen\r
45  */\r
46 public class ShowInBrowser extends AbstractHandler {\r
47 \r
48         private static final String DEFAULT_BROWSER_VIEW_ID = "org.simantics.modeling.ui.browser"; //$NON-NLS-1$\r
49 \r
50         private static Read<Collection<NodeContext>> getParentsRequest(BrowseContext bc, NodeContext context) {\r
51                 return new UniqueRead<Collection<NodeContext>>() {\r
52                         @Override\r
53                         public Collection<NodeContext> perform(ReadGraph graph) throws DatabaseException {\r
54                                 return bc.getParents(graph, context);\r
55                         }\r
56                 };\r
57         }\r
58 \r
59         private static Read<NodeType> getNodeTypeRequest(Resource element) {\r
60                 return new UniqueRead<NodeType>() {\r
61                         @Override\r
62                         public NodeType perform(ReadGraph graph) throws DatabaseException {\r
63                                 return EntityNodeType.getNodeTypeFor(graph, element);\r
64                         }\r
65                 };\r
66         }\r
67 \r
68         private static Collection<NodeContext> tryGetParents(BrowseContext bc, NodeContext context) {\r
69                 try {\r
70                         return Simantics.getSession().syncRequest(getParentsRequest(bc, context));\r
71                 } catch (DatabaseException e) {\r
72                         return Collections.emptyList();\r
73                 }\r
74         }\r
75 \r
76         private static boolean show(GraphExplorer explorer, BrowseContext browseContext, NodeContext context, Deque<NodeContext> path) {\r
77                 if (explorer.isVisible(context))\r
78                         return explorer.selectPath(path);\r
79                 else {\r
80                         for (NodeContext parent : tryGetParents(browseContext, context)) {\r
81                                 path.addFirst(parent);\r
82                                 if (show(explorer, browseContext, parent, path))\r
83                                         return true;\r
84                                 path.removeFirst();\r
85                         }\r
86                 }\r
87                 return false;\r
88         }\r
89 \r
90         public static Object defaultExecute(ISelection selection, String browserViewId) {\r
91                 IViewPart browser = (IViewPart) WorkbenchUtils.findView(browserViewId);\r
92                 if (browser == null)\r
93                         return null;\r
94 \r
95                 GraphExplorer explorer = (GraphExplorer) browser.getAdapter(GraphExplorer.class);\r
96                 BrowseContext browseContext = (BrowseContext) browser.getAdapter(BrowseContext.class);\r
97                 if (explorer == null || browseContext == null)\r
98                         return null;\r
99 \r
100                 try {\r
101                         final Resource element = WorkbenchSelectionUtils.getPossibleResource(selection);\r
102                         WorkbenchUtils.showView(browserViewId, IWorkbenchPage.VIEW_VISIBLE);\r
103                         NodeType nodeType = Simantics.getSession().syncRequest(getNodeTypeRequest(element));;\r
104                         NodeContext context = NodeContextBuilder.buildWithData(NodeType.KEY_SEQUENCE, new Object[] { element, nodeType });\r
105                         Deque<NodeContext> path = new LinkedList<>();\r
106                         path.add(context);\r
107                         if (show(explorer, browseContext, context, path)) {\r
108                                 WorkbenchUtils.activateView(browserViewId);\r
109                         }\r
110                 } catch (DatabaseException e) {\r
111                 } catch (PartInitException e) {\r
112                 }\r
113 \r
114                 return null;\r
115         }\r
116 \r
117         public static Object defaultExecute(ISelection selection) {\r
118                 return defaultExecute(selection, DEFAULT_BROWSER_VIEW_ID);\r
119         }\r
120 \r
121         @Override\r
122         public Object execute(ExecutionEvent event) throws ExecutionException {\r
123                 return defaultExecute(HandlerUtil.getCurrentSelectionChecked(event));\r
124         }\r
125 \r
126 }