1 18 19 package org.objectweb.jac.ide.diagrams; 20 21 import CH.ifa.draw.framework.*; 22 import CH.ifa.draw.standard.*; 23 import java.awt.*; 24 import java.util.*; 25 import org.apache.log4j.Logger; 26 import org.objectweb.jac.core.Wrappee; 27 import org.objectweb.jac.core.Wrapping; 28 import org.objectweb.jac.core.rtti.ClassItem; 29 import org.objectweb.jac.core.rtti.ClassRepository; 30 import org.objectweb.jac.core.rtti.CollectionItem; 31 import org.objectweb.jac.core.rtti.FieldItem; 32 import org.objectweb.jac.ide.ModelElement; 33 import org.objectweb.jac.util.Log; 34 35 public class GenericObjectFigure extends CompositeFigure 36 implements ModelElementFigure 37 { 38 static Logger logger = Logger.getLogger("figures"); 39 40 public void close() {} 41 public static int SHAPE_RECT=0; 42 public static int SHAPE_ROUNDRECT=1; 43 44 private static final int BORDER = 3; 45 private Rectangle fDisplayBox; 46 47 Vector fieldFigures=new Vector(); 48 49 int shape=SHAPE_RECT; 50 51 55 public int getShape() { 56 return shape; 57 } 58 59 63 public void setShape(int v) { 64 this.shape = v; 65 } 66 67 DrawingView view; 68 69 org.objectweb.jac.ide.GenericFigure genericFigure; 70 71 75 public org.objectweb.jac.ide.GenericFigure getGenericFigure() { 76 return genericFigure; 77 } 78 79 83 public void setGenericFigure(org.objectweb.jac.ide.GenericFigure v) { 84 this.genericFigure = v; 85 } 86 87 public GenericObjectFigure() {} 88 89 public GenericObjectFigure(org.objectweb.jac.ide.GenericFigure fig, 90 org.objectweb.jac.ide.Package pack, 91 DrawingView view) { 92 this.genericFigure = fig; 93 this.containerPackage = pack; 94 this.view = view; 95 initialize(); 96 } 97 98 void initFields() { 99 if (substance == null) { 100 logger.warn("UNRESOLVED OBJECT!"); 101 } else { 102 logger.debug("init generic object figure "+substance); 103 FieldItem[] fields=null; 104 ClassItem substClass=ClassRepository.get().getClass(substance); 105 if(substClass.getAttribute("Gui.attributesOrder")!=null) { 106 fields=(FieldItem[])substClass.getAttribute("Gui.attributesOrder"); 107 } else { 108 fields=substClass.getPrimitiveFields(); 109 } 110 for(int i=0;i<fields.length;i++) { 111 if(!fields[i].isPrimitive()) continue; 112 logger.debug(" adding field "+fields[i]+" subtance="+substance); 113 AttributeValueFigure ff=new AttributeValueFigure(fields[i],substance); 114 ff.setFont(defaultFont); 115 fieldFigures.add(ff); 116 add(ff); 117 } 118 Wrapping.invokeRoleMethod((Wrappee)substance,"addView",new Object []{this}); 119 } 120 } 121 122 void removeAllMembers() { 123 FigureEnumeration it = figures(); 124 it.nextFigure(); 126 while (it.hasMoreElements()) { 127 Figure f = it.nextFigure(); 128 logger.debug("removing "+f); 129 remove(f); 130 } 131 fieldFigures.clear(); 132 } 133 134 protected void basicMoveBy(int x, int y) { 135 fDisplayBox.translate(x, y); 136 super.basicMoveBy(x, y); 137 } 138 139 public Rectangle displayBox() { 140 return new Rectangle( 141 fDisplayBox.x, 142 fDisplayBox.y, 143 fDisplayBox.width, 144 fDisplayBox.height); 145 } 146 147 public void basicDisplayBox(Point origin, Point corner) { 148 fDisplayBox = new Rectangle(origin); 149 fDisplayBox.add(corner); 150 layout(); 151 } 152 153 protected void drawBorder(Graphics g) { 154 155 Rectangle r = displayBox(); 156 157 g.setColor(Color.white); 158 if(shape==SHAPE_RECT) 159 g.fillRect(r.x, r.y, r.width, r.height); 160 else if(shape==SHAPE_ROUNDRECT) 161 g.fillRoundRect(r.x, r.y, r.width, r.height,7,7); 162 163 g.setColor(Color.black); 164 if(shape==SHAPE_RECT) 165 g.drawRect(r.x, r.y, r.width, r.height); 166 else if(shape==SHAPE_ROUNDRECT) 167 g.drawRoundRect(r.x, r.y, r.width, r.height,7,7); 168 169 171 181 182 } 183 184 public void draw(Graphics g) { 185 drawBorder(g); 186 super.draw(g); 187 } 188 189 public Vector handles() { 190 Vector handles = new Vector(); 191 handles.addElement(new NullHandle(this, RelativeLocator.northWest())); 192 handles.addElement(new NullHandle(this, RelativeLocator.northEast())); 193 handles.addElement(new NullHandle(this, RelativeLocator.southWest())); 194 handles.addElement(new NullHandle(this, RelativeLocator.southEast())); 195 return handles; 196 } 197 198 private void initialize() { 199 fDisplayBox = new Rectangle(0, 0, 0, 0); 200 } 201 202 Font defaultFont=new Font("Helvetica", Font.PLAIN, 10); 203 204 208 public Font getDefaultFont() { 209 return defaultFont; 210 } 211 212 216 public void setDefaultFont(Font v) { 217 this.defaultFont = v; 218 } 219 220 221 public void layout() { 222 Point partOrigin = new Point(fDisplayBox.x, fDisplayBox.y); 223 partOrigin.translate(BORDER, BORDER); 224 Dimension extent = new Dimension(0, 0); 225 226 Iterator it = fieldFigures.iterator(); 227 228 while(it.hasNext()) { 229 Figure f = (Figure)it.next(); 230 231 Dimension partExtent = f.size(); 232 Point corner = new Point( 233 partOrigin.x+partExtent.width, 234 partOrigin.y+partExtent.height); 235 f.basicDisplayBox(partOrigin, corner); 236 237 extent.width = Math.max(extent.width, partExtent.width); 238 extent.height += partExtent.height; 239 partOrigin.y += partExtent.height; 240 } 241 fDisplayBox.width = extent.width + 2*BORDER; 242 fDisplayBox.height = extent.height + 2*BORDER; 243 } 244 245 private boolean needsLayout() { 246 255 return true; 256 } 257 258 public void update(FigureChangeEvent e) { 259 if (needsLayout()) { 260 layout(); 261 changed(); 262 } 263 } 264 265 public void figureChanged(FigureChangeEvent e) { 266 update(e); 267 } 268 269 270 public void figureRemoved(FigureChangeEvent e) { 271 update(e); 272 } 273 274 Object substance; 275 276 299 300 304 public ModelElement getSubstance() { 305 return (ModelElement)substance; 306 } 307 308 312 public void setSubstance(Object v) { 313 this.substance = v; 314 } 315 316 CollectionItem collection; 317 318 322 public CollectionItem getCollection() { 323 return collection; 324 } 325 326 330 public void setCollection(CollectionItem v) { 331 this.collection = v; 332 } 333 334 org.objectweb.jac.ide.Package containerPackage; 335 336 340 public org.objectweb.jac.ide.Package getContainerPackage() { 341 return containerPackage; 342 } 343 344 348 public void setContainerPackage(org.objectweb.jac.ide.Package v) { 349 this.containerPackage = v; 350 } 351 352 } 353 | Popular Tags |