package org.simantics.sysdyn.ui.project;\r
\r
+import org.eclipse.swt.widgets.Shell;\r
+import org.eclipse.ui.PartInitException;\r
+import org.simantics.db.Builtins;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.uri.URIToResource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.service.LifecycleSupport;\r
+import org.simantics.layer0.utils.direct.GraphUtils;\r
+import org.simantics.project.ProjectElementType;\r
+import org.simantics.project.ProjectElements;\r
import org.simantics.project.ProjectKeys;\r
import org.simantics.project.exception.ProjectException;\r
import org.simantics.project.features.AbstractProjectFeature;\r
+import org.simantics.project.features.UIModelManager;\r
+import org.simantics.project.features.UIModelManagerBase;\r
+import org.simantics.project.features.UIModelManagerBase.WriteRunnable;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.ui.workbench.action.ChooseActionRequest;\r
+import org.simantics.utils.datastructures.Callback;\r
+import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
\r
public class SysdynProject extends AbstractProjectFeature {\r
private static final String DEFAULT_PERSPECTIVE = "org.simantics.sysdyn.ui.perspective";\r
\r
+ class ModelManager extends UIModelManagerBase {\r
+ @Override\r
+ public void create(ProjectElementType kind, Shell parentShell, Session session, Resource container,\r
+ Callback<Resource> callback, Callback<Throwable> errorCallback) {\r
+\r
+ if (ProjectElements.MODEL.equals(kind)) {\r
+ request(session, container, callback, errorCallback, CREATE_MODEL);\r
+ return;\r
+ }\r
+\r
+ super.create(kind, parentShell, session, container, callback, errorCallback);\r
+ }\r
+\r
+ /**\r
+ * \r
+ * \r
+ * @see org.simantics.project.features.UIModelManager#openEditor(org.simantics.db.Session, java.lang.String)\r
+ */\r
+ @Override\r
+ public void openEditor(Session session, String uri) throws PartInitException {\r
+ try {\r
+ Resource resource = session.syncRequest(new URIToResource(uri));\r
+ session.syncRequest(new ChooseActionRequest(null, resource, WorkbenchUtils.getCurrentPerspectiveId()));\r
+ } catch (DatabaseException e) {\r
+ LifecycleSupport ls = session.getService(LifecycleSupport.class);\r
+ throw new PartInitException("Failed to open editor for URI '" + uri + "' with session '" + ls.getSessionReference() + "', see exception for details.", e);\r
+ }\r
+ }\r
+ }\r
+\r
+ private final WriteRunnable CREATE_MODEL = new WriteRunnable() {\r
+ public void run(WriteGraph g, Resource library, Callback<Resource> callback, Callback<Throwable> errorCallback) {\r
+ try {\r
+ Builtins b = g.getBuiltins();\r
+ SysdynResource sr = SysdynResource.getInstance(g);\r
+ Resource model = g.newResource();\r
+ g.claimValue(model, b.HasName, GraphUtils.findFreshName(g, "Configuration", library));\r
+ g.claim(model, b.InstanceOf, sr.Configuration);\r
+ g.claim(model, b.PartOf, library);\r
+\r
+ } catch(Exception e) {\r
+ errorCallback.run(e);\r
+ }\r
+ }\r
+ };\r
+\r
+ ModelManager mm;\r
+ \r
@Override\r
public void configure() throws ProjectException {\r
addToCollectionHint(ProjectKeys.PERSPECTIVES, DEFAULT_PERSPECTIVE);\r
addToCollectionHint(ProjectKeys.OPEN_PERSPECTIVES, DEFAULT_PERSPECTIVE);\r
getProjectElement().setHint(ProjectKeys.DEFAULT_PERSPECTIVE, DEFAULT_PERSPECTIVE);\r
+ \r
+ mm = new ModelManager();\r
+ getProjectElement().setHint(UIModelManager.KEY_MODEL_MANAGER, mm);\r
}\r
\r
@Override\r
public void deconfigure() throws ProjectException {\r
+ if (getProjectElement().getHint(UIModelManager.KEY_MODEL_MANAGER) == mm)\r
+ getProjectElement().removeHint(UIModelManager.KEY_MODEL_MANAGER);\r
+ mm = null;\r
}\r
\r
}\r