]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/typicals/TypicalCompositeCopyHandler.java
Fixed ComponentTypeCommands.setUnit to support unit == null
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / typicals / TypicalCompositeCopyHandler.java
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.typicals;
13
14 import java.util.ArrayList;
15
16 import org.simantics.db.ReadGraph;
17 import org.simantics.db.Resource;
18 import org.simantics.db.common.NamedResource;
19 import org.simantics.db.common.request.ObjectsWithType;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.layer0.util.TransferableGraphConfiguration2;
22 import org.simantics.diagram.stubs.DiagramResource;
23 import org.simantics.layer0.Layer0;
24 import org.simantics.modeling.ModelingResources;
25 import org.simantics.modeling.ModelingUtils.CompositeInfo;
26 import org.simantics.modeling.ModelingUtils.DiagramComponentInfo;
27 import org.simantics.modeling.adapters.CompositeCopyHandler;
28 import org.simantics.structural.stubs.StructuralResource2;
29
30 /**
31  * @author Tuukka Lehtonen
32  */
33 public class TypicalCompositeCopyHandler extends CompositeCopyHandler {
34
35     public TypicalCompositeCopyHandler(Resource composite) {
36         super(composite);
37     }
38     
39     @Override
40     protected TransferableGraphConfiguration2 createConfiguration(ReadGraph graph, boolean cut) throws DatabaseException {
41         
42         Layer0 L0 = Layer0.getInstance(graph);
43         StructuralResource2 SR = StructuralResource2.getInstance(graph);
44         DiagramResource DIA = DiagramResource.getInstance(graph);
45         ModelingResources MOD = ModelingResources.getInstance(graph);
46
47         Resource resource = getResource();
48         
49         // Process joins
50         ArrayList<Resource> exclusions = new ArrayList<Resource>();
51         for(Resource join : graph.getObjects(resource, SR.HasConnectionJoin)) {
52                 // Internals are included
53                 if(graph.getPossibleObject(join, SR.JoinsComposite) != null) continue;
54                 // Externals are omitted
55                 exclusions.add(join);
56         }
57         // Exclude both composite and possible diagram types from copy
58         for (Resource type : graph.getObjects(resource, L0.InstanceOf)) {
59             if (graph.isInheritedFrom(type, MOD.TypicalComposite)) {
60                 exclusions.add(type);
61             }
62         }
63         Resource diagram = graph.getPossibleObject(resource, MOD.CompositeToDiagram);
64         if (diagram != null) {
65             for (Resource type : graph.getObjects(diagram, L0.InstanceOf)) {
66                 if (graph.isInheritedFrom(type, DIA.Diagram)) {
67                     exclusions.add(type);
68                 }
69             }
70         }
71
72         ArrayList<NamedResource> roots = new ArrayList<NamedResource>();
73         // Include resource as root
74         CompositeInfo info = CompositeInfo.fromResource(graph, resource);
75         //String rootName = graph.getRelatedValue(resource, L0.HasName, Bindings.STRING);
76         roots.add(new NamedResource(info.getTGName(), resource));
77         // Include components as roots
78         for(Resource child : graph.sync(new ObjectsWithType(resource, L0.ConsistsOf, SR.Component))) {
79                 DiagramComponentInfo cinfo = DiagramComponentInfo.fromResource(graph, info, child);
80                 roots.add(new NamedResource(cinfo.getTGName(info), child));
81         }
82         
83         TransferableGraphConfiguration2 result = TransferableGraphConfiguration2.createWithNames(graph, roots, exclusions, true, false);
84         return  result;
85         
86     }
87
88 }