annotate java/src/uk/ac/qmul/eecs/ccmi/gui/Finder.java @ 4:2c67ac862920

bug fix (correct haptic painting after renaming nodes and after nodes deletion with an edge with more than 2 nodes)
author Fiore Martin <fiore@eecs.qmul.ac.uk>
date Tue, 29 May 2012 15:32:19 +0100
parents 9e67171477bc
children d66dd5880081
rev   line source
fiore@0 1 /*
fiore@0 2 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
fiore@0 3
fiore@0 4 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
fiore@0 5
fiore@0 6 This program is free software: you can redistribute it and/or modify
fiore@0 7 it under the terms of the GNU General Public License as published by
fiore@0 8 the Free Software Foundation, either version 3 of the License, or
fiore@0 9 (at your option) any later version.
fiore@0 10
fiore@0 11 This program is distributed in the hope that it will be useful,
fiore@0 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
fiore@0 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
fiore@0 14 GNU General Public License for more details.
fiore@0 15
fiore@0 16 You should have received a copy of the GNU General Public License
fiore@0 17 along with this program. If not, see <http://www.gnu.org/licenses/>.
fiore@0 18 */
fiore@0 19 package uk.ac.qmul.eecs.ccmi.gui;
fiore@0 20
fiore@0 21 import java.awt.geom.Point2D;
fiore@0 22 import java.util.Collection;
fiore@0 23
fiore@0 24 import uk.ac.qmul.eecs.ccmi.diagrammodel.DiagramElement;
fiore@3 25 import uk.ac.qmul.eecs.ccmi.diagrammodel.DiagramTreeNode;
fiore@0 26
fiore@0 27 /**
fiore@0 28 *
fiore@0 29 * A utility class which provides methods for searching either a node or an edge
fiore@0 30 * in a collection or array.
fiore@0 31 *
fiore@0 32 */
fiore@0 33 public abstract class Finder {
fiore@0 34 public static Node findNode(String nodeClass,Node[] prototypes){
fiore@0 35 for(Node n : prototypes){
fiore@0 36 if(n.getType().equals(nodeClass)){
fiore@0 37 return n;
fiore@0 38 }
fiore@0 39 }
fiore@0 40 return null;
fiore@0 41 }
fiore@0 42
fiore@0 43 public static Edge findEdge(String edgeClass,Edge[] prototypes){
fiore@0 44 for(Edge e : prototypes){
fiore@0 45 if(e.getType().equals(edgeClass)){
fiore@0 46 return e;
fiore@0 47 }
fiore@0 48 }
fiore@0 49 return null;
fiore@0 50 }
fiore@0 51
fiore@0 52 public static Node findNode(Long id, Collection<Node> collection){
fiore@0 53 for(Node n : collection)
fiore@0 54 if(n.getId() == id)
fiore@0 55 return n;
fiore@0 56 return null;
fiore@0 57 }
fiore@0 58
fiore@0 59 public static Node findNode(Point2D p, Collection<Node> collection){
fiore@0 60 for (Node n : collection)
fiore@0 61 if (n.contains(p))
fiore@0 62 return n;
fiore@0 63 return null;
fiore@0 64 }
fiore@0 65
fiore@0 66 public static Edge findEdge(Point2D p, Collection<Edge> collection){
fiore@0 67 for (Edge e : collection)
fiore@0 68 if (e.contains(p))
fiore@0 69 return e;
fiore@0 70 return null;
fiore@0 71 }
fiore@0 72
fiore@0 73 public static Edge findEdge(Long id, Collection<Edge> collection){
fiore@0 74 for(Edge e : collection)
fiore@0 75 if(e.getId() == id)
fiore@0 76 return e;
fiore@0 77 return null;
fiore@0 78 }
fiore@0 79
fiore@0 80 public static DiagramElement findElement(Long id, Collection<DiagramElement> collection){
fiore@0 81 for(DiagramElement e : collection)
fiore@0 82 if(e.getId() == id)
fiore@0 83 return e;
fiore@0 84 return null;
fiore@0 85 }
fiore@0 86
fiore@0 87 public static DiagramElement findElementByHashcode(long identityHashcode, Collection<DiagramElement> collection){
fiore@0 88 for(DiagramElement de : collection){
fiore@0 89 if(System.identityHashCode(de) == identityHashcode){
fiore@0 90 return de;
fiore@0 91 }
fiore@0 92 }
fiore@0 93 return null;
fiore@0 94 }
fiore@0 95
fiore@0 96 /**
fiore@0 97 * Return the tree node whose path is described by the variable path
fiore@0 98 * where path contains the indexes returned by each node n of the path upon calling n.getParent().getChildAt(n)
fiore@0 99 *
fiore@0 100 * @param path
fiore@0 101 * @param root
fiore@0 102 * @return
fiore@0 103 */
fiore@3 104 public static DiagramTreeNode findTreeNode(int[] path, DiagramTreeNode root){
fiore@3 105 DiagramTreeNode retVal = root;
fiore@0 106 for(int i=0;i<path.length;i++){
fiore@0 107 if(retVal.getChildCount() <= path[i])
fiore@0 108 return null;
fiore@0 109 retVal = retVal.getChildAt(path[i]);
fiore@0 110 }
fiore@0 111 return retVal;
fiore@0 112 }
fiore@0 113
fiore@0 114 }