KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > whiteboard > graph > GraphUtils


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.applications.whiteboard.graph;
20
21 import java.util.Date JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.jgraph.graph.*;
27
28 public class GraphUtils
29 {
30     public static DefaultGraphCell findCorrespondingCell(MyGraph graph, DefaultGraphCell cell)
31     {
32         if(cell == null)
33             return null;
34         
35         Object JavaDoc[] cells = graph.getAllCells();
36
37         //check with timestamp
38
Object JavaDoc cellTime = cell.getAttributes().get("timestamp");
39         for(int i=0;i<cells.length;i++)
40         {
41             DefaultGraphCell child = (DefaultGraphCell)cells[i];
42             Object JavaDoc myTime = child.getAttributes().get("timestamp");
43             if(myTime != null && myTime.equals(cellTime))
44                 return child;
45         }
46             
47         return null;
48     }
49     
50     public static Object JavaDoc[] cloneCells(MyGraph graph, Object JavaDoc[] cells)
51     {
52         Map JavaDoc cloneMap = graph.getModel().cloneCells(cells);
53         Object JavaDoc[] clones = new Object JavaDoc[cloneMap.size()];
54         int i = 0;
55         
56         Iterator JavaDoc keys = cloneMap.keySet().iterator();
57         while(keys.hasNext())
58             clones[i++] = cloneMap.get(keys.next());
59         
60         return clones;
61     }
62     
63     public static ParentMap createParentMap(Object JavaDoc[] cells)
64     {
65         ParentMap map = new ParentMap();
66         
67         for(int i=0;i<cells.length;i++)
68         {
69             DefaultGraphCell cell = (DefaultGraphCell)cells[i];
70             if(cell.getParent() != null)
71                 map.addEntry(cell, cell.getParent());
72         }
73         
74         return map;
75     }
76     
77     public static void changeTimeStamp(Object JavaDoc[] cells)
78     {
79         for(int i=0;i<cells.length;i++)
80         {
81             DefaultGraphCell cell = (DefaultGraphCell)cells[i];
82             AttributeMap attr = cell.getAttributes();
83             Date JavaDoc old = (Date JavaDoc)attr.get("timestamp");
84             if(old != null)
85                 attr.put("timestamp", new Date JavaDoc((long)Math.random()*Integer.MAX_VALUE));
86         }
87     }
88     
89     public static Map JavaDoc createAttributeMap(Object JavaDoc[] cells)
90     {
91         Map JavaDoc map = new HashMap JavaDoc();
92         for(int i=0;i<cells.length;i++)
93         {
94             DefaultGraphCell cell = (DefaultGraphCell)cells[i];
95             map.put(cell, cell.getAttributes());
96         }
97         return map;
98     }
99     
100 }
Popular Tags