Coverage Report - net.sourceforge.combean.adapters.jgraph.JGraphUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
JGraphUtils
54%
14/26
40%
4/10
2
 
 1  
 /*
 2  
     This file is part of Combean.
 3  
 
 4  
     Combean is free software; you can redistribute it and/or modify
 5  
     it under the terms of the GNU General Public License as published by
 6  
     the Free Software Foundation; either version 2 of the License, or
 7  
     (at your option) any later version.
 8  
 
 9  
     Combean is distributed in the hope that it will be useful,
 10  
     but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12  
     GNU General Public License for more details.
 13  
 
 14  
     You should have received a copy of the GNU General Public License
 15  
     along with Combean; if not, write to the Free Software
 16  
     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 17  
 */
 18  
 /*
 19  
  * Created on 19.03.2005
 20  
  *
 21  
  */
 22  
 package net.sourceforge.combean.adapters.jgraph;
 23  
 
 24  
 import org.jgraph.graph.AttributeMap;
 25  
 import org.jgraph.graph.DefaultEdge;
 26  
 import org.jgraph.graph.DefaultGraphCell;
 27  
 import org.jgraph.graph.DefaultPort;
 28  
 
 29  
 import net.sourceforge.combean.interfaces.graph.NodeIterator;
 30  
 
 31  
 /**
 32  
  * Utilities for working with JGraph classes
 33  
  * 
 34  
  * @author schickin
 35  
  *
 36  
  */
 37  0
 public final class JGraphUtils {
 38  
     
 39  
     /**
 40  
      * Locate the first node in a JGraphModelAsGraph for which a
 41  
      * given attribute has a specified value
 42  
      * 
 43  
      * @param g the graph where the first matching node shall be located
 44  
      * @param attributeKey the attribute which is used for matching the node
 45  
      * @param queryValue a node matches if the given attribute has this value
 46  
      * @return the first matching node
 47  
      */
 48  
     public static JGraphCellAsNode locateFirstNodeWithAttribute(
 49  
             JGraphModelAsGraph g,
 50  
             Object attributeKey,
 51  
             Object queryValue )
 52  
     {
 53  0
         JGraphCellAsNode result = null;
 54  
         
 55  0
         NodeIterator itAllNodes = g.getAllNodesIterator();
 56  0
         while (itAllNodes.hasNext()) {
 57  0
             JGraphCellAsNode v = (JGraphCellAsNode) itAllNodes.next();
 58  0
             AttributeMap attribs = v.getGraphCell().getAttributes();
 59  0
             Object value = attribs.get(attributeKey);
 60  0
             if (value != null &&
 61  
                 queryValue.toString().equals(value.toString())) {
 62  0
                 result = v;
 63  0
                 break;
 64  
             }
 65  0
         }
 66  0
         return result;
 67  
     }
 68  
     
 69  
     public static DefaultGraphCell[] createGraphCells(int num) {
 70  39
         DefaultGraphCell[] result = new DefaultGraphCell[num];
 71  
         
 72  141
         for (int i = 0; i < num; i++) {
 73  102
             result[i] = new DefaultGraphCell();
 74  102
             DefaultPort port = new DefaultPort();
 75  102
             result[i].add(port);
 76  
         }
 77  
         
 78  39
         return result;
 79  
     }
 80  
     
 81  
     public static DefaultEdge[] createEdges(DefaultGraphCell[] nodes, int[][] edges) {
 82  27
         DefaultEdge[] result = new DefaultEdge[edges.length];
 83  
         
 84  54
         for (int i = 0; i < edges.length; i++) {
 85  27
             result[i] = createEdge(nodes, edges[i][0], edges[i][1]);
 86  
         }
 87  
         
 88  27
         return result;
 89  
     }
 90  
     
 91  
     public static DefaultEdge createEdge(DefaultGraphCell[] nodes, int from, int to) {
 92  81
         DefaultEdge result = new DefaultEdge();
 93  
         
 94  81
         result.setSource(nodes[from].getChildAt(0));
 95  81
         result.setTarget(nodes[to].getChildAt(0));
 96  
         
 97  81
         return result;
 98  
     }
 99  
 }