1 18 19 package org.objectweb.jac.ide.diagrams; 20 21 import CH.ifa.draw.figures.TextFigure; 22 import CH.ifa.draw.framework.Connector; 23 import CH.ifa.draw.framework.DrawingView; 24 import CH.ifa.draw.framework.Figure; 25 import CH.ifa.draw.framework.FigureChangeEvent; 26 import CH.ifa.draw.framework.FigureEnumeration; 27 import CH.ifa.draw.standard.CompositeFigure; 28 import CH.ifa.draw.standard.NullHandle; 29 import CH.ifa.draw.standard.RelativeLocator; 30 import org.objectweb.jac.aspects.gui.ObjectUpdate; 31 import org.objectweb.jac.aspects.gui.Utils; 32 import org.objectweb.jac.core.Wrappee; 33 import org.objectweb.jac.core.Wrapping; 34 import org.objectweb.jac.ide.Class; 35 import org.objectweb.jac.ide.Field; 36 import org.objectweb.jac.ide.Method; 37 import org.objectweb.jac.ide.ModelElement; 38 import org.objectweb.jac.ide.Package; 39 import org.objectweb.jac.ide.Projects; 40 import org.objectweb.jac.util.Log; 41 import java.awt.Color ; 42 import java.awt.Dimension ; 43 import java.awt.Graphics ; 44 import java.awt.Point ; 45 import java.awt.Rectangle ; 46 import java.util.Iterator ; 47 import java.util.Vector ; 48 49 public class ClassFigure extends CompositeFigure 50 implements ObjectUpdate, ModelElementFigure 51 { 52 53 private static final int BORDER = 3; 54 56 Vector relationLinkFigures = new Vector (); 57 Vector inheritanceLinkFigures = new Vector (); 58 Vector fieldFigures = new Vector (); 59 Vector methodFigures = new Vector (); 60 61 DrawingView view; 62 63 org.objectweb.jac.ide.ClassFigure classFig; 64 Dimension dimension = new Dimension (); 65 66 public ClassFigure(org.objectweb.jac.ide.ClassFigure figure, Package pack, 67 DrawingView view) { 68 initialize(); 69 this.classFig = figure; 70 this.containerPackage = pack; 71 this.view = view; 72 73 ClassNameFigure name=new ClassNameFigure(figure.getCl(),this); 74 name.setZValue(1); 75 add(name); 76 77 DiagramView.init = true; 78 try { 79 initFields(); 80 initMethods(); 81 } finally { 82 DiagramView.init = false; 83 } 84 Utils.registerObject(classFig.getCl(),this); 85 Utils.registerObject(classFig,this); 86 87 layout(); 88 } 89 90 public void objectUpdated(Object object, Object extra) { 92 Vector fs = new Vector (); 93 fs.add(this); 94 initClass(); 95 layout(); 96 changed(); 97 } 100 101 public void close() { 102 Utils.unregisterObject(classFig.getCl(),this); 104 Utils.unregisterObject(classFig,this); 105 } 106 107 private static final long serialVersionUID = -7877776240236946511L; 109 private int pertFigureSerializedDataVersion = 1; 110 111 112 boolean isInternal() { 113 return containerPackage.getClasses().contains(classFig.getCl()); 114 } 115 116 Package containerPackage; 117 118 122 public Package getContainerPackage() { 123 return containerPackage; 124 } 125 126 130 public void setContainerPackage(Package v) { 131 this.containerPackage = v; 132 } 133 134 void initClass() { 135 if (classFig.getCl() == null) { 136 Log.warning("UNRESOLVED CLASS!"); 137 } else { 138 Log.trace("figures","init class "+classFig.getCl().getName()); 139 DiagramView.init = true; 140 try { 141 removeAllMembers(); 142 initTitle(); 143 initFields(); 144 initMethods(); 145 } finally { 146 DiagramView.init = false; 147 } 148 154 } 155 } 156 157 void initTitle() { 158 ClassNameFigure nf = (ClassNameFigure)figures().nextFigure(); 159 nf.setSubstance(classFig.getCl()); 160 nf.setText(classFig.getCl().getName()); 161 } 162 163 164 void initFields() { 165 if (!classFig.isHideFields()) { 166 Iterator it = classFig.getCl().getFields().iterator(); 167 while(it.hasNext()) { 168 Field field = (Field)it.next(); 169 FieldFigure fieldFigure = new FieldFigure(field,this); 170 Log.trace("figures","adding field "+field); 171 fieldFigures.add(fieldFigure); 172 add(fieldFigure); 173 } 174 } 175 } 176 177 void initMethods() { 178 if (!classFig.isHideMethods()) { 179 Iterator it = classFig.getCl().getAllMethods().iterator(); 180 while(it.hasNext()) { 181 Method method = (Method)it.next(); 182 MethodFigure methodFigure = new MethodFigure(method,this); 183 Log.trace("figures","adding method "+method); 184 add(methodFigure); 185 methodFigures.add(methodFigure); 186 } 187 } 188 } 189 190 void removeAllMembers() { 191 FigureEnumeration it = figures(); 192 it.nextFigure(); 194 while (it.hasMoreElements()) { 195 Figure f = it.nextFigure(); 196 Log.trace("figures","removing "+f); 197 remove(f); 198 Wrapping.invokeRoleMethod( (Wrappee) ((ModelElementFigure)f).getSubstance(), 199 "unregisterObject",new Object []{this}); 200 } 201 methodFigures.clear(); 202 fieldFigures.clear(); 203 } 204 205 protected void basicMoveBy(int dx, int dy) { 206 Log.trace("figures",2,this+".basicMoveBy "+dx+","+dy); 207 classFig.translate(dx, dy); 208 super.basicMoveBy(dx, dy); 209 } 210 211 public Rectangle displayBox() { 212 return new Rectangle (classFig.getCorner(),dimension); 213 } 214 215 public void basicDisplayBox(Point origin, Point corner) { 216 classFig.setCorner(origin); 217 dimension.width = corner.x-origin.x; 218 dimension.height = corner.y-origin.y; 219 layout(); 220 } 221 222 protected Color getFillColor() { 223 if (isInternal()) 224 return Color.white; 225 else 226 return Color.lightGray; 227 } 228 229 static final Color VERY_LIGHT_GRAY = new Color (220,220,220); 230 231 protected Color getColor() { 232 if (classFig.getCl().getContainer()!=null) 233 return Color.black; 234 else 235 return VERY_LIGHT_GRAY; 236 } 237 238 protected void drawBorder(Graphics g) { 239 Rectangle r = displayBox(); 240 241 g.setColor(getFillColor()); 242 g.fillRect(r.x, r.y, r.width, r.height); 243 244 g.setColor(getColor()); 245 g.drawRect(r.x, r.y, r.width, r.height); 246 247 Figure f = figureAt(0); 248 Rectangle rf = f.displayBox(); 249 250 g.drawLine(r.x,r.y+rf.height+1,r.x+r.width,r.y+rf.height+1); 251 252 if( fieldFigures.size() > 0 ) { 253 f = (Figure) fieldFigures.get(0); 254 rf = f.displayBox(); 255 g.drawLine(r.x,rf.y,r.x+r.width,rf.y); 256 } 257 } 258 259 public void draw(Graphics g) { 260 Log.trace("diagram.draw",this+".draw"); 261 drawBorder(g); 262 super.draw(g); 263 } 264 265 public Vector handles() { 266 Vector handles = new Vector (); 267 handles.addElement(new NullHandle(this, RelativeLocator.northWest())); 268 handles.addElement(new NullHandle(this, RelativeLocator.northEast())); 269 handles.addElement(new NullHandle(this, RelativeLocator.southWest())); 270 handles.addElement(new NullHandle(this, RelativeLocator.southEast())); 271 return handles; 272 } 273 274 void addRelationLinkFigure(RelationLinkFigure a) { 275 relationLinkFigures.add(a); 276 } 277 278 void addInheritanceLinkFigure(InheritanceLinkFigure link) { 279 inheritanceLinkFigures.add(link); 280 } 281 282 void removeInheritanceLinkFigure(InheritanceLinkFigure link) { 283 inheritanceLinkFigures.remove(link); 284 } 285 286 void removeRelationLinkFigure(RelationLinkFigure a) { 287 relationLinkFigures.remove(a); 288 } 289 290 public Vector getRelationLinkFigures() { 291 return relationLinkFigures; 292 } 293 294 void addFieldFigure() { 295 addFieldFigure("newField","void"); 296 } 297 298 303 void addFieldFigure(String name, String type) { 304 Field f = new Field(); 305 f.setName(name); 306 f.setType(Projects.types.resolveType(type)); 307 classFig.getCl().addField(f); 308 } 309 310 315 316 void addMethodFigure() { 317 Method m = new Method(); 318 m.setName("newMethod"); 319 m.setType(Projects.types.resolveType("void")); 320 classFig.getCl().addMethod(m); 321 } 322 323 void addMethodFigure(MethodFigure f) { 324 methodFigures.add(f); 325 } 326 327 public String getName() { 328 return ((TextFigure)figures().nextFigure()).getText(); 329 } 330 331 335 Vector getClassFigures() { 336 Vector ret = new Vector (); 337 ret.add(figures().nextFigure()); 338 ret.addAll(methodFigures); 339 ret.addAll(fieldFigures); 340 return ret; 341 } 342 343 private void initialize() { 344 setZValue(2); 346 } 347 348 352 public void layout() { 353 Point partOrigin = (Point )classFig.getCorner().clone(); 354 partOrigin.translate(BORDER, BORDER); 355 Dimension extent = new Dimension (0, 0); 356 357 Iterator it = getClassFigures().iterator(); 358 while(it.hasNext()) { 359 Figure f = (Figure)it.next(); 360 361 Dimension partExtent = f.size(); 362 Point corner = new Point ( 363 partOrigin.x+partExtent.width, 364 partOrigin.y+partExtent.height); 365 f.basicDisplayBox(partOrigin, corner); 366 367 extent.width = Math.max(extent.width, partExtent.width); 368 extent.height += partExtent.height; 369 partOrigin.y += partExtent.height; 370 } 371 dimension.width = extent.width + 2*BORDER; 372 dimension.height = extent.height + 2*BORDER; 373 } 374 375 private boolean needsLayout() { 376 387 return true; 388 } 389 390 public void update(FigureChangeEvent e) { 391 Log.trace("figures",this+".update"); 392 if (needsLayout()) { 393 layout(); 394 changed(); 395 } 396 } 397 398 public void figureChanged(FigureChangeEvent e) { 399 update(e); 400 } 401 402 403 public void figureRemoved(FigureChangeEvent e) { 404 update(e); 405 } 406 407 public void release() { 408 Log.trace("diagram","release "+this); 409 super.release(); 410 close(); 411 if (classFig!=null && classFig.getDiagram()!=null) 412 classFig.getDiagram().removeFigure(classFig); 413 } 414 415 public org.objectweb.jac.ide.ClassFigure getClassFig() { 416 return classFig; 417 } 418 419 public void setClassFig(org.objectweb.jac.ide.ClassFigure classFig) { 420 this.classFig = classFig; 421 initClass(); 422 layout(); 423 } 424 425 public Point getCorner() { 426 return classFig.getCorner(); 427 } 428 429 public ModelElement getSubstance() { 430 return classFig.getCl(); 431 } 432 433 public Class getClassElement() { 434 return classFig.getCl(); 435 } 436 437 public Connector connectorAt(Point p) { 439 return connectorAt(p.x,p.y); 440 } 441 } 442 443 | Popular Tags |