]> gerrit.simantics Code Review - simantics/platform.git/blob
8739f904f1440292433e910ccc2a233a8d59a68a
[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.diagramEditor;
13
14 import java.util.Collection;
15
16 import org.eclipse.ui.PartInitException;
17 import org.eclipse.ui.PlatformUI;
18 import org.simantics.Simantics;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.common.request.PossibleIndexRoot;
22 import org.simantics.db.common.request.ReadRequest;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.db.layer0.variable.RVI;
25 import org.simantics.diagram.stubs.DiagramResource;
26 import org.simantics.modeling.ModelingResources;
27 import org.simantics.modeling.ui.Activator;
28 import org.simantics.structural.stubs.StructuralResource2;
29 import org.simantics.ui.workbench.ResourceEditorInput2;
30 import org.simantics.ui.workbench.editor.AbstractResourceEditorAdapter;
31 import org.simantics.utils.ui.ErrorLogger;
32 import org.simantics.utils.ui.workbench.WorkbenchUtils;
33
34 /**
35  * @author Tuukka Lehtonen
36  */
37 public class OpenDiagramFromSymbolAdapter extends AbstractResourceEditorAdapter {
38
39     private static final String EDITOR_ID = "org.simantics.modeling.ui.symbolEditor";
40
41     public OpenDiagramFromSymbolAdapter() {
42         super("Symbol Editor", Activator.SYMBOL_ICON);
43     }
44
45     protected String getEditorId() {
46         return EDITOR_ID;
47     }
48
49     @Override
50     public boolean canHandle(ReadGraph g, Resource r) throws DatabaseException {
51         if(!g.isInheritedFrom(r, DiagramResource.getInstance(g).DefinedElement))
52             return false;
53         Resource componentType = g.getPossibleObject(r, ModelingResources.getInstance(g).SymbolToComponentType);
54         if(componentType == null)
55             return false;
56         return !OpenDiagramFromConfigurationAdapter.isLocked(g, componentType);
57     }
58
59     @Override
60     public void openEditor(final Resource r) throws Exception {
61         Simantics.getSession().asyncRequest(new ReadRequest() {
62             @Override
63             public void run(ReadGraph g) throws DatabaseException {
64                 StructuralResource2 sr = StructuralResource2.getInstance(g);
65                 final Collection<Resource> dias = g.getObjects(r, sr.IsDefinedBy);
66                 if (dias.isEmpty())
67                     return;
68
69                 final Resource model = g.sync(new PossibleIndexRoot(r));
70                 if(model == null) return;
71
72                 PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
73                     @Override
74                     public void run() {
75                         for(Resource dia : dias)
76                             try {
77                                 String editorId = getEditorId();
78                                 WorkbenchUtils.openEditor(editorId, new ResourceEditorInput2(editorId, dia, model, (RVI)null));
79                             } catch (PartInitException e) {
80                                 ErrorLogger.defaultLogError(e);
81                             }
82                     }
83                 });
84             }
85         });
86     }
87
88 }