Logo Search packages:      
Sourcecode: kdesdk version File versions

void UMLView::createAutoAssociations ( UMLWidget widget  ) 

Creates automatically any Associations that the given UMLWidget may have on any diagram. This method is used when you just add the UMLWidget to a diagram.

Definition at line 2181 of file umlview.cpp.

References addAssociation(), AssocRules::allowAssociation(), AssociationWidget::calculateEndingPoints(), UMLPackage::containedObjects(), findAssocWidget(), UMLCanvasObject::getAssociations(), UMLAssociation::getAssocType(), UMLObject::getBaseType(), UMLAssociation::getChangeability(), UMLWidget::getID(), UMLObject::getID(), UMLAssociation::getMulti(), UMLAssociation::getObject(), UMLAssociation::getRoleName(), LinkWidget::getUMLObject(), UMLObject::getUMLPackage(), UMLAssociation::getVisibility(), m_WidgetList, AssociationWidget::setActivated(), AssociationWidget::setAssocType(), AssociationWidget::setChangeability(), AssociationWidget::setMulti(), AssociationWidget::setRoleName(), AssociationWidget::setVisibility(), and AssociationWidget::setWidget().

                                                         {
      if( !widget )
            return;
      // Recipe:
      // If this widget has an underlying UMLCanvasObject then
      //   for each of the UMLCanvasObject's UMLAssociations
      //     if umlassoc's "other" role has a widget representation on this view then
      //       if the AssocWidget does not already exist then
      //         if the assoc type is permitted in the current diagram type then
      //           create the AssocWidget
      //         end if
      //       end if
      //     end if
      //   end loop
      //   if this object is capable of containing nested objects then
      //     for each of the object's containedObjects
      //       if the containedObject has a widget representation on this view then
      //         create the containment AssocWidget
      //       end if
      //     end loop
      //   end if
      //   if the UMLCanvasObject has a parentPackage then
      //     if the parentPackage has a widget representation on this view then
      //       create the containment AssocWidget
      //     end if
      //   end if
      // end if
      UMLObject *tmpUmlObj = widget->getUMLObject();
      if (tmpUmlObj == NULL)
            return;
      UMLCanvasObject *umlObj = dynamic_cast<UMLCanvasObject*>(tmpUmlObj);
      if (umlObj == NULL)
            return;
      const UMLAssociationList& umlAssocs = umlObj->getAssociations();
      UMLAssociationListIt it(umlAssocs);
      UMLAssociation *assoc = NULL;
      int myID = umlObj->getID();
      while ((assoc = it.current()) != NULL) {
            ++it;
            UMLCanvasObject *other = NULL;
            UMLObject *roleAObj = assoc->getObject(A);
            if (roleAObj == NULL) {
                  kdDebug() << "createAutoAssociations: roleA object is NULL at UMLAssoc "
                          << assoc->getID() << endl;
                  continue;
            }
            UMLObject *roleBObj = assoc->getObject(B);
            if (roleBObj == NULL) {
                  kdDebug() << "createAutoAssociations: roleB object is NULL at UMLAssoc "
                          << assoc->getID() << endl;
                  continue;
            }
            if (roleAObj->getID() == myID) {
                  other = static_cast<UMLCanvasObject*>(roleBObj);
            } else if (roleBObj->getID() == myID) {
                  other = static_cast<UMLCanvasObject*>(roleAObj);
            } else {
                  kdDebug() << "createAutoAssociations: Can't find own object "
                          << myID << " in UMLAssoc " << assoc->getID() << endl;
                  continue;
            }
            // Now that we have determined the "other" UMLObject, seek it in
            // this view's UMLWidgets.
            int otherID = other->getID();
            UMLWidget *pOtherWidget;
            UMLWidgetListIt wit(m_WidgetList);
            while ((pOtherWidget = wit.current()) != NULL) {
                  ++wit;
                  if (pOtherWidget->getID() == otherID)
                        break;
            }
            if (pOtherWidget == NULL)
                  continue;
            // Both objects are represented in this view:
            // Assign widget roles as indicated by the UMLAssociation.
            UMLWidget *widgetA, *widgetB;
            if (myID == roleAObj->getID()) {
                  widgetA = widget;
                  widgetB = pOtherWidget;
            } else {
                  widgetA = pOtherWidget;
                  widgetB = widget;
            }
            // Check that the assocwidget does not already exist.
            Uml::Association_Type assocType = assoc->getAssocType();
            AssociationWidget * assocwidget = findAssocWidget(assocType, widgetA, widgetB);
            if (assocwidget) {
                  assocwidget->calculateEndingPoints();  // recompute assoc lines
                  continue;
            }
            // Check that the assoc is allowed.
            if (!AssocRules::allowAssociation(assocType, widgetA, widgetB, false)) {
                  kdDebug() << "createAutoAssociations: not transferring assoc "
                          << "of type " << assocType << endl;
                  continue;
            }
            // Create the AssociationWidget.
            assocwidget = new AssociationWidget( this );
            assocwidget->setWidget(widgetA, A);
            assocwidget->setWidget(widgetB, B);
            assocwidget->setAssocType(assocType);
            // Call calculateEndingPoints() before setting the FloatingTexts
            // because their positions are computed according to the
            // assocwidget line positions.
            assocwidget->calculateEndingPoints();
            assocwidget->setVisibility(assoc->getVisibility(A), A);
            assocwidget->setVisibility(assoc->getVisibility(B), B);
            assocwidget->setChangeability(assoc->getChangeability(A), A);
            assocwidget->setChangeability(assoc->getChangeability(B), B);
            assocwidget->setMulti(assoc->getMulti(A), A);
            assocwidget->setMulti(assoc->getMulti(B), B);
            assocwidget->setRoleName(assoc->getRoleName(A), A);
            assocwidget->setRoleName(assoc->getRoleName(B), B);
            assocwidget->setActivated(true);
            if (! addAssociation(assocwidget))
                  delete assocwidget;
      }
      // if this object is capable of containing nested objects then
      Uml::UMLObject_Type t = umlObj->getBaseType();
      if (t == ot_Package || t == ot_Class || t == ot_Interface) {
            // for each of the object's containedObjects
            UMLPackage *umlPkg = static_cast<UMLPackage*>(umlObj);
            UMLObjectList& lst = umlPkg->containedObjects();
            for (UMLObject *obj = lst.first(); obj; obj = lst.next()) {
                  // if the containedObject has a widget representation on this view then
                  int id = obj->getID();
                  for (UMLWidget *w = m_WidgetList.first(); w; w = m_WidgetList.next()) {
                        if (w->getID() != id)
                              continue;
                        // create the containment AssocWidget
                        AssociationWidget *a = new AssociationWidget(this, widget,
                                                           at_Containment, w);
                        a->calculateEndingPoints();
                        a->setActivated(true);
                        if (! addAssociation(a))
                              delete a;
                  }
            }
      }
      // if the UMLCanvasObject has a parentPackage then
      UMLPackage *parent = umlObj->getUMLPackage();
      if (parent == NULL)
            return;
      // if the parentPackage has a widget representation on this view then
      int pkgID = parent->getID();
      UMLWidget *pWidget;
      UMLWidgetListIt wit(m_WidgetList);
      while ((pWidget = wit.current()) != NULL) {
            ++wit;
            if (pWidget->getID() == pkgID)
                  break;
      }
      if (pWidget == NULL)
            return;
      // create the containment AssocWidget
      AssociationWidget *a = new AssociationWidget(this, pWidget, at_Containment, widget);
      a->calculateEndingPoints();
      a->setActivated(true);
      if (! addAssociation(a))
            delete a;
}


Generated by  Doxygen 1.6.0   Back to index