1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 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 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.ui.documents;
14 import java.net.MalformedURLException;
17 import org.eclipse.jface.resource.ImageDescriptor;
18 import org.eclipse.ui.PartInitException;
19 import org.eclipse.ui.PlatformUI;
20 import org.simantics.Simantics;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.Resource;
23 import org.simantics.db.common.request.ReadRequest;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.layer0.variable.RVI;
26 import org.simantics.db.layer0.variable.Variable;
27 import org.simantics.db.layer0.variable.Variables;
28 import org.simantics.diagram.stubs.DiagramResource;
29 import org.simantics.modeling.ModelingResources;
30 import org.simantics.ui.workbench.ResourceEditorInput2;
31 import org.simantics.ui.workbench.editor.AbstractResourceEditorAdapter;
32 import org.simantics.utils.ui.workbench.WorkbenchUtils;
34 public class OpenMappedDocumentAdapter extends AbstractResourceEditorAdapter {
36 private static final String EDITOR_ID = "org.simantics.document.ui.editor";
38 public OpenMappedDocumentAdapter() throws MalformedURLException {
39 super("Document Editor", ImageDescriptor.createFromURL(new URL("platform:/plugin/com.famfamfam.silk/icons/table.png")));
42 protected String getEditorId() {
47 public boolean canHandle(ReadGraph g, Resource r) throws DatabaseException {
48 return g.isInstanceOf(r, DiagramResource.getInstance(g).Element);
52 public void openEditor(final Resource element) throws Exception {
53 Simantics.getSession().asyncRequest(new ReadRequest() {
55 public void run(ReadGraph graph) throws DatabaseException {
57 final Resource component = graph.getPossibleObject(element, ModelingResources.getInstance(graph).ElementToComponent);
58 if(component == null) return;
60 Variable variable = graph.adapt(component, Variable.class);
61 final Resource model = Variables.getModel(graph, variable);
62 final RVI rvi = variable.getRVI(graph);
63 // final String RVI = Variables.getPossibleRVI(graph, variable);
64 // final String variableURI = variable.getURI(graph);
66 PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
70 String editorId = getEditorId();
72 WorkbenchUtils.openEditor(editorId, new ResourceEditorInput2(editorId, component, model, rvi));
74 // WorkbenchUtils.openEditor(editorId, new ResourceEditorInput2(editorId, component, variableURI, null));
75 } catch (PartInitException e) {