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
10 * Semantum Oy - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.debug.ui;
\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
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
43 * @author Antti Villberg
\r
44 * @author Tuukka Lehtonen
\r
46 public class ShowInBrowser extends AbstractHandler {
\r
48 private static final String DEFAULT_BROWSER_VIEW_ID = "org.simantics.modeling.ui.browser"; //$NON-NLS-1$
\r
50 private static Read<Collection<NodeContext>> getParentsRequest(BrowseContext bc, NodeContext context) {
\r
51 return new UniqueRead<Collection<NodeContext>>() {
\r
53 public Collection<NodeContext> perform(ReadGraph graph) throws DatabaseException {
\r
54 return bc.getParents(graph, context);
\r
59 private static Read<NodeType> getNodeTypeRequest(Resource element) {
\r
60 return new UniqueRead<NodeType>() {
\r
62 public NodeType perform(ReadGraph graph) throws DatabaseException {
\r
63 return EntityNodeType.getNodeTypeFor(graph, element);
\r
68 private static Collection<NodeContext> tryGetParents(BrowseContext bc, NodeContext context) {
\r
70 return Simantics.getSession().syncRequest(getParentsRequest(bc, context));
\r
71 } catch (DatabaseException e) {
\r
72 return Collections.emptyList();
\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
80 for (NodeContext parent : tryGetParents(browseContext, context)) {
\r
81 path.addFirst(parent);
\r
82 if (show(explorer, browseContext, parent, path))
\r
90 public static Object defaultExecute(ISelection selection, String browserViewId) {
\r
91 IViewPart browser = (IViewPart) WorkbenchUtils.findView(browserViewId);
\r
92 if (browser == null)
\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
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
107 if (show(explorer, browseContext, context, path)) {
\r
108 WorkbenchUtils.activateView(browserViewId);
\r
110 } catch (DatabaseException e) {
\r
111 } catch (PartInitException e) {
\r
117 public static Object defaultExecute(ISelection selection) {
\r
118 return defaultExecute(selection, DEFAULT_BROWSER_VIEW_ID);
\r
122 public Object execute(ExecutionEvent event) throws ExecutionException {
\r
123 return defaultExecute(HandlerUtil.getCurrentSelectionChecked(event));
\r