1 /*******************************************************************************
2 * Copyright (c) 2012, 2015 Association for Decentralized Information Management in
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 * Semantum Oy - platform #5873: customizable instantiation logic
12 *******************************************************************************/
13 package org.simantics.modeling.adapters;
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.Status;
18 import org.eclipse.core.runtime.jobs.Job;
19 import org.simantics.DatabaseJob;
20 import org.simantics.Simantics;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.Resource;
23 import org.simantics.db.Session;
24 import org.simantics.db.WriteGraph;
25 import org.simantics.db.common.request.UniqueRead;
26 import org.simantics.db.common.request.WriteResultRequest;
27 import org.simantics.db.common.utils.NameUtils;
28 import org.simantics.db.exception.CancelTransactionException;
29 import org.simantics.db.exception.DatabaseException;
30 import org.simantics.db.layer0.adapter.ActionFactory;
31 import org.simantics.db.request.Read;
32 import org.simantics.db.request.WriteResult;
33 import org.simantics.layer0.Layer0;
34 import org.simantics.modeling.internal.Plugin;
35 import org.simantics.scl.runtime.function.Function3;
36 import org.simantics.structural.stubs.StructuralResource2;
37 import org.simantics.structural2.utils.StructuralUtils;
38 import org.simantics.ui.workbench.action.DefaultActions;
39 import org.simantics.utils.logging.TimeLogger;
41 public class NewCompositeActionFactory implements ActionFactory {
43 Resource compositeType;
46 public NewCompositeActionFactory(Resource compositeType, String defaultName) {
47 this.compositeType = compositeType;
48 this.defaultName = defaultName;
52 public Runnable create(Object target_) {
53 final Resource target = (Resource)target_;
54 return new Runnable() {
57 TimeLogger.resetTimeAndLog("NewCompositeActionFactory: create composite");
58 Job job = new DatabaseJob("Create Diagram") {
60 protected IStatus run(IProgressMonitor monitor) {
61 monitor.beginTask("Create Diagram...", IProgressMonitor.UNKNOWN);
63 createComposite(compositeType, defaultName, target);
64 return Status.OK_STATUS;
65 } catch (CancelTransactionException e) {
66 return new Status(IStatus.INFO , Plugin.PLUGIN_ID, e.getMessage());
67 } catch (DatabaseException e) {
68 return new Status(IStatus.ERROR, Plugin.PLUGIN_ID, e.getMessage(), e);
80 private static Resource createComposite(Resource compositeType, String defaultName, Resource target) throws DatabaseException {
81 Session session = Simantics.getSession();
82 session.markUndoPoint();
84 Function3<Resource, String, Resource, Resource> instantiator = session.syncRequest( possibleCompositeInstantiator(compositeType) );
85 if (instantiator != null)
86 return instantiator.apply(compositeType, defaultName, target);
88 Resource composite = session.syncRequest( createCompositeRequest( target, defaultName, compositeType ) );
89 DefaultActions.asyncPerformDefaultAction(session, composite, false, false, true);
93 private static Read<Function3<Resource, String, Resource, Resource>> possibleCompositeInstantiator(final Resource compositeType) {
94 return new UniqueRead<Function3<Resource, String, Resource, Resource>>() {
96 public Function3<Resource, String, Resource, Resource> perform(ReadGraph graph) throws DatabaseException {
97 return graph.getPossibleRelatedValue2(
99 StructuralResource2.getInstance(graph).Composite_instantiationFunction);
104 public static WriteResult<Resource> createCompositeRequest(final Resource target, final String defaultName, final Resource compositeType) {
105 return new WriteResultRequest<Resource>() {
107 public Resource perform(WriteGraph graph) throws DatabaseException {
108 return createComposite(graph, target, defaultName, compositeType);
113 public static Resource createComposite(WriteGraph graph, Resource target, String defaultName, Resource compositeType) throws DatabaseException {
114 String name = NameUtils.findFreshName(graph, defaultName, target, Layer0.getInstance(graph).ConsistsOf, "%s%d");
115 return StructuralUtils.newComponent(graph, target, name, compositeType);