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.diagramEditor;
14 import java.util.Collection;
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;
35 * @author Tuukka Lehtonen
37 public class OpenDiagramFromSymbolAdapter extends AbstractResourceEditorAdapter {
39 private static final String EDITOR_ID = "org.simantics.modeling.ui.symbolEditor";
41 public OpenDiagramFromSymbolAdapter() {
42 super("Symbol Editor", Activator.SYMBOL_ICON);
45 protected String getEditorId() {
50 public boolean canHandle(ReadGraph g, Resource r) throws DatabaseException {
51 if(!g.isInheritedFrom(r, DiagramResource.getInstance(g).DefinedElement))
53 Resource componentType = g.getPossibleObject(r, ModelingResources.getInstance(g).SymbolToComponentType);
54 if(componentType == null)
56 return !OpenDiagramFromConfigurationAdapter.isLocked(g, componentType);
60 public void openEditor(final Resource r) throws Exception {
61 Simantics.getSession().asyncRequest(new ReadRequest() {
63 public void run(ReadGraph g) throws DatabaseException {
64 StructuralResource2 sr = StructuralResource2.getInstance(g);
65 final Collection<Resource> dias = g.getObjects(r, sr.IsDefinedBy);
69 final Resource model = g.sync(new PossibleIndexRoot(r));
70 if(model == null) return;
72 PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
75 for(Resource dia : dias)
77 String editorId = getEditorId();
78 WorkbenchUtils.openEditor(editorId, new ResourceEditorInput2(editorId, dia, model, (RVI)null));
79 } catch (PartInitException e) {
80 ErrorLogger.defaultLogError(e);