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