]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.template2d.ui/src/org/simantics/modeling/template2d/ui/actions/FixLinkedList.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.modeling.template2d.ui / src / org / simantics / modeling / template2d / ui / actions / FixLinkedList.java
1 /*******************************************************************************
2  * Copyright (c) 2012 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  *******************************************************************************/
12 package org.simantics.modeling.template2d.ui.actions;
13
14 import java.util.Collection;
15 import java.util.HashSet;
16 import java.util.List;
17 import java.util.Set;
18
19 import org.simantics.Simantics;
20 import org.simantics.db.Resource;
21 import org.simantics.db.WriteGraph;
22 import org.simantics.db.common.request.ObjectsWithType;
23 import org.simantics.db.common.request.WriteRequest;
24 import org.simantics.db.common.utils.ListUtils;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.db.layer0.adapter.ActionFactory;
27 import org.simantics.db.layer0.util.RemoverUtil;
28 import org.simantics.layer0.Layer0;
29 import org.simantics.scenegraph.ontology.ScenegraphResources;
30
31 public class FixLinkedList implements ActionFactory {
32
33     @Override
34     public Runnable create(Object target) {
35         if(!(target instanceof Resource))
36             return null;
37         final Resource parent = (Resource)target;
38
39         return new Runnable() {
40             @Override
41             public void run() {
42                 Simantics.getSession().asyncRequest(new WriteRequest() {
43                     @Override
44                     public void perform(WriteGraph g) throws DatabaseException {
45                         g.markUndoPoint();
46                         Layer0 L0 = Layer0.getInstance(g);
47                         ScenegraphResources SG = ScenegraphResources.getInstance(g);
48
49                         Collection<Resource> consist = g.syncRequest(new ObjectsWithType(parent, L0.ConsistsOf, SG.Node));
50
51                         Resource list = g.getPossibleObject(parent, SG.Node_children);
52                         if (list != null) {
53                             try {
54                                 List<Resource> children = ListUtils.toList(g, list);
55                                 Set<Resource> childSet = new HashSet<Resource>(children);
56                                 Set<Resource> consistSet = new HashSet<Resource>(consist);
57                                 if (childSet.equals(consistSet)) {
58                                     return;
59                                 }
60                             } catch (DatabaseException e) {
61                             }
62
63                             // List is invalid, recreate it.
64                             RemoverUtil.remove(g, list);
65                             g.deny(parent, SG.Node_children);
66                             list = ListUtils.create(g, consist);
67                             g.claim(parent, SG.Node_children, list);
68
69                         } else {
70                             // No list at all - create one from children.
71                             list = ListUtils.create(g, consist);
72                             g.claim(parent, SG.Node_children, list);
73                         }
74                     }
75                 });
76             }
77         };
78     }
79 }