]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/OpenDiagramAdapter.java
fd1e6360547a13fce458d4ba781d83affe5444cd
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / diagramEditor / OpenDiagramAdapter.java
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.diagramEditor;
13
14 import org.eclipse.ui.PartInitException;
15 import org.eclipse.ui.PlatformUI;
16 import org.simantics.db.ReadGraph;
17 import org.simantics.db.Resource;
18 import org.simantics.db.common.request.ReadRequest;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.db.layer0.request.PossibleModel;
21 import org.simantics.db.layer0.variable.RVI;
22 import org.simantics.diagram.stubs.DiagramResource;
23 import org.simantics.modeling.ui.Activator;
24 import org.simantics.ui.SimanticsUI;
25 import org.simantics.ui.workbench.ResourceEditorInput2;
26 import org.simantics.ui.workbench.editor.AbstractResourceEditorAdapter;
27 import org.simantics.utils.ui.ErrorLogger;
28 import org.simantics.utils.ui.workbench.WorkbenchUtils;
29
30 /**
31  * @author Tuukka Lehtonen
32  */
33 public class OpenDiagramAdapter extends AbstractResourceEditorAdapter {
34
35     private static final String EDITOR_ID = "org.simantics.modeling.ui.plainDiagramEditor";
36
37     public OpenDiagramAdapter() {
38         super("Diagram Editor", Activator.COMPOSITE_ICON);
39     }
40
41     protected String getEditorId() {
42         return EDITOR_ID;
43     }
44
45     @Override
46     public boolean canHandle(ReadGraph g, Resource r) throws DatabaseException {
47         return g.isInstanceOf(r, DiagramResource.getInstance(g).Diagram);
48     }
49
50     @Override
51     public void openEditor(final Resource r) throws Exception {
52         
53         SimanticsUI.getSession().asyncRequest(new ReadRequest() {
54                 
55             @Override
56             public void run(ReadGraph g) throws DatabaseException {
57
58                 final Resource model = g.sync(new PossibleModel(r));
59                 if(model == null) return;
60
61                 PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
62                     @Override
63                     public void run() {
64                         try {
65                                 String editorId = getEditorId();
66                                 WorkbenchUtils.openEditor(editorId, new ResourceEditorInput2(editorId, r, model, (RVI)null));
67                         } catch (PartInitException e) {
68                                 ErrorLogger.defaultLogError(e);
69                         }
70                     }
71                 });
72             }
73         });
74     }
75
76 }