hierarchicalcodeblock.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "hierarchicalcodeblock.h"
00019
00020
00021 #include <kdebug.h>
00022
00023
00024 #include "codedocument.h"
00025 #include "classifiercodedocument.h"
00026 #include "codeclassfield.h"
00027 #include "codegenerationpolicy.h"
00028 #include "codegenerators/codegenfactory.h"
00029 #include "uml.h"
00030
00031
00032
00033
00034 HierarchicalCodeBlock::HierarchicalCodeBlock ( CodeDocument * doc , const QString &start, const QString &endString, const QString &comment )
00035 : CodeBlockWithComments (doc, start, comment), CodeGenObjectWithTextBlocks(doc)
00036 {
00037 setEndText(endString);
00038 initAttributes();
00039 }
00040
00041 HierarchicalCodeBlock::~HierarchicalCodeBlock ( ) { }
00042
00043
00044
00045
00046
00047
00048
00049
00054 void HierarchicalCodeBlock::setEndText ( const QString &new_var ) {
00055 m_endText = new_var;
00056 }
00057
00062 QString HierarchicalCodeBlock::getEndText ( ) {
00063 return m_endText;
00064 }
00065
00066 QString HierarchicalCodeBlock::getUniqueTag()
00067 {
00068 return getUniqueTag("hblock_tag");
00069 }
00070
00071 QString HierarchicalCodeBlock::getUniqueTag( QString prefix )
00072 {
00073 return getParentDocument()->getUniqueTag(prefix);
00074 }
00075
00076
00077
00078 CodeBlock * HierarchicalCodeBlock::newCodeBlock() {
00079 return getParentDocument()->newCodeBlock();
00080 }
00081
00082 CodeBlockWithComments * HierarchicalCodeBlock::newCodeBlockWithComments() {
00083 return getParentDocument()->newCodeBlockWithComments();
00084 }
00085
00086 HierarchicalCodeBlock * HierarchicalCodeBlock::newHierarchicalCodeBlock() {
00087 HierarchicalCodeBlock *hb = new HierarchicalCodeBlock(getParentDocument());
00088
00089 return hb;
00090 }
00091
00095 bool HierarchicalCodeBlock::addTextBlock(TextBlock* add_object )
00096 {
00097
00098 if(CodeGenObjectWithTextBlocks::addTextBlock(add_object))
00099 {
00100 getParentDocument()->addChildTagToMap(add_object->getTag(), add_object);
00101 return true;
00102 }
00103 return false;
00104 }
00105
00110 bool HierarchicalCodeBlock::insertTextBlock(TextBlock * newBlock, TextBlock * existingBlock, bool after)
00111 {
00112
00113 if(!newBlock || !existingBlock)
00114 return false;
00115
00116 QString tag = existingBlock->getTag();
00117
00118
00119
00120
00121
00122
00123 if(!getParentDocument()->findTextBlockByTag(tag, true))
00124 return false;
00125
00126 int index = m_textblockVector.findRef(existingBlock);
00127 if(index < 0)
00128 {
00129
00130 for(TextBlock * tb = m_textblockVector.first(); tb ; tb = m_textblockVector.next())
00131 {
00132 HierarchicalCodeBlock * hb = dynamic_cast<HierarchicalCodeBlock*>(tb);
00133 if(hb && hb->insertTextBlock(newBlock, existingBlock, after))
00134 return true;
00135 }
00136 kWarning()<<" Warning: couldnt insert text block (tag:"<<newBlock->getTag()<<"). Reference text block (tag:"<<existingBlock->getTag()<<") not found."<<endl;
00137 return false;
00138 }
00139
00140
00141
00142
00143 QString new_tag = newBlock->getTag();
00144
00145
00146 if(new_tag.isEmpty())
00147 {
00148 new_tag = getUniqueTag();
00149 newBlock->setTag(new_tag);
00150 }
00151
00152 if(m_textBlockTagMap.contains(new_tag))
00153 return false;
00154 else {
00155 m_textBlockTagMap.insert(new_tag, newBlock);
00156 getParentDocument()->addChildTagToMap(new_tag, newBlock);
00157 }
00158
00159
00160 if(after)
00161 index++;
00162
00163 m_textblockVector.insert(index,newBlock);
00164
00165 return true;
00166 }
00167
00171 bool HierarchicalCodeBlock::removeTextBlock ( TextBlock * remove_object ) {
00172
00173
00174 if(!m_textblockVector.removeRef(remove_object))
00175 {
00176
00177 for(TextBlock * tb = m_textblockVector.first(); tb ; tb = m_textblockVector.next())
00178 {
00179 HierarchicalCodeBlock * hb = dynamic_cast<HierarchicalCodeBlock*>(tb);
00180 if(hb && hb->removeTextBlock(remove_object))
00181 return true;
00182 }
00183 return false;
00184 }
00185
00186
00187 QString tag = remove_object->getTag();
00188 if(!(tag.isEmpty()))
00189 {
00190 m_textBlockTagMap.erase(tag);
00191 getParentDocument()->removeChildTagFromMap(tag);
00192 }
00193 return true;
00194
00195 }
00196
00200 void HierarchicalCodeBlock::setStartText ( const QString &text ) {
00201 m_startText = text;
00202 }
00203
00207 QString HierarchicalCodeBlock::getStartText ( ) {
00208 return m_startText;
00209 }
00210
00211
00212
00213
00214 void HierarchicalCodeBlock::addCodeClassFieldMethods(CodeClassFieldList &list )
00215 {
00216
00217 for (CodeClassFieldListIt ccflit(list); ccflit.current(); ++ccflit)
00218 {
00219 CodeClassField * field = ccflit.current();
00220 CodeAccessorMethodList list = field->getMethodList();
00221 CodeAccessorMethod * method;
00222 for (CodeAccessorMethodListIt it(list); (method = it.current()) != NULL; ++it)
00223 {
00224 QString tag = method->getTag();
00225 if(tag.isEmpty())
00226 {
00227 tag = getUniqueTag();
00228 method->setTag(tag);
00229 }
00230
00231 addTextBlock(method);
00232
00233 }
00234
00235 }
00236
00237 }
00238
00242 void HierarchicalCodeBlock::saveToXMI ( QDomDocument & doc, QDomElement & root ) {
00243 QDomElement blockElement = doc.createElement( "hierarchicalcodeblock" );
00244
00245 setAttributesOnNode(doc, blockElement);
00246
00247 root.appendChild( blockElement );
00248 }
00249
00250 void HierarchicalCodeBlock::setAttributesOnNode (QDomDocument & doc, QDomElement & elem ) {
00251
00252
00253 CodeBlockWithComments::setAttributesOnNode(doc, elem);
00254 CodeGenObjectWithTextBlocks::setAttributesOnNode(doc, elem);
00255
00256
00257 if(getContentType() != CodeBlock::AutoGenerated)
00258 {
00259 QString endLine = UMLApp::app()->getCommonPolicy()->getNewLineEndingChars();
00260 elem.setAttribute("startText",encodeText(getStartText(),endLine));
00261 elem.setAttribute("endText",encodeText(getEndText(),endLine));
00262 }
00263 }
00264
00268 void HierarchicalCodeBlock::loadFromXMI ( QDomElement & root ) {
00269 setAttributesFromNode(root);
00270 }
00271
00272
00276 void HierarchicalCodeBlock::setAttributesFromNode ( QDomElement & root)
00277 {
00278
00279
00280 CodeBlockWithComments::setAttributesFromNode(root);
00281
00282 if(getContentType() != CodeBlock::AutoGenerated)
00283 {
00284 QString endLine = UMLApp::app()->getCommonPolicy()->getNewLineEndingChars();
00285 setStartText(decodeText(root.attribute("startText",""),endLine));
00286 setEndText(decodeText(root.attribute("endText",""),endLine));
00287 }
00288
00289
00290 CodeGenObjectWithTextBlocks::setAttributesFromNode(root);
00291
00292 }
00293
00296 void HierarchicalCodeBlock::setAttributesFromObject (TextBlock * obj) {
00297
00298 CodeBlockWithComments::setAttributesFromObject(obj);
00299
00300 HierarchicalCodeBlock * hb = dynamic_cast<HierarchicalCodeBlock*>(obj);
00301 if(hb)
00302 {
00303 setStartText(hb->getStartText());
00304 setEndText(hb->getEndText());
00305 CodeGenObjectWithTextBlocks *cgowtb = dynamic_cast<CodeGenObjectWithTextBlocks*>(obj);
00306 CodeGenObjectWithTextBlocks::setAttributesFromObject(cgowtb);
00307 }
00308
00309 }
00310
00311
00315 QString HierarchicalCodeBlock::toString ( ) {
00316
00317 QString string = QString();
00318
00319 if(getWriteOutText()) {
00320 QString indent = getIndentationString();
00321 QString endLine = UMLApp::app()->getCommonPolicy()->getNewLineEndingChars();
00322 QString startText = "";
00323 QString endText = "";
00324 if (!getStartText().isEmpty())
00325 startText = formatMultiLineText (getStartText(), indent, endLine);
00326 if (!getEndText().isEmpty())
00327 endText = formatMultiLineText (getEndText(), indent, endLine);
00328
00329 QString body = childTextBlocksToString();
00330 QString comment = getComment()->toString();
00331
00332
00333 if(!comment.isEmpty() && getComment()->getWriteOutText())
00334 string.append(comment);
00335
00336 if (!startText.isEmpty())
00337 string.append(startText);
00338
00339 if (!body.isEmpty())
00340 string.append(body);
00341
00342 if (!endText.isEmpty())
00343 string.append(endText);
00344 }
00345
00346 return string;
00347 }
00348
00349 QString HierarchicalCodeBlock::childTextBlocksToString() {
00350 TextBlockList* list = getTextBlockList();
00351 QString retString = "";
00352 for(TextBlock *block = list->first() ; block; block=list->next())
00353 {
00354 QString blockValue = block->toString();
00355 if(!blockValue.isEmpty())
00356 retString.append(blockValue);
00357 }
00358 return retString;
00359 }
00360
00361 TextBlock * HierarchicalCodeBlock::findCodeClassFieldTextBlockByTag ( const QString &tag )
00362 {
00363
00364 ClassifierCodeDocument * cdoc = dynamic_cast<ClassifierCodeDocument*>(getParentDocument());
00365 if(cdoc)
00366 return cdoc->findCodeClassFieldTextBlockByTag(tag);
00367 else
00368 kError()<<" HierarchicalCodeBlock: findCodeClassFieldTextBlockByTag() finds NO parent document! Badly constructed textblock?!?"<<endl;
00369
00370
00371 return (TextBlock*) NULL;
00372
00373 }
00374
00375 void HierarchicalCodeBlock::initAttributes ( ) {
00376 m_canDelete = false;
00377 m_startText = "";
00378 m_endText = "";
00379 }
00380
00381 void HierarchicalCodeBlock::release () {
00382 resetTextBlocks();
00383 TextBlock::release();
00384 }
00385
00386 #include "hierarchicalcodeblock.moc"
This file is part of the documentation for umbrello Version 3.1.0.