]> gerrit.simantics Code Review - simantics/platform.git/blob
c9b8af6d38d81cac36914a46798542c20bff47ee
[simantics/platform.git] /
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.ui.documents;
13
14 import java.net.MalformedURLException;
15 import java.net.URL;
16
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;
33
34 public class OpenMappedDocumentAdapter extends AbstractResourceEditorAdapter {
35
36     private static final String EDITOR_ID = "org.simantics.document.ui.editor";
37
38     public OpenMappedDocumentAdapter() throws MalformedURLException {
39         super("Document Editor", ImageDescriptor.createFromURL(new URL("platform:/plugin/com.famfamfam.silk/icons/table.png")));
40     }
41
42     protected String getEditorId() {
43         return EDITOR_ID;
44     }
45
46     @Override
47     public boolean canHandle(ReadGraph g, Resource r) throws DatabaseException {
48         return g.isInstanceOf(r, DiagramResource.getInstance(g).Element);
49     }
50
51     @Override
52     public void openEditor(final Resource element) throws Exception {
53         Simantics.getSession().asyncRequest(new ReadRequest() {
54             @Override
55             public void run(ReadGraph graph) throws DatabaseException {
56
57                 final Resource component = graph.getPossibleObject(element, ModelingResources.getInstance(graph).ElementToComponent);
58                 if(component == null) return;
59
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);
65
66                 PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
67                     @Override
68                     public void run() {
69                         try {
70                             String editorId = getEditorId();
71                             if(rvi != null)
72                                 WorkbenchUtils.openEditor(editorId, new ResourceEditorInput2(editorId, component, model, rvi));
73 //                            else 
74 //                              WorkbenchUtils.openEditor(editorId, new ResourceEditorInput2(editorId, component, variableURI, null));
75                         } catch (PartInitException e) {
76                             e.printStackTrace();
77                         }
78                     }
79                 });
80             }
81         });
82     }
83
84 }