view java/src/uk/ac/qmul/eecs/ccmi/gui/Finder.java @ 8:ea7885bd9bff tip

fixed bug : render solid line as dotted/dashed when moving the stylus from dotted/dashed to solid
author ccmi-guest
date Thu, 03 Jul 2014 16:12:20 +0100
parents d66dd5880081
children
line wrap: on
line source
/*  
 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
  
 Copyright (C) 2011  Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package uk.ac.qmul.eecs.ccmi.gui;

import java.awt.geom.Point2D;
import java.util.Collection;

import uk.ac.qmul.eecs.ccmi.diagrammodel.DiagramElement;
import uk.ac.qmul.eecs.ccmi.diagrammodel.DiagramTreeNode;

/**
 * 
 * A utility class providing static methods for searching either a node or an edge 
 * in a collection or array.
 */
public abstract class Finder {
	/**
	 * Finds a node of a type in an array of nodes. The types should be all different
	 * from each other as only the first node encountered will be returned. If none of the nodes
	 * as the type passed as argument, {@code null} is returned.   
	 * 
	 * @param nodeType the type of the node to find.
	 * @param nodes the array to search for the node
	 * @return the first node with type {@code nodeType} or {@code null} if such node
	 * doesn't exist 
	 */
	public static Node findNode(String nodeType,Node[] nodes){
		for(Node n : nodes){
			if(n.getType().equals(nodeType)){
				return n;
			}
		}
		return null;
	}
	
	/**
	 * Finds an edge of a {@code edgeType} type in an array of nodes. The types should be all different
	 * from each other as only the first edge encountered will be returned. If none of the edges
	 * as the type passed as argument, {@code null} is returned.   
	 * 
	 * @param edgeType the type of the edge to find
	 * @param edges the array to search for the edge
	 * @return the first edge with type {@code nodeType} or {@code null} if such edge
	 * doesn't exist 
	 */
	public static Edge findEdge(String edgeType,Edge[] edges){
		for(Edge e : edges){
			if(e.getType().equals(edgeType)){
				return e;
			}
		}
		return null;
	}
	
	/**
	 * Finds a node with an id in a {@code Collection} of nodes. If none of the nodes
	 * has the id passed as argument, {@code null} is returned.  
	 * 
	 * @param id the id of the node to find 
	 * @param collection the collection to search for the node 
	 * @return the node with the specified id, or {@code null} if such node doesn't exist 
	 */
	public static Node findNode(Long id, Collection<Node> collection){
		for(Node n : collection)
			if(n.getId() == id)
				return n;
		return null;
	}
	
	/**
	 * Finds a node containing a point {@code p} in a {@code Collection} of nodes. If none of the nodes
	 * contains the point, {@code null} is returned.  
	 *
	 * @param p the point in a graphic environment
	 * @param collection the collection to search for the node 
	 * @return the node containing {@code p}, or {@code null} if such node doesn't exist 
	 */
	public static Node findNode(Point2D p, Collection<Node> collection){
		for (Node n : collection)
			if (n.contains(p)) 
				return n;
		return null;
	}
	
	/**
	 * Finds an edge with an id in a {@code Collection} of edges. If none of the edges
	 * has the id passed as argument, {@code null} is returned.  
	 * 
	 * @param id the id of the edge to find 
	 * @param collection the collection to search for the edge 
	 * @return the edge with the specified id, or {@code null} if such edge doesn't exist 
	 */
	public static Edge findEdge(Long id, Collection<Edge> collection){
		for(Edge e : collection)
			if(e.getId() == id)
				return e;
		return null;
	}
	
	/**
	 * Finds an edge containing a point {@code p} in a {@code Collection} of edges. If none of the edges
	 * contains the point, {@code null} is returned.  
	 * 
	 * @param p the point in a graphic environment
	 * @param collection the collection to search for the edge 
	 * @return the edge containing {@code p}, or {@code null} if such edge doesn't exist 
	 */
	public static Edge findEdge(Point2D p, Collection<Edge> collection){
		for (Edge e : collection)
			if (e.contains(p)) 
				return e;
		return null;
	}
	
	/**
	 * Finds a element (node or edge) with an id in a {@code Collection} of diagram elements. If none of the elements
	 * has the id passed as argument, {@code null} is returned.  
	 * 
	 * @param id the id of the element to find 
	 * @param collection the collection to search for the element 
	 * @return the element with the specified id, or {@code null} if such element doesn't exist 
	 */
	public static DiagramElement findElement(Long id, Collection<DiagramElement> collection){
		for(DiagramElement e : collection)
			if(e.getId() == id)
				return e;
		return null;
	}
	
	/**
	 * Finds a element (node or edge) with an identity hash code in a {@code Collection} of diagram elements. If none of the elements
	 * has the code passed as argument, {@code null} is returned.  
	 * 
	 * @param identityHashcode the identity hash code of the element to find 
	 * @param collection the collection to search for the element 
	 * @return the element with the specified identity hash code, or {@code null} if such element doesn't exist
	 * 
	 *  @see Object#hashCode()
	 */
	public static DiagramElement findElementByHashcode(long identityHashcode, Collection<DiagramElement> collection){
		for(DiagramElement de : collection){
			if(System.identityHashCode(de) == identityHashcode){
				return de;
			}
		}
		return null;
	}
	
	/**
	 * Returns the tree node whose path is described by the variable path
	 * where path contains the indexes returned by each node n of the 
	 * path upon calling n.getParent().getChildAt(n). 
	 * 
	 * @param path the path for the node in the tree 
	 * @param root the node where the path starts
	 * @return the node at the specified path, or {@code null} otherwise
	 */
	public static DiagramTreeNode findTreeNode(int[] path, DiagramTreeNode root){
		DiagramTreeNode retVal = root;
		for(int i=0;i<path.length;i++){
			if(retVal.getChildCount() <= path[i])
				return null;
			retVal = retVal.getChildAt(path[i]);
		}
		return retVal;
	}

}