Salome HOME
8c6f980de6f7fbf2fee85bafc11ce759440294fe
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_GenericAttribute_i.cxx
1 //  SALOME SALOMEDS : data structure of SALOME and sources of Salome data server 
2 //
3 //  Copyright (C) 2003  CEA/DEN, EDF R&D
4 //
5 //
6 //
7 //  File   : SALOMEDS_GenericAttribute_i.cxx
8 //  Author : Mikhail PONIKAROV
9 //  Module : SALOME
10 //  $Header$
11
12 #include <TDataStd_TreeNode.hxx>
13 #include <TDataStd_UAttribute.hxx>
14
15 #include "SALOMEDS_AttributeDrawable_i.hxx"
16 #include "SALOMEDS_AttributeSelectable_i.hxx"
17 #include "SALOMEDS_AttributeOpened_i.hxx"
18 #include "SALOMEDS_AttributeFlags_i.hxx"
19 #include "SALOMEDS_AttributeGraphic_i.hxx"
20 #include "SALOMEDS_AttributeTextColor_i.hxx"
21 #include "SALOMEDS_AttributeTextHighlightColor_i.hxx"
22 #include "SALOMEDS_AttributePixMap_i.hxx"
23 #include "SALOMEDS_AttributeExpandable_i.hxx"
24 #include "SALOMEDS_AttributeLocalID_i.hxx"
25 #include "SALOMEDS_AttributeTarget_i.hxx"
26 #include "SALOMEDS_AttributeTableOfInteger_i.hxx"
27 #include "SALOMEDS_AttributeTableOfReal_i.hxx"
28 #include "SALOMEDS_AttributeTableOfString_i.hxx"
29 #include "SALOMEDS_AttributeStudyProperties_i.hxx"
30 #include "SALOMEDS_AttributePythonObject_i.hxx"
31 #include "SALOMEDS_AttributeTreeNode_i.hxx"
32 #include "SALOMEDS_AttributeUserID_i.hxx"
33 #include "SALOMEDS_AttributeExternalFileDef_i.hxx"
34 #include "SALOMEDS_AttributeFileType_i.hxx"
35 #include "SALOMEDS_StudyBuilder_i.hxx"
36
37 #include "SALOMEDS_SequenceOfRealAttribute.hxx"
38 #include "SALOMEDS_SequenceOfIntegerAttribute.hxx"
39 #include "SALOMEDS_IORAttribute.hxx"
40 #include "SALOMEDS_PersRefAttribute.hxx"
41 #include "SALOMEDS_DrawableAttribute.hxx"
42 #include "SALOMEDS_SelectableAttribute.hxx"
43 #include "SALOMEDS_ExpandableAttribute.hxx"
44 #include "SALOMEDS_OpenedAttribute.hxx"
45 #include "SALOMEDS_FlagsAttribute.hxx"
46 #include "SALOMEDS_GraphicAttribute.hxx"
47 #include "SALOMEDS_TextColorAttribute.hxx"
48 #include "SALOMEDS_TextHighlightColorAttribute.hxx"
49 #include "SALOMEDS_PixMapAttribute.hxx"
50 #include "SALOMEDS_LocalIDAttribute.hxx"
51 #include "SALOMEDS_PythonObjectAttribute.hxx"
52 #include "SALOMEDS_TargetAttribute.hxx"
53 #include "SALOMEDS_StudyPropertiesAttribute.hxx"
54 #include "SALOMEDS_TableOfIntegerAttribute.hxx"
55 #include "SALOMEDS_TableOfRealAttribute.hxx"
56 #include "SALOMEDS_TableOfStringAttribute.hxx"
57 #include "SALOMEDS_ExternalFileDef.hxx"
58 #include "SALOMEDS_FileType.hxx"
59 #include "Utils_ExceptHandlers.hxx"
60
61 using namespace std;
62
63 #define RegisteredAttributes 28
64
65 UNEXPECT_CATCH(GALockProtection, SALOMEDS::GenericAttribute::LockProtection);
66
67 const char AttributesTypeNames[RegisteredAttributes][30] = {
68   "AttributeReal", "AttributeInteger",
69   "AttributeSequenceOfReal", "AttributeSequenceOfInteger",
70   "AttributeName", "AttributeComment",
71   "AttributeIOR", "AttributePersistentRef",
72   "AttributeDrawable", "AttributeSelectable",
73   "AttributeExpandable", "AttributeOpened",
74   "AttributeTextColor", "AttributeTextHighlightColor", "AttributePixMap",
75   "AttributeTreeNode", "AttributeLocalID",
76   "AttributeTarget",
77   "AttributeTableOfInteger", "AttributeTableOfReal", "AttributeTableOfString",
78   "AttributeStudyProperties",
79   "AttributePythonObject",
80   "AttributeUserID",
81   "AttributeExternalFileDef", "AttributeFileType",
82   "AttributeFlags",
83   "AttributeGraphic"
84 };
85
86 const Standard_GUID AttributesGUIDs[RegisteredAttributes] = {
87   TDataStd_Real::GetID(), TDataStd_Integer::GetID(),
88   SALOMEDS_SequenceOfRealAttribute::GetID(), SALOMEDS_SequenceOfIntegerAttribute::GetID(),
89   TDataStd_Name::GetID(), TDataStd_Comment::GetID(),
90   SALOMEDS_IORAttribute::GetID(), SALOMEDS_PersRefAttribute::GetID(),
91   SALOMEDS_DrawableAttribute::GetID(), SALOMEDS_SelectableAttribute::GetID(),
92   SALOMEDS_ExpandableAttribute::GetID(), SALOMEDS_OpenedAttribute::GetID(),
93   SALOMEDS_TextColorAttribute::GetID(), SALOMEDS_TextHighlightColorAttribute::GetID(), SALOMEDS_PixMapAttribute::GetID(),
94   TDataStd_TreeNode::GetDefaultTreeID(), SALOMEDS_LocalIDAttribute::GetID(),
95   SALOMEDS_TargetAttribute::GetID(),
96   SALOMEDS_TableOfIntegerAttribute::GetID(), SALOMEDS_TableOfRealAttribute::GetID(), SALOMEDS_TableOfStringAttribute::GetID(),
97   SALOMEDS_StudyPropertiesAttribute::GetID(),
98   SALOMEDS_PythonObjectAttribute::GetID(),
99   Standard_GUID("FFFFFFFF-D9CD-11d6-945D-1050DA506788"),
100   SALOMEDS_ExternalFileDef::GetID(), SALOMEDS_FileType::GetID(),
101   SALOMEDS_FlagsAttribute::GetID(),
102   SALOMEDS_GraphicAttribute::GetID()
103 };
104
105 void SALOMEDS_GenericAttribute_i::CheckLocked() throw (SALOMEDS::GenericAttribute::LockProtection) {
106   Unexpect aCatch(GALockProtection);
107   if (!_myAttr.IsNull() && _myAttr->IsValid() && !CORBA::is_nil(_myOrb)) {
108     Handle(SALOMEDS_IORAttribute) Att;
109     if (_myAttr->Label().Root().FindAttribute(SALOMEDS_IORAttribute::GetID(),Att)){
110       TCollection_AsciiString ch(Att->Get());
111       char* IOR = CORBA::string_dup(ch.ToCString());
112       CORBA::Object_var obj = _myOrb->string_to_object(IOR);
113       SALOMEDS::Study_ptr aStudy = SALOMEDS::Study::_narrow(obj);
114 //      ASSERT(!CORBA::is_nil(aStudy));
115       if (aStudy->NewBuilder()->HasOpenCommand()) return;
116       if (aStudy->GetProperties()->IsLocked()) {
117 //        MESSAGE("Locked document modification !!!");
118         throw SALOMEDS::GenericAttribute::LockProtection();
119       }
120     }
121   }
122 }
123
124 SALOMEDS::SObject_ptr SALOMEDS_GenericAttribute_i::GetSObject() {
125   if (_myAttr.IsNull() || _myAttr->Label().IsNull()) return SALOMEDS::SObject::_nil();
126   SALOMEDS_SObject_i *  so_servant = new SALOMEDS_SObject_i (_myAttr->Label(),_myOrb);
127   SALOMEDS::SObject_var so = SALOMEDS::SObject::_narrow(so_servant->_this()); 
128   return so._retn();
129 }
130
131 Standard_GUID SALOMEDS_GenericAttribute_i::GetGUID(const char* theType) {
132   Standard_Integer i;
133   for(i = 0; i < RegisteredAttributes; i++) {
134     if (strcmp(AttributesTypeNames[i], theType) == 0) return AttributesGUIDs[i];
135   }
136
137   if (strncmp(theType, "AttributeTreeNodeGUID",21) == 0) {
138     char* aGUIDString = new char[40];
139     sprintf(aGUIDString, &(theType[21]));
140     Standard_GUID aGUID = Standard_GUID(aGUIDString); // create tree node GUID by name
141     delete(aGUIDString);
142     return aGUID;
143   }
144   return Standard_GUID();
145 }
146
147 char* SALOMEDS_GenericAttribute_i::Type() {
148   if (_myAttr.IsNull()) return "";
149   int i;
150   for(i = 0; i < RegisteredAttributes; i++) {
151     if (_myAttr->ID() == AttributesGUIDs[i]) {
152       CORBA::String_var aString(AttributesTypeNames[i]);
153       return aString._retn();
154     }
155   }
156   Handle(TDataStd_TreeNode) aNode = Handle(TDataStd_TreeNode)::DownCast(_myAttr);
157   if (!aNode.IsNull()) {
158     char* aNodeName = new char[60];
159     char aGUID[40];
160     aNode->ID().ToCString(aGUID);
161     sprintf(aNodeName, "AttributeTreeNodeGUID%s",aGUID);
162     return aNodeName;
163   }
164
165   Handle(TDataStd_UAttribute) aUAttr = Handle(TDataStd_UAttribute)::DownCast(_myAttr);
166   if (!aUAttr.IsNull()) {
167     char* aUAttrName = new char[60];
168     char aGUID[40];
169     aUAttr->ID().ToCString(aGUID);
170     sprintf(aUAttrName, "AttributeUserID_%s",aGUID); 
171     return aUAttrName;
172   }
173
174   return "";
175 }
176
177 SALOMEDS::GenericAttribute_ptr SALOMEDS_GenericAttribute_i::CreateAttribute(CORBA::ORB_ptr theOrb,
178                                                                             const Handle(TDF_Attribute)& theAttr) {
179
180   __ReturnCORBAAttribute(TDataStd_Real, AttributeReal);
181   __ReturnCORBAAttribute(TDataStd_Integer, AttributeInteger);
182   __ReturnCORBAAttribute(SALOMEDS_SequenceOfRealAttribute, AttributeSequenceOfReal);
183   __ReturnCORBAAttribute(SALOMEDS_SequenceOfIntegerAttribute, AttributeSequenceOfInteger);
184   __ReturnCORBAAttribute(TDataStd_Name, AttributeName);
185   __ReturnCORBAAttribute(TDataStd_Comment, AttributeComment);
186   __ReturnCORBAAttribute(SALOMEDS_IORAttribute, AttributeIOR);
187   __ReturnCORBAAttribute(SALOMEDS_PersRefAttribute, AttributePersistentRef);
188   __ReturnCORBAAttribute(SALOMEDS_DrawableAttribute, AttributeDrawable);
189   __ReturnCORBAAttribute(SALOMEDS_SelectableAttribute, AttributeSelectable);
190   __ReturnCORBAAttribute(SALOMEDS_ExpandableAttribute, AttributeExpandable);
191   __ReturnCORBAAttribute(SALOMEDS_OpenedAttribute, AttributeOpened);
192   __ReturnCORBAAttribute(SALOMEDS_TextColorAttribute, AttributeTextColor);
193   __ReturnCORBAAttribute(SALOMEDS_TextHighlightColorAttribute, AttributeTextHighlightColor);
194   __ReturnCORBAAttribute(SALOMEDS_PixMapAttribute, AttributePixMap);
195   __ReturnCORBAAttribute(SALOMEDS_LocalIDAttribute, AttributeLocalID);
196   __ReturnCORBAAttribute(SALOMEDS_TargetAttribute, AttributeTarget);
197   __ReturnCORBAAttribute(SALOMEDS_TableOfIntegerAttribute, AttributeTableOfInteger);
198   __ReturnCORBAAttribute(SALOMEDS_TableOfRealAttribute, AttributeTableOfReal);
199   __ReturnCORBAAttribute(SALOMEDS_TableOfStringAttribute, AttributeTableOfString);
200   __ReturnCORBAAttribute(SALOMEDS_StudyPropertiesAttribute, AttributeStudyProperties);
201   __ReturnCORBAAttribute(SALOMEDS_PythonObjectAttribute, AttributePythonObject);
202   __ReturnCORBAAttribute(SALOMEDS_ExternalFileDef, AttributeExternalFileDef);
203   __ReturnCORBAAttribute(SALOMEDS_FileType, AttributeFileType);
204   __ReturnCORBAAttribute(SALOMEDS_FlagsAttribute, AttributeFlags);
205   __ReturnCORBAAttribute(SALOMEDS_GraphicAttribute, AttributeGraphic);
206
207   Handle(TDataStd_TreeNode) aNode = Handle(TDataStd_TreeNode)::DownCast(theAttr);
208   if (!aNode.IsNull()) {
209     SALOMEDS_AttributeTreeNode_i* Attr = new SALOMEDS_AttributeTreeNode_i(aNode, theOrb); \
210     return Attr->AttributeTreeNode::_this(); \
211   }
212
213   Handle(TDataStd_UAttribute) aUAttr = Handle(TDataStd_UAttribute)::DownCast(theAttr);
214   if (!aUAttr.IsNull()) {
215     SALOMEDS_AttributeUserID_i* Attr = new SALOMEDS_AttributeUserID_i(aUAttr, theOrb); \
216     return Attr->AttributeUserID::_this(); \
217   }
218   return SALOMEDS::GenericAttribute::_nil();
219 }