Salome HOME
DCQ : Merge with Ecole_ete_a6.
[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 using namespace std;
13
14 #include <TDataStd_TreeNode.hxx>
15 #include <TDataStd_UAttribute.hxx>
16
17 #include "SALOMEDS_AttributeDrawable_i.hxx"
18 #include "SALOMEDS_AttributeSelectable_i.hxx"
19 #include "SALOMEDS_AttributeOpened_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_TextColorAttribute.hxx"
46 #include "SALOMEDS_TextHighlightColorAttribute.hxx"
47 #include "SALOMEDS_PixMapAttribute.hxx"
48 #include "SALOMEDS_LocalIDAttribute.hxx"
49 #include "SALOMEDS_PythonObjectAttribute.hxx"
50 #include "SALOMEDS_TargetAttribute.hxx"
51 #include "SALOMEDS_StudyPropertiesAttribute.hxx"
52 #include "SALOMEDS_TableOfIntegerAttribute.hxx"
53 #include "SALOMEDS_TableOfRealAttribute.hxx"
54 #include "SALOMEDS_TableOfStringAttribute.hxx"
55 #include "SALOMEDS_ExternalFileDef.hxx"
56 #include "SALOMEDS_FileType.hxx"
57 #include "Utils_ExceptHandlers.hxx"
58 #define RegisteredAttributes 26
59
60 UNEXPECT_CATCH(GALockProtection, SALOMEDS::GenericAttribute::LockProtection);
61
62 const char AttributesTypeNames[RegisteredAttributes][30] = {
63   "AttributeReal", "AttributeInteger",
64   "AttributeSequenceOfReal", "AttributeSequenceOfInteger",
65   "AttributeName", "AttributeComment",
66   "AttributeIOR", "AttributePersistentRef",
67   "AttributeDrawable", "AttributeSelectable",
68   "AttributeExpandable", "AttributeOpened",
69   "AttributeTextColor", "AttributeTextHighlightColor", "AttributePixMap",
70   "AttributeTreeNode", "AttributeLocalID",
71   "AttributeTarget",
72   "AttributeTableOfInteger", "AttributeTableOfReal", "AttributeTableOfString",
73   "AttributeStudyProperties",
74   "AttributePythonObject",
75   "AttributeUserID",
76   "AttributeExternalFileDef", "AttributeFileType"
77 };
78
79 const Standard_GUID AttributesGUIDs[RegisteredAttributes] = {
80   TDataStd_Real::GetID(), TDataStd_Integer::GetID(),
81   SALOMEDS_SequenceOfRealAttribute::GetID(), SALOMEDS_SequenceOfIntegerAttribute::GetID(),
82   TDataStd_Name::GetID(), TDataStd_Comment::GetID(),
83   SALOMEDS_IORAttribute::GetID(), SALOMEDS_PersRefAttribute::GetID(),
84   SALOMEDS_DrawableAttribute::GetID(), SALOMEDS_SelectableAttribute::GetID(),
85   SALOMEDS_ExpandableAttribute::GetID(), SALOMEDS_OpenedAttribute::GetID(),
86   SALOMEDS_TextColorAttribute::GetID(), SALOMEDS_TextHighlightColorAttribute::GetID(), SALOMEDS_PixMapAttribute::GetID(),
87   TDataStd_TreeNode::GetDefaultTreeID(), SALOMEDS_LocalIDAttribute::GetID(),
88   SALOMEDS_TargetAttribute::GetID(),
89   SALOMEDS_TableOfIntegerAttribute::GetID(), SALOMEDS_TableOfRealAttribute::GetID(), SALOMEDS_TableOfStringAttribute::GetID(),
90   SALOMEDS_StudyPropertiesAttribute::GetID(),
91   SALOMEDS_PythonObjectAttribute::GetID(),
92   Standard_GUID("FFFFFFFF-D9CD-11d6-945D-1050DA506788"),
93   SALOMEDS_ExternalFileDef::GetID(), SALOMEDS_FileType::GetID()
94 };
95
96 void SALOMEDS_GenericAttribute_i::CheckLocked() throw (SALOMEDS::GenericAttribute::LockProtection) {
97   Unexpect aCatch(GALockProtection);
98   if (!_myAttr.IsNull() && _myAttr->IsValid() && !CORBA::is_nil(_myOrb)) {
99     Handle(SALOMEDS_IORAttribute) Att;
100     if (_myAttr->Label().Root().FindAttribute(SALOMEDS_IORAttribute::GetID(),Att)){
101       TCollection_AsciiString ch(Att->Get());
102       char* IOR = CORBA::string_dup(ch.ToCString());
103       CORBA::Object_var obj = _myOrb->string_to_object(IOR);
104       SALOMEDS::Study_ptr aStudy = SALOMEDS::Study::_narrow(obj);
105 //      ASSERT(!CORBA::is_nil(aStudy));
106       if (aStudy->NewBuilder()->HasOpenCommand()) return;
107       if (aStudy->GetProperties()->IsLocked()) {
108 //        MESSAGE("Locked document modification !!!");
109         throw SALOMEDS::GenericAttribute::LockProtection();
110       }
111     }
112   }
113 }
114
115 SALOMEDS::SObject_ptr SALOMEDS_GenericAttribute_i::GetSObject() {
116   if (_myAttr.IsNull() || _myAttr->Label().IsNull()) return SALOMEDS::SObject::_nil();
117   SALOMEDS_SObject_i *  so_servant = new SALOMEDS_SObject_i (_myAttr->Label(),_myOrb);
118   SALOMEDS::SObject_var so = SALOMEDS::SObject::_narrow(so_servant->_this()); 
119   return so._retn();
120 }
121
122 Standard_GUID SALOMEDS_GenericAttribute_i::GetGUID(const char* theType) {
123   Standard_Integer i;
124   for(i = 0; i < RegisteredAttributes; i++) {
125     if (strcmp(AttributesTypeNames[i], theType) == 0) return AttributesGUIDs[i];
126   }
127
128   if (strncmp(theType, "AttributeTreeNodeGUID",21) == 0) {
129     char* aGUIDString = new char[40];
130     sprintf(aGUIDString, &(theType[21]));
131     Standard_GUID aGUID = Standard_GUID(aGUIDString); // create tree node GUID by name
132     delete(aGUIDString);
133     return aGUID;
134   }
135   return Standard_GUID();
136 }
137
138 char* SALOMEDS_GenericAttribute_i::Type() {
139   if (_myAttr.IsNull()) return "";
140   int i;
141   for(i = 0; i < RegisteredAttributes; i++) {
142     if (_myAttr->ID() == AttributesGUIDs[i]) {
143       CORBA::String_var aString(AttributesTypeNames[i]);
144       return aString._retn();
145     }
146   }
147   Handle(TDataStd_TreeNode) aNode = Handle(TDataStd_TreeNode)::DownCast(_myAttr);
148   if (!aNode.IsNull()) {
149     char* aNodeName = new char[60];
150     char aGUID[40];
151     aNode->ID().ToCString(aGUID);
152     sprintf(aNodeName, "AttributeTreeNodeGUID%s",aGUID);
153     return aNodeName;
154   }
155
156   Handle(TDataStd_UAttribute) aUAttr = Handle(TDataStd_UAttribute)::DownCast(_myAttr);
157   if (!aUAttr.IsNull()) {
158     char* aUAttrName = new char[60];
159     char aGUID[40];
160     aUAttr->ID().ToCString(aGUID);
161     sprintf(aUAttrName, "AttributeUserID_%s",aGUID); 
162     return aUAttrName;
163   }
164
165   return "";
166 }
167
168 SALOMEDS::GenericAttribute_ptr SALOMEDS_GenericAttribute_i::CreateAttribute(CORBA::ORB_ptr theOrb,
169                                                                             const Handle(TDF_Attribute)& theAttr) {
170
171   __ReturnCORBAAttribute(TDataStd_Real, AttributeReal);
172   __ReturnCORBAAttribute(TDataStd_Integer, AttributeInteger);
173   __ReturnCORBAAttribute(SALOMEDS_SequenceOfRealAttribute, AttributeSequenceOfReal);
174   __ReturnCORBAAttribute(SALOMEDS_SequenceOfIntegerAttribute, AttributeSequenceOfInteger);
175   __ReturnCORBAAttribute(TDataStd_Name, AttributeName);
176   __ReturnCORBAAttribute(TDataStd_Comment, AttributeComment);
177   __ReturnCORBAAttribute(SALOMEDS_IORAttribute, AttributeIOR);
178   __ReturnCORBAAttribute(SALOMEDS_PersRefAttribute, AttributePersistentRef);
179   __ReturnCORBAAttribute(SALOMEDS_DrawableAttribute, AttributeDrawable);
180   __ReturnCORBAAttribute(SALOMEDS_SelectableAttribute, AttributeSelectable);
181   __ReturnCORBAAttribute(SALOMEDS_ExpandableAttribute, AttributeExpandable);
182   __ReturnCORBAAttribute(SALOMEDS_OpenedAttribute, AttributeOpened);
183   __ReturnCORBAAttribute(SALOMEDS_TextColorAttribute, AttributeTextColor);
184   __ReturnCORBAAttribute(SALOMEDS_TextHighlightColorAttribute, AttributeTextHighlightColor);
185   __ReturnCORBAAttribute(SALOMEDS_PixMapAttribute, AttributePixMap);
186   __ReturnCORBAAttribute(SALOMEDS_LocalIDAttribute, AttributeLocalID);
187   __ReturnCORBAAttribute(SALOMEDS_TargetAttribute, AttributeTarget);
188   __ReturnCORBAAttribute(SALOMEDS_TableOfIntegerAttribute, AttributeTableOfInteger);
189   __ReturnCORBAAttribute(SALOMEDS_TableOfRealAttribute, AttributeTableOfReal);
190   __ReturnCORBAAttribute(SALOMEDS_TableOfStringAttribute, AttributeTableOfString);
191   __ReturnCORBAAttribute(SALOMEDS_StudyPropertiesAttribute, AttributeStudyProperties);
192   __ReturnCORBAAttribute(SALOMEDS_PythonObjectAttribute, AttributePythonObject);
193   __ReturnCORBAAttribute(SALOMEDS_ExternalFileDef, AttributeExternalFileDef);
194   __ReturnCORBAAttribute(SALOMEDS_FileType, AttributeFileType);
195
196   Handle(TDataStd_TreeNode) aNode = Handle(TDataStd_TreeNode)::DownCast(theAttr);
197   if (!aNode.IsNull()) {
198     SALOMEDS_AttributeTreeNode_i* Attr = new SALOMEDS_AttributeTreeNode_i(aNode, theOrb); \
199     return Attr->AttributeTreeNode::_this(); \
200   }
201
202   Handle(TDataStd_UAttribute) aUAttr = Handle(TDataStd_UAttribute)::DownCast(theAttr);
203   if (!aUAttr.IsNull()) {
204     SALOMEDS_AttributeUserID_i* Attr = new SALOMEDS_AttributeUserID_i(aUAttr, theOrb); \
205     return Attr->AttributeUserID::_this(); \
206   }
207   return SALOMEDS::GenericAttribute::_nil();
208 }