annotate java/src/uk/ac/qmul/eecs/ccmi/gui/Finder.java @ 1:e3935c01cde2 tip

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