]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/NewCompositeActionFactory.java
WIP: Some old-ish adapter registry initialization improvements
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / adapters / NewCompositeActionFactory.java
1 /*******************************************************************************
2  * Copyright (c) 2012, 2015 Association for Decentralized Information Management in
3  * 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  *     Semantum Oy - platform #5873: customizable instantiation logic
12  *******************************************************************************/
13 package org.simantics.modeling.adapters;
14
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;
40
41 public class NewCompositeActionFactory implements ActionFactory {
42
43     Resource compositeType;
44     String defaultName;
45
46     public NewCompositeActionFactory(Resource compositeType, String defaultName) {
47         this.compositeType = compositeType;
48         this.defaultName = defaultName;
49     }
50
51     @Override
52     public Runnable create(Object target_) {
53         final Resource target = (Resource)target_;
54         return new Runnable() {
55             @Override
56             public void run() {
57                 TimeLogger.resetTimeAndLog("NewCompositeActionFactory: create composite");
58                 Job job = new DatabaseJob("Create Diagram") {
59                     @Override
60                     protected IStatus run(IProgressMonitor monitor) {
61                         monitor.beginTask("Create Diagram...", IProgressMonitor.UNKNOWN);
62                         try {
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);
69                         } finally {
70                             monitor.done();
71                         }
72                     }
73                 };
74                 //job.setUser(true);
75                 job.schedule();
76             }
77         };
78     }
79
80     private static Resource createComposite(Resource compositeType, String defaultName, Resource target) throws DatabaseException {
81         Session session = Simantics.getSession();
82         session.markUndoPoint();
83
84         Function3<Resource, String, Resource, Resource> instantiator = session.syncRequest( possibleCompositeInstantiator(compositeType) );
85         if (instantiator != null)
86             return instantiator.apply(compositeType, defaultName, target);
87
88         Resource composite = session.syncRequest( createCompositeRequest( target, defaultName, compositeType ) );
89         DefaultActions.asyncPerformDefaultAction(session, composite, false, false, true);
90         return composite;
91     }
92
93     private static Read<Function3<Resource, String, Resource, Resource>> possibleCompositeInstantiator(final Resource compositeType) {
94         return new UniqueRead<Function3<Resource, String, Resource, Resource>>() {
95             @Override
96             public Function3<Resource, String, Resource, Resource> perform(ReadGraph graph) throws DatabaseException {
97                 return graph.getPossibleRelatedValue2(
98                         compositeType,
99                         StructuralResource2.getInstance(graph).Composite_instantiationFunction);
100             }
101         };
102     }
103
104     public static WriteResult<Resource> createCompositeRequest(final Resource target, final String defaultName, final Resource compositeType) {
105         return new WriteResultRequest<Resource>() {
106             @Override
107             public Resource perform(WriteGraph graph) throws DatabaseException {
108                 return createComposite(graph, target, defaultName, compositeType);
109             }
110         };
111     }
112
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);
116     }
117
118 }