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@0: import uk.ac.qmul.eecs.ccmi.diagrammodel.DiagramModelTreeNode; fiore@0: fiore@0: /** fiore@0: * fiore@0: * A utility class which provides methods for searching either a node or an edge fiore@0: * in a collection or array. fiore@0: * fiore@0: */ fiore@0: public abstract class Finder { fiore@0: public static Node findNode(String nodeClass,Node[] prototypes){ fiore@0: for(Node n : prototypes){ fiore@0: if(n.getType().equals(nodeClass)){ fiore@0: return n; fiore@0: } fiore@0: } fiore@0: return null; fiore@0: } fiore@0: fiore@0: public static Edge findEdge(String edgeClass,Edge[] prototypes){ fiore@0: for(Edge e : prototypes){ fiore@0: if(e.getType().equals(edgeClass)){ fiore@0: return e; fiore@0: } fiore@0: } fiore@0: return null; fiore@0: } fiore@0: 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@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@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@0: public static Edge findEdge(Long id, Collection collection){ fiore@0: for(Edge e : collection) fiore@0: if(e.getId() == id) fiore@0: return e; fiore@0: return null; fiore@0: } fiore@0: 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@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@0: * Return the tree node whose path is described by the variable path fiore@0: * where path contains the indexes returned by each node n of the path upon calling n.getParent().getChildAt(n) fiore@0: * fiore@0: * @param path fiore@0: * @param root fiore@0: * @return fiore@0: */ fiore@0: public static DiagramModelTreeNode findTreeNode(int[] path, DiagramModelTreeNode root){ fiore@0: DiagramModelTreeNode retVal = root; fiore@0: for(int i=0;i