1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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.diagram.synchronization.graph;
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.List;
21 import org.simantics.db.Resource;
22 import org.simantics.db.WriteGraph;
23 import org.simantics.db.common.utils.OrderedSetUtils;
24 import org.simantics.diagram.synchronization.ModificationAdapter;
25 import org.simantics.diagram.ui.DiagramModelHints;
26 import org.simantics.g2d.diagram.IDiagram;
27 import org.simantics.g2d.element.ElementUtils;
28 import org.simantics.g2d.element.IElement;
31 * This modification reorders the specified diagram in the graph backend
32 * according to the order of the elements specified argument element list.
35 * The algorithm is linear with respect to the amount of elements on the
36 * diagram. It tries to minimize the number of ordered set operations.
38 * @author Tuukka Lehtonen
40 public class ElementReorder extends ModificationAdapter {
45 public ElementReorder(IDiagram diagram, List<IElement> order) {
47 this.diagram = diagram;
52 public void perform(WriteGraph g) throws Exception {
53 Resource l = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE);
55 List<Resource> graphOrder = OrderedSetUtils.toList(g, l);
56 Set<Resource> graphContents = new HashSet<Resource>(graphOrder);
58 List<Resource> newGraphOrder = new ArrayList<>();
59 for (IElement e : order) {
60 Object obj = ElementUtils.getObject(e);
61 if (obj instanceof Resource) {
62 Resource r = (Resource) obj;
63 // Only consider resources that still are in the diagram.
64 // This prevents errors in situations where #order contains
65 // elements that no longer exist in the diagram.
66 if (graphContents.contains(r)) {
72 // Safety measure for possible missing elements
73 if (graphOrder.size() != newGraphOrder.size()) {
74 Set<Resource> added = new HashSet<Resource>(newGraphOrder);
75 for (Resource r : graphOrder) {
76 if (!added.contains(r)) {
82 OrderedSetUtils.reorder(g, l, newGraphOrder);