1 /*******************************************************************************
2 * Copyright (c) 2012 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 *******************************************************************************/
12 package org.simantics.modeling.template2d.ui.actions;
14 import java.util.Collection;
15 import java.util.HashSet;
16 import java.util.List;
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;
31 public class FixLinkedList implements ActionFactory {
34 public Runnable create(Object target) {
35 if(!(target instanceof Resource))
37 final Resource parent = (Resource)target;
39 return new Runnable() {
42 Simantics.getSession().asyncRequest(new WriteRequest() {
44 public void perform(WriteGraph g) throws DatabaseException {
46 Layer0 L0 = Layer0.getInstance(g);
47 ScenegraphResources SG = ScenegraphResources.getInstance(g);
49 Collection<Resource> consist = g.syncRequest(new ObjectsWithType(parent, L0.ConsistsOf, SG.Node));
51 Resource list = g.getPossibleObject(parent, SG.Node_children);
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)) {
60 } catch (DatabaseException e) {
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);
70 // No list at all - create one from children.
71 list = ListUtils.create(g, consist);
72 g.claim(parent, SG.Node_children, list);