Salome HOME
Revert "Synchronize adm files"
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_AttributeTreeNode.cxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  File   : SALOMEDSImpl_AttributeTreeNode.cxx
24 //  Author : Sergey RUIN
25 //  Module : SALOME
26 //
27 #include "SALOMEDSImpl_AttributeTreeNode.hxx"
28 #include <string.h>
29
30 const std::string&  SALOMEDSImpl_AttributeTreeNode::GetDefaultTreeID()
31 {
32   static std::string TreeNodeID ("0E1C36E6-379B-4d90-AC37-17A14310E648");
33   return TreeNodeID;
34 }    
35
36
37 SALOMEDSImpl_AttributeTreeNode::SALOMEDSImpl_AttributeTreeNode() 
38 :SALOMEDSImpl_GenericAttribute("AttributeTreeNode"), myFather(NULL),  myPrevious(NULL), myNext(NULL), myFirst(NULL) 
39 {}
40
41
42 SALOMEDSImpl_AttributeTreeNode* SALOMEDSImpl_AttributeTreeNode::Set (const DF_Label& L, const std::string& ID) 
43 {
44   SALOMEDSImpl_AttributeTreeNode* TN = NULL;
45
46   if (!(TN=(SALOMEDSImpl_AttributeTreeNode*)L.FindAttribute(ID))) {
47     TN = new SALOMEDSImpl_AttributeTreeNode ();
48     TN->SetTreeID(ID);
49     L.AddAttribute(TN);
50   }
51
52   return TN;    
53 }
54
55 //=======================================================================
56 //TreeNode : ID
57 //purpose  : Returns GUID of the TreeNode
58 //=======================================================================
59 const std::string& SALOMEDSImpl_AttributeTreeNode::ID() const
60 {
61   return myTreeID;
62 }  
63
64 //=======================================================================
65 //function : Append
66 //purpose  : Add <TN> as last child of me
67 //=======================================================================
68 bool SALOMEDSImpl_AttributeTreeNode::Append (SALOMEDSImpl_AttributeTreeNode* TN)
69 {
70   CheckLocked();
71
72   if (!(TN->ID() == myTreeID)) throw DFexception("SALOMEDSImpl_AttributeTreeNode::Append : uncompatible GUID");
73
74   if(TN->Label() == Label()) throw DFexception("Attempt of self linking");
75
76   TN->SetNext(NULL); // Deconnects from next.
77
78   // Find the last
79   if (!HasFirst()) {
80     SetFirst(TN);
81     TN->SetPrevious(NULL); // Deconnects from previous.
82   }
83   else {
84     SALOMEDSImpl_AttributeTreeNode* Last = GetFirst();
85     while (Last && Last->HasNext()) {
86       Last = Last->GetNext();
87     }
88     Last->SetNext(TN);
89     TN->SetPrevious(Last);
90   }
91   // Set Father
92   TN->SetFather(this);
93   
94   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
95   
96   return (TN);
97 }
98
99 //=======================================================================
100 //function : Prepend
101 //purpose  : Add <TN> as first child of me
102 //=======================================================================
103 bool SALOMEDSImpl_AttributeTreeNode::Prepend (SALOMEDSImpl_AttributeTreeNode* TN)
104 {
105   CheckLocked();
106
107   if (!(TN->ID() == myTreeID) ) throw DFexception("SALOMEDSImpl_AttributeTreeNode::Prepend : uncompatible GUID");
108
109   if(TN->Label() == Label()) throw DFexception("Attempt of self linking");
110
111   TN->SetPrevious(NULL);
112   if (HasFirst()) {
113     TN->SetNext(GetFirst());
114     GetFirst()->SetPrevious(TN);
115   }
116   else {
117     TN->SetNext(NULL);
118   }
119   TN->SetFather(this);
120   SetFirst(TN);
121   
122   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
123   
124   return (TN);
125 }                     
126
127
128 //=======================================================================
129 //function : InsertBefore
130 //purpose  : Inserts the TreeNode  <TN> before me
131 //=======================================================================
132 bool SALOMEDSImpl_AttributeTreeNode::InsertBefore (SALOMEDSImpl_AttributeTreeNode* TN)
133 {
134   CheckLocked();
135
136   if (!(TN->ID() == myTreeID) ) throw DFexception("SALOMEDSImpl_AttributeTreeNode::InsertBefore : uncompatible GUID");
137
138   if(TN->Label() == Label()) throw DFexception("Attempt of self linking");
139
140   TN->SetFather(GetFather());
141   TN->SetPrevious(GetPrevious());
142   TN->SetNext(this);
143
144   if (!HasPrevious())
145     GetFather()->SetFirst(TN);
146   else
147     GetPrevious()->SetNext(TN);
148
149   SetPrevious(TN);
150   
151   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
152   
153   return (TN);
154 }
155
156 //=======================================================================
157 //function : InsertAfter
158 //purpose  : Inserts the TreeNode  <TN> after me
159 //=======================================================================
160 bool SALOMEDSImpl_AttributeTreeNode::InsertAfter (SALOMEDSImpl_AttributeTreeNode* TN)
161 {
162   CheckLocked();
163
164   if(TN->Label() == Label()) throw DFexception("Attempt of self linking");
165
166   if (!(TN->ID() == myTreeID) ) throw DFexception("SALOMEDSImpl_AttributeTreeNode::InsertAfter : uncompatible GUID");
167
168   TN->SetFather(GetFather());
169   TN->SetPrevious(this);
170   TN->SetNext(GetNext());
171
172   if (HasNext()) GetNext()->SetPrevious(TN);
173
174   SetNext(TN);
175   
176   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
177   
178   return (TN);
179 }         
180
181 //=======================================================================
182 //function : Remove
183 //purpose  : Removees the function from the function tree
184 //=======================================================================
185 bool SALOMEDSImpl_AttributeTreeNode::Remove ()
186 {
187   CheckLocked();
188
189   if (IsRoot()) return true;
190
191   if (!HasPrevious())
192     GetFather()->SetFirst(GetNext());
193   else
194     GetPrevious()->SetNext(GetNext());
195
196   if (HasNext()) {
197     if (HasPrevious()) GetNext()->SetPrevious(GetPrevious());
198     else GetNext()->SetPrevious(NULL);
199   }
200   else {
201     if (HasPrevious()) GetPrevious()->SetNext(NULL);
202   }
203
204   if (GetFather()->HasFirst()) {
205     if (this == GetFather()->GetFirst()) {
206       if (HasNext()) {
207         GetFather()->SetFirst(GetNext());
208       }
209       else GetFather()->SetFirst(NULL);
210     }
211   }
212
213   SetFather(NULL);
214   SetNext(NULL);
215   SetPrevious(NULL);
216
217   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
218
219   return true;
220 }         
221
222 //=======================================================================
223 //function : Depth
224 //purpose  :
225 //=======================================================================
226 int SALOMEDSImpl_AttributeTreeNode::Depth () const
227 {
228   int depth = 0;
229   SALOMEDSImpl_AttributeTreeNode* current = (SALOMEDSImpl_AttributeTreeNode*)this;
230   while (current) {
231     depth++;
232     current = current->GetFather();
233   }
234   return depth;
235 }
236
237 //=======================================================================
238 //function : SetTreeID
239 //purpose  : Finds or creates a TreeNode  attribute with explicit ID
240 //         : a driver for it
241 //=======================================================================
242 void SALOMEDSImpl_AttributeTreeNode::SetTreeID (const std::string& explicitID)
243 {
244   myTreeID = explicitID;
245   
246   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
247 }
248
249
250 //=======================================================================
251 //function : IsAscendant
252 //purpose  :
253 //=======================================================================
254 bool SALOMEDSImpl_AttributeTreeNode::IsAscendant (const SALOMEDSImpl_AttributeTreeNode* ofTN) const
255 {
256   return ofTN->IsDescendant(this);
257 }                
258
259 //=======================================================================
260 //function : IsDescendant
261 //purpose  :
262 //=======================================================================
263
264 bool SALOMEDSImpl_AttributeTreeNode::IsDescendant (const SALOMEDSImpl_AttributeTreeNode* ofTN) const
265 {
266   SALOMEDSImpl_AttributeTreeNode* current = (SALOMEDSImpl_AttributeTreeNode*)this;
267   while (current) {
268     if (current->GetFather() == ofTN) return true;
269     current = current->GetFather();
270   }
271   return false;
272 }
273
274 //=======================================================================
275 //function : IsFather
276 //purpose  :
277 //=======================================================================
278
279 bool SALOMEDSImpl_AttributeTreeNode::IsFather (const SALOMEDSImpl_AttributeTreeNode* ofTN) const
280 {
281   return (ofTN->GetFather() == this);
282 }
283
284
285 //=======================================================================
286 //function : IsChild
287 //purpose  :
288 //=======================================================================
289
290 bool SALOMEDSImpl_AttributeTreeNode::IsChild (const SALOMEDSImpl_AttributeTreeNode* ofTN) const
291 {
292   return (myFather == ofTN);
293 }
294
295 //=======================================================================
296 //TreeNode : IsRoot
297 //purpose  : Returns Standard_True if the TreeNode is not attached to a
298 //           TreeNode tree or hasn't an Father.
299 //=======================================================================
300 bool SALOMEDSImpl_AttributeTreeNode::IsRoot() const
301 {
302   if (!myFather && !myPrevious && !myNext)
303     return true;
304   return false;
305 }
306
307 //=======================================================================
308 //TreeNode : Root
309 //purpose  : Returns the TreeNode which has no Father
310 //=======================================================================
311 SALOMEDSImpl_AttributeTreeNode* SALOMEDSImpl_AttributeTreeNode::Root() const
312 {
313   SALOMEDSImpl_AttributeTreeNode* O = (SALOMEDSImpl_AttributeTreeNode*)this;
314   while (O && O->HasFather())
315     O = O->GetFather();
316   return O;
317 }       
318
319 //=======================================================================
320 //TreeNode : SetFather
321 //purpose  : Sets the TreeNode F as Father of me
322 //=======================================================================
323 void SALOMEDSImpl_AttributeTreeNode::SetFather(const SALOMEDSImpl_AttributeTreeNode* F)
324 {
325   CheckLocked();
326   Backup();
327   myFather = (SALOMEDSImpl_AttributeTreeNode*)F;
328   
329   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
330 }
331
332 //=======================================================================
333 //TreeNode : SetNext
334 //purpose  : Sets the TreeNode F next to me
335 //=======================================================================
336 void SALOMEDSImpl_AttributeTreeNode::SetNext(const SALOMEDSImpl_AttributeTreeNode* F)
337 {
338   CheckLocked();
339   Backup();
340   myNext = (SALOMEDSImpl_AttributeTreeNode*)F;
341   
342   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
343 }
344
345
346 //=======================================================================
347 //TreeNode : SetPrevious
348 //purpose  : Sets the TreeNode F previous to me
349 //=======================================================================
350 void SALOMEDSImpl_AttributeTreeNode::SetPrevious(const SALOMEDSImpl_AttributeTreeNode* F)
351 {
352   CheckLocked();
353   Backup();
354   myPrevious = (SALOMEDSImpl_AttributeTreeNode*)F;
355   
356   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
357 }
358
359 //=======================================================================
360 //TreeNode : SetFirst
361 //purpose  : Sets the TreeNode F as first in the TreeNode tree
362 //=======================================================================
363 void SALOMEDSImpl_AttributeTreeNode::SetFirst(const SALOMEDSImpl_AttributeTreeNode* F)
364 {
365   CheckLocked();
366   Backup();
367   myFirst = (SALOMEDSImpl_AttributeTreeNode*)F;
368   
369   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
370 }         
371
372 //=======================================================================
373 //TreeNode : AfterAddition
374 //purpose  : Connects the TreeNode to the tree.
375 //=======================================================================
376 void SALOMEDSImpl_AttributeTreeNode::AfterAddition() 
377 {
378   if (myPrevious) {
379     myPrevious->SetNext(this);
380   }
381   else if (myFather) {
382     myFather->SetFirst(this);
383   }
384   if (myNext) {
385     myNext->SetPrevious(this);
386   }
387 }
388
389 //=======================================================================
390 //TreeNode : BeforeForget
391 //purpose  : Disconnect the TreeNode from the tree.
392 //=======================================================================
393 void SALOMEDSImpl_AttributeTreeNode::BeforeForget() 
394 {
395     Remove();
396     while (HasFirst()) GetFirst()->Remove();
397 }
398
399 //=======================================================================
400 //TreeNode : Restore
401 //purpose  :
402 //=======================================================================
403 void SALOMEDSImpl_AttributeTreeNode::Restore(DF_Attribute* other) 
404 {
405   SALOMEDSImpl_AttributeTreeNode* F =  dynamic_cast<SALOMEDSImpl_AttributeTreeNode*>(other);
406   myFather     = F->myFather;
407   myPrevious   = F->myPrevious;
408   myNext       = F->myNext;
409   myFirst      = F->myFirst;
410   myTreeID     = F->myTreeID;
411 }       
412
413 //=======================================================================
414 //TreeNode : Paste
415 //purpose  : Method for Copy mechanism
416 //=======================================================================
417
418 void SALOMEDSImpl_AttributeTreeNode::Paste(DF_Attribute* into)
419 {
420   SALOMEDSImpl_AttributeTreeNode* intof = dynamic_cast<SALOMEDSImpl_AttributeTreeNode*>(into);
421   intof->SetFather(myFather);
422   intof->SetNext(myNext);
423   intof->SetPrevious(myPrevious);
424   intof->SetFirst(myFirst);
425   intof->SetTreeID(myTreeID);
426 }
427
428 //=======================================================================
429 //TreeNode : NewEmpty
430 //purpose  : Returns new empty TreeNode attribute
431 //=======================================================================
432
433 DF_Attribute* SALOMEDSImpl_AttributeTreeNode::NewEmpty() const
434 {
435   SALOMEDSImpl_AttributeTreeNode* T = new SALOMEDSImpl_AttributeTreeNode();
436   T->SetTreeID(myTreeID);
437   return T;
438 }
439
440 std::string SALOMEDSImpl_AttributeTreeNode::Type()
441 {
442    char* aNodeName = new char[127];
443    sprintf(aNodeName, "AttributeTreeNodeGUID%s", ID().c_str());
444    std::string ret(aNodeName); 
445    delete [] aNodeName;
446    
447    return ret;                               
448 }
449
450 std::string SALOMEDSImpl_AttributeTreeNode::Save() 
451 {
452   std::string aFather, aPrevious, aNext, aFirst;
453
454   if (HasFather()) aFather = GetFather()->Label().Entry(); else aFather = "!";
455   if (HasPrevious()) aPrevious = GetPrevious()->Label().Entry(); else aPrevious = "!";
456   if (HasNext()) aNext = GetNext()->Label().Entry(); else aNext = "!";
457   if (HasFirst()) aFirst = GetFirst()->Label().Entry(); else aFirst = "!";
458
459   int aLength = 4;
460   aLength += aFather.size() + aPrevious.size() + aNext.size() + aFirst.size();
461   char* aResult = new char[aLength];
462   sprintf(aResult, "%s %s %s %s", aFather.c_str(), aPrevious.c_str(), aNext.c_str(), aFirst.c_str());
463   std::string ret(aResult);
464   delete [] aResult;
465   return ret;
466 }
467
468 void SALOMEDSImpl_AttributeTreeNode::Load(const std::string& value) 
469 {
470   char* aCopy = (char*)value.c_str();
471   char* adr = strtok(aCopy, " ");
472   
473   DF_Label aLabel;
474   SALOMEDSImpl_AttributeTreeNode* aDepNode = NULL;
475
476   if (adr && adr[0] != '!') {
477     aLabel = DF_Label::Label(Label(), adr, true);
478     if (!(aDepNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(ID()))) 
479       aDepNode =  SALOMEDSImpl_AttributeTreeNode::Set(aLabel, ID());
480
481     SetFather(aDepNode);
482   }
483
484   adr = strtok(NULL, " ");
485   if (adr && adr[0] != '!') {
486     aLabel = DF_Label::Label(Label(), adr, true);
487     if (!(aDepNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(ID()))) 
488       aDepNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, ID());
489     SetPrevious(aDepNode);
490   }
491
492   adr = strtok(NULL, " ");
493   if (adr && adr[0] != '!') {
494     aLabel = DF_Label::Label(Label(), adr, true);
495     if (!(aDepNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(ID()))) 
496       aDepNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, ID());
497     SetNext(aDepNode);
498   }
499
500   adr = strtok(NULL, " ");
501   if (adr && adr[0] != '!') {
502     aLabel = DF_Label::Label(Label(), adr, true);
503     if (!(aDepNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(ID()))) 
504       aDepNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, ID());
505     SetFirst(aDepNode);
506   }
507 }
508
509