/******************************************************************************* * Copyright (c) 2007, 2010 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: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package org.simantics.modeling.ui.modelBrowser.handlers; 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.PlatformUI; import org.eclipse.ui.handlers.HandlerUtil; import org.simantics.Simantics; import org.simantics.databoard.Bindings; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.common.request.ResourceRead; import org.simantics.db.common.utils.Logger; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.variable.Variable; import org.simantics.modeling.ModelingResources; import org.simantics.modeling.PropertyVariables; import org.simantics.ui.selection.WorkbenchSelectionUtils; import org.simantics.utils.ui.AdaptionUtils; public class ContextualHelp extends AbstractHandler { private static String getPossibleId(ExecutionEvent event) { String id = null; try { Resource element = WorkbenchSelectionUtils.getPossibleResource(event); id = Simantics.getSession().syncRequest(new ResourceRead(element) { @Override public String perform(ReadGraph graph) throws DatabaseException { ModelingResources MOD = ModelingResources.getInstance(graph); Resource component = graph.getPossibleObject(element, MOD.ElementToComponent); if (component != null) return graph.getPossibleRelatedValue2(component, MOD.contextualHelpId, Bindings.STRING); Variable var = WorkbenchSelectionUtils.getPossibleVariable(event); if (var != null) return var.getPossiblePropertyValue(graph, MOD.contextualHelpId, Bindings.STRING); ISelection sel = HandlerUtil.getCurrentSelection(event); if (sel != null) { PropertyVariables vars = AdaptionUtils.adaptToSingle(sel, PropertyVariables.class); if (vars != null) { var = vars.getConfiguration(); if (var != null) var.getPossiblePropertyValue(graph, MOD.contextualHelpId, Bindings.STRING); } } return null; } }); } catch (DatabaseException e) { Logger.defaultLogError(e); } return id; } @Override public Object execute(ExecutionEvent event) throws ExecutionException { String id = getPossibleId(event); if (id != null) PlatformUI.getWorkbench().getHelpSystem().displayHelp(id); return null; } }