KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > whiteboard > operations > GraphLayoutEdit


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.operations;
20
21 import java.util.Date JavaDoc;
22 import java.util.Hashtable JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.jgraph.event.GraphModelEvent;
26 import org.jgraph.graph.*;
27 import org.lucane.applications.whiteboard.graph.GraphUtils;
28 import org.lucane.applications.whiteboard.graph.MyGraph;
29
30 public class GraphLayoutEdit implements GraphOperation
31 {
32     public static final int BRING_TO_FRONT = 0;
33     public static final int BRING_TO_BACK = 1;
34     public static final int GROUP = 2;
35     
36     private Object JavaDoc[] changed;
37     private int operation;
38     private Date JavaDoc timestamp;
39     
40     public void init(GraphModelEvent gme) {}
41     
42     public void init(Object JavaDoc[] cells, int operation)
43     {
44         init(cells, operation, null);
45     }
46     
47     public void init(Object JavaDoc[] cells, int operation, Date JavaDoc timestamp)
48     {
49         this.changed = cells;
50         this.operation = operation;
51         this.timestamp = timestamp;
52     }
53
54     public void apply(MyGraph graph)
55     {
56         GraphModel model = graph.getModel();
57         Object JavaDoc[] cells = new Object JavaDoc[changed.length];
58         for(int i=0;i<changed.length;i++)
59         {
60             DefaultGraphCell cell = (DefaultGraphCell)changed[i];
61             cells[i] = GraphUtils.findCorrespondingCell(graph, cell);
62         }
63         
64         if(operation == BRING_TO_FRONT)
65             bringToFront(graph, cells);
66         else if(operation == BRING_TO_BACK)
67             bringToBack(graph, cells);
68         else if(operation == GROUP)
69             group(graph, cells);
70     }
71     
72     private void bringToFront(MyGraph graph, Object JavaDoc[] cells)
73     {
74         graph.getGraphLayoutCache().toFront(cells);
75     }
76     
77     private void bringToBack(MyGraph graph, Object JavaDoc[] cells)
78     {
79         graph.getGraphLayoutCache().toBack(cells);
80     }
81
82     private void group(MyGraph graph, Object JavaDoc[] cells)
83     {
84         DefaultGraphCell group = new DefaultGraphCell("Group");
85         AttributeMap attrs = new AttributeMap();
86         attrs.put("timestamp", timestamp);
87         Map JavaDoc attributes = new Hashtable JavaDoc();
88         attributes.put(group, attrs);
89         
90         ParentMap map = new ParentMap();
91         for (int i = cells.length-1; i >=0; i--)
92             map.addEntry(cells[i], group);
93
94         graph.getGraphLayoutCache().insert(
95             new Object JavaDoc[] {group}, attributes, null, map, null);
96     }
97 }
Popular Tags