00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "object_factory.h"
00014
00015
00016 #include <qregexp.h>
00017 #include <qstringlist.h>
00018 #include <kapplication.h>
00019 #include <klocale.h>
00020 #include <kmessagebox.h>
00021 #include <kdebug.h>
00022 #include <kinputdialog.h>
00023
00024
00025 #include "umlobject.h"
00026 #include "umlpackagelist.h"
00027 #include "package.h"
00028 #include "folder.h"
00029 #include "classifier.h"
00030 #include "attribute.h"
00031 #include "operation.h"
00032 #include "enum.h"
00033 #include "entity.h"
00034 #include "actor.h"
00035 #include "usecase.h"
00036 #include "component.h"
00037 #include "node.h"
00038 #include "artifact.h"
00039 #include "stereotype.h"
00040 #include "association.h"
00041 #include "umldoc.h"
00042 #include "uml.h"
00043 #include "codegenerator.h"
00044 #include "model_utils.h"
00045 #include "uniqueid.h"
00046
00047 namespace Object_Factory {
00048
00049 Uml::IDType g_predefinedId = Uml::id_None;
00050
00051 void assignUniqueIdOnCreation(bool yesno) {
00052 if (yesno)
00053 g_predefinedId = Uml::id_None;
00054 else
00055 g_predefinedId = Uml::id_Reserved;
00056 }
00057
00058 bool assignUniqueIdOnCreation() {
00059 return (g_predefinedId == Uml::id_None);
00060 }
00061
00062 UMLObject* createNewUMLObject(Uml::Object_Type type, const QString &name,
00063 UMLPackage *parentPkg) {
00064 if (parentPkg == NULL) {
00065 kError() << "Object_Factory::createNewUMLObject(" << name
00066 << "): parentPkg is NULL" << endl;
00067 return NULL;
00068 }
00069 UMLObject *o = NULL;
00070 switch (type) {
00071 case Uml::ot_Actor:
00072 o = new UMLActor(name, g_predefinedId);
00073 break;
00074 case Uml::ot_UseCase:
00075 o = new UMLUseCase(name, g_predefinedId);
00076 break;
00077 case Uml::ot_Class:
00078 o = new UMLClassifier(name, g_predefinedId);
00079 break;
00080 case Uml::ot_Package:
00081 o = new UMLPackage(name, g_predefinedId);
00082 break;
00083 case Uml::ot_Component:
00084 o = new UMLComponent(name, g_predefinedId);
00085 break;
00086 case Uml::ot_Node:
00087 o = new UMLNode(name, g_predefinedId);
00088 break;
00089 case Uml::ot_Artifact:
00090 o = new UMLArtifact(name, g_predefinedId);
00091 break;
00092 case Uml::ot_Interface: {
00093 UMLClassifier *c = new UMLClassifier(name, g_predefinedId);
00094 c->setBaseType(Uml::ot_Interface);
00095 o = c;
00096 break;
00097 }
00098 case Uml::ot_Datatype: {
00099 UMLClassifier *c = new UMLClassifier(name, g_predefinedId);
00100 c->setBaseType(Uml::ot_Datatype);
00101 o = c;
00102 break;
00103 }
00104 case Uml::ot_Enum:
00105 o = new UMLEnum(name, g_predefinedId);
00106 break;
00107 case Uml::ot_Entity:
00108 o = new UMLEntity(name, g_predefinedId);
00109 break;
00110 case Uml::ot_Folder:
00111 o = new UMLFolder(name, g_predefinedId);
00112 break;
00113 default:
00114 kWarning() << "createNewUMLObject error unknown type: " << type << endl;
00115 return NULL;
00116 }
00117 o->setUMLPackage(parentPkg);
00118 UMLDoc *doc = UMLApp::app()->getDocument();
00119 parentPkg->addObject(o);
00120 doc->signalUMLObjectCreated(o);
00121 kapp->processEvents();
00122 return o;
00123 }
00124
00125 UMLObject* createUMLObject(Uml::Object_Type type, const QString &n,
00126 UMLPackage *parentPkg ,
00127 bool solicitNewName ) {
00128 UMLDoc *doc = UMLApp::app()->getDocument();
00129 if (parentPkg == NULL) {
00130 if (type == Uml::ot_Datatype) {
00131 parentPkg = doc->getDatatypeFolder();
00132 } else {
00133 Uml::Model_Type mt = Model_Utils::convert_OT_MT(type);
00134 kDebug() << "Object_Factory::createUMLObject(" << n << "): "
00135 << "parentPkg is not set, assuming Model_Type " << mt << endl;
00136 parentPkg = doc->getRootFolder(mt);
00137 }
00138 }
00139 if (!n.isEmpty()) {
00140 UMLObject *o = doc->findUMLObject(n, type, parentPkg);
00141 if (o) {
00142 if (!solicitNewName)
00143 return o;
00144 } else {
00145 o = createNewUMLObject(type, n, parentPkg);
00146 return o;
00147 }
00148 }
00149 bool ok = false;
00150 QString name = Model_Utils::uniqObjectName(type, parentPkg, n);
00151 bool bValidNameEntered = false;
00152 do {
00153 name = KInputDialog::getText(i18n("Name"), i18n("Enter name:"), name, &ok, (QWidget*)UMLApp::app());
00154 if (!ok) {
00155 return 0;
00156 }
00157 if (name.length() == 0) {
00158 KMessageBox::error(0, i18n("That is an invalid name."),
00159 i18n("Invalid Name"));
00160 continue;
00161 }
00162 CodeGenerator *codegen = UMLApp::app()->getGenerator();
00163 if (codegen != NULL && codegen->isReservedKeyword(name)) {
00164 KMessageBox::error(0, i18n("This is a reserved keyword for the language of the configured code generator."),
00165 i18n("Reserved Keyword"));
00166 continue;
00167 }
00168 if (! doc->isUnique(name, parentPkg)) {
00169 KMessageBox::error(0, i18n("That name is already being used."),
00170 i18n("Not a Unique Name"));
00171 continue;
00172 }
00173 bValidNameEntered = true;
00174 } while (bValidNameEntered == false);
00175 UMLObject *o = createNewUMLObject(type, name, parentPkg);
00176 return o;
00177 }
00178
00179 UMLAttribute *createAttribute(UMLObject *parent, const QString& name, UMLObject *type) {
00180 UMLAttribute *attr = new UMLAttribute(parent);
00181 attr->setName(name);
00182 attr->setType(type);
00183 if (g_predefinedId == Uml::id_None)
00184 attr->setID(UniqueID::gen());
00185 return attr;
00186 }
00187
00188 UMLOperation *createOperation(UMLClassifier *parent, const QString& name) {
00189 UMLOperation *op = new UMLOperation(parent, name, g_predefinedId);
00190 return op;
00191 }
00192
00193 UMLClassifierListItem* createChildObject(UMLClassifier* parent, Uml::Object_Type type) {
00194 UMLObject* returnObject = NULL;
00195 switch (type) {
00196 case Uml::ot_Attribute:
00197 case Uml::ot_EntityAttribute: {
00198 UMLClassifier *c = dynamic_cast<UMLClassifier*>(parent);
00199 if (c && !c->isInterface())
00200 returnObject = c->createAttribute();
00201 break;
00202 }
00203 case Uml::ot_Operation: {
00204 UMLClassifier *c = dynamic_cast<UMLClassifier*>(parent);
00205 if (c)
00206 returnObject = c->createOperation();
00207 break;
00208 }
00209 case Uml::ot_Template: {
00210 UMLClassifier *c = dynamic_cast<UMLClassifier*>(parent);
00211 if (c)
00212 returnObject = c->createTemplate();
00213 break;
00214 }
00215 case Uml::ot_EnumLiteral: {
00216 UMLEnum* umlenum = dynamic_cast<UMLEnum*>(parent);
00217 if (umlenum) {
00218 returnObject = umlenum->createEnumLiteral();
00219 }
00220 break;
00221 }
00222 default:
00223 kDebug() << "ERROR UMLDoc::createChildObject type:" << type << endl;
00224 }
00225 return static_cast<UMLClassifierListItem*>(returnObject);
00226 }
00227
00228 UMLObject* makeObjectFromXMI(const QString& xmiTag,
00229 const QString& stereoID ) {
00230 UMLObject* pObject = 0;
00231 if (Uml::tagEq(xmiTag, "UseCase")) {
00232 pObject = new UMLUseCase();
00233 } else if (Uml::tagEq(xmiTag, "Actor")) {
00234 pObject = new UMLActor();
00235 } else if (Uml::tagEq(xmiTag, "Class")) {
00236 pObject = new UMLClassifier();
00237 } else if (Uml::tagEq(xmiTag, "Package")) {
00238 if (!stereoID.isEmpty()) {
00239 UMLDoc *doc = UMLApp::app()->getDocument();
00240 UMLObject *stereo = doc->findStereotypeById(STR2ID(stereoID));
00241 if (stereo && stereo->getName() == "folder")
00242 pObject = new UMLFolder();
00243 }
00244 if (pObject == NULL)
00245 pObject = new UMLPackage();
00246 } else if (Uml::tagEq(xmiTag, "Component")) {
00247 pObject = new UMLComponent();
00248 } else if (Uml::tagEq(xmiTag, "Node")) {
00249 pObject = new UMLNode();
00250 } else if (Uml::tagEq(xmiTag, "Artifact")) {
00251 pObject = new UMLArtifact();
00252 } else if (Uml::tagEq(xmiTag, "Interface")) {
00253 UMLClassifier *c = new UMLClassifier();
00254 c->setBaseType(Uml::ot_Interface);
00255 pObject = c;
00256 } else if (Uml::tagEq(xmiTag, "DataType") || Uml::tagEq(xmiTag, "Primitive")
00257 || Uml::tagEq(xmiTag, "Datatype")) {
00258 UMLClassifier *c = new UMLClassifier();
00259 c->setBaseType(Uml::ot_Datatype);
00260 pObject = c;
00261 } else if (Uml::tagEq(xmiTag, "Enumeration") ||
00262 Uml::tagEq(xmiTag, "Enum")) {
00263 pObject = new UMLEnum();
00264 } else if (Uml::tagEq(xmiTag, "Entity")) {
00265 pObject = new UMLEntity();
00266 } else if (Uml::tagEq(xmiTag, "Stereotype")) {
00267 pObject = new UMLStereotype();
00268 } else if (Uml::tagEq(xmiTag, "Association") ||
00269 Uml::tagEq(xmiTag, "AssociationClass")) {
00270 pObject = new UMLAssociation();
00271 } else if (Uml::tagEq(xmiTag, "Generalization")) {
00272 pObject = new UMLAssociation(Uml::at_Generalization);
00273 } else if (Uml::tagEq(xmiTag, "Realization") ||
00274 Uml::tagEq(xmiTag, "Abstraction")) {
00275 pObject = new UMLAssociation(Uml::at_Realization);
00276 } else if (Uml::tagEq(xmiTag, "Dependency")) {
00277 pObject = new UMLAssociation(Uml::at_Dependency);
00278 }
00279 return pObject;
00280 }
00281
00282 }
00283