Back to index

salome-kernel  6.5.0
SALOMEDSImpl_AttributeTreeNode.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  File   : SALOMEDSImpl_AttributeTreeNode.cxx
00024 //  Author : Sergey RUIN
00025 //  Module : SALOME
00026 //
00027 #include "SALOMEDSImpl_AttributeTreeNode.hxx"
00028 #include <string.h>
00029 
00030 const std::string&  SALOMEDSImpl_AttributeTreeNode::GetDefaultTreeID()
00031 {
00032   static std::string TreeNodeID ("0E1C36E6-379B-4d90-AC37-17A14310E648");
00033   return TreeNodeID;
00034 }    
00035 
00036 
00037 SALOMEDSImpl_AttributeTreeNode::SALOMEDSImpl_AttributeTreeNode() 
00038 :SALOMEDSImpl_GenericAttribute("AttributeTreeNode"), myFather(NULL),  myPrevious(NULL), myNext(NULL), myFirst(NULL) 
00039 {}
00040 
00041 
00042 SALOMEDSImpl_AttributeTreeNode* SALOMEDSImpl_AttributeTreeNode::Set (const DF_Label& L, const std::string& ID) 
00043 {
00044   SALOMEDSImpl_AttributeTreeNode* TN = NULL;
00045 
00046   if (!(TN=(SALOMEDSImpl_AttributeTreeNode*)L.FindAttribute(ID))) {
00047     TN = new SALOMEDSImpl_AttributeTreeNode ();
00048     TN->SetTreeID(ID);
00049     L.AddAttribute(TN);
00050   }
00051 
00052   return TN;    
00053 }
00054 
00055 //=======================================================================
00056 //TreeNode : ID
00057 //purpose  : Returns GUID of the TreeNode
00058 //=======================================================================
00059 const std::string& SALOMEDSImpl_AttributeTreeNode::ID() const
00060 {
00061   return myTreeID;
00062 }  
00063 
00064 //=======================================================================
00065 //function : Append
00066 //purpose  : Add <TN> as last child of me
00067 //=======================================================================
00068 bool SALOMEDSImpl_AttributeTreeNode::Append (SALOMEDSImpl_AttributeTreeNode* TN)
00069 {
00070   CheckLocked();
00071 
00072   if (!(TN->ID() == myTreeID)) throw DFexception("SALOMEDSImpl_AttributeTreeNode::Append : uncompatible GUID");
00073 
00074   if(TN->Label() == Label()) throw DFexception("Attempt of self linking");
00075 
00076   TN->SetNext(NULL); // Deconnects from next.
00077 
00078   // Find the last
00079   if (!HasFirst()) {
00080     SetFirst(TN);
00081     TN->SetPrevious(NULL); // Deconnects from previous.
00082   }
00083   else {
00084     SALOMEDSImpl_AttributeTreeNode* Last = GetFirst();
00085     while (Last && Last->HasNext()) {
00086       Last = Last->GetNext();
00087     }
00088     Last->SetNext(TN);
00089     TN->SetPrevious(Last);
00090   }
00091   // Set Father
00092   TN->SetFather(this);
00093   
00094   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
00095   
00096   return (TN);
00097 }
00098 
00099 //=======================================================================
00100 //function : Prepend
00101 //purpose  : Add <TN> as first child of me
00102 //=======================================================================
00103 bool SALOMEDSImpl_AttributeTreeNode::Prepend (SALOMEDSImpl_AttributeTreeNode* TN)
00104 {
00105   CheckLocked();
00106 
00107   if (!(TN->ID() == myTreeID) ) throw DFexception("SALOMEDSImpl_AttributeTreeNode::Prepend : uncompatible GUID");
00108 
00109   if(TN->Label() == Label()) throw DFexception("Attempt of self linking");
00110 
00111   TN->SetPrevious(NULL);
00112   if (HasFirst()) {
00113     TN->SetNext(GetFirst());
00114     GetFirst()->SetPrevious(TN);
00115   }
00116   else {
00117     TN->SetNext(NULL);
00118   }
00119   TN->SetFather(this);
00120   SetFirst(TN);
00121   
00122   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
00123   
00124   return (TN);
00125 }                     
00126 
00127 
00128 //=======================================================================
00129 //function : InsertBefore
00130 //purpose  : Inserts the TreeNode  <TN> before me
00131 //=======================================================================
00132 bool SALOMEDSImpl_AttributeTreeNode::InsertBefore (SALOMEDSImpl_AttributeTreeNode* TN)
00133 {
00134   CheckLocked();
00135 
00136   if (!(TN->ID() == myTreeID) ) throw DFexception("SALOMEDSImpl_AttributeTreeNode::InsertBefore : uncompatible GUID");
00137 
00138   if(TN->Label() == Label()) throw DFexception("Attempt of self linking");
00139 
00140   TN->SetFather(GetFather());
00141   TN->SetPrevious(GetPrevious());
00142   TN->SetNext(this);
00143 
00144   if (!HasPrevious())
00145     GetFather()->SetFirst(TN);
00146   else
00147     GetPrevious()->SetNext(TN);
00148 
00149   SetPrevious(TN);
00150   
00151   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
00152   
00153   return (TN);
00154 }
00155 
00156 //=======================================================================
00157 //function : InsertAfter
00158 //purpose  : Inserts the TreeNode  <TN> after me
00159 //=======================================================================
00160 bool SALOMEDSImpl_AttributeTreeNode::InsertAfter (SALOMEDSImpl_AttributeTreeNode* TN)
00161 {
00162   CheckLocked();
00163 
00164   if(TN->Label() == Label()) throw DFexception("Attempt of self linking");
00165 
00166   if (!(TN->ID() == myTreeID) ) throw DFexception("SALOMEDSImpl_AttributeTreeNode::InsertAfter : uncompatible GUID");
00167 
00168   TN->SetFather(GetFather());
00169   TN->SetPrevious(this);
00170   TN->SetNext(GetNext());
00171 
00172   if (HasNext()) GetNext()->SetPrevious(TN);
00173 
00174   SetNext(TN);
00175   
00176   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
00177   
00178   return (TN);
00179 }         
00180 
00181 //=======================================================================
00182 //function : Remove
00183 //purpose  : Removees the function from the function tree
00184 //=======================================================================
00185 bool SALOMEDSImpl_AttributeTreeNode::Remove ()
00186 {
00187   CheckLocked();
00188 
00189   if (IsRoot()) return true;
00190 
00191   if (!HasPrevious())
00192     GetFather()->SetFirst(GetNext());
00193   else
00194     GetPrevious()->SetNext(GetNext());
00195 
00196   if (HasNext()) {
00197     if (HasPrevious()) GetNext()->SetPrevious(GetPrevious());
00198     else GetNext()->SetPrevious(NULL);
00199   }
00200   else {
00201     if (HasPrevious()) GetPrevious()->SetNext(NULL);
00202   }
00203 
00204   if (GetFather()->HasFirst()) {
00205     if (this == GetFather()->GetFirst()) {
00206       if (HasNext()) {
00207         GetFather()->SetFirst(GetNext());
00208       }
00209       else GetFather()->SetFirst(NULL);
00210     }
00211   }
00212 
00213   SetFather(NULL);
00214   SetNext(NULL);
00215   SetPrevious(NULL);
00216 
00217   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
00218 
00219   return true;
00220 }         
00221 
00222 //=======================================================================
00223 //function : Depth
00224 //purpose  :
00225 //=======================================================================
00226 int SALOMEDSImpl_AttributeTreeNode::Depth () const
00227 {
00228   int depth = 0;
00229   SALOMEDSImpl_AttributeTreeNode* current = (SALOMEDSImpl_AttributeTreeNode*)this;
00230   while (current) {
00231     depth++;
00232     current = current->GetFather();
00233   }
00234   return depth;
00235 }
00236 
00237 //=======================================================================
00238 //function : SetTreeID
00239 //purpose  : Finds or creates a TreeNode  attribute with explicit ID
00240 //         : a driver for it
00241 //=======================================================================
00242 void SALOMEDSImpl_AttributeTreeNode::SetTreeID (const std::string& explicitID)
00243 {
00244   myTreeID = explicitID;
00245   
00246   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
00247 }
00248 
00249 
00250 //=======================================================================
00251 //function : IsAscendant
00252 //purpose  :
00253 //=======================================================================
00254 bool SALOMEDSImpl_AttributeTreeNode::IsAscendant (const SALOMEDSImpl_AttributeTreeNode* ofTN) const
00255 {
00256   return ofTN->IsDescendant(this);
00257 }                
00258 
00259 //=======================================================================
00260 //function : IsDescendant
00261 //purpose  :
00262 //=======================================================================
00263 
00264 bool SALOMEDSImpl_AttributeTreeNode::IsDescendant (const SALOMEDSImpl_AttributeTreeNode* ofTN) const
00265 {
00266   SALOMEDSImpl_AttributeTreeNode* current = (SALOMEDSImpl_AttributeTreeNode*)this;
00267   while (current) {
00268     if (current->GetFather() == ofTN) return true;
00269     current = current->GetFather();
00270   }
00271   return false;
00272 }
00273 
00274 //=======================================================================
00275 //function : IsFather
00276 //purpose  :
00277 //=======================================================================
00278 
00279 bool SALOMEDSImpl_AttributeTreeNode::IsFather (const SALOMEDSImpl_AttributeTreeNode* ofTN) const
00280 {
00281   return (ofTN->GetFather() == this);
00282 }
00283 
00284 
00285 //=======================================================================
00286 //function : IsChild
00287 //purpose  :
00288 //=======================================================================
00289 
00290 bool SALOMEDSImpl_AttributeTreeNode::IsChild (const SALOMEDSImpl_AttributeTreeNode* ofTN) const
00291 {
00292   return (myFather == ofTN);
00293 }
00294 
00295 //=======================================================================
00296 //TreeNode : IsRoot
00297 //purpose  : Returns Standard_True if the TreeNode is not attached to a
00298 //           TreeNode tree or hasn't an Father.
00299 //=======================================================================
00300 bool SALOMEDSImpl_AttributeTreeNode::IsRoot() const
00301 {
00302   if (!myFather && !myPrevious && !myNext)
00303     return true;
00304   return false;
00305 }
00306 
00307 //=======================================================================
00308 //TreeNode : Root
00309 //purpose  : Returns the TreeNode which has no Father
00310 //=======================================================================
00311 SALOMEDSImpl_AttributeTreeNode* SALOMEDSImpl_AttributeTreeNode::Root() const
00312 {
00313   SALOMEDSImpl_AttributeTreeNode* O = (SALOMEDSImpl_AttributeTreeNode*)this;
00314   while (O && O->HasFather())
00315     O = O->GetFather();
00316   return O;
00317 }       
00318 
00319 //=======================================================================
00320 //TreeNode : SetFather
00321 //purpose  : Sets the TreeNode F as Father of me
00322 //=======================================================================
00323 void SALOMEDSImpl_AttributeTreeNode::SetFather(const SALOMEDSImpl_AttributeTreeNode* F)
00324 {
00325   CheckLocked();
00326   Backup();
00327   myFather = (SALOMEDSImpl_AttributeTreeNode*)F;
00328   
00329   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
00330 }
00331 
00332 //=======================================================================
00333 //TreeNode : SetNext
00334 //purpose  : Sets the TreeNode F next to me
00335 //=======================================================================
00336 void SALOMEDSImpl_AttributeTreeNode::SetNext(const SALOMEDSImpl_AttributeTreeNode* F)
00337 {
00338   CheckLocked();
00339   Backup();
00340   myNext = (SALOMEDSImpl_AttributeTreeNode*)F;
00341   
00342   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
00343 }
00344 
00345 
00346 //=======================================================================
00347 //TreeNode : SetPrevious
00348 //purpose  : Sets the TreeNode F previous to me
00349 //=======================================================================
00350 void SALOMEDSImpl_AttributeTreeNode::SetPrevious(const SALOMEDSImpl_AttributeTreeNode* F)
00351 {
00352   CheckLocked();
00353   Backup();
00354   myPrevious = (SALOMEDSImpl_AttributeTreeNode*)F;
00355   
00356   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
00357 }
00358 
00359 //=======================================================================
00360 //TreeNode : SetFirst
00361 //purpose  : Sets the TreeNode F as first in the TreeNode tree
00362 //=======================================================================
00363 void SALOMEDSImpl_AttributeTreeNode::SetFirst(const SALOMEDSImpl_AttributeTreeNode* F)
00364 {
00365   CheckLocked();
00366   Backup();
00367   myFirst = (SALOMEDSImpl_AttributeTreeNode*)F;
00368   
00369   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
00370 }         
00371 
00372 //=======================================================================
00373 //TreeNode : AfterAddition
00374 //purpose  : Connects the TreeNode to the tree.
00375 //=======================================================================
00376 void SALOMEDSImpl_AttributeTreeNode::AfterAddition() 
00377 {
00378   if (myPrevious) {
00379     myPrevious->SetNext(this);
00380   }
00381   else if (myFather) {
00382     myFather->SetFirst(this);
00383   }
00384   if (myNext) {
00385     myNext->SetPrevious(this);
00386   }
00387 }
00388 
00389 //=======================================================================
00390 //TreeNode : BeforeForget
00391 //purpose  : Disconnect the TreeNode from the tree.
00392 //=======================================================================
00393 void SALOMEDSImpl_AttributeTreeNode::BeforeForget() 
00394 {
00395     Remove();
00396     while (HasFirst()) GetFirst()->Remove();
00397 }
00398 
00399 //=======================================================================
00400 //TreeNode : Restore
00401 //purpose  :
00402 //=======================================================================
00403 void SALOMEDSImpl_AttributeTreeNode::Restore(DF_Attribute* other) 
00404 {
00405   SALOMEDSImpl_AttributeTreeNode* F =  dynamic_cast<SALOMEDSImpl_AttributeTreeNode*>(other);
00406   myFather     = F->myFather;
00407   myPrevious   = F->myPrevious;
00408   myNext       = F->myNext;
00409   myFirst      = F->myFirst;
00410   myTreeID     = F->myTreeID;
00411 }       
00412 
00413 //=======================================================================
00414 //TreeNode : Paste
00415 //purpose  : Method for Copy mechanism
00416 //=======================================================================
00417 
00418 void SALOMEDSImpl_AttributeTreeNode::Paste(DF_Attribute* into)
00419 {
00420   SALOMEDSImpl_AttributeTreeNode* intof = dynamic_cast<SALOMEDSImpl_AttributeTreeNode*>(into);
00421   intof->SetFather(myFather);
00422   intof->SetNext(myNext);
00423   intof->SetPrevious(myPrevious);
00424   intof->SetFirst(myFirst);
00425   intof->SetTreeID(myTreeID);
00426 }
00427 
00428 //=======================================================================
00429 //TreeNode : NewEmpty
00430 //purpose  : Returns new empty TreeNode attribute
00431 //=======================================================================
00432 
00433 DF_Attribute* SALOMEDSImpl_AttributeTreeNode::NewEmpty() const
00434 {
00435   SALOMEDSImpl_AttributeTreeNode* T = new SALOMEDSImpl_AttributeTreeNode();
00436   T->SetTreeID(myTreeID);
00437   return T;
00438 }
00439 
00440 std::string SALOMEDSImpl_AttributeTreeNode::Type()
00441 {
00442    char* aNodeName = new char[127];
00443    sprintf(aNodeName, "AttributeTreeNodeGUID%s", ID().c_str());
00444    std::string ret(aNodeName); 
00445    delete [] aNodeName;
00446    
00447    return ret;                               
00448 }
00449 
00450 std::string SALOMEDSImpl_AttributeTreeNode::Save() 
00451 {
00452   std::string aFather, aPrevious, aNext, aFirst;
00453 
00454   if (HasFather()) aFather = GetFather()->Label().Entry(); else aFather = "!";
00455   if (HasPrevious()) aPrevious = GetPrevious()->Label().Entry(); else aPrevious = "!";
00456   if (HasNext()) aNext = GetNext()->Label().Entry(); else aNext = "!";
00457   if (HasFirst()) aFirst = GetFirst()->Label().Entry(); else aFirst = "!";
00458 
00459   int aLength = 4;
00460   aLength += aFather.size() + aPrevious.size() + aNext.size() + aFirst.size();
00461   char* aResult = new char[aLength];
00462   sprintf(aResult, "%s %s %s %s", aFather.c_str(), aPrevious.c_str(), aNext.c_str(), aFirst.c_str());
00463   std::string ret(aResult);
00464   delete [] aResult;
00465   return ret;
00466 }
00467 
00468 void SALOMEDSImpl_AttributeTreeNode::Load(const std::string& value) 
00469 {
00470   char* aCopy = (char*)value.c_str();
00471   char* adr = strtok(aCopy, " ");
00472   
00473   DF_Label aLabel;
00474   SALOMEDSImpl_AttributeTreeNode* aDepNode = NULL;
00475 
00476   if (adr && adr[0] != '!') {
00477     aLabel = DF_Label::Label(Label(), adr, true);
00478     if (!(aDepNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(ID()))) 
00479       aDepNode =  SALOMEDSImpl_AttributeTreeNode::Set(aLabel, ID());
00480 
00481     SetFather(aDepNode);
00482   }
00483 
00484   adr = strtok(NULL, " ");
00485   if (adr && adr[0] != '!') {
00486     aLabel = DF_Label::Label(Label(), adr, true);
00487     if (!(aDepNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(ID()))) 
00488       aDepNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, ID());
00489     SetPrevious(aDepNode);
00490   }
00491 
00492   adr = strtok(NULL, " ");
00493   if (adr && adr[0] != '!') {
00494     aLabel = DF_Label::Label(Label(), adr, true);
00495     if (!(aDepNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(ID()))) 
00496       aDepNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, ID());
00497     SetNext(aDepNode);
00498   }
00499 
00500   adr = strtok(NULL, " ");
00501   if (adr && adr[0] != '!') {
00502     aLabel = DF_Label::Label(Label(), adr, true);
00503     if (!(aDepNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(ID()))) 
00504       aDepNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, ID());
00505     SetFirst(aDepNode);
00506   }
00507 }
00508 
00509