KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > explorer > swing > graph > LayoutAlgorithm


1 /*====================================================================
2
3  Objectweb Explorer Framework
4  Copyright (C) 2000-2004 INRIA - USTL - LIFL - GOAL
5  Contact: openccm@objectweb.org
6
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public
9  License as published by the Free Software Foundation; either
10  version 2.1 of the License, or any later version.
11
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License for more details.
16
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library; if not, write to the Free Software
19  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20  USA
21
22  Initial developer(s): Laëtitia Lafeuille.
23  Contributor(s): ______________________________________.
24
25  ====================================================================*/

26 package org.objectweb.util.explorer.swing.graph;
27
28 import java.awt.Dimension JavaDoc;
29 import java.awt.Point JavaDoc;
30 import java.awt.Rectangle JavaDoc;
31 import java.util.Hashtable JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Vector JavaDoc;
34
35 import org.jgraph.JGraph;
36 import org.jgraph.graph.CellView;
37 import org.jgraph.graph.GraphConstants;
38
39 /**
40  *
41  * @version 0.2
42  */

43 public class LayoutAlgorithm {
44
45     /** Vector that serves for the layout algorithm*/
46     private Vector JavaDoc vertecesPosition = new Vector JavaDoc();
47     
48     /** The interface where are defined the graphics method to draw a vertex */
49     private VertexGraphicsInterface vg;
50
51     /** Boolean that indicates the first passage in a method*/
52     private boolean isFirst;
53
54     public LayoutAlgorithm(Vector JavaDoc vertecesPosition, VertexGraphicsInterface vg) {
55         isFirst = true;
56         this.vertecesPosition = vertecesPosition;
57         this.vg=vg;
58     }
59
60     /**
61      * Research a vertexView for a specific vertex
62      * @param graph, the graph that contains all the components.
63      * @param vertex
64      * @return the cellView corresponding to the vertex
65      */

66     private CellView getVertexView(JGraph graph, PrimitiveVertex vertex) {
67         CellView[] roots = graph.getGraphLayoutCache().getRoots();
68         for (int i = 0; i < roots.length; i++) {
69             if (((PrimitiveVertex) roots[i].getCell()).getName().equals(
70                     vertex.getName()))
71                 return roots[i];
72         }
73         return null;
74     }
75
76     /**
77      * Update the position of the vertex in parameter.
78      * @param graph, the graph that contains all the components.
79      * @param vertex
80      * @param x, the new vertex x location
81      * @param y, the new vertex y location
82      */

83     public void refreshGraph(JGraph graph, PrimitiveVertex vertex, int x, int y) {
84         Map JavaDoc nested = new Hashtable JavaDoc();
85         Map JavaDoc vAttributes = vertex.getAttributes();
86         Dimension JavaDoc size = vertex.getSize();
87         GraphConstants.setBounds(vAttributes, new Rectangle JavaDoc(x, y, size.width,
88                 size.height));
89         nested.put(vertex, vAttributes);
90         graph.getGraphLayoutCache().edit(nested, null, null, null);
91     }
92
93     /**
94      * Resize a vertex
95      * @param graph, the graph that contains all the components.
96      * @param vertex
97      * @param origin, the origin point location of the vertex
98      * @param width, the new width of the vertex
99      * @param height, the new height of the vertex
100      */

101     public void resizeComposite(JGraph graph, CompositeVertex vertex,
102             Point JavaDoc origin, int width, int height) {
103         if (vertex != null) {
104             vertex.setSize((new Dimension JavaDoc(width, height)));
105             Map JavaDoc nested = new Hashtable JavaDoc();
106             Map JavaDoc vAttributes = vertex.getAttributes();
107             Dimension JavaDoc size = vertex.getSize();
108             GraphConstants.setBounds(vAttributes, new Rectangle JavaDoc(origin.x,
109                     origin.y, width, height));
110             nested.put(vertex, vAttributes);
111             //refresh all the graph
112
graph.getGraphLayoutCache().edit(nested, null, null, null);
113         }
114     }
115
116     /**
117      * @param line, a line in the vertecesPosition vector
118      * @return the width of the line
119      */

120     private int getLineWidth(Vector JavaDoc line) {
121         int width = 0;
122         for (int i = 0; i < line.size(); i++) {
123             PrimitiveVertex vertex = (PrimitiveVertex) line.get(i);
124             if (vertex != null) {
125                 width = width + vertex.getSize().width;
126             }
127         }
128         return width;
129     }
130
131     /**
132      * @param columnIndex, the index of a column int the vertecesPosition vector
133      * @return, the height of the column
134      */

135     private int getColumnHeight(int columnIndex) {
136         int height = 0;
137         for (int i = 0; i < vertecesPosition.size(); i++) {
138             Vector JavaDoc column = (Vector JavaDoc) vertecesPosition.get(i);
139             if (column.size() > columnIndex) {
140                 PrimitiveVertex vertex = (PrimitiveVertex) column
141                         .get(columnIndex);
142                 if (vertex != null) {
143                     height = height + vertex.getSize().height;
144                 }
145             }
146         }
147         return height;
148     }
149
150     /**
151      * @return the number of columns in the vertecesPosition vector
152      */

153     private int getNbColumn() {
154         int nb = 0;
155         for (int i = 0; i < vertecesPosition.size(); i++) {
156             Vector JavaDoc line = (Vector JavaDoc) vertecesPosition.get(i);
157             int lineSize = 0;
158             for (int j = 0; j < line.size(); j++) {
159                 if (line.get(j) != null)
160                     lineSize++;
161             }
162             if (lineSize > nb) {
163                 nb = lineSize;
164             }
165         }
166         return nb;
167     }
168
169     /**
170      * @return the number of lines in the vertecesPosition vector
171      */

172     private int getNbLine() {
173         return vertecesPosition.size();
174     }
175
176     /**
177      * @return the accumulate width of all the verteces
178      */

179     private int getGraphWidth() {
180         int width = 0;
181         for (int i = 0; i < vertecesPosition.size(); i++) {
182             Vector JavaDoc line = (Vector JavaDoc) vertecesPosition.get(i);
183             if (getLineWidth(line) > width)
184                 width = getLineWidth(line);
185         }
186         return width;
187     }
188
189     /**
190      * @return the accumulate height of all the verteces
191      */

192     private int getGraphHeight() {
193         int height = 0;
194         for (int i = 0; i < getNbColumn(); i++) {
195             if (getColumnHeight(i) > height)
196                 height = getColumnHeight(i);
197         }
198         return height;
199     }
200
201     /**
202      * Apply the layout algorithm to place the verteces
203      * @param graph, the graph that contains all the components.
204      * @param compositeOrigin, the super composite/frame origin
205      * @param compositeSize, the super composite/frame size
206      * @param composite, the super composite
207      */

208     public void applyLayout(JGraph graph, Point JavaDoc compositeOrigin,
209             Dimension JavaDoc compositeSize, CompositeVertex composite) {
210         int currentX = compositeOrigin.x
211                 + VertexGraphicsInterface.FIRST_VETEX_LOCATION.x;
212         int currentY = compositeOrigin.y
213                 + VertexGraphicsInterface.FIRST_VETEX_LOCATION.y;
214         int w = getGraphWidth();
215         int h = getGraphHeight();
216         int nbLine = getNbLine() - 1;
217         if (nbLine == 0)
218             nbLine++;
219         int nbColumn = getNbColumn() - 1;
220         if (nbColumn == 0)
221             nbColumn++;
222         //the width interval between two verteces
223
int intervalX = (compositeSize.width - getGraphWidth() - VertexGraphicsInterface.FIRST_VETEX_LOCATION.x * 2)
224                 / nbColumn - VertexGraphicsInterface.COMPOSITE_MEMBRANE_SIZE*2;
225         //the height interval between two verteces
226
int intervalY = (compositeSize.height - getGraphHeight() - VertexGraphicsInterface.FIRST_VETEX_LOCATION.y * 3)
227                 / nbLine;
228         
229         if (intervalX < LayoutGraphicsInterface.MIN_INTERVAL_WIDTH) {
230             if (intervalX < 0)
231                 intervalX = 0;
232             int newWidth = compositeSize.width + nbColumn
233                     * (LayoutGraphicsInterface.MIN_INTERVAL_WIDTH - intervalX);
234             if (composite != null) {
235                 resizeComposite(graph, composite, compositeOrigin, newWidth,
236                         compositeSize.height);
237                 applyLayout(graph, compositeOrigin, compositeSize, composite);
238             }
239             else {
240                 compositeSize.width = newWidth;
241                 applyLayout(graph, compositeOrigin, compositeSize, composite);
242             }
243         }
244         
245          else if (intervalY < LayoutGraphicsInterface.MIN_INTERVAL_HEIGTH) {
246             if (intervalY < 0)
247                 intervalY = 0;
248             int newHeight = compositeSize.height + nbLine
249                     * (LayoutGraphicsInterface.MIN_INTERVAL_HEIGTH - intervalY);
250             if (composite != null) {
251                 resizeComposite(graph, composite, compositeOrigin,
252                         compositeSize.width, newHeight);
253                 applyLayout(graph, compositeOrigin, compositeSize, composite);
254             } else {
255                 compositeSize.height = newHeight;
256                 applyLayout(graph, compositeOrigin, compositeSize, composite);
257             }
258         }
259         
260         else {
261             isFirst = false;
262             resizeComposite(graph, composite, compositeOrigin,
263                     compositeSize.width, compositeSize.height);
264             for (int i = 0; i < vertecesPosition.size(); i++) {
265                 int precedentWidth = 0;
266                 currentX = compositeOrigin.x
267                         + VertexGraphicsInterface.FIRST_VETEX_LOCATION.x;
268                 Vector JavaDoc courantLine = (Vector JavaDoc) vertecesPosition.get(i);
269                 int maxVertexHeight = 0;
270                 for (int j = 0; j < courantLine.size(); j++) {
271                     PrimitiveVertex currentVertex = (PrimitiveVertex) courantLine
272                             .get(j);
273                     Dimension JavaDoc vertexSize = new Dimension JavaDoc(0, 0);
274                     if (currentVertex != null) {
275                         vertexSize = currentVertex.getSize();
276                         refreshGraph(graph, currentVertex, currentX, currentY);
277                         if (maxVertexHeight < vertexSize.height)
278                             maxVertexHeight = vertexSize.height;
279                     } else {
280                         vertexSize = vg.getDefaultSize();
281                     }
282                     precedentWidth = (int) vertexSize.getWidth();
283                     currentX = currentX + precedentWidth + intervalX;
284                 }
285
286                 currentY = currentY + maxVertexHeight + intervalY;
287             }
288         }
289     }
290 }
291
292
Popular Tags