-/*******************************************************************************\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
+/*******************************************************************************
+ * Copyright (c) 2016 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Semantum Oy - initial API and implementation
+ *******************************************************************************/
+package org.simantics.debug.ui;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Deque;
+import java.util.LinkedList;
+
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.ui.IViewPart;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.handlers.HandlerUtil;
+import org.simantics.Simantics;
+import org.simantics.browsing.ui.GraphExplorer;
+import org.simantics.browsing.ui.NodeContext;
+import org.simantics.browsing.ui.common.NodeContextBuilder;
+import org.simantics.browsing.ui.model.browsecontexts.BrowseContext;
+import org.simantics.browsing.ui.model.nodetypes.EntityNodeType;
+import org.simantics.browsing.ui.model.nodetypes.NodeType;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.request.UniqueRead;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.request.Read;
+import org.simantics.ui.selection.WorkbenchSelectionUtils;
+import org.simantics.utils.ui.workbench.WorkbenchUtils;
+
+/**
+ * @author Antti Villberg
+ * @author Tuukka Lehtonen
+ */
+public class ShowInBrowser extends AbstractHandler {
+
+ private static final String DEFAULT_BROWSER_VIEW_ID = "org.simantics.modeling.ui.browser"; //$NON-NLS-1$
+
+ private static Read<Collection<NodeContext>> getParentsRequest(BrowseContext bc, NodeContext context) {
+ return new UniqueRead<Collection<NodeContext>>() {
+ @Override
+ public Collection<NodeContext> perform(ReadGraph graph) throws DatabaseException {
+ return bc.getParents(graph, context);
+ }
+ };
+ }
+
+ private static Read<NodeType> getNodeTypeRequest(Resource element) {
+ return new UniqueRead<NodeType>() {
+ @Override
+ public NodeType perform(ReadGraph graph) throws DatabaseException {
+ return EntityNodeType.getNodeTypeFor(graph, element);
+ }
+ };
+ }
+
+ private static Collection<NodeContext> tryGetParents(BrowseContext bc, NodeContext context) {
+ try {
+ return Simantics.getSession().syncRequest(getParentsRequest(bc, context));
+ } catch (DatabaseException e) {
+ return Collections.emptyList();
+ }
+ }
+
+ private static boolean show(GraphExplorer explorer, BrowseContext browseContext, NodeContext context, Deque<NodeContext> path) {
+ if (explorer.isVisible(context))
+ return explorer.selectPath(path);
+ else {
+ for (NodeContext parent : tryGetParents(browseContext, context)) {
+ path.addFirst(parent);
+ if (show(explorer, browseContext, parent, path))
+ return true;
+ path.removeFirst();
+ }
+ }
+ return false;
+ }
+
+ public static Object defaultExecute(ISelection selection, String browserViewId) {
+ IViewPart browser = (IViewPart) WorkbenchUtils.findView(browserViewId);
+ if (browser == null)
+ return null;
+
+ GraphExplorer explorer = (GraphExplorer) browser.getAdapter(GraphExplorer.class);
+ BrowseContext browseContext = (BrowseContext) browser.getAdapter(BrowseContext.class);
+ if (explorer == null || browseContext == null)
+ return null;
+
+ try {
+ final Resource element = WorkbenchSelectionUtils.getPossibleResource(selection);
+ WorkbenchUtils.showView(browserViewId, IWorkbenchPage.VIEW_VISIBLE);
+ NodeType nodeType = Simantics.getSession().syncRequest(getNodeTypeRequest(element));;
+ NodeContext context = NodeContextBuilder.buildWithData(NodeType.KEY_SEQUENCE, new Object[] { element, nodeType });
+ Deque<NodeContext> path = new LinkedList<>();
+ path.add(context);
+ if (show(explorer, browseContext, context, path)) {
+ WorkbenchUtils.activateView(browserViewId);
+ }
+ } catch (DatabaseException e) {
+ } catch (PartInitException e) {
+ }
+
+ return null;
+ }
+
+ public static Object defaultExecute(ISelection selection) {
+ return defaultExecute(selection, DEFAULT_BROWSER_VIEW_ID);
+ }
+
+ @Override
+ public Object execute(ExecutionEvent event) throws ExecutionException {
+ return defaultExecute(HandlerUtil.getCurrentSelectionChecked(event));
+ }
+
}
\ No newline at end of file