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    
022    import org.objectweb.jac.ide.InheritanceLink;
023    import org.objectweb.jac.util.Log;
024    import java.awt.Point;
025    import java.util.HashSet;
026    import java.util.Iterator;
027    import java.util.List;
028    import java.util.Set;
029    import java.util.Vector;
030    
031    public class Diagram extends ModelElement {
032    
033        public Diagram() {
034        }
035    
036        public Diagram(String name) {
037            this.name = name;
038        }
039    
040        Package container;
041       
042        /**
043         * Get the value of container.
044         * @return value of container.
045         */
046        public Package getContainer() {
047            return container;
048        }
049       
050        /**
051         * Set the value of container.
052         * @param v  Value to assign to container.
053         */
054        public void setContainer(Package  v) {
055            this.container = v;
056        }
057    
058        /* the figures (classes, links, etc) contained in this diagram */
059        HashSet figures = new HashSet();
060    
061        public Set getFigures() {
062            return figures;
063        }
064    
065        public void addFigure(Figure figure) {
066            figures.add(figure);
067        }
068    
069        public void removeFigure(Figure figure) {
070            figures.remove(figure);
071        }
072     
073        /**
074         * Removes the figure of an element.
075         * @param element the element whose figure shall be removed
076         */
077        public void removeElement(ModelElement element) {
078            Iterator it = figures.iterator();
079            while (it.hasNext()) {
080                Figure figure = (Figure)it.next();
081                if (figure.getElement()==element) {
082                    removeFigure(figure);
083                    return;
084                }
085            }
086        }
087    
088        /**
089         * Removes an inheritance link between two classes
090         * @param cl the subclass
091         * @param superClass the superclass
092         */
093        public void removeInheritanceLink(Class cl, Class superClass) {
094            Iterator it = figures.iterator();
095            while (it.hasNext()) {
096                Figure figure = (Figure)it.next();
097                if (figure.getElement() instanceof InheritanceLink) {
098                    InheritanceLink link = (InheritanceLink)figure.getElement();
099                    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 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 cl, Point 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 getMissingRelations(Class cl) {
135            List relations = new Vector();
136    
137            // find relation links
138            Iterator 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 && contains(superClass)) {
150                relations.add(new InheritanceLink(cl,(Class)superClass));
151            }
152            it = figures.iterator();
153            while (it.hasNext()) {
154                Figure figure = (Figure)it.next();
155                if (figure.getElement() instanceof Class) {
156                    Class otherClass = (Class)figure.getElement();
157                    superClass = otherClass.getSuperClass();
158                    if (superClass instanceof Class && superClass==cl) {
159                        relations.add(new InheritanceLink(otherClass,cl));
160                    }
161                }
162            }
163    
164            return relations;
165        }
166    }