KickJava   Java API By Example, From Geeks To Geeks.

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


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
22 import org.objectweb.jac.ide.InheritanceLink;
23 import org.objectweb.jac.util.Log;
24 import java.awt.Point JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.Vector JavaDoc;
30
31 public class Diagram extends ModelElement {
32
33     public Diagram() {
34     }
35
36     public Diagram(String JavaDoc name) {
37         this.name = name;
38     }
39
40     Package JavaDoc container;
41    
42     /**
43      * Get the value of container.
44      * @return value of container.
45      */

46     public Package JavaDoc getContainer() {
47         return container;
48     }
49    
50     /**
51      * Set the value of container.
52      * @param v Value to assign to container.
53      */

54     public void setContainer(Package JavaDoc v) {
55         this.container = v;
56     }
57
58     /* the figures (classes, links, etc) contained in this diagram */
59     HashSet JavaDoc figures = new HashSet JavaDoc();
60
61     public Set JavaDoc getFigures() {
62         return figures;
63     }
64
65     public void addFigure(Figure figure) {
66         figures.add(figure);
67     }
68
69     public void removeFigure(Figure figure) {
70         figures.remove(figure);
71     }
72  
73     /**
74      * Removes the figure of an element.
75      * @param element the element whose figure shall be removed
76      */

77     public void removeElement(ModelElement element) {
78         Iterator JavaDoc it = figures.iterator();
79         while (it.hasNext()) {
80             Figure figure = (Figure)it.next();
81             if (figure.getElement()==element) {
82                 removeFigure(figure);
83                 return;
84             }
85         }
86     }
87
88     /**
89      * Removes an inheritance link between two classes
90      * @param cl the subclass
91      * @param superClass the superclass
92      */

93     public void removeInheritanceLink(Class JavaDoc cl, Class JavaDoc superClass) {
94         Iterator JavaDoc it = figures.iterator();
95         while (it.hasNext()) {
96             Figure figure = (Figure)it.next();
97             if (figure.getElement() instanceof InheritanceLink) {
98                 InheritanceLink link = (InheritanceLink)figure.getElement();
99                 if (link.getStart()==cl && link.getEnd()==superClass) {
100                     removeFigure(figure);
101                     return;
102                 }
103             }
104         }
105     }
106
107     /**
108      * Tells if the diagram contains a figure that represents the given
109      * model element. */

110     public boolean contains(ModelElement element) {
111         Iterator JavaDoc it = figures.iterator();
112         while (it.hasNext()) {
113             Figure figure = (Figure)it.next();
114             if (figure.getElement()==element) {
115                 return true;
116             }
117         }
118         return false;
119     }
120
121     /**
122      * Create a new figure for an existing class
123      */

124     public void importClass(Class JavaDoc cl, Point JavaDoc corner) {
125         figures.add(new ClassFigure(cl,corner));
126     }
127
128     /**
129      * Gets relations of a class with other classes on the diagram
130      * which are not on the diagram.
131      * @param cl the class
132      * @return a list of Link
133      */

134     public List JavaDoc getMissingRelations(Class JavaDoc cl) {
135         List JavaDoc relations = new Vector JavaDoc();
136
137         // find relation links
138
Iterator JavaDoc it = cl.getRelationLinks().iterator();
139         while (it.hasNext()) {
140             RelationLink relation = (RelationLink)it.next();
141             if (!contains(relation) &&
142                 contains(relation.getEnd()) && contains(relation.getStart())) {
143                 relations.add(relation);
144             }
145         }
146
147         // find inheritance links
148
Type superClass = cl.getSuperClass();
149         if (superClass instanceof Class JavaDoc && contains(superClass)) {
150             relations.add(new InheritanceLink(cl,(Class JavaDoc)superClass));
151         }
152         it = figures.iterator();
153         while (it.hasNext()) {
154             Figure figure = (Figure)it.next();
155             if (figure.getElement() instanceof Class JavaDoc) {
156                 Class JavaDoc otherClass = (Class JavaDoc)figure.getElement();
157                 superClass = otherClass.getSuperClass();
158                 if (superClass instanceof Class JavaDoc && superClass==cl) {
159                     relations.add(new InheritanceLink(otherClass,cl));
160                 }
161             }
162         }
163
164         return relations;
165     }
166 }
167
Popular Tags