umbrello/umbrello
package.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "package.h"
00014
00015
00016 #include <kdebug.h>
00017 #include <klocale.h>
00018 #include <kinputdialog.h>
00019 #include <kmessagebox.h>
00020
00021
00022 #include "uml.h"
00023 #include "umldoc.h"
00024 #include "classifier.h"
00025 #include "association.h"
00026 #include "entity.h"
00027 #include "object_factory.h"
00028 #include "model_utils.h"
00029
00030 using namespace Uml;
00031
00032 UMLPackage::UMLPackage(const QString & name, Uml::IDType id)
00033 : UMLCanvasObject(name, id)
00034 {
00035 m_BaseType = ot_Package;
00036 }
00037
00038 UMLPackage::~UMLPackage()
00039 {}
00040
00041 void UMLPackage::copyInto(UMLObject *lhs) const
00042 {
00043 UMLPackage *target = static_cast<UMLPackage*>(lhs);
00044
00045 UMLCanvasObject::copyInto(target);
00046
00047 m_objects.copyInto(&(target->m_objects));
00048 }
00049
00050 UMLObject* UMLPackage::clone() const
00051 {
00052 UMLPackage *clone = new UMLPackage();
00053 copyInto(clone);
00054
00055 return clone;
00056 }
00057
00058 void UMLPackage::addAssocToConcepts(UMLAssociation* a)
00059 {
00060 if (! UMLAssociation::assocTypeHasUMLRepresentation(a->getAssocType()) )
00061 return;
00062 Uml::IDType AId = a->getObjectId(Uml::A);
00063 Uml::IDType BId = a->getObjectId(Uml::B);
00064 UMLObject *o = NULL;
00065 for (UMLObjectListIt it(m_objects); it.hasNext(); ) {
00066 o = it.next();
00067 UMLCanvasObject *c = dynamic_cast<UMLCanvasObject*>(o);
00068 if (c == NULL)
00069 continue;
00070 if (AId == c->getID() || (BId == c->getID())) {
00071 if (c->hasAssociation(a))
00072 uDebug() << c->getName() << " already has association id=" << ID2STR(a->getID())
00073 << endl;
00074 else
00075 c->addAssociationEnd(a);
00076 }
00077 UMLPackage *pkg = dynamic_cast<UMLPackage*>(c);
00078 if (pkg)
00079 pkg->addAssocToConcepts(a);
00080 }
00081 }
00082
00083 void UMLPackage::removeAssocFromConcepts(UMLAssociation *assoc)
00084 {
00085 UMLObject *o = NULL;
00086 for (UMLObjectListIt it(m_objects); it.hasNext(); ) {
00087 o = it.next();
00088 UMLCanvasObject *c = dynamic_cast<UMLCanvasObject*>(o);
00089 if (c == NULL)
00090 continue;
00091 if (c->hasAssociation(assoc))
00092 c->removeAssociationEnd(assoc);
00093 UMLPackage *pkg = dynamic_cast<UMLPackage*>(c);
00094 if (pkg)
00095 pkg->removeAssocFromConcepts(assoc);
00096 }
00097 }
00098
00099 bool UMLPackage::addObject(UMLObject *pObject)
00100 {
00101 if (pObject == NULL) {
00102 uError() << "is called with a NULL object" << endl;
00103 return false;
00104 }
00105 if (m_objects.indexOf(pObject) != -1) {
00106 uDebug() << pObject->getName() << " is already there" << endl;
00107 return false;
00108 }
00109 if (pObject->getBaseType() == Uml::ot_Association) {
00110 UMLAssociation *assoc = static_cast<UMLAssociation*>(pObject);
00111
00112
00113
00114 if (assoc->getObject(Uml::A) && assoc->getObject(Uml::B)) {
00115 UMLPackage *pkg = pObject->getUMLPackage();
00116 if (pkg != this) {
00117 uError() << "UMLPackage " << m_Name << " addObject: "
00118 << "assoc's UMLPackage is " << pkg->getName() << endl;
00119 }
00120 addAssocToConcepts(assoc);
00121 }
00122 } else {
00123
00124 QString name = pObject->getName();
00125 QString oldName = name;
00126 while ( findObject( name ) != NULL ) {
00127 name = Model_Utils::uniqObjectName(pObject->getBaseType(),this);
00128 bool ok = true;
00129 name = KInputDialog::getText(i18n("Name"), i18n("An object with this name already exists in the package %1.<br /> Please enter a new name:", this->getName()), name, &ok, (QWidget*)UMLApp::app());
00130 if (!ok) {
00131 name = oldName;
00132 continue;
00133 }
00134 if (name.length() == 0) {
00135 KMessageBox::error(0, i18n("That is an invalid name."),
00136 i18n("Invalid Name"));
00137 continue;
00138 }
00139 }
00140 if ( oldName != name ) {
00141 pObject->setName(name);
00142 }
00143 }
00144 m_objects.append( pObject );
00145 return true;
00146 }
00147
00148 void UMLPackage::removeObject(UMLObject *pObject)
00149 {
00150 if (pObject->getBaseType() == Uml::ot_Association) {
00151 UMLObject *o = const_cast<UMLObject*>(pObject);
00152 UMLAssociation *assoc = static_cast<UMLAssociation*>(o);
00153 removeAssocFromConcepts(assoc);
00154 }
00155 if (m_objects.indexOf(pObject) == -1)
00156 uDebug() << m_Name << " removeObject: object with id="
00157 << ID2STR(pObject->getID()) << "not found." << endl;
00158 else
00159 m_objects.removeAll(pObject);
00160 }
00161
00162 void UMLPackage::removeAllObjects()
00163 {
00164 UMLCanvasObject::removeAllChildObjects();
00165 UMLObject *o = NULL;
00166
00167 while ( !m_objects.isEmpty() && (o = m_objects.first()) != NULL ) {
00168 UMLPackage *pkg = dynamic_cast<UMLPackage*>(o);
00169 if (pkg)
00170 pkg->removeAllObjects();
00171 removeObject(o);
00172
00173
00174 o->deleteLater();
00175 }
00176 }
00177
00178 UMLObjectList UMLPackage::containedObjects()
00179 {
00180 return m_objects;
00181 }
00182
00183 UMLObject * UMLPackage::findObject(const QString &name)
00184 {
00185 const bool caseSensitive = UMLApp::app()->activeLanguageIsCaseSensitive();
00186 for (UMLObjectListIt oit(m_objects); oit.hasNext(); ) {
00187 UMLObject *obj = oit.next();
00188 if (caseSensitive) {
00189 if (obj->getName() == name)
00190 return obj;
00191 } else if (obj->getName().toLower() == name.toLower()) {
00192 return obj;
00193 }
00194 }
00195 return NULL;
00196 }
00197
00198 UMLObject * UMLPackage::findObjectById(Uml::IDType id)
00199 {
00200 return Model_Utils::findObjectInList(id, m_objects);
00201 }
00202
00203
00204 void UMLPackage::appendPackages(UMLPackageList& packages, bool includeNested )
00205 {
00206 for (UMLObjectListIt oit(m_objects); oit.hasNext(); ) {
00207 UMLObject *o = oit.next();
00208 Object_Type ot = o->getBaseType();
00209 if (ot == ot_Package) {
00210 packages.append((UMLPackage *)o);
00211 if (includeNested) {
00212 UMLPackage *inner = static_cast<UMLPackage*>(o);
00213 inner->appendPackages(packages);
00214 }
00215 }
00216 }
00217 }
00218
00219
00220 void UMLPackage::appendClassifiers(UMLClassifierList& classifiers,
00221 bool includeNested )
00222 {
00223 for (UMLObjectListIt oit(m_objects); oit.hasNext(); ) {
00224 UMLObject *o = oit.next();
00225 Object_Type ot = o->getBaseType();
00226 if (ot == ot_Class || ot == ot_Interface ||
00227 ot == ot_Datatype || ot == ot_Enum || ot == ot_Entity) {
00228 classifiers.append((UMLClassifier *)o);
00229 } else if (includeNested && (ot == ot_Package || ot == ot_Folder)) {
00230 UMLPackage *inner = static_cast<UMLPackage *>(o);
00231 inner->appendClassifiers(classifiers);
00232 }
00233 }
00234 }
00235
00236 void UMLPackage::appendClasses(UMLClassifierList& classes,
00237 bool includeNested )
00238 {
00239 for (UMLObjectListIt oit(m_objects); oit.hasNext(); ) {
00240 UMLObject *o = oit.next();
00241 Object_Type ot = o->getBaseType();
00242 if (ot == ot_Class) {
00243 UMLClassifier *c = static_cast<UMLClassifier*>(o);
00244 classes.append(c);
00245 } else if (includeNested && (ot == ot_Package || ot == ot_Folder)) {
00246 UMLPackage *inner = static_cast<UMLPackage *>(o);
00247 inner->appendClasses(classes);
00248 }
00249 }
00250 }
00251
00252 void UMLPackage::appendEntities( UMLEntityList& entities,
00253 bool includeNested )
00254 {
00255 for (UMLObjectListIt oit(m_objects); oit.hasNext(); ) {
00256 UMLObject *o = oit.next();
00257 Object_Type ot = o->getBaseType();
00258 if (ot == ot_Entity) {
00259 UMLEntity *c = static_cast<UMLEntity*>(o);
00260 entities.append(c);
00261 } else if (includeNested && (ot == ot_Package || ot == ot_Folder)) {
00262 UMLPackage *inner = static_cast<UMLPackage *>(o);
00263 inner->appendEntities(entities);
00264 }
00265 }
00266 }
00267
00268 void UMLPackage::appendClassesAndInterfaces(UMLClassifierList& classifiers,
00269 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_Class || ot == ot_Interface) {
00275 UMLClassifier *c = static_cast<UMLClassifier*>(o);
00276 classifiers.append(c);
00277 } else if (includeNested && (ot == ot_Package || ot == ot_Folder)) {
00278 UMLPackage *inner = static_cast<UMLPackage *>(o);
00279 inner->appendClassesAndInterfaces(classifiers);
00280 }
00281 }
00282 }
00283
00284 void UMLPackage::appendInterfaces( UMLClassifierList& interfaces,
00285 bool includeNested )
00286 {
00287 for (UMLObjectListIt oit(m_objects); oit.hasNext(); ) {
00288 UMLObject *o = oit.next();
00289 Object_Type ot = o->getBaseType();
00290 if (ot == ot_Interface) {
00291 UMLClassifier *c = static_cast<UMLClassifier*>(o);
00292 interfaces.append(c);
00293 } else if (includeNested && (ot == ot_Package || ot == ot_Folder)) {
00294 UMLPackage *inner = static_cast<UMLPackage *>(o);
00295 inner->appendInterfaces(interfaces);
00296 }
00297 }
00298 }
00299
00300 bool UMLPackage::resolveRef()
00301 {
00302 bool overallSuccess = UMLCanvasObject::resolveRef();
00303 for (UMLObjectListIt oit(m_objects); oit.hasNext(); ) {
00304 UMLObject *obj = oit.next();
00305 if (! obj->resolveRef()) {
00306 Uml::Object_Type ot = obj->getBaseType();
00307 if (ot != Uml::ot_Package && ot != Uml::ot_Folder)
00308 m_objects.removeAll(obj);
00309 overallSuccess = false;
00310 }
00311 }
00312 return overallSuccess;
00313 }
00314
00315 void UMLPackage::saveToXMI(QDomDocument& qDoc, QDomElement& qElement)
00316 {
00317 QDomElement packageElement = UMLObject::save("UML:Package", qDoc);
00318 QDomElement ownedElement = qDoc.createElement("UML:Namespace.ownedElement");
00319 UMLObject *obj = NULL;
00320
00321 for (UMLObjectListIt oit(m_objects); oit.hasNext(); ) {
00322 obj = oit.next();
00323 obj->saveToXMI (qDoc, ownedElement);
00324 }
00325
00326 for (UMLObjectListIt ait(m_List); ait.hasNext(); ) {
00327 obj = ait.next();
00328 obj->saveToXMI (qDoc, ownedElement);
00329 }
00330
00331 packageElement.appendChild(ownedElement);
00332 qElement.appendChild(packageElement);
00333 }
00334
00335 bool UMLPackage::load(QDomElement& element)
00336 {
00337 for (QDomNode node = element.firstChild(); !node.isNull();
00338 node = node.nextSibling()) {
00339 if (node.isComment())
00340 continue;
00341 QDomElement tempElement = node.toElement();
00342 QString type = tempElement.tagName();
00343 if (Model_Utils::isCommonXMIAttribute(type))
00344 continue;
00345 if (tagEq(type, "Namespace.ownedElement") ||
00346 tagEq(type, "Namespace.contents")) {
00347
00348
00349
00350 if (! load(tempElement))
00351 return false;
00352 continue;
00353 }
00354 UMLObject *pObject = Object_Factory::makeObjectFromXMI(type);
00355 if( !pObject ) {
00356 uWarning() << "Unknown type of umlobject to create: " << type << endl;
00357 continue;
00358 }
00359 pObject->setUMLPackage(this);
00360 if (!pObject->loadFromXMI(tempElement)) {
00361 removeObject(pObject);
00362 delete pObject;
00363 }
00364 }
00365 return true;
00366 }
00367
00368 #include "package.moc"