comparison java/src/uk/ac/qmul/eecs/ccmi/gui/Finder.java @ 0:78b7fc5391a2

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