KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.Color JavaDoc;
22 import java.util.Date JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.jgraph.JGraph;
27 import org.jgraph.cellview.JGraphDiamondView;
28 import org.jgraph.cellview.JGraphEllipseView;
29 import org.jgraph.cellview.JGraphMultilineView;
30 import org.jgraph.cellview.JGraphRoundRectView;
31 import org.jgraph.graph.AttributeMap;
32 import org.jgraph.graph.CellMapper;
33 import org.jgraph.graph.CellView;
34 import org.jgraph.graph.DefaultGraphCell;
35 import org.jgraph.graph.DefaultGraphModel;
36 import org.jgraph.graph.GraphConstants;
37 import org.jgraph.graph.GraphModel;
38 import org.jgraph.graph.GraphUndoManager;
39 import org.jgraph.graph.ParentMap;
40 import org.jgraph.graph.Port;
41 import org.jgraph.graph.VertexView;
42 import org.lucane.applications.whiteboard.graph.cells.DiamondCell;
43 import org.lucane.applications.whiteboard.graph.cells.EllipseCell;
44 import org.lucane.applications.whiteboard.graph.cells.ImageCell;
45 import org.lucane.applications.whiteboard.graph.cells.RoundRectangleCell;
46 import org.lucane.applications.whiteboard.graph.cells.TextCell;
47
48 public class MyGraph extends JGraph
49 {
50     private GraphUndoManager undoManager;
51     private Color JavaDoc cellBorder = Color.BLACK;
52     private Color JavaDoc cellBackground = null;
53     private int lineWidth = 1;
54     private float[] dashPattern = new float[] { 1, 0 };
55     private Object JavaDoc[] clipboard = new Object JavaDoc[0];
56     
57     public MyGraph(GraphModel model)
58     {
59         super(model);
60         this.undoManager = new GraphUndoManager();
61         model.addUndoableEditListener(undoManager);
62     }
63     
64     public void setModel(GraphModel model)
65     {
66         super.setModel(model);
67         this.undoManager = new GraphUndoManager();
68         model.addUndoableEditListener(undoManager);
69     }
70     
71     public void resetUndoManager()
72     {
73         this.undoManager = new GraphUndoManager();
74         this.getModel().addUndoableEditListener(undoManager);
75     }
76     
77     protected VertexView createVertexView(JGraph graph, CellMapper cm, Object JavaDoc v )
78     {
79         if (v instanceof EllipseCell)
80             return new JGraphEllipseView(v, this, cm);
81         else if (v instanceof DiamondCell)
82             return new JGraphDiamondView(v, this, cm);
83         else if (v instanceof RoundRectangleCell)
84             return new JGraphRoundRectView(v, this, cm);
85         else if (v instanceof ImageCell)
86             return new ScaledVertexView(v, this, cm);
87         else if ((v instanceof TextCell) && ((TextCell) v).isMultiLined())
88             return new JGraphMultilineView(v, this, cm);
89         return super.createVertexView(graph, cm, v);
90     }
91     
92     public Object JavaDoc[] getAllCells()
93     {
94       return getDescendants(getRoots());
95     }
96     
97     public boolean isGroup(Object JavaDoc cell)
98     {
99         // Map the Cell to its View
100
CellView view = getGraphLayoutCache().getMapping(cell, false);
101         if (view != null)
102             return !view.isLeaf();
103         
104         return false;
105     }
106     
107     public void setCellBorder(Color JavaDoc color)
108     {
109         this.cellBorder = color == null ? Color.BLACK : color;
110
111         Object JavaDoc[] cells = getSelectionCells();
112         HashMap JavaDoc changes = new HashMap JavaDoc();
113         for(int i=0;i<cells.length;i++)
114         {
115             DefaultGraphCell cell = (DefaultGraphCell)cells[i];
116             AttributeMap attr = new AttributeMap();
117             GraphConstants.setBorderColor(attr, cellBorder);
118             GraphConstants.setLineColor(attr, cellBorder);
119             changes.put(cell, attr);
120         }
121         getModel().edit(changes, null, null, null);
122     }
123     
124     public Color JavaDoc getCellBorder()
125     {
126         return this.cellBorder;
127     }
128     
129     public void setCellBackground(Color JavaDoc color)
130     {
131         this.cellBackground = color;
132         
133         Object JavaDoc[] cells = getSelectionCells();
134         HashMap JavaDoc changes = new HashMap JavaDoc();
135         for(int i=0;i<cells.length;i++)
136         {
137             DefaultGraphCell cell = (DefaultGraphCell)cells[i];
138             AttributeMap attr = new AttributeMap();
139             GraphConstants.setOpaque(attr, cellBackground!=null);
140             if(cellBackground != null)
141                 GraphConstants.setBackground(attr, cellBackground);
142             changes.put(cell, attr);
143         }
144         getModel().edit(changes, null, null, null);
145     }
146     
147     public Color JavaDoc getCellBackground()
148     {
149         return this.cellBackground;
150     }
151     
152     public void setLineWidth(int w)
153     {
154         this.lineWidth = w;
155         Object JavaDoc[] cells = getSelectionCells();
156         HashMap JavaDoc changes = new HashMap JavaDoc();
157         for(int i=0;i<cells.length;i++)
158         {
159             DefaultGraphCell cell = (DefaultGraphCell)cells[i];
160             AttributeMap attr = new AttributeMap();
161             GraphConstants.setLineWidth(attr, lineWidth);
162             changes.put(cell, attr);
163         }
164         getModel().edit(changes, null, null, null);
165     }
166     
167     public int getLineWidth()
168     {
169         return this.lineWidth;
170     }
171     
172     public void setDashPattern(float[] pattern)
173     {
174         this.dashPattern = pattern;
175         Object JavaDoc[] cells = getSelectionCells();
176         HashMap JavaDoc changes = new HashMap JavaDoc();
177         for(int i=0;i<cells.length;i++)
178         {
179             DefaultGraphCell cell = (DefaultGraphCell)cells[i];
180             AttributeMap attr = new AttributeMap();
181             GraphConstants.setDashPattern(attr, pattern);
182             changes.put(cell, attr);
183         }
184         getModel().edit(changes, null, null, null);
185     }
186     
187     public float[] getDashPattern()
188     {
189         return this.dashPattern;
190     }
191     
192     public void cut()
193     {
194         Object JavaDoc[] cells = getSelectionCells();
195         if (cells != null)
196             cells = DefaultGraphModel.getDescendants(getModel(), cells).toArray();
197             
198         
199         this.copy();
200         getModel().remove(cells);
201     }
202     
203     public void copy()
204     {
205         Object JavaDoc[] cells = getSelectionCells();
206         clipboard = GraphUtils.cloneCells(this, cells);
207         System.out.println("copy: " + clipboard.length);
208     }
209     
210     public boolean paste()
211     {
212         if(clipboard.length == 0)
213             return false;
214         
215         Object JavaDoc[] copy = GraphUtils.cloneCells(this, clipboard);
216         GraphUtils.changeTimeStamp(copy);
217         ParentMap parents = GraphUtils.createParentMap(copy);
218         Map JavaDoc attrs = GraphUtils.createAttributeMap(copy);
219         
220         getModel().insert(copy, attrs, null, parents, null);
221         System.out.println("paste: " + clipboard.length);
222         return true;
223     }
224     
225     public boolean undo()
226     {
227         if(undoManager.canUndo())
228         {
229             undoManager.undo(getModel());
230             return true;
231         }
232         return false;
233     }
234
235     public boolean redo()
236     {
237         if(undoManager.canRedo())
238         {
239             undoManager.redo(getModel());
240             return true;
241         }
242         return false;
243     }
244 }
Popular Tags