1 26 package org.objectweb.util.explorer.swing.graph; 27 28 import java.awt.Dimension ; 29 import java.awt.Point ; 30 import java.awt.Rectangle ; 31 import java.util.Hashtable ; 32 import java.util.Map ; 33 import java.util.Vector ; 34 35 import org.jgraph.JGraph; 36 import org.jgraph.graph.CellView; 37 import org.jgraph.graph.GraphConstants; 38 39 43 public class LayoutAlgorithm { 44 45 46 private Vector vertecesPosition = new Vector (); 47 48 49 private VertexGraphicsInterface vg; 50 51 52 private boolean isFirst; 53 54 public LayoutAlgorithm(Vector vertecesPosition, VertexGraphicsInterface vg) { 55 isFirst = true; 56 this.vertecesPosition = vertecesPosition; 57 this.vg=vg; 58 } 59 60 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 83 public void refreshGraph(JGraph graph, PrimitiveVertex vertex, int x, int y) { 84 Map nested = new Hashtable (); 85 Map vAttributes = vertex.getAttributes(); 86 Dimension size = vertex.getSize(); 87 GraphConstants.setBounds(vAttributes, new Rectangle (x, y, size.width, 88 size.height)); 89 nested.put(vertex, vAttributes); 90 graph.getGraphLayoutCache().edit(nested, null, null, null); 91 } 92 93 101 public void resizeComposite(JGraph graph, CompositeVertex vertex, 102 Point origin, int width, int height) { 103 if (vertex != null) { 104 vertex.setSize((new Dimension (width, height))); 105 Map nested = new Hashtable (); 106 Map vAttributes = vertex.getAttributes(); 107 Dimension size = vertex.getSize(); 108 GraphConstants.setBounds(vAttributes, new Rectangle (origin.x, 109 origin.y, width, height)); 110 nested.put(vertex, vAttributes); 111 graph.getGraphLayoutCache().edit(nested, null, null, null); 113 } 114 } 115 116 120 private int getLineWidth(Vector 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 135 private int getColumnHeight(int columnIndex) { 136 int height = 0; 137 for (int i = 0; i < vertecesPosition.size(); i++) { 138 Vector column = (Vector ) 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 153 private int getNbColumn() { 154 int nb = 0; 155 for (int i = 0; i < vertecesPosition.size(); i++) { 156 Vector line = (Vector ) 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 172 private int getNbLine() { 173 return vertecesPosition.size(); 174 } 175 176 179 private int getGraphWidth() { 180 int width = 0; 181 for (int i = 0; i < vertecesPosition.size(); i++) { 182 Vector line = (Vector ) vertecesPosition.get(i); 183 if (getLineWidth(line) > width) 184 width = getLineWidth(line); 185 } 186 return width; 187 } 188 189 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 208 public void applyLayout(JGraph graph, Point compositeOrigin, 209 Dimension 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 int intervalX = (compositeSize.width - getGraphWidth() - VertexGraphicsInterface.FIRST_VETEX_LOCATION.x * 2) 224 / nbColumn - VertexGraphicsInterface.COMPOSITE_MEMBRANE_SIZE*2; 225 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 courantLine = (Vector ) 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 vertexSize = new Dimension (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 |