fiore@0
|
1 /*
|
fiore@0
|
2 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
|
fiore@0
|
3
|
fiore@0
|
4 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
|
fiore@0
|
5
|
fiore@0
|
6 This program is free software: you can redistribute it and/or modify
|
fiore@0
|
7 it under the terms of the GNU General Public License as published by
|
fiore@0
|
8 the Free Software Foundation, either version 3 of the License, or
|
fiore@0
|
9 (at your option) any later version.
|
fiore@0
|
10
|
fiore@0
|
11 This program is distributed in the hope that it will be useful,
|
fiore@0
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
fiore@0
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
fiore@0
|
14 GNU General Public License for more details.
|
fiore@0
|
15
|
fiore@0
|
16 You should have received a copy of the GNU General Public License
|
fiore@0
|
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
|
fiore@0
|
18 */
|
fiore@0
|
19 package uk.ac.qmul.eecs.ccmi.gui;
|
fiore@0
|
20
|
fiore@0
|
21 import java.awt.geom.Point2D;
|
fiore@0
|
22 import java.util.Collection;
|
fiore@0
|
23
|
fiore@0
|
24 import uk.ac.qmul.eecs.ccmi.diagrammodel.DiagramElement;
|
fiore@3
|
25 import uk.ac.qmul.eecs.ccmi.diagrammodel.DiagramTreeNode;
|
fiore@0
|
26
|
fiore@0
|
27 /**
|
fiore@0
|
28 *
|
fiore@5
|
29 * A utility class providing static methods for searching either a node or an edge
|
fiore@0
|
30 * in a collection or array.
|
fiore@0
|
31 */
|
fiore@0
|
32 public abstract class Finder {
|
fiore@5
|
33 /**
|
fiore@5
|
34 * Finds a node of a type in an array of nodes. The types should be all different
|
fiore@5
|
35 * from each other as only the first node encountered will be returned. If none of the nodes
|
fiore@5
|
36 * as the type passed as argument, {@code null} is returned.
|
fiore@5
|
37 *
|
fiore@5
|
38 * @param nodeType the type of the node to find.
|
fiore@5
|
39 * @param nodes the array to search for the node
|
fiore@5
|
40 * @return the first node with type {@code nodeType} or {@code null} if such node
|
fiore@5
|
41 * doesn't exist
|
fiore@5
|
42 */
|
fiore@5
|
43 public static Node findNode(String nodeType,Node[] nodes){
|
fiore@5
|
44 for(Node n : nodes){
|
fiore@5
|
45 if(n.getType().equals(nodeType)){
|
fiore@0
|
46 return n;
|
fiore@0
|
47 }
|
fiore@0
|
48 }
|
fiore@0
|
49 return null;
|
fiore@0
|
50 }
|
fiore@0
|
51
|
fiore@5
|
52 /**
|
fiore@5
|
53 * Finds an edge of a {@code edgeType} type in an array of nodes. The types should be all different
|
fiore@5
|
54 * from each other as only the first edge encountered will be returned. If none of the edges
|
fiore@5
|
55 * as the type passed as argument, {@code null} is returned.
|
fiore@5
|
56 *
|
fiore@5
|
57 * @param edgeType the type of the edge to find
|
fiore@5
|
58 * @param edges the array to search for the edge
|
fiore@5
|
59 * @return the first edge with type {@code nodeType} or {@code null} if such edge
|
fiore@5
|
60 * doesn't exist
|
fiore@5
|
61 */
|
fiore@5
|
62 public static Edge findEdge(String edgeType,Edge[] edges){
|
fiore@5
|
63 for(Edge e : edges){
|
fiore@5
|
64 if(e.getType().equals(edgeType)){
|
fiore@0
|
65 return e;
|
fiore@0
|
66 }
|
fiore@0
|
67 }
|
fiore@0
|
68 return null;
|
fiore@0
|
69 }
|
fiore@0
|
70
|
fiore@5
|
71 /**
|
fiore@5
|
72 * Finds a node with an id in a {@code Collection} of nodes. If none of the nodes
|
fiore@5
|
73 * has the id passed as argument, {@code null} is returned.
|
fiore@5
|
74 *
|
fiore@5
|
75 * @param id the id of the node to find
|
fiore@5
|
76 * @param collection the collection to search for the node
|
fiore@5
|
77 * @return the node with the specified id, or {@code null} if such node doesn't exist
|
fiore@5
|
78 */
|
fiore@0
|
79 public static Node findNode(Long id, Collection<Node> collection){
|
fiore@0
|
80 for(Node n : collection)
|
fiore@0
|
81 if(n.getId() == id)
|
fiore@0
|
82 return n;
|
fiore@0
|
83 return null;
|
fiore@0
|
84 }
|
fiore@0
|
85
|
fiore@5
|
86 /**
|
fiore@5
|
87 * Finds a node containing a point {@code p} in a {@code Collection} of nodes. If none of the nodes
|
fiore@5
|
88 * contains the point, {@code null} is returned.
|
fiore@5
|
89 *
|
fiore@5
|
90 * @param p the point in a graphic environment
|
fiore@5
|
91 * @param collection the collection to search for the node
|
fiore@5
|
92 * @return the node containing {@code p}, or {@code null} if such node doesn't exist
|
fiore@5
|
93 */
|
fiore@0
|
94 public static Node findNode(Point2D p, Collection<Node> collection){
|
fiore@0
|
95 for (Node n : collection)
|
fiore@0
|
96 if (n.contains(p))
|
fiore@0
|
97 return n;
|
fiore@0
|
98 return null;
|
fiore@0
|
99 }
|
fiore@0
|
100
|
fiore@5
|
101 /**
|
fiore@5
|
102 * Finds an edge with an id in a {@code Collection} of edges. If none of the edges
|
fiore@5
|
103 * has the id passed as argument, {@code null} is returned.
|
fiore@5
|
104 *
|
fiore@5
|
105 * @param id the id of the edge to find
|
fiore@5
|
106 * @param collection the collection to search for the edge
|
fiore@5
|
107 * @return the edge with the specified id, or {@code null} if such edge doesn't exist
|
fiore@5
|
108 */
|
fiore@5
|
109 public static Edge findEdge(Long id, Collection<Edge> collection){
|
fiore@5
|
110 for(Edge e : collection)
|
fiore@5
|
111 if(e.getId() == id)
|
fiore@5
|
112 return e;
|
fiore@5
|
113 return null;
|
fiore@5
|
114 }
|
fiore@5
|
115
|
fiore@5
|
116 /**
|
fiore@5
|
117 * Finds an edge containing a point {@code p} in a {@code Collection} of edges. If none of the edges
|
fiore@5
|
118 * contains the point, {@code null} is returned.
|
fiore@5
|
119 *
|
fiore@5
|
120 * @param p the point in a graphic environment
|
fiore@5
|
121 * @param collection the collection to search for the edge
|
fiore@5
|
122 * @return the edge containing {@code p}, or {@code null} if such edge doesn't exist
|
fiore@5
|
123 */
|
fiore@0
|
124 public static Edge findEdge(Point2D p, Collection<Edge> collection){
|
fiore@0
|
125 for (Edge e : collection)
|
fiore@0
|
126 if (e.contains(p))
|
fiore@0
|
127 return e;
|
fiore@0
|
128 return null;
|
fiore@0
|
129 }
|
fiore@0
|
130
|
fiore@5
|
131 /**
|
fiore@5
|
132 * Finds a element (node or edge) with an id in a {@code Collection} of diagram elements. If none of the elements
|
fiore@5
|
133 * has the id passed as argument, {@code null} is returned.
|
fiore@5
|
134 *
|
fiore@5
|
135 * @param id the id of the element to find
|
fiore@5
|
136 * @param collection the collection to search for the element
|
fiore@5
|
137 * @return the element with the specified id, or {@code null} if such element doesn't exist
|
fiore@5
|
138 */
|
fiore@0
|
139 public static DiagramElement findElement(Long id, Collection<DiagramElement> collection){
|
fiore@0
|
140 for(DiagramElement e : collection)
|
fiore@0
|
141 if(e.getId() == id)
|
fiore@0
|
142 return e;
|
fiore@0
|
143 return null;
|
fiore@0
|
144 }
|
fiore@0
|
145
|
fiore@5
|
146 /**
|
fiore@5
|
147 * Finds a element (node or edge) with an identity hash code in a {@code Collection} of diagram elements. If none of the elements
|
fiore@5
|
148 * has the code passed as argument, {@code null} is returned.
|
fiore@5
|
149 *
|
fiore@5
|
150 * @param identityHashcode the identity hash code of the element to find
|
fiore@5
|
151 * @param collection the collection to search for the element
|
fiore@5
|
152 * @return the element with the specified identity hash code, or {@code null} if such element doesn't exist
|
fiore@5
|
153 *
|
fiore@5
|
154 * @see Object#hashCode()
|
fiore@5
|
155 */
|
fiore@0
|
156 public static DiagramElement findElementByHashcode(long identityHashcode, Collection<DiagramElement> collection){
|
fiore@0
|
157 for(DiagramElement de : collection){
|
fiore@0
|
158 if(System.identityHashCode(de) == identityHashcode){
|
fiore@0
|
159 return de;
|
fiore@0
|
160 }
|
fiore@0
|
161 }
|
fiore@0
|
162 return null;
|
fiore@0
|
163 }
|
fiore@0
|
164
|
fiore@0
|
165 /**
|
fiore@5
|
166 * Returns the tree node whose path is described by the variable path
|
fiore@5
|
167 * where path contains the indexes returned by each node n of the
|
fiore@5
|
168 * path upon calling n.getParent().getChildAt(n).
|
fiore@0
|
169 *
|
fiore@5
|
170 * @param path the path for the node in the tree
|
fiore@5
|
171 * @param root the node where the path starts
|
fiore@5
|
172 * @return the node at the specified path, or {@code null} otherwise
|
fiore@0
|
173 */
|
fiore@3
|
174 public static DiagramTreeNode findTreeNode(int[] path, DiagramTreeNode root){
|
fiore@3
|
175 DiagramTreeNode retVal = root;
|
fiore@0
|
176 for(int i=0;i<path.length;i++){
|
fiore@0
|
177 if(retVal.getChildCount() <= path[i])
|
fiore@0
|
178 return null;
|
fiore@0
|
179 retVal = retVal.getChildAt(path[i]);
|
fiore@0
|
180 }
|
fiore@0
|
181 return retVal;
|
fiore@0
|
182 }
|
fiore@0
|
183
|
fiore@0
|
184 }
|