01: /*
02: * Lucane - a collaborative platform
03: * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation; either
08: * version 2.1 of the License, or (at your option) any later version.
09: *
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;
22: import java.util.HashMap;
23: import java.util.Iterator;
24: import java.util.Map;
25:
26: import org.jgraph.graph.*;
27:
28: public class GraphUtils {
29: public static DefaultGraphCell findCorrespondingCell(MyGraph graph,
30: DefaultGraphCell cell) {
31: if (cell == null)
32: return null;
33:
34: Object[] cells = graph.getAllCells();
35:
36: //check with timestamp
37: Object cellTime = cell.getAttributes().get("timestamp");
38: for (int i = 0; i < cells.length; i++) {
39: DefaultGraphCell child = (DefaultGraphCell) cells[i];
40: Object myTime = child.getAttributes().get("timestamp");
41: if (myTime != null && myTime.equals(cellTime))
42: return child;
43: }
44:
45: return null;
46: }
47:
48: public static Object[] cloneCells(MyGraph graph, Object[] cells) {
49: Map cloneMap = graph.getModel().cloneCells(cells);
50: Object[] clones = new Object[cloneMap.size()];
51: int i = 0;
52:
53: Iterator keys = cloneMap.keySet().iterator();
54: while (keys.hasNext())
55: clones[i++] = cloneMap.get(keys.next());
56:
57: return clones;
58: }
59:
60: public static ParentMap createParentMap(Object[] cells) {
61: ParentMap map = new ParentMap();
62:
63: for (int i = 0; i < cells.length; i++) {
64: DefaultGraphCell cell = (DefaultGraphCell) cells[i];
65: if (cell.getParent() != null)
66: map.addEntry(cell, cell.getParent());
67: }
68:
69: return map;
70: }
71:
72: public static void changeTimeStamp(Object[] cells) {
73: for (int i = 0; i < cells.length; i++) {
74: DefaultGraphCell cell = (DefaultGraphCell) cells[i];
75: AttributeMap attr = cell.getAttributes();
76: Date old = (Date) attr.get("timestamp");
77: if (old != null)
78: attr.put("timestamp", new Date((long) Math.random()
79: * Integer.MAX_VALUE));
80: }
81: }
82:
83: public static Map createAttributeMap(Object[] cells) {
84: Map map = new HashMap();
85: for (int i = 0; i < cells.length; i++) {
86: DefaultGraphCell cell = (DefaultGraphCell) cells[i];
87: map.put(cell, cell.getAttributes());
88: }
89: return map;
90: }
91:
92: }
|