umbrello API Documentation

hierarchicalcodeblock.cpp

00001 /***************************************************************************
00002  *                                                                         *
00003  *   This program is free software; you can redistribute it and/or modify  *
00004  *   it under the terms of the GNU General Public License as published by  *
00005  *   the Free Software Foundation; either version 2 of the License, or     *
00006  *   (at your option) any later version.                                   *
00007  *                                                                         *
00008  *   copyright (C) 2004-2007                                               *
00009  *   Umbrello UML Modeller Authors <uml-devel@uml.sf.net>                  *
00010  ***************************************************************************/
00011 
00012 /*  This code generated by:
00013  *      Author : thomas
00014  *      Date   : Wed Jun 18 2003
00015  */
00016 
00017 // own header
00018 #include "hierarchicalcodeblock.h"
00019 
00020 // qt/kde includes
00021 #include <kdebug.h>
00022 
00023 // local includes
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 // Constructors/Destructors
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 // Methods
00045 //
00046 
00047 // Accessor methods
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 // other methods
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     //hb->update();
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     // FIX: just do a quick check if the parent DOCUMENT has this.
00118     // IF it does, then the lack of an index will force us into
00119     // a search of any child hierarchical codeblocks we may have
00120     // Its not efficient, but works. I don't think speed is a problem
00121     // right now for the current implementation, but in the future
00122     // when code import/roundtripping is done, it *may* be. -b.t.
00123     if(!getParentDocument()->findTextBlockByTag(tag, true))
00124         return false;
00125 
00126     int index = m_textblockVector.findRef(existingBlock);
00127     if(index < 0)
00128     {
00129         // may be hiding in child hierarchical codeblock
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; // found, and inserted, otherwise keep going
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     // if we get here.. it was in this object so insert
00141 
00142     // check for tag FIRST
00143     QString new_tag = newBlock->getTag();
00144 
00145     // assign a tag if one doesn't already exist
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; // return false, we already have some object with this tag in the list
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     // try to remove from the list in this object
00174     if(!m_textblockVector.removeRef(remove_object))
00175     {
00176         // may be hiding in child hierarchical codeblock
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; // because we got in child hb;
00182         }
00183         return false;
00184     }
00185 
00186     // IF we get here, the text block was in THIS object (and not a child)..
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 // Other methods
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); // wont add if already exists in object;
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     // set super-class attributes
00253     CodeBlockWithComments::setAttributesOnNode(doc, elem);
00254     CodeGenObjectWithTextBlocks::setAttributesOnNode(doc, elem);
00255 
00256     // set local class attributes
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     // set attributes from the XMI
00280     CodeBlockWithComments::setAttributesFromNode(root); // superclass load
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     // do this *after* all other attributes saved
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         // tack in text, if there is something there..
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     // if we get here, we failed.
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"
KDE Logo
This file is part of the documentation for umbrello Version 3.1.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Jun 26 08:07:57 2007 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003