KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > ide > Package


1 /*
2   Copyright (C) 2002-2003 Renaud Pawlak <renaud@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA */

18
19 package org.objectweb.jac.ide;
20
21 import java.io.File JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Hashtable JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Vector JavaDoc;
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 JavaDoc parent;
46    
47     /**
48      * Get the value of parent.
49      * @return value of parent.
50      */

51     public Package JavaDoc getParent() {
52         return parent;
53     }
54    
55     /**
56      * Set the value of parent.
57      * @param v Value to assign to parent.
58      */

59     public void setParent(Package JavaDoc v) {
60         this.parent = v;
61     }
62
63     public String JavaDoc getPath() {
64         if (parent == null) {
65             return name;
66         } else {
67             return parent.getPath()+"/"+name;
68         }
69     }
70
71     public String JavaDoc getPPath() {
72         if( parent == null ) {
73             return name;
74         } else {
75             return parent.getPPath()+"."+name;
76         }
77     }
78    
79     List JavaDoc subPackages=new Vector JavaDoc();
80     public static String JavaDoc packagePathToFile(String JavaDoc path) {
81         return path.replace('.',System.getProperty("file.separator").charAt(0));
82     }
83
84     /**
85      * Get the value of packages.
86      * @return value of packages.
87      */

88     public List JavaDoc getSubPackages() {
89         return subPackages;
90     }
91    
92     /**
93      * Set the value of packages.
94      * @param v Value to assign to packages.
95      */

96     public void setSubPackages(Vector JavaDoc v) {
97         this.subPackages = v;
98     }
99    
100     public void addSubPackage(Package JavaDoc p) {
101         subPackages.add(p);
102         //p.setParent(this);
103
}
104    
105     public void removeSubPackage(Package JavaDoc p) {
106         subPackages.remove(p);
107     }
108
109     public Package JavaDoc getPackageByName(String JavaDoc pkgName) {
110         Iterator JavaDoc it = subPackages.iterator();
111         while (it.hasNext()) {
112             Package JavaDoc pkg = (Package JavaDoc)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 JavaDoc findPackage(String JavaDoc pkgName) {
124         int dot = pkgName.indexOf('.');
125         if (dot!=-1) {
126             Package JavaDoc 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 JavaDoc diagrams = new Vector JavaDoc();
137    
138     /**
139      * Get the value of diagrams.
140      * @return value of diagrams.
141      */

142     public List JavaDoc 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 JavaDoc 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 JavaDoc classes = new Vector JavaDoc();
164    
165     /**
166      * Get the value of classes.
167      * @return value of classes.
168      */

169     public List JavaDoc 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 JavaDoc v) {
178         this.classes = v;
179     }
180    
181     public void addClass(Class JavaDoc c) {
182         classes.add(c);
183     }
184
185     public void addInterface(Interface i) {
186         classes.add(i);
187     }
188    
189     public void removeClass(Class JavaDoc c) {
190         // this part should be handled by integrity
191
c.getLinks().clear();
192
193         Collection JavaDoc diagrams = ObjectRepository.getObjects(Diagram.class);
194         Iterator JavaDoc 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 JavaDoc 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     /**
231      * Returns all classes of this package or a subpackage of it.
232      */

233     public Collection JavaDoc getAllClasses() {
234         Vector JavaDoc result = new Vector JavaDoc();
235         result.addAll(classes);
236         Iterator JavaDoc it = subPackages.iterator();
237         while (it.hasNext()) {
238             Package JavaDoc pkg = (Package JavaDoc)it.next();
239             result.addAll(pkg.getAllClasses());
240         }
241         return result;
242     }
243
244
245     /**
246      * Returns all resources of this package or a subpackage of it, in
247      * a Map whose keys are packages and values are Attachements.
248      */

249     public Map JavaDoc getAllResources() {
250         Hashtable JavaDoc result = new Hashtable JavaDoc();
251         Iterator JavaDoc it = resources.iterator();
252         while (it.hasNext()) {
253             result.put(this,it.next());
254         }
255         it = subPackages.iterator();
256         while (it.hasNext()) {
257             Package JavaDoc pkg = (Package JavaDoc)it.next();
258             result.putAll(pkg.getAllResources());
259         }
260         return result;
261     }
262
263     /**
264      * Gets a class by its name.
265      * @param className the requested class name
266      * @return of class of the package whose name or generation name is
267      * className, or null
268      */

269     public Class JavaDoc getClassByName(String JavaDoc className) {
270         Iterator JavaDoc it = classes.iterator();
271         while (it.hasNext()) {
272             Class JavaDoc cl = (Class JavaDoc)it.next();
273             if (cl.getName().equals(className) || cl.getGenerationName().equals(className))
274                 return cl;
275         }
276         return null;
277     }
278
279     /**
280      * Gets a class by its name. Subpackages are searched recursively.
281      * @param className the requested class name (partial fully
282      * qualified class name:
283      * &lt;sub_pkg1&gt;.&lt;sub_pkg2&gt;.&lt;class_name&gt;
284      * @return a class of the package whose name is className, or null */

285     public Class JavaDoc findClass(String JavaDoc className) {
286         int dot = className.indexOf('.');
287         if (dot!=-1) {
288             String JavaDoc packageName = className.substring(0,dot);
289             Package JavaDoc 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 JavaDoc instances = new Vector JavaDoc();
304    
305     /**
306      * Get the value of instances.
307      * @return value of instances.
308      */

309     public List JavaDoc getInstances() {
310         return instances;
311     }
312    
313     /**
314      * Set the value of instances.
315      * @param v Value to assign to instances.
316      */

317     public void setInstances(Vector JavaDoc 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 JavaDoc groups = new Vector JavaDoc();
328    
329     /**
330      * Get the value of groups.
331      * @return value of groups.
332      */

333     public List JavaDoc 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 JavaDoc resources = new Vector JavaDoc();
344     public List JavaDoc 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 JavaDoc getFullName() {
355         return getPPath();
356     }
357
358     /**
359      * Returns available main classes (Classes wich have a static void
360      * main(String[]) method)
361      */

362     public Collection JavaDoc getMainClasses() {
363         Vector JavaDoc mainClasses = new Vector JavaDoc();
364         Vector JavaDoc parameters = new Vector JavaDoc();
365         parameters.add(Projects.types.resolveType("String","java.lang"));
366         Iterator JavaDoc it = classes.iterator();
367         while (it.hasNext()) {
368             Class JavaDoc cl = (Class JavaDoc)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 JavaDoc pkg = (Package JavaDoc)it.next();
376             mainClasses.addAll(pkg.getMainClasses());
377         }
378         return mainClasses;
379     }
380
381 }
382
Popular Tags