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