umbrello/umbrello
package.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include "package.h"
00013
00014
00015 #include "uml.h"
00016 #include "umldoc.h"
00017 #include "classifier.h"
00018 #include "association.h"
00019 #include "entity.h"
00020 #include "object_factory.h"
00021 #include "model_utils.h"
00022
00023
00024 #include <kdebug.h>
00025 #include <klocale.h>
00026 #include <kinputdialog.h>
00027 #include <kmessagebox.h>
00028
00029 using namespace Uml;
00030
00036 UMLPackage::UMLPackage(const QString & name, Uml::IDType id)
00037 : UMLCanvasObject(name, id)
00038 {
00039 m_BaseType = ot_Package;
00040 }
00041
00045 UMLPackage::~UMLPackage()
00046 {
00047 }
00048
00052 void UMLPackage::copyInto(UMLObject *lhs) const
00053 {
00054 UMLPackage *target = static_cast<UMLPackage*>(lhs);
00055
00056 UMLCanvasObject::copyInto(target);
00057
00058 m_objects.copyInto(&(target->m_objects));
00059 }
00060
00064 UMLObject* UMLPackage::clone() const
00065 {
00066 UMLPackage *clone = new UMLPackage();
00067 copyInto(clone);
00068
00069 return clone;
00070 }
00071
00080 void UMLPackage::addAssocToConcepts(UMLAssociation* assoc)
00081 {
00082 if (! UMLAssociation::assocTypeHasUMLRepresentation(assoc->getAssocType()) )
00083 return;
00084 Uml::IDType AId = assoc->getObjectId(Uml::A);
00085 Uml::IDType BId = assoc->getObjectId(Uml::B);
00086 UMLObject *o = NULL;
00087 for (UMLObjectListIt oit(m_objects); oit.hasNext(); ) {
00088 o = oit.next();
00089 UMLCanvasObject *c = dynamic_cast<UMLCanvasObject*>(o);
00090 if (c == NULL)
00091 continue;
00092 if (AId == c->getID() || (BId == c->getID())) {
00093 if (c->hasAssociation(assoc))
00094 uDebug() << c->getName() << " already has association id=" << ID2STR(assoc->getID());
00095 else
00096 c->addAssociationEnd(assoc);
00097 }
00098 UMLPackage *pkg = dynamic_cast<UMLPackage*>(c);
00099 if (pkg)
00100 pkg->addAssocToConcepts(assoc);
00101 }
00102 }
00103
00108 void UMLPackage::removeAssocFromConcepts(UMLAssociation *assoc)
00109 {
00110 UMLObject *o = 0;
00111 for (UMLObjectListIt oit(m_objects); oit.hasNext(); ) {
00112 o = oit.next();
00113 UMLCanvasObject *c = dynamic_cast<UMLCanvasObject*>(o);
00114 if (c) {
00115 if (c->hasAssociation(assoc))
00116 c->removeAssociationEnd(assoc);
00117 UMLPackage *pkg = dynamic_cast<UMLPackage*>(c);
00118 if (pkg)
00119 pkg->removeAssocFromConcepts(assoc);
00120 }
00121 }
00122 }
00123
00130 bool UMLPackage::addObject(UMLObject *pObject)
00131 {
00132 if (pObject == NULL) {
00133 uError() << "is called with a NULL object";
00134 return false;
00135 }
00136 if (m_objects.indexOf(pObject) != -1) {
00137 uDebug() << pObject->getName() << " is already there";
00138 return false;
00139 }
00140 if (pObject->getBaseType() == Uml::ot_Association) {
00141 UMLAssociation *assoc = static_cast<UMLAssociation*>(pObject);
00142
00143
00144
00145 if (assoc->getObject(Uml::A) && assoc->getObject(Uml::B)) {
00146 UMLPackage *pkg = pObject->getUMLPackage();
00147 if (pkg != this) {
00148 uError() << "UMLPackage " << m_Name << " addObject: "
00149 << "assoc's UMLPackage is " << pkg->getName();
00150 }
00151 addAssocToConcepts(assoc);
00152 }
00153 } else {
00154
00155 QString name = pObject->getName();
00156 QString oldName = name;
00157 while ( findObject( name ) != NULL ) {
00158 name = Model_Utils::uniqObjectName(pObject->getBaseType(),this);
00159 bool ok = true;
00160 name = KInputDialog::getText(i18nc("object name", "Name"),
00161 i18n("An object with the name %1\nalready exists in the package %2.\nPlease enter a new name:", name, this->getName()),
00162 name, &ok, (QWidget*)UMLApp::app());
00163 if (!ok) {
00164 name = oldName;
00165 continue;
00166 }
00167 if (name.length() == 0) {
00168 KMessageBox::error(0, i18n("That is an invalid name."),
00169 i18n("Invalid Name"));
00170 continue;
00171 }
00172 }
00173 if ( oldName != name ) {
00174 pObject->setName(name);
00175 }
00176 }
00177 m_objects.append( pObject );
00178 return true;
00179 }
00180
00187 void UMLPackage::removeObject(UMLObject *pObject)
00188 {
00189 if (pObject->getBaseType() == Uml::ot_Association) {
00190 UMLObject *o = const_cast<UMLObject*>(pObject);
00191 UMLAssociation *assoc = static_cast<UMLAssociation*>(o);
00192 removeAssocFromConcepts(assoc);
00193 }
00194 if (m_objects.indexOf(pObject) == -1)
00195 uDebug() << m_Name << " removeObject: object with id="
00196 << ID2STR(pObject->getID()) << "not found.";
00197 else
00198 m_objects.removeAll(pObject);
00199 }
00200
00205 void UMLPackage::removeAllObjects()
00206 {
00207 UMLCanvasObject::removeAllChildObjects();
00208 UMLObject *o = NULL;
00209
00210 while ( !m_objects.isEmpty() && (o = m_objects.first()) != NULL ) {
00211 UMLPackage *pkg = dynamic_cast<UMLPackage*>(o);
00212 if (pkg)
00213 pkg->removeAllObjects();
00214 removeObject(o);
00215
00216
00217 o->deleteLater();
00218 }
00219 }
00220
00224 UMLObjectList UMLPackage::containedObjects()
00225 {
00226 return m_objects;
00227 }
00228
00235 UMLObject * UMLPackage::findObject(const QString &name)
00236 {
00237 const bool caseSensitive = UMLApp::app()->activeLanguageIsCaseSensitive();
00238 for (UMLObjectListIt oit(m_objects); oit.hasNext(); ) {
00239 UMLObject *obj = oit.next();
00240 if (caseSensitive) {
00241 if (obj->getName() == name)
00242 return obj;
00243 } else if (obj->getName().toLower() == name.toLower()) {
00244 return obj;
00245 }
00246 }
00247 return NULL;
00248 }
00249
00256 UMLObject * UMLPackage::findObjectById(Uml::IDType id)
00257 {
00258 return Model_Utils::findObjectInList(id, m_objects);
00259 }
00260
00269 void UMLPackage::appendPackages(UMLPackageList& packages, bool includeNested )
00270 {
00271 for (UMLObjectListIt oit(m_objects); oit.hasNext(); ) {
00272 UMLObject *o = oit.next();
00273 Object_Type ot = o->getBaseType();
00274 if (ot == ot_Package) {
00275 packages.append((UMLPackage *)o);
00276 if (includeNested) {
00277 UMLPackage *inner = static_cast<UMLPackage*>(o);
00278 inner->appendPackages(packages);
00279 }
00280 }
00281 }
00282 }
00283
00292 void UMLPackage::appendClassifiers(UMLClassifierList& classifiers,
00293 bool includeNested )
00294 {
00295 for (UMLObjectListIt oit(m_objects); oit.hasNext(); ) {
00296 UMLObject *o = oit.next();
00297 Object_Type ot = o->getBaseType();
00298 if (ot == ot_Class || ot == ot_Interface ||
00299 ot == ot_Datatype || ot == ot_Enum || ot == ot_Entity) {
00300 classifiers.append((UMLClassifier *)o);
00301 } else if (includeNested && (ot == ot_Package || ot == ot_Folder)) {
00302 UMLPackage *inner = static_cast<UMLPackage *>(o);
00303 inner->appendClassifiers(classifiers);
00304 }
00305 }
00306 }
00307
00316 void UMLPackage::appendClasses(UMLClassifierList& classes,
00317 bool includeNested )
00318 {
00319 for (UMLObjectListIt oit(m_objects); oit.hasNext(); ) {
00320 UMLObject *o = oit.next();
00321 Object_Type ot = o->getBaseType();
00322 if (ot == ot_Class) {
00323 UMLClassifier *c = static_cast<UMLClassifier*>(o);
00324 classes.append(c);
00325 } else if (includeNested && (ot == ot_Package || ot == ot_Folder)) {
00326 UMLPackage *inner = static_cast<UMLPackage *>(o);
00327 inner->appendClasses(classes);
00328 }
00329 }
00330 }
00331
00340 void UMLPackage::appendEntities( UMLEntityList& entities,
00341 bool includeNested )
00342 {
00343 for (UMLObjectListIt oit(m_objects); oit.hasNext(); ) {
00344 UMLObject *o = oit.next();
00345 Object_Type ot = o->getBaseType();
00346 if (ot == ot_Entity) {
00347 UMLEntity *c = static_cast<UMLEntity*>(o);
00348 entities.append(c);
00349 } else if (includeNested && (ot == ot_Package || ot == ot_Folder)) {
00350 UMLPackage *inner = static_cast<UMLPackage *>(o);
00351 inner->appendEntities(entities);
00352 }
00353 }
00354 }
00355
00364 void UMLPackage::appendClassesAndInterfaces(UMLClassifierList& classifiers,
00365 bool includeNested )
00366 {
00367 for (UMLObjectListIt oit(m_objects); oit.hasNext(); ) {
00368 UMLObject *o = oit.next();
00369 Object_Type ot = o->getBaseType();
00370 if (ot == ot_Class || ot == ot_Interface) {
00371 UMLClassifier *c = static_cast<UMLClassifier*>(o);
00372 classifiers.append(c);
00373 } else if (includeNested && (ot == ot_Package || ot == ot_Folder)) {
00374 UMLPackage *inner = static_cast<UMLPackage *>(o);
00375 inner->appendClassesAndInterfaces(classifiers);
00376 }
00377 }
00378 }
00379
00388 void UMLPackage::appendInterfaces( UMLClassifierList& interfaces,
00389 bool includeNested )
00390 {
00391 for (UMLObjectListIt oit(m_objects); oit.hasNext(); ) {
00392 UMLObject *o = oit.next();
00393 Object_Type ot = o->getBaseType();
00394 if (ot == ot_Interface) {
00395 UMLClassifier *c = static_cast<UMLClassifier*>(o);
00396 interfaces.append(c);
00397 } else if (includeNested && (ot == ot_Package || ot == ot_Folder)) {
00398 UMLPackage *inner = static_cast<UMLPackage *>(o);
00399 inner->appendInterfaces(interfaces);
00400 }
00401 }
00402 }
00403
00412 bool UMLPackage::resolveRef()
00413 {
00414 bool overallSuccess = UMLCanvasObject::resolveRef();
00415 for (UMLObjectListIt oit(m_objects); oit.hasNext(); ) {
00416 UMLObject *obj = oit.next();
00417 if (! obj->resolveRef()) {
00418 Uml::Object_Type ot = obj->getBaseType();
00419 if (ot != Uml::ot_Package && ot != Uml::ot_Folder)
00420 m_objects.removeAll(obj);
00421 overallSuccess = false;
00422 }
00423 }
00424 return overallSuccess;
00425 }
00426
00430 void UMLPackage::saveToXMI(QDomDocument& qDoc, QDomElement& qElement)
00431 {
00432 QDomElement packageElement = UMLObject::save("UML:Package", qDoc);
00433 QDomElement ownedElement = qDoc.createElement("UML:Namespace.ownedElement");
00434 UMLObject *obj = NULL;
00435
00436 for (UMLObjectListIt oit(m_objects); oit.hasNext(); ) {
00437 obj = oit.next();
00438 obj->saveToXMI (qDoc, ownedElement);
00439 }
00440
00441 for (UMLObjectListIt ait(m_List); ait.hasNext(); ) {
00442 obj = ait.next();
00443 obj->saveToXMI (qDoc, ownedElement);
00444 }
00445
00446 packageElement.appendChild(ownedElement);
00447 qElement.appendChild(packageElement);
00448 }
00449
00454 bool UMLPackage::load(QDomElement& element)
00455 {
00456 for (QDomNode node = element.firstChild(); !node.isNull();
00457 node = node.nextSibling()) {
00458 if (node.isComment())
00459 continue;
00460 QDomElement tempElement = node.toElement();
00461 QString type = tempElement.tagName();
00462 if (Model_Utils::isCommonXMIAttribute(type))
00463 continue;
00464 if (tagEq(type, "Namespace.ownedElement") ||
00465 tagEq(type, "Namespace.contents")) {
00466
00467
00468
00469 if (! load(tempElement))
00470 return false;
00471 continue;
00472 }
00473 UMLObject *pObject = Object_Factory::makeObjectFromXMI(type);
00474 if( !pObject ) {
00475 uWarning() << "Unknown type of umlobject to create: " << type;
00476 continue;
00477 }
00478 pObject->setUMLPackage(this);
00479 if (!pObject->loadFromXMI(tempElement)) {
00480 removeObject(pObject);
00481 delete pObject;
00482 }
00483 }
00484 return true;
00485 }
00486
00487 #include "package.moc"