001 /* 002 Copyright (C) 2002-2003 Renaud Pawlak <renaud@aopsys.com> 003 004 This program is free software; you can redistribute it and/or modify 005 it under the terms of the GNU Lesser General Public License as 006 published by the Free Software Foundation; either version 2 of the 007 License, or (at your option) any later version. 008 009 This program is distributed in the hope that it will be useful, 010 but WITHOUT ANY WARRANTY; without even the implied warranty of 011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 GNU Lesser General Public License for more details. 013 014 You should have received a copy of the GNU Lesser General Public 015 License along with this program; if not, write to the Free Software 016 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 017 USA */ 018 019 package org.objectweb.jac.ide; 020 021 import java.io.File; 022 import java.util.Collection; 023 import java.util.Hashtable; 024 import java.util.Iterator; 025 import java.util.List; 026 import java.util.Map; 027 import java.util.Vector; 028 import org.objectweb.jac.core.ObjectRepository; 029 import org.objectweb.jac.lib.Attachment; 030 031 public class Package extends ModelElement { 032 033 Project project; 034 public Project getProject() { 035 if (parent!=null) { 036 return parent.getProject(); 037 } else { 038 return project; 039 } 040 } 041 public void setProject(Project project) { 042 this.project = project; 043 } 044 045 Package parent; 046 047 /** 048 * Get the value of parent. 049 * @return value of parent. 050 */ 051 public Package getParent() { 052 return parent; 053 } 054 055 /** 056 * Set the value of parent. 057 * @param v Value to assign to parent. 058 */ 059 public void setParent(Package v) { 060 this.parent = v; 061 } 062 063 public String getPath() { 064 if (parent == null) { 065 return name; 066 } else { 067 return parent.getPath()+"/"+name; 068 } 069 } 070 071 public String getPPath() { 072 if( parent == null ) { 073 return name; 074 } else { 075 return parent.getPPath()+"."+name; 076 } 077 } 078 079 List subPackages=new Vector(); 080 public static String packagePathToFile(String path) { 081 return path.replace('.',System.getProperty("file.separator").charAt(0)); 082 } 083 084 /** 085 * Get the value of packages. 086 * @return value of packages. 087 */ 088 public List getSubPackages() { 089 return subPackages; 090 } 091 092 /** 093 * Set the value of packages. 094 * @param v Value to assign to packages. 095 */ 096 public void setSubPackages(Vector v) { 097 this.subPackages = v; 098 } 099 100 public void addSubPackage(Package p) { 101 subPackages.add(p); 102 //p.setParent(this); 103 } 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 /** 120 * Find a package by its full name (my.package.ClassName) 121 * @param pkgName the package name to find 122 */ 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 /** 139 * Get the value of diagrams. 140 * @return value of diagrams. 141 */ 142 public List getDiagrams() { 143 return diagrams; 144 } 145 146 /** 147 * Set the value of diagrams. 148 * @param v Value to assign to diagrams. 149 */ 150 public void setDiagrams(Vector v) { 151 this.diagrams = v; 152 } 153 154 public void addDiagram(Diagram d) { 155 //d.setContainer(this); 156 diagrams.add(d); 157 } 158 159 public void removeDiagram(Diagram d) { 160 diagrams.remove(d); 161 } 162 163 List classes = new Vector(); 164 165 /** 166 * Get the value of classes. 167 * @return value of classes. 168 */ 169 public List getClasses() { 170 return classes; 171 } 172 173 /** 174 * Set the value of classes. 175 * @param v Value to assign to classes. 176 */ 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 // this part should be handled by integrity 191 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 /** 203 * Add a repository for a class. Creates a relation, a singleton 204 * static field and static method to initialize the singleton. 205 */ 206 public void addRepository(Class itemClass) { 207 Repository repository = new Repository(itemClass); 208 addClass(repository); 209 210 RelationLink relation = new RelationLink(); 211 relation.setStart(repository); 212 ((RelationRole)relation.getStartRole()).setCardinality("0-*"); 213 relation.setEnd(itemClass); 214 ((RelationRole)relation.getEndRole()).setCardinality("1"); 215 relation.setOrientation(RelationLink.ORIENTATION_STRAIGHT); 216 relation.setAggregation(true); 217 repository.setItemsRole((RelationRole)relation.getStartRole()); 218 219 Field singleton = new Field(); 220 singleton.setName("singleton"); 221 singleton.setStatic(true); 222 singleton.setType(repository); 223 repository.addField(singleton); 224 225 Method init = new Method(); 226 init.setName("init"); 227 init.setStatic(true); 228 init.setBody("setSingleton(new "+repository.getGenerationName()+"());"); 229 repository.addMethod(init); 230 } 231 232 /** 233 * Returns all classes of this package or a subpackage of it. 234 */ 235 public Collection getAllClasses() { 236 Vector result = new Vector(); 237 result.addAll(classes); 238 Iterator it = subPackages.iterator(); 239 while (it.hasNext()) { 240 Package pkg = (Package)it.next(); 241 result.addAll(pkg.getAllClasses()); 242 } 243 return result; 244 } 245 246 247 /** 248 * Returns all resources of this package or a subpackage of it, in 249 * a Map whose keys are packages and values are Attachements. 250 */ 251 public Map getAllResources() { 252 Hashtable result = new Hashtable(); 253 Iterator it = resources.iterator(); 254 while (it.hasNext()) { 255 result.put(this,it.next()); 256 } 257 it = subPackages.iterator(); 258 while (it.hasNext()) { 259 Package pkg = (Package)it.next(); 260 result.putAll(pkg.getAllResources()); 261 } 262 return result; 263 } 264 265 /** 266 * Gets a class by its name. 267 * @param className the requested class name 268 * @return of class of the package whose name or generation name is 269 * className, or null 270 */ 271 public Class getClassByName(String className) { 272 Iterator it = classes.iterator(); 273 while (it.hasNext()) { 274 Class cl = (Class)it.next(); 275 if (cl.getName().equals(className) || cl.getGenerationName().equals(className)) 276 return cl; 277 } 278 return null; 279 } 280 281 /** 282 * Gets a class by its name. Subpackages are searched recursively. 283 * @param className the requested class name (partial fully 284 * qualified class name: 285 * <sub_pkg1>.<sub_pkg2>.<class_name> 286 * @return a class of the package whose name is className, or null */ 287 public Class findClass(String className) { 288 int dot = className.indexOf('.'); 289 if (dot!=-1) { 290 String packageName = className.substring(0,dot); 291 Package pkg = getPackageByName(packageName); 292 if (pkg!=null) 293 return pkg.findClass(className.substring(dot+1)); 294 else 295 return null; 296 } else { 297 return getClassByName(className); 298 } 299 } 300 301 public void addAspect(Aspect a) { 302 classes.add(a); 303 } 304 305 List instances = new Vector(); 306 307 /** 308 * Get the value of instances. 309 * @return value of instances. 310 */ 311 public List getInstances() { 312 return instances; 313 } 314 315 /** 316 * Set the value of instances. 317 * @param v Value to assign to instances. 318 */ 319 public void setInstances(Vector v) { 320 this.instances = v; 321 } 322 public void addInstance(Instance i) { 323 instances.add(i); 324 } 325 public void removeInstance(Instance i) { 326 instances.remove(i); 327 } 328 329 List groups = new Vector(); 330 331 /** 332 * Get the value of groups. 333 * @return value of groups. 334 */ 335 public List getGroups() { 336 return groups; 337 } 338 public void addGroup(Group g) { 339 this.groups.add(g); 340 } 341 public void removeGroup(Group g) { 342 this.groups.remove(g); 343 } 344 345 List resources = new Vector(); 346 public List getResources() { 347 return resources; 348 } 349 public void addResource(Attachment resource) { 350 resources.add(resource); 351 } 352 public void removeResource(Attachment resource) { 353 resources.remove(resource); 354 } 355 356 public String getFullName() { 357 return getPPath(); 358 } 359 360 /** 361 * Returns available main classes (Classes wich have a static void 362 * main(String[]) method) 363 */ 364 public Collection getMainClasses() { 365 Vector mainClasses = new Vector(); 366 Vector parameters = new Vector(); 367 parameters.add(Projects.types.resolveType("String","java.lang")); 368 Iterator it = classes.iterator(); 369 while (it.hasNext()) { 370 Class cl = (Class)it.next(); 371 if (cl.findMethod("main",parameters)!=null) 372 mainClasses.add(cl); 373 } 374 375 it = subPackages.iterator(); 376 while (it.hasNext()) { 377 Package pkg = (Package)it.next(); 378 mainClasses.addAll(pkg.getMainClasses()); 379 } 380 return mainClasses; 381 } 382 383 }