f@0: /*
f@0: CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
f@0:
f@0: Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
f@0:
f@0: This program is free software: you can redistribute it and/or modify
f@0: it under the terms of the GNU General Public License as published by
f@0: the Free Software Foundation, either version 3 of the License, or
f@0: (at your option) any later version.
f@0:
f@0: This program is distributed in the hope that it will be useful,
f@0: but WITHOUT ANY WARRANTY; without even the implied warranty of
f@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f@0: GNU General Public License for more details.
f@0:
f@0: You should have received a copy of the GNU General Public License
f@0: along with this program. If not, see .
f@0: */
f@0: package uk.ac.qmul.eecs.ccmi.gui;
f@0:
f@0: import java.awt.geom.Point2D;
f@0: import java.util.Collection;
f@0:
f@0: import uk.ac.qmul.eecs.ccmi.diagrammodel.DiagramElement;
f@0: import uk.ac.qmul.eecs.ccmi.diagrammodel.DiagramTreeNode;
f@0:
f@0: /**
f@0: *
f@0: * A utility class providing static methods for searching either a node or an edge
f@0: * in a collection or array.
f@0: */
f@0: public abstract class Finder {
f@0: /**
f@0: * Finds a node of a type in an array of nodes. The types should be all different
f@0: * from each other as only the first node encountered will be returned. If none of the nodes
f@0: * as the type passed as argument, {@code null} is returned.
f@0: *
f@0: * @param nodeType the type of the node to find.
f@0: * @param nodes the array to search for the node
f@0: * @return the first node with type {@code nodeType} or {@code null} if such node
f@0: * doesn't exist
f@0: */
f@0: public static Node findNode(String nodeType,Node[] nodes){
f@0: for(Node n : nodes){
f@0: if(n.getType().equals(nodeType)){
f@0: return n;
f@0: }
f@0: }
f@0: return null;
f@0: }
f@0:
f@0: /**
f@0: * Finds an edge of a {@code edgeType} type in an array of nodes. The types should be all different
f@0: * from each other as only the first edge encountered will be returned. If none of the edges
f@0: * as the type passed as argument, {@code null} is returned.
f@0: *
f@0: * @param edgeType the type of the edge to find
f@0: * @param edges the array to search for the edge
f@0: * @return the first edge with type {@code nodeType} or {@code null} if such edge
f@0: * doesn't exist
f@0: */
f@0: public static Edge findEdge(String edgeType,Edge[] edges){
f@0: for(Edge e : edges){
f@0: if(e.getType().equals(edgeType)){
f@0: return e;
f@0: }
f@0: }
f@0: return null;
f@0: }
f@0:
f@0: /**
f@0: * Finds a node with an id in a {@code Collection} of nodes. If none of the nodes
f@0: * has the id passed as argument, {@code null} is returned.
f@0: *
f@0: * @param id the id of the node to find
f@0: * @param collection the collection to search for the node
f@0: * @return the node with the specified id, or {@code null} if such node doesn't exist
f@0: */
f@0: public static Node findNode(Long id, Collection collection){
f@0: for(Node n : collection)
f@0: if(n.getId() == id)
f@0: return n;
f@0: return null;
f@0: }
f@0:
f@0: /**
f@0: * Finds a node containing a point {@code p} in a {@code Collection} of nodes. If none of the nodes
f@0: * contains the point, {@code null} is returned.
f@0: *
f@0: * @param p the point in a graphic environment
f@0: * @param collection the collection to search for the node
f@0: * @return the node containing {@code p}, or {@code null} if such node doesn't exist
f@0: */
f@0: public static Node findNode(Point2D p, Collection collection){
f@0: for (Node n : collection)
f@0: if (n.contains(p))
f@0: return n;
f@0: return null;
f@0: }
f@0:
f@0: /**
f@0: * Finds an edge with an id in a {@code Collection} of edges. If none of the edges
f@0: * has the id passed as argument, {@code null} is returned.
f@0: *
f@0: * @param id the id of the edge to find
f@0: * @param collection the collection to search for the edge
f@0: * @return the edge with the specified id, or {@code null} if such edge doesn't exist
f@0: */
f@0: public static Edge findEdge(Long id, Collection collection){
f@0: for(Edge e : collection)
f@0: if(e.getId() == id)
f@0: return e;
f@0: return null;
f@0: }
f@0:
f@0: /**
f@0: * Finds an edge containing a point {@code p} in a {@code Collection} of edges. If none of the edges
f@0: * contains the point, {@code null} is returned.
f@0: *
f@0: * @param p the point in a graphic environment
f@0: * @param collection the collection to search for the edge
f@0: * @return the edge containing {@code p}, or {@code null} if such edge doesn't exist
f@0: */
f@0: public static Edge findEdge(Point2D p, Collection collection){
f@0: for (Edge e : collection)
f@0: if (e.contains(p))
f@0: return e;
f@0: return null;
f@0: }
f@0:
f@0: /**
f@0: * Finds a element (node or edge) with an id in a {@code Collection} of diagram elements. If none of the elements
f@0: * has the id passed as argument, {@code null} is returned.
f@0: *
f@0: * @param id the id of the element to find
f@0: * @param collection the collection to search for the element
f@0: * @return the element with the specified id, or {@code null} if such element doesn't exist
f@0: */
f@0: public static DiagramElement findElement(Long id, Collection collection){
f@0: for(DiagramElement e : collection)
f@0: if(e.getId() == id)
f@0: return e;
f@0: return null;
f@0: }
f@0:
f@0: /**
f@0: * 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: * has the code passed as argument, {@code null} is returned.
f@0: *
f@0: * @param identityHashcode the identity hash code of the element to find
f@0: * @param collection the collection to search for the element
f@0: * @return the element with the specified identity hash code, or {@code null} if such element doesn't exist
f@0: *
f@0: * @see Object#hashCode()
f@0: */
f@0: public static DiagramElement findElementByHashcode(long identityHashcode, Collection collection){
f@0: for(DiagramElement de : collection){
f@0: if(System.identityHashCode(de) == identityHashcode){
f@0: return de;
f@0: }
f@0: }
f@0: return null;
f@0: }
f@0:
f@0: /**
f@0: * Returns the tree node whose path is described by the variable path
f@0: * where path contains the indexes returned by each node n of the
f@0: * path upon calling n.getParent().getChildAt(n).
f@0: *
f@0: * @param path the path for the node in the tree
f@0: * @param root the node where the path starts
f@0: * @return the node at the specified path, or {@code null} otherwise
f@0: */
f@0: public static DiagramTreeNode findTreeNode(int[] path, DiagramTreeNode root){
f@0: DiagramTreeNode retVal = root;
f@0: for(int i=0;i