1 18 19 package org.objectweb.jac.ide; 20 21 import java.io.File ; 22 import java.util.Collection ; 23 import java.util.Hashtable ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Map ; 27 import java.util.Vector ; 28 import org.objectweb.jac.core.ObjectRepository; 29 import org.objectweb.jac.lib.Attachment; 30 31 public class Package extends ModelElement { 32 33 Project project; 34 public Project getProject() { 35 if (parent!=null) { 36 return parent.getProject(); 37 } else { 38 return project; 39 } 40 } 41 public void setProject(Project project) { 42 this.project = project; 43 } 44 45 Package parent; 46 47 51 public Package getParent() { 52 return parent; 53 } 54 55 59 public void setParent(Package v) { 60 this.parent = v; 61 } 62 63 public String getPath() { 64 if (parent == null) { 65 return name; 66 } else { 67 return parent.getPath()+"/"+name; 68 } 69 } 70 71 public String getPPath() { 72 if( parent == null ) { 73 return name; 74 } else { 75 return parent.getPPath()+"."+name; 76 } 77 } 78 79 List subPackages=new Vector (); 80 public static String packagePathToFile(String path) { 81 return path.replace('.',System.getProperty("file.separator").charAt(0)); 82 } 83 84 88 public List getSubPackages() { 89 return subPackages; 90 } 91 92 96 public void setSubPackages(Vector v) { 97 this.subPackages = v; 98 } 99 100 public void addSubPackage(Package p) { 101 subPackages.add(p); 102 } 104 105 public void removeSubPackage(Package p) { 106 subPackages.remove(p); 107 } 108 109 public Package getPackageByName(String pkgName) { 110 Iterator it = subPackages.iterator(); 111 while (it.hasNext()) { 112 Package pkg = (Package )it.next(); 113 if (pkg.getName().equals(pkgName)) 114 return pkg; 115 } 116 return null; 117 } 118 119 123 public Package findPackage(String pkgName) { 124 int dot = pkgName.indexOf('.'); 125 if (dot!=-1) { 126 Package pkg = getPackageByName(pkgName.substring(0,dot)); 127 if (pkg!=null) 128 return pkg.findPackage(pkgName.substring(dot+1)); 129 else 130 return null; 131 } else { 132 return getPackageByName(pkgName); 133 } 134 } 135 136 List diagrams = new Vector (); 137 138 142 public List getDiagrams() { 143 return diagrams; 144 } 145 146 150 public void setDiagrams(Vector v) { 151 this.diagrams = v; 152 } 153 154 public void addDiagram(Diagram d) { 155 diagrams.add(d); 157 } 158 159 public void removeDiagram(Diagram d) { 160 diagrams.remove(d); 161 } 162 163 List classes = new Vector (); 164 165 169 public List getClasses() { 170 return classes; 171 } 172 173 177 public void setClasses(Vector v) { 178 this.classes = v; 179 } 180 181 public void addClass(Class c) { 182 classes.add(c); 183 } 184 185 public void addInterface(Interface i) { 186 classes.add(i); 187 } 188 189 public void removeClass(Class c) { 190 c.getLinks().clear(); 192 193 Collection diagrams = ObjectRepository.getObjects(Diagram.class); 194 Iterator it = diagrams.iterator(); 195 while(it.hasNext()) { 196 Diagram d = (Diagram)it.next(); 197 d.removeElement(c); 198 } 199 classes.remove(c); 200 } 201 202 206 public void addRepository(Class itemClass) { 207 Repository repository = new Repository(itemClass); 208 addClass(repository); 209 210 RelationLink relation = new RelationLink(repository,itemClass); 211 ((RelationRole)relation.getStartRole()).setCardinality("0-*"); 212 ((RelationRole)relation.getEndRole()).setCardinality("1"); 213 relation.setOrientation(RelationLink.ORIENTATION_STRAIGHT); 214 relation.setAggregation(true); 215 repository.setItemsRole((RelationRole)relation.getStartRole()); 216 217 Field singleton = new Field(); 218 singleton.setName("singleton"); 219 singleton.setStatic(true); 220 singleton.setType(repository); 221 repository.addField(singleton); 222 223 Method init = new Method(); 224 init.setName("init"); 225 init.setStatic(true); 226 init.setBody("setSingleton(new "+repository.getGenerationName()+"());"); 227 repository.addMethod(init); 228 } 229 230 233 public Collection getAllClasses() { 234 Vector result = new Vector (); 235 result.addAll(classes); 236 Iterator it = subPackages.iterator(); 237 while (it.hasNext()) { 238 Package pkg = (Package )it.next(); 239 result.addAll(pkg.getAllClasses()); 240 } 241 return result; 242 } 243 244 245 249 public Map getAllResources() { 250 Hashtable result = new Hashtable (); 251 Iterator it = resources.iterator(); 252 while (it.hasNext()) { 253 result.put(this,it.next()); 254 } 255 it = subPackages.iterator(); 256 while (it.hasNext()) { 257 Package pkg = (Package )it.next(); 258 result.putAll(pkg.getAllResources()); 259 } 260 return result; 261 } 262 263 269 public Class getClassByName(String className) { 270 Iterator it = classes.iterator(); 271 while (it.hasNext()) { 272 Class cl = (Class )it.next(); 273 if (cl.getName().equals(className) || cl.getGenerationName().equals(className)) 274 return cl; 275 } 276 return null; 277 } 278 279 285 public Class findClass(String className) { 286 int dot = className.indexOf('.'); 287 if (dot!=-1) { 288 String packageName = className.substring(0,dot); 289 Package pkg = getPackageByName(packageName); 290 if (pkg!=null) 291 return pkg.findClass(className.substring(dot+1)); 292 else 293 return null; 294 } else { 295 return getClassByName(className); 296 } 297 } 298 299 public void addAspect(Aspect a) { 300 classes.add(a); 301 } 302 303 List instances = new Vector (); 304 305 309 public List getInstances() { 310 return instances; 311 } 312 313 317 public void setInstances(Vector v) { 318 this.instances = v; 319 } 320 public void addInstance(Instance i) { 321 instances.add(i); 322 } 323 public void removeInstance(Instance i) { 324 instances.remove(i); 325 } 326 327 List groups = new Vector (); 328 329 333 public List getGroups() { 334 return groups; 335 } 336 public void addGroup(Group g) { 337 this.groups.add(g); 338 } 339 public void removeGroup(Group g) { 340 this.groups.remove(g); 341 } 342 343 List resources = new Vector (); 344 public List getResources() { 345 return resources; 346 } 347 public void addResource(Attachment resource) { 348 resources.add(resource); 349 } 350 public void removeResource(Attachment resource) { 351 resources.remove(resource); 352 } 353 354 public String getFullName() { 355 return getPPath(); 356 } 357 358 362 public Collection getMainClasses() { 363 Vector mainClasses = new Vector (); 364 Vector parameters = new Vector (); 365 parameters.add(Projects.types.resolveType("String","java.lang")); 366 Iterator it = classes.iterator(); 367 while (it.hasNext()) { 368 Class cl = (Class )it.next(); 369 if (cl.findMethod("main",parameters)!=null) 370 mainClasses.add(cl); 371 } 372 373 it = subPackages.iterator(); 374 while (it.hasNext()) { 375 Package pkg = (Package )it.next(); 376 mainClasses.addAll(pkg.getMainClasses()); 377 } 378 return mainClasses; 379 } 380 381 } 382 | Popular Tags |