Mercurial > hg > ccmieditor
comparison java/src/uk/ac/qmul/eecs/ccmi/gui/Finder.java @ 0:9418ab7b7f3f
Initial import
author | Fiore Martin <fiore@eecs.qmul.ac.uk> |
---|---|
date | Fri, 16 Dec 2011 17:35:51 +0000 |
parents | |
children | 9e67171477bc |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:9418ab7b7f3f |
---|---|
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.DiagramModelTreeNode; | |
26 | |
27 /** | |
28 * | |
29 * A utility class which provides methods for searching either a node or an edge | |
30 * in a collection or array. | |
31 * | |
32 */ | |
33 public abstract class Finder { | |
34 public static Node findNode(String nodeClass,Node[] prototypes){ | |
35 for(Node n : prototypes){ | |
36 if(n.getType().equals(nodeClass)){ | |
37 return n; | |
38 } | |
39 } | |
40 return null; | |
41 } | |
42 | |
43 public static Edge findEdge(String edgeClass,Edge[] prototypes){ | |
44 for(Edge e : prototypes){ | |
45 if(e.getType().equals(edgeClass)){ | |
46 return e; | |
47 } | |
48 } | |
49 return null; | |
50 } | |
51 | |
52 public static Node findNode(Long id, Collection<Node> collection){ | |
53 for(Node n : collection) | |
54 if(n.getId() == id) | |
55 return n; | |
56 return null; | |
57 } | |
58 | |
59 public static Node findNode(Point2D p, Collection<Node> collection){ | |
60 for (Node n : collection) | |
61 if (n.contains(p)) | |
62 return n; | |
63 return null; | |
64 } | |
65 | |
66 public static Edge findEdge(Point2D p, Collection<Edge> collection){ | |
67 for (Edge e : collection) | |
68 if (e.contains(p)) | |
69 return e; | |
70 return null; | |
71 } | |
72 | |
73 public static Edge findEdge(Long id, Collection<Edge> collection){ | |
74 for(Edge e : collection) | |
75 if(e.getId() == id) | |
76 return e; | |
77 return null; | |
78 } | |
79 | |
80 public static DiagramElement findElement(Long id, Collection<DiagramElement> collection){ | |
81 for(DiagramElement e : collection) | |
82 if(e.getId() == id) | |
83 return e; | |
84 return null; | |
85 } | |
86 | |
87 public static DiagramElement findElementByHashcode(long identityHashcode, Collection<DiagramElement> collection){ | |
88 for(DiagramElement de : collection){ | |
89 if(System.identityHashCode(de) == identityHashcode){ | |
90 return de; | |
91 } | |
92 } | |
93 return null; | |
94 } | |
95 | |
96 /** | |
97 * Return the tree node whose path is described by the variable path | |
98 * where path contains the indexes returned by each node n of the path upon calling n.getParent().getChildAt(n) | |
99 * | |
100 * @param path | |
101 * @param root | |
102 * @return | |
103 */ | |
104 public static DiagramModelTreeNode findTreeNode(int[] path, DiagramModelTreeNode root){ | |
105 DiagramModelTreeNode retVal = root; | |
106 for(int i=0;i<path.length;i++){ | |
107 if(retVal.getChildCount() <= path[i]) | |
108 return null; | |
109 retVal = retVal.getChildAt(path[i]); | |
110 } | |
111 return retVal; | |
112 } | |
113 | |
114 } |