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