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.handler;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.EnumSet;
17 import java.util.HashSet;
18 import java.util.List;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.Resource;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.diagram.content.EdgeResource;
25 import org.simantics.diagram.stubs.DiagramResource;
26 import org.simantics.g2d.element.ElementHints;
27 import org.simantics.g2d.element.ElementUtils;
28 import org.simantics.g2d.element.IElement;
29 import org.simantics.modeling.ModelingResources;
32 * An element assortment is used to categorize diagram contents in diagram
33 * cut-copy-paste operations.
36 * This version of {@link ElementAssortment} contains only the back-end objects
37 * (see {@link ElementHints#KEY_OBJECT}) of IElement instances instead of the
38 * IElement instances themselves. This version doesn't have the dependency on
39 * the diagram runtime model that {@link ElementAssortment} has. This object can
40 * be used even if the diagram runtime model and its elements are disposed.
42 * @author Tuukka Lehtonen
44 public class ElementObjectAssortment implements IElementAssortment {
46 private EnumSet<ElementType> contents;
48 public Set<Object> all;
50 public final List<Resource> nodeList = new ArrayList<Resource>();
51 public final Set<Resource> nodes = new HashSet<Resource>();
52 public final Set<Resource> connections = new HashSet<Resource>();
53 public final Set<EdgeResource> edges = new HashSet<EdgeResource>();
54 public final Set<Resource> branchPoints = new HashSet<Resource>();
55 public final Set<Resource> flags = new HashSet<Resource>();
56 public final Set<Resource> references = new HashSet<Resource>();
57 public final Set<Resource> monitors = new HashSet<Resource>();
58 public final Set<Resource> others = new HashSet<Resource>();
59 public final Set<Object> noncopyables = new HashSet<Object>();
61 public ElementObjectAssortment(ReadGraph graph, Collection<Resource> elements) throws DatabaseException {
62 all = new HashSet<Object>(elements);
63 contents = EnumSet.noneOf(ElementType.class);
64 sort(graph, elements);
67 private void sort(ReadGraph graph, Collection<Resource> elements) throws DatabaseException {
68 DiagramResource DIA = DiagramResource.getInstance(graph);
69 ModelingResources MOD = ModelingResources.getInstance(graph);
70 for (Resource element : elements) {
71 if (graph.isInstanceOf(element, DIA.Flag)) {
73 contents.add(ElementType.Flag);
74 } else if (graph.isInstanceOf(element, DIA.Connection)) {
75 connections.add(element);
76 contents.add(ElementType.Connection);
77 } else if (graph.isInstanceOf(element, DIA.Monitor)) {
78 monitors.add(element);
79 contents.add(ElementType.Monitor);
80 } else if (graph.isInstanceOf(element, MOD.ReferenceElement)) {
81 references.add(element);
82 contents.add(ElementType.Reference);
83 } else if (graph.isInstanceOf(element, DIA.DefinedElement)) {
84 nodeList.add(element);
86 contents.add(ElementType.Node);
87 } else if (graph.isInstanceOf(element, DIA.Element)) {
89 contents.add(ElementType.Other);
95 * @param set all the elements to initially sort out. This object assumes
96 * ownership of the set.
98 public static ElementObjectAssortment fromElements(Set<IElement> set) {
99 return new ElementObjectAssortment( new ElementAssortment( set ) );
103 * @param set all the elements to initially sort out. This object assumes
104 * ownership of the set.
106 public ElementObjectAssortment(ElementAssortment assortment) {
107 if (assortment == null)
108 throw new IllegalArgumentException("null element assortment");
112 private void load(ElementAssortment es) {
113 this.contents = es.contents;
114 this.all = getElementObjectSet(es.all, Object.class);
115 getElementObjects(es.nodeList, Resource.class, this.nodeList);
116 getElementObjects(es.nodes, Resource.class, this.nodes);
117 getElementObjects(es.connections, Resource.class, this.connections);
118 getElementObjects(es.connections, Resource.class, this.connections);
119 getElementObjects(es.edges, EdgeResource.class, this.edges);
120 getElementObjects(es.branchPoints, Resource.class, this.branchPoints);
121 getElementObjects(es.flags, Resource.class, this.flags);
122 getElementObjects(es.references, Resource.class, this.references);
123 getElementObjects(es.monitors, Resource.class, this.monitors);
124 getElementObjects(es.others, Resource.class, this.others);
125 getElementObjects(es.noncopyables, Object.class, this.noncopyables);
128 public Set<Object> getAll() {
129 return new HashSet<Object>(all);
132 public boolean contains(ElementType et) {
133 return contents.contains(et);
136 public boolean contains(Collection<ElementType> ets) {
137 return contents.containsAll(ets);
140 public boolean containsAny(Collection<ElementType> ets) {
141 for (ElementType et : ets)
142 if (contents.contains(et))
148 public int count(ElementType et) {
150 case BranchPoint: return branchPoints.size();
151 case Connection: return connections.size();
152 case Edge: return edges.size();
153 case Flag: return flags.size();
154 case Reference: return references.size();
155 case Monitor: return monitors.size();
156 case Node: return nodes.size();
157 case Other: return others.size();
158 case NonCopyable: return noncopyables.size();
164 public String toString() {
165 StringBuilder b = new StringBuilder();
166 b.append("ElementObjectAssortment:\n");
167 b.append("\t CONTAINS: ");
170 for(Resource e : nodes) b.append("\t-node " + e + "\n");
171 for(Resource e : connections) b.append("\t-connection " + e + "\n");
172 for(EdgeResource e : edges) b.append("\t-edge " + e + "\n");
173 for(Resource e : branchPoints) b.append("\t-branch " + e + "\n");
174 for(Resource e : flags) b.append("\t-flag " + e + "\n");
175 for(Resource e : references) b.append("\t-reference " + e + "\n");
176 for(Resource e : monitors) b.append("\t-monitor " + e + "\n");
177 for(Object e : others) b.append("\t-other " + e + "\n");
178 for(Object e : noncopyables) b.append("\t-non-copyable " + e + "\n");
182 public boolean isEmpty() {
183 return all.isEmpty();
186 @SuppressWarnings("unchecked")
187 public static <T> Collection<T> getElementObjects(Collection<IElement> elements, Class<T> clazz, Collection<T> result) {
188 for (IElement e : elements) {
189 Object o = ElementUtils.getObject(e);
190 if (clazz.isInstance(o))
196 public static <T> Set<T> getElementObjectSet(Collection<IElement> elements, Class<T> clazz) {
197 Set<T> result = new HashSet<T>();
198 getElementObjects(elements, clazz, result);