Coverage Report - org.argouml.uml.diagram.collaboration.CollabDiagramGraphModel
 
Classes in this File Line Coverage Branch Coverage Complexity
CollabDiagramGraphModel
5%
7/135
1%
2/110
7.417
 
 1  
 /* $Id: CollabDiagramGraphModel.java 17854 2010-01-12 19:55:57Z linus $
 2  
  *****************************************************************************
 3  
  * Copyright (c) 2009 Contributors - see below
 4  
  * All rights reserved. This program and the accompanying materials
 5  
  * are made available under the terms of the Eclipse Public License v1.0
 6  
  * which accompanies this distribution, and is available at
 7  
  * http://www.eclipse.org/legal/epl-v10.html
 8  
  *
 9  
  * Contributors:
 10  
  *    tfmorris
 11  
  *****************************************************************************
 12  
  *
 13  
  * Some portions of this file was previously release using the BSD License:
 14  
  */
 15  
 
 16  
 // Copyright (c) 1996-2006 The Regents of the University of California. All
 17  
 // Rights Reserved. Permission to use, copy, modify, and distribute this
 18  
 // software and its documentation without fee, and without a written
 19  
 // agreement is hereby granted, provided that the above copyright notice
 20  
 // and this paragraph appear in all copies.  This software program and
 21  
 // documentation are copyrighted by The Regents of the University of
 22  
 // California. The software program and documentation are supplied "AS
 23  
 // IS", without any accompanying services from The Regents. The Regents
 24  
 // does not warrant that the operation of the program will be
 25  
 // uninterrupted or error-free. The end-user understands that the program
 26  
 // was developed for research purposes and is advised not to rely
 27  
 // exclusively on the program for any reason.  IN NO EVENT SHALL THE
 28  
 // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
 29  
 // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
 30  
 // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 31  
 // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 32  
 // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
 33  
 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 34  
 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 35  
 // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 36  
 // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
 37  
 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 38  
 
 39  
 package org.argouml.uml.diagram.collaboration;
 40  
 
 41  
 import java.beans.PropertyChangeEvent;
 42  
 import java.beans.VetoableChangeListener;
 43  
 import java.util.ArrayList;
 44  
 import java.util.Collection;
 45  
 import java.util.Collections;
 46  
 import java.util.Iterator;
 47  
 import java.util.List;
 48  
 
 49  
 import org.apache.log4j.Logger;
 50  
 import org.argouml.model.Model;
 51  
 import org.argouml.uml.CommentEdge;
 52  
 import org.argouml.uml.diagram.UMLMutableGraphSupport;
 53  
 
 54  
 /**
 55  
  * This class defines a bridge between the UML meta-model
 56  
  * representation of the design and the GraphModel interface used by
 57  
  * GEF.  This class handles only UML Collaboration Diagrams.
 58  
  */
 59  19
 public class CollabDiagramGraphModel extends UMLMutableGraphSupport
 60  
     implements VetoableChangeListener {
 61  
     /**
 62  
      * Logger.
 63  
      */
 64  19
     private static final Logger LOG =
 65  
         Logger.getLogger(CollabDiagramGraphModel.class);
 66  
 
 67  
     /**
 68  
      * @param collaboration the collaboration to be set for this diagram
 69  
      */
 70  
     public void setCollaboration(Object collaboration) {
 71  
         try {
 72  19
             if (collaboration == null) {
 73  0
                 throw new IllegalArgumentException(
 74  
                     "A null collaboration was supplied");
 75  
             }
 76  19
             if (!(Model.getFacade().isACollaboration(collaboration))) {
 77  0
                 throw new IllegalArgumentException(
 78  
                     "Expected a collaboration. The type received was "
 79  
                     + collaboration.getClass().getName());
 80  
             }
 81  0
         } catch (IllegalArgumentException e) {
 82  0
             LOG.error("Illegal Argument to setCollaboration", e);
 83  0
             throw e;
 84  19
         }
 85  19
         setHomeModel(collaboration);
 86  19
     }
 87  
 
 88  
 
 89  
     ////////////////////////////////////////////////////////////////
 90  
     // GraphModel implementation
 91  
 
 92  
 
 93  
     /*
 94  
      * Return all ports on node or edge.
 95  
      *
 96  
      * @see org.tigris.gef.graph.GraphModel#getPorts(java.lang.Object)
 97  
      */
 98  
     public List getPorts(Object nodeOrEdge) {
 99  0
         if (Model.getFacade().isAClassifierRole(nodeOrEdge)) {
 100  0
             List result = new ArrayList();
 101  0
             result.add(nodeOrEdge);
 102  0
             return result;
 103  
         }
 104  0
         return Collections.EMPTY_LIST;
 105  
     }
 106  
 
 107  
     /*
 108  
      * Return the node or edge that owns the given port.
 109  
      *
 110  
      * @see org.tigris.gef.graph.BaseGraphModel#getOwner(java.lang.Object)
 111  
      */
 112  
     public Object getOwner(Object port) {
 113  0
         return port;
 114  
     }
 115  
 
 116  
     /*
 117  
      * Return all edges going to given port.
 118  
      *
 119  
      * @see org.tigris.gef.graph.GraphModel#getInEdges(java.lang.Object)
 120  
      */
 121  
     public List getInEdges(Object port) {
 122  
 
 123  0
         if (Model.getFacade().isAClassifierRole(port)) {
 124  0
             Object cr = port;
 125  0
             Collection ends = Model.getFacade().getAssociationEnds(cr);
 126  0
             if (ends == null) {
 127  0
                 return Collections.EMPTY_LIST;
 128  
             }
 129  0
             List result = new ArrayList();
 130  0
             for (Object end : ends) {
 131  0
                 result.add(Model.getFacade().getAssociation(end));
 132  
             }
 133  
         }
 134  0
         return Collections.EMPTY_LIST;
 135  
     }
 136  
 
 137  
     /*
 138  
      * Return all edges going from given port.
 139  
      *
 140  
      * @see org.tigris.gef.graph.GraphModel#getOutEdges(java.lang.Object)
 141  
      */
 142  
     public List getOutEdges(Object port) {
 143  0
         return Collections.EMPTY_LIST; // TODO:?
 144  
     }
 145  
 
 146  
     ////////////////////////////////////////////////////////////////
 147  
     // MutableGraphModel implementation
 148  
 
 149  
     /*
 150  
      * Return true if the given object is a valid node in this graph.
 151  
      *
 152  
      * @see org.tigris.gef.graph.MutableGraphModel#canAddNode(java.lang.Object)
 153  
      */
 154  
     @Override
 155  
     public boolean canAddNode(Object node) {
 156  0
         if (node == null) {
 157  0
             return false;
 158  
         }
 159  0
         if (Model.getFacade().isAAssociation(node)
 160  
                 && !Model.getFacade().isANaryAssociation(node)) {
 161  
             // A binary association is not a node so reject.
 162  0
             return false;
 163  
         }
 164  
     
 165  0
         if (containsNode(node)) {
 166  0
             return false;
 167  
         }
 168  0
         return (Model.getFacade().isAClassifierRole(node)
 169  
             || Model.getFacade().isAMessage(node)
 170  
             || Model.getFacade().isAComment(node));
 171  
     }
 172  
 
 173  
     /*
 174  
      * Return true if the given object is a valid edge in this graph.
 175  
      *
 176  
      * @see org.tigris.gef.graph.MutableGraphModel#canAddEdge(java.lang.Object)
 177  
      */
 178  
     @Override
 179  
     public boolean canAddEdge(Object edge)  {
 180  0
         if (edge == null) {
 181  0
             return false;
 182  
         }
 183  0
         if (containsEdge(edge)) {
 184  0
             return false;
 185  
         }
 186  0
         Object end0 = null;
 187  0
         Object end1 = null;
 188  0
         if (Model.getFacade().isAAssociationRole(edge)) {
 189  0
             Collection conns = Model.getFacade().getConnections(edge);
 190  0
             Iterator iter = conns.iterator();
 191  0
             if (conns.size() < 2) {
 192  0
                 return false;
 193  
             }
 194  0
             Object associationEndRole0 = iter.next();
 195  0
             Object associationEndRole1 = iter.next();
 196  0
             if (associationEndRole0 == null || associationEndRole1 == null) {
 197  0
                 return false;
 198  
             }
 199  0
             end0 = Model.getFacade().getType(associationEndRole0);
 200  0
             end1 = Model.getFacade().getType(associationEndRole1);
 201  0
         } else if (Model.getFacade().isAGeneralization(edge)) {
 202  0
             Object gen = /*(MGeneralization)*/ edge;
 203  0
             end0 = Model.getFacade().getGeneral(gen);
 204  0
             end1 = Model.getFacade().getSpecific(gen);
 205  0
         } else if (Model.getFacade().isADependency(edge)) {
 206  0
             Collection clients = Model.getFacade().getClients(edge);
 207  0
             Collection suppliers = Model.getFacade().getSuppliers(edge);
 208  0
             if (clients == null || clients.isEmpty() 
 209  
                     || suppliers == null || suppliers.isEmpty()) {
 210  0
                 return false;
 211  
             }
 212  0
             end0 = clients.iterator().next();
 213  0
             end1 = suppliers.iterator().next();
 214  0
         } else if (edge instanceof CommentEdge) {
 215  0
             end0 = ((CommentEdge) edge).getSource();
 216  0
             end1 = ((CommentEdge) edge).getDestination();
 217  
         } else {
 218  0
             return false;       
 219  
         }
 220  
         
 221  
         // Both ends must be defined and nodes that are on the graph already.
 222  0
         if (end0 == null || end1 == null) {
 223  0
             LOG.error("Edge rejected. Its ends are not attached to anything");
 224  0
             return false;
 225  
         }
 226  
         
 227  0
         if (!containsNode(end0)
 228  
                 && !containsEdge(end0)) {
 229  0
             LOG.error("Edge rejected. Its source end is attached to " + end0
 230  
                     + " but this is not in the graph model");
 231  0
             return false;
 232  
         }
 233  0
         if (!containsNode(end1)
 234  
                 && !containsEdge(end1)) {
 235  0
             LOG.error("Edge rejected. Its destination end is attached to "
 236  
                     + end1 + " but this is not in the graph model");
 237  0
             return false;
 238  
         }
 239  
         
 240  0
         return true;
 241  
     }
 242  
 
 243  
 
 244  
     /*
 245  
      * Add the given node to the graph, if valid.
 246  
      *
 247  
      * @see org.tigris.gef.graph.MutableGraphModel#addNode(java.lang.Object)
 248  
      */
 249  
     @Override
 250  
     public void addNode(Object node) {
 251  0
         LOG.debug("adding MClassifierRole node!!");
 252  0
         if (!canAddNode(node)) {
 253  0
             return;
 254  
         }
 255  0
         getNodes().add(node);
 256  
         // TODO: assumes public, user pref for default visibility?
 257  0
         if (Model.getFacade().isAClassifier(node)) {
 258  0
             Model.getCoreHelper().addOwnedElement(getHomeModel(), node);
 259  
             // ((MClassifier)node).setNamespace(_collab.getNamespace());
 260  
         }
 261  
 
 262  0
         fireNodeAdded(node);
 263  0
     }
 264  
 
 265  
     /*
 266  
      * Add the given edge to the graph, if valid.
 267  
      *
 268  
      * @see org.tigris.gef.graph.MutableGraphModel#addEdge(java.lang.Object)
 269  
      */
 270  
     @Override
 271  
     public void addEdge(Object edge) {
 272  0
         LOG.debug("adding class edge!!!!!!");
 273  0
         if (!canAddEdge(edge)) {
 274  0
             return;
 275  
         }
 276  0
         getEdges().add(edge);
 277  
         // TODO: assumes public
 278  0
         if (Model.getFacade().isAModelElement(edge)
 279  
             && Model.getFacade().getNamespace(edge) == null) {
 280  0
             Model.getCoreHelper().addOwnedElement(getHomeModel(), edge);
 281  
         }
 282  0
         fireEdgeAdded(edge);
 283  0
     }
 284  
 
 285  
     /*
 286  
      * @see org.tigris.gef.graph.MutableGraphModel#addNodeRelatedEdges(java.lang.Object)
 287  
      */
 288  
     @Override
 289  
     public void addNodeRelatedEdges(Object node) {
 290  0
         super.addNodeRelatedEdges(node);
 291  
 
 292  0
         if (Model.getFacade().isAClassifier(node)) {
 293  0
             Collection ends = Model.getFacade().getAssociationEnds(node);
 294  0
             for (Object end : ends) {
 295  0
                 if (canAddEdge(Model.getFacade().getAssociation(end))) {
 296  0
                     addEdge(Model.getFacade().getAssociation(end));
 297  
                 }
 298  
             }
 299  
         }
 300  0
         if (Model.getFacade().isAGeneralizableElement(node)) {
 301  0
             Collection generalizations = 
 302  
                 Model.getFacade().getGeneralizations(node);
 303  0
             for (Object generalization : generalizations) {
 304  0
                 if (canAddEdge(generalization)) {
 305  0
                     addEdge(generalization);
 306  0
                     return;
 307  
                 }
 308  
             }
 309  0
             Collection specializations = Model.getFacade().getSpecializations(node);
 310  0
             for (Object specialization : specializations) {
 311  0
                 if (canAddEdge(specialization)) {
 312  0
                     addEdge(specialization);
 313  0
                     return;
 314  
                 }
 315  
             }
 316  
         }
 317  0
         if (Model.getFacade().isAModelElement(node)) {
 318  0
             Collection dependencies =
 319  
                 new ArrayList(Model.getFacade().getClientDependencies(node));
 320  0
             dependencies.addAll(Model.getFacade().getSupplierDependencies(node));
 321  0
             for (Object dependency : dependencies) {
 322  0
                 if (canAddEdge(dependency)) {
 323  0
                     addEdge(dependency);
 324  0
                     return;
 325  
                 }
 326  
             }
 327  
         }
 328  0
     }
 329  
 
 330  
 
 331  
     /*
 332  
      * Return true if the two given ports can be connected by a
 333  
      * kind of edge to be determined by the ports.
 334  
      *
 335  
      * @see org.tigris.gef.graph.MutableGraphModel#canConnect(java.lang.Object,
 336  
      * java.lang.Object)
 337  
      */
 338  
     @Override
 339  
     public boolean canConnect(Object fromP, Object toP) {
 340  0
         if ((Model.getFacade().isAClassifierRole(fromP))
 341  
             && (Model.getFacade().isAClassifierRole(toP))) {
 342  0
             return true;
 343  
         }
 344  0
         return false;
 345  
     }
 346  
 
 347  
     ////////////////////////////////////////////////////////////////
 348  
     // VetoableChangeListener implementation
 349  
 
 350  
     /*
 351  
      * @see java.beans.VetoableChangeListener#vetoableChange(java.beans.PropertyChangeEvent)
 352  
      */
 353  
     public void vetoableChange(PropertyChangeEvent pce) {
 354  
         //throws PropertyVetoException
 355  
 
 356  0
         if ("ownedElement".equals(pce.getPropertyName())) {
 357  0
             List oldOwned = (List) pce.getOldValue();
 358  0
             Object eo = /*(MElementImport)*/ pce.getNewValue();
 359  0
             Object me = Model.getFacade().getModelElement(eo);
 360  0
             if (oldOwned.contains(eo)) {
 361  0
                 LOG.debug("model removed " + me);
 362  0
                 if (Model.getFacade().isAClassifier(me)) {
 363  0
                     removeNode(me);
 364  
                 }
 365  0
                 if (Model.getFacade().isAMessage(me)) {
 366  0
                     removeNode(me);
 367  
                 }
 368  0
                 if (Model.getFacade().isAAssociation(me)) {
 369  0
                     removeEdge(me);
 370  
                 }
 371  
             } else {
 372  0
                 LOG.debug("model added " + me);
 373  
             }
 374  
         }
 375  0
     }
 376  
 
 377  
     /**
 378  
      * The UID.
 379  
      */
 380  
     private static final long serialVersionUID = -4895696235473642985L;
 381  
 } /* end class CollabDiagramGraphModel */