GME  13
Attribute.cpp
Go to the documentation of this file.
00001 // Attribute.cpp: implementation of the CAttribute class.
00002 //
00004 
00005 #include "stdafx.h"
00006 #include "objectinspector.h"
00007 #include "ObjectInspectorCtl.h"
00008 
00009 #include "Attribute.h"
00010 
00011 #ifdef _DEBUG
00012 #undef THIS_FILE
00013 static char THIS_FILE[]=__FILE__;
00014 #define new DEBUG_NEW
00015 #endif
00016 
00017 enum project_attributes {PROJECT_AUTHOR, PROJECT_VERSION, PROJECT_COMMENT};
00018 TCHAR *project_attribute_tooltips[] = {
00019         _T("Author(s) of the project"),
00020         _T("Current version string"),
00021         _T("Arbitrary comments"),
00022 };
00023 
00024 
00026 // Construction/Destruction
00028 
00029 CAttribute::CAttribute()
00030 {
00031 
00032 }
00033 
00034 CAttribute::~CAttribute()
00035 {
00036 
00037 }
00038 
00039 // Must be in transaction
00040 void CAttribute::CreateList(const CMgaFCOPtrList& MgaFCOPtrList,CArray<CListItem,CListItem&> &ListItemArray)
00041 {
00042         m_MetaAttributePtrList.RemoveAll();
00043         ListItemArray.RemoveAll();
00044 
00045         if(MgaFCOPtrList.GetCount()==0)return;
00046 
00047         POSITION posFirst=MgaFCOPtrList.GetHeadPosition();
00048         POSITION posSecond=posFirst;
00049 
00050         // Getting the first FCO and update the position of the second
00051         CComPtr<IMgaFCO> ccpFirstFCO=MgaFCOPtrList.GetNext(posSecond);
00052 
00053         // Iterating through the FirstFCO's attributes
00054         CComPtr<IMgaAttributes> ccpMgaAttributes;
00055         COMTHROW(ccpFirstFCO->get_Attributes(&ccpMgaAttributes));
00056                         
00057         
00058         
00059         MGACOLL_ITERATE(IMgaAttribute, ccpMgaAttributes)
00060         {
00061                 CComPtr<IMgaMetaAttribute> ccpMetaAttribute;
00062                 COMTHROW(MGACOLL_ITER->get_Meta(&ccpMetaAttribute));
00063         
00064                 // Iterate through FCOs checking if they have the current attribute common
00065                 POSITION posIterate=posSecond;
00066                 
00067                         
00068                 bool bIsCommonAttr = isViewable( ccpFirstFCO, ccpMetaAttribute);
00069                 bool bIsDirty=false; //is attribute values different for attributes of same type?
00070                 
00071                 // Getting the value of the first attribute
00072                 CComVariant ccvtFirstValue;
00073                 COMTHROW(MGACOLL_ITER->get_Value(&ccvtFirstValue));
00074                 
00075                 // Check whether it is a default value (if it is defined in the meta it's default)
00076                 long lStatus;
00077                 COMTHROW(MGACOLL_ITER->get_Status(&lStatus));
00078                 bool bIsDefault=(lStatus==-1?true:false);
00079 
00080                 while(posIterate)
00081                 {
00082                         const CComPtr<IMgaFCO>ccpCurrFCO=MgaFCOPtrList.GetNext(posIterate);
00083 
00084                         CComPtr<IMgaAttribute> ccpCurrAttribute;
00085 
00086                         HRESULT hrRes=ccpCurrFCO->get_Attribute(ccpMetaAttribute,&ccpCurrAttribute);
00087                         
00088                         
00089                         if(hrRes==E_META_INVALIDATTR)
00090                         {
00091                                 bIsCommonAttr=false;
00092                                 break;
00093                         }
00094                         else
00095                         {
00096                                 COMTHROW(hrRes);
00097                         }
00098 
00099                         if( !(bIsCommonAttr = isViewable( ccpCurrFCO, ccpMetaAttribute)))
00100                         {
00101                                 break;
00102                         }
00103 
00104                         COMTHROW(ccpCurrAttribute->get_Status(&lStatus));
00105                         
00106                         // Check whether it is a default value (if it is defined in the meta it's default)
00107                         bIsDefault&=(lStatus==-1?true:false);
00108 
00109                         
00110                         // Check whether the value is the same for all
00111                         CComVariant ccvtCurrValue;
00112                         COMTHROW(ccpCurrAttribute->get_Value(&ccvtCurrValue));
00113                         if(ccvtCurrValue!=ccvtFirstValue)
00114                         {
00115                                 bIsDirty=true;
00116                         }
00117 
00118                 }
00119 
00120                 if(bIsCommonAttr)
00121                 {
00122                         POSITION posCurrent=m_MetaAttributePtrList.AddTail(ADAPT_META_ATTRIBUTE(ccpMetaAttribute));
00123                         CListItem ListItem;
00124 
00125                         ASSERT(sizeof(POSITION)==sizeof(DWORD)); // Unusual conversion - let us check it
00126                         if(CreateListItem(ListItem,ccvtFirstValue,bIsDirty,bIsDefault,DWORD(posCurrent)))
00127                         {
00128                                 ListItemArray.Add(ListItem);
00129                         }
00130                 }
00131 
00132         }MGACOLL_ITERATE_END;
00133 
00134 
00135 
00136 }
00137 
00138 // Must be in transaction
00139 void CAttribute::CreateList(CComPtr<IMgaProject> ccpProject, CArray<CListItem,CListItem&> &ListItemArray)
00140 {
00141         m_MetaAttributePtrList.RemoveAll();
00142         ListItemArray.RemoveAll();
00143 
00144         if(ccpProject == NULL) return;
00145 
00146 
00147         {
00148                 CListItem ListItem;
00149 
00150                 ListItem.strName = _T("Author");
00151                 ListItem.dwKeyValue = PROJECT_AUTHOR;
00152                 ListItem.bIsDefault = false;
00153                 ListItem.bIsDifferentValue = false;
00154                 ListItem.strToolTip = project_attribute_tooltips[PROJECT_AUTHOR];
00155 
00156                 CComBSTR bstr;
00157                 COMTHROW(ccpProject->get_Author(&bstr));
00158                 CString strValue(bstr);
00159                 ListItem.Value.SetStringValue(strValue);
00160 
00161                 ListItem.DefValue.SetStringValue(_T(""));
00162                 
00163                 ListItemArray.Add(ListItem);
00164         }
00165 
00166         {
00167                 CListItem ListItem;
00168 
00169                 ListItem.strName = _T("Version");
00170                 ListItem.dwKeyValue = PROJECT_VERSION;
00171                 ListItem.bIsDefault = false;
00172                 ListItem.bIsDifferentValue = false;
00173                 ListItem.strToolTip = project_attribute_tooltips[PROJECT_VERSION];
00174 
00175                 CComBSTR bstr;
00176                 COMTHROW(ccpProject->get_Version(&bstr));
00177                 CString strValue(bstr);
00178                 ListItem.Value.SetStringValue(strValue);
00179 
00180                 ListItem.DefValue.SetStringValue(_T(""));
00181                 
00182                 ListItemArray.Add(ListItem);
00183         }
00184 
00185         {
00186                 CListItem ListItem;
00187 
00188                 ListItem.strName = _T("Comment");
00189                 ListItem.dwKeyValue = PROJECT_COMMENT;
00190                 ListItem.bIsDefault = false;
00191                 ListItem.bIsDifferentValue = false;
00192                 ListItem.strToolTip = project_attribute_tooltips[PROJECT_COMMENT];
00193 
00194                 // Setting value
00195                 CComBSTR bstr;
00196                 COMTHROW(ccpProject->get_Comment(&bstr));
00197                 CString strValue(bstr);
00198                 CStringArray strValueArray;
00199                 str_split(strValue, strValueArray);
00200                 ListItem.Value.SetStringValue(strValueArray,6);
00201 
00202                 // Setting default value
00203                 CString strDefValue = _T("");
00204                 CStringArray strDefValueArray;
00205                 strDefValueArray.Add(strDefValue);
00206                 ListItem.DefValue.SetStringValue(strDefValueArray,6);
00207                 
00208                 ListItemArray.Add(ListItem);
00209         }
00210 
00211 
00212 }
00213 
00214 //
00215 // We decide if a model attribute is shown based on the model's first aspect
00216 // (currently for all aspects the same attribute list is used)
00217 bool CAttribute::isViewable( const CComPtr<IMgaFCO>& fco, const CComPtr<IMgaMetaAttribute>& attr_meta) const
00218 {
00219         VARIANT_BOOL vbl = VARIANT_TRUE;
00220         COMTHROW( attr_meta->get_Viewable( &vbl));
00221         return vbl == VARIANT_TRUE;
00222         // attribute hiding policy changed by introducing 'viewable' attribute
00223         //  for IMgaMetaAttribute. Section below commented out:
00224         /*
00225         bool res = true;
00226         objtype_enum objtype;
00227         COMTHROW( fco->get_ObjType( &objtype) );
00228         if( objtype == OBJTYPE_MODEL)
00229         {
00230                 res = false;
00231                 CComPtr<IMgaMetaFCO> meta;
00232                 CComPtr<IMgaMetaModel> model_meta;
00233                 CComPtr<IMgaMetaAspects> asps;
00234                 CComPtr<IMgaMetaAttributes> shown_attrs;
00235                 metaref_type req_metaid;
00236 
00237                 COMTHROW( fco->get_Meta( &meta));
00238                 COMTHROW( meta.QueryInterface( &model_meta));
00239                 COMTHROW( model_meta->get_Aspects( &asps ));
00240                 COMTHROW( attr_meta->get_MetaRef( &req_metaid));
00241                 
00242                 MGACOLL_ITERATE(IMgaMetaAspect, asps) {
00243                         COMTHROW( MGACOLL_ITER->get_Attributes( &shown_attrs));
00244                         break;
00245                 }
00246                 MGACOLL_ITERATE_END;
00247 
00248 
00249                 MGACOLL_ITERATE(IMgaMetaAttribute, shown_attrs) {
00250                         metaref_type metaid;
00251                         COMTHROW(MGACOLL_ITER->get_MetaRef(&metaid));
00252 
00253                         if( metaid == req_metaid)
00254                         {
00255                                 res = true;
00256                                 break;
00257                         }
00258                         
00259                 }
00260                 MGACOLL_ITERATE_END;
00261         }
00262         return res;
00263         */
00264 }
00265 
00266 bool CAttribute::CreateListItem(CListItem &ListItem, const CComVariant &ccvtValue, bool bIsDirty, bool bIsDefault, DWORD dwKey)
00267 {
00268         
00269         CComPtr<IMgaMetaAttribute> ccpMetaAttribute=m_MetaAttributePtrList.GetAt(POSITION(dwKey));
00270         
00271         // Getting displayed name
00272         CComBSTR bstrName;
00273         COMTHROW(ccpMetaAttribute->get_DisplayedName(&bstrName));
00274 
00275         // Getting default value
00276         CComVariant ccvtDefValue;
00277         COMTHROW(ccpMetaAttribute->get_DefaultValue(&ccvtDefValue));
00278         
00279         ListItem.strName=bstrName;
00280         ListItem.dwKeyValue=dwKey;
00281         ListItem.bIsDefault=bIsDefault;
00282         ListItem.bIsDifferentValue=bIsDirty;
00283 
00284         // Getting helpText (Undocumented Feature - hopefully coming soon in the MetaInterpreter)
00285         CComBSTR bstrHelpTextPref;
00286         CComBSTR bstrHelpTextPrefPath = ATTRIBUTE_HELP_REGISTRY_PATH;
00287         COMTHROW(ccpMetaAttribute->get_RegistryValue(bstrHelpTextPrefPath,&bstrHelpTextPref));
00288         CString strHelpText=bstrHelpTextPref;
00289         ListItem.strToolTip=strHelpText;
00290 
00291         // Getting content-type (stored in the registry like 'helpText')
00292         CComBSTR bstrContTypePref;
00293         CComBSTR bstrContTypePrefPath = ATTRIBUTE_CONTENTTYPE_REGISTRY_PATH;
00294         COMTHROW(ccpMetaAttribute->get_RegistryValue(bstrContTypePrefPath,&bstrContTypePref));
00295         CString strContentType=bstrContTypePref;
00296         ListItem.strContentType=strContentType;
00297 
00298         // Getting attribute type
00299         attval_enum aeAttributeType;
00300         COMTHROW(ccpMetaAttribute->get_ValueType(&aeAttributeType));
00301 
00302         switch(aeAttributeType)
00303         {
00304                 case ATTVAL_STRING:
00305                         {
00306                                 // Do not trust in VARIANT and MS.
00307                                 ASSERT(ccvtValue.vt==VT_BSTR);
00308                                 ASSERT(ccvtDefValue.vt==VT_BSTR);
00309                         
00310                                 // Checking if it is multiline - reading it from registry       
00311                                 CComBSTR bstrMultiLinePref;
00312                                 CComBSTR bstrMultiLinePrefPath = ATTRIBUTE_MULTILINE_PREFERENCE_REGISTRY_PATH;
00313                                 COMTHROW(ccpMetaAttribute->get_RegistryValue(bstrMultiLinePrefPath,&bstrMultiLinePref));
00314                                 
00315                                 // Converting to int
00316                                 CString strLineNum(bstrMultiLinePref);
00317                                 int nLineNumMin = _tstoi(strLineNum);
00318                                 
00319                                 if(nLineNumMin<2) // Single line edit box
00320                                 {
00321                                         CString strValue=CComBSTR(ccvtValue.bstrVal);
00322                                         ListItem.Value.SetStringValue(strValue);
00323 
00324                                         CString strDefValue=CComBSTR(ccvtDefValue.bstrVal);
00325                                         ListItem.DefValue.SetStringValue(strDefValue);
00326                                 }
00327                                 else               // Multiline edit box
00328                                 {
00329 
00330                                         // Setting value
00331                                         CString strValue=CComBSTR(ccvtValue.bstrVal);
00332                                         CStringArray strValueArray;
00333                                         str_split(strValue, strValueArray);
00334                                         int nLineNum = max(nLineNumMin, strValueArray.GetCount());
00335                                         ListItem.Value.SetStringValue(strValueArray, nLineNum);
00336 
00337                                         // Setting default value
00338                                         CString strDefValue=CComBSTR(ccvtDefValue.bstrVal);
00339                                         CStringArray strDefValueArray;
00340                                         strDefValueArray.Add(strDefValue);
00341                                         nLineNum = max(nLineNum, max(nLineNumMin, strDefValueArray.GetCount()));
00342                                         ListItem.DefValue.SetStringValue(strDefValueArray, nLineNum);
00343 
00344                                 }
00345 
00346                         }break;
00347                 
00348                 case ATTVAL_INTEGER:
00349                         {
00350                                 // Do not trust in VARIANT and MS.
00351                                 ASSERT(ccvtValue.vt==VT_I4);
00352                                 ASSERT(ccvtDefValue.vt==VT_I4);
00353 
00354                                 ListItem.Value.SetIntValue(ccvtValue.intVal);
00355                                 ListItem.DefValue.SetIntValue(ccvtDefValue.intVal);
00356                         }break;
00357                 
00358                 case ATTVAL_DOUBLE:
00359                         {
00360                                 // Do not trust in VARIANT and MS.
00361                                 ASSERT(ccvtValue.vt==VT_R8);
00362                                 ASSERT(ccvtDefValue.vt==VT_R8);
00363                                 
00364                                 ListItem.Value.SetDoubleValue(ccvtValue.dblVal);
00365                                 ListItem.DefValue.SetDoubleValue(ccvtDefValue.dblVal);
00366                         }break;
00367 
00368                 case ATTVAL_BOOLEAN:
00369                         {
00370                                 // Do not trust in VARIANT and MS.
00371                                 ASSERT(ccvtValue.vt==VT_BOOL);
00372                                 ASSERT(ccvtDefValue.vt==VT_BOOL);
00373 
00374                                 ListItem.Value.SetBoolValue(ccvtValue.boolVal!=FALSE);
00375                                 ListItem.DefValue.SetBoolValue(ccvtDefValue.boolVal!=FALSE);
00376 
00377                         }break;
00378         
00379                 case ATTVAL_ENUM:
00380                         {
00381                                 // Do not trust in VARIANT and MS.
00382                                 ASSERT(ccvtValue.vt==VT_BSTR);
00383                                 ASSERT(ccvtDefValue.vt==VT_BSTR);
00384 
00385                                 CComBSTR bstrValue(ccvtValue.bstrVal);
00386                                 CComBSTR bstrDefValue(ccvtDefValue.bstrVal);
00387 
00388                                 CString strValue=bstrValue;
00389                                 CString strDefValue=bstrDefValue;
00390 
00391 
00392                                 // Getting enum attributes from meta 
00393                                 CComPtr<IMgaMetaEnumItems> ccpMgaEnumItems;
00394                                 COMTHROW(ccpMetaAttribute->get_EnumItems(&ccpMgaEnumItems));
00395 
00396 
00397 
00398                                 int nSelItem=-1;
00399                                 int nSelDefaultItem=-1;
00400                                 int i=0;
00401                                 CStringArray strItemLabelArray;
00402                                 CStringArray strItemNameArray;
00403 
00404                                 MGACOLL_ITERATE(IMgaMetaEnumItem,ccpMgaEnumItems) 
00405                                 {
00406                                         // Getting item labels
00407                                         CComBSTR bstrItemLabel;
00408                                         COMTHROW(MGACOLL_ITER->get_DisplayedName(&bstrItemLabel));
00409                                         CString strItemLabel(bstrItemLabel);
00410                                         strItemLabelArray.Add(strItemLabel);
00411 
00412                                         CComBSTR bstrItemValue;
00413                                         COMTHROW(MGACOLL_ITER->get_Value(&bstrItemValue));
00414                                         CString strItemValue(bstrItemValue);
00415                                         strItemNameArray.Add(strItemValue);
00416 
00417                                         if(strItemValue==strValue)
00418                                         {
00419                                                 nSelItem=i;
00420                                         }
00421 
00422                                         if(strItemValue==strDefValue)
00423                                         {
00424                                                 nSelDefaultItem=i;
00425                                         }
00426 
00427                                         i++;
00428                                 }
00429                                 MGACOLL_ITERATE_END;
00430 
00431                                 // Selected and default item must be among the fixed list elements
00432                                 
00433                                 ASSERT(nSelItem>-1);
00434                                 ASSERT(nSelDefaultItem>-1);
00435                                 ASSERT(nSelItem<=strItemLabelArray.GetSize());
00436                                 ASSERT(nSelDefaultItem<=strItemLabelArray.GetSize());
00437                                 
00438 
00439                                 ListItem.Value.SetListValue(strItemLabelArray,strItemNameArray,nSelItem);
00440                                 ListItem.DefValue.SetListValue(strItemLabelArray,strItemNameArray, nSelDefaultItem);
00441                         
00442                         }break;
00443 
00444                 case ATTVAL_REFERENCE: // Not implemented !!!
00445                         {
00446                                 return false;
00447                         }break;
00448 
00449                 case ATTVAL_DYNAMIC:   // Not implemented !!!
00450                         {
00451                                 return false;
00452                         }break;
00453 
00454                 default:
00455                         {
00456                                 // Unknown MGA attribute type
00457                                 ASSERT(false);
00458                         }
00459         }
00460         return true;
00461 
00462 }
00463 
00464 void CAttribute::WriteItemToMga(CListItem ListItem,const CMgaFCOPtrList& MgaFCOPtrList)
00465 {
00466 
00467 
00468         POSITION posMetaAttribute=(POSITION)ListItem.dwKeyValue;
00469         CComPtr<IMgaMetaAttribute> ccpMetaAttribute=m_MetaAttributePtrList.GetAt(posMetaAttribute);
00470 
00471 
00472         CComBSTR bstrAttrName;
00473         COMTHROW(ccpMetaAttribute->get_Name(&bstrAttrName));
00474 
00475         POSITION posFCOPtrList=MgaFCOPtrList.GetHeadPosition();
00476         
00477         while(posFCOPtrList)
00478         {
00479                 CComPtr<IMgaFCO> ccpCurrFCO=MgaFCOPtrList.GetNext(posFCOPtrList);
00480 
00481                 
00482                 if(ListItem.bIsDefault)
00483                 {
00484                         COMTHROW(ccpCurrFCO->ClearAttrByName(bstrAttrName));
00485                 }
00486                 else
00487                 {
00488                         switch(ListItem.Value.dataType)
00489                         {
00490                                 case ITEMDATA_STRING:
00491                                 {       
00492                                         CString strValue;
00493                                         ListItem.Value.toString(strValue);
00494 
00495                                         strValue.Replace(_T("\r\n"), _T("\n"));
00496 
00497                                         ccpCurrFCO->__SetStrAttrByNameDisp(static_cast<const BSTR>(bstrAttrName), _bstr_t(strValue));                                   
00498                                 }break;
00499                         
00500                                 case ITEMDATA_FIXED_LIST:
00501                                 {                       
00502                                         CString strValue;
00503                                         strValue=ListItem.Value.stringListNames.GetAt(ListItem.Value.listVal);
00504                                         CComBSTR bstrValue(strValue);
00505                                         COMTHROW(ccpCurrFCO->put_StrAttrByName(bstrAttrName,bstrValue));
00506                                 }break;
00507 
00508                                 case ITEMDATA_INTEGER:
00509                                         {
00510                                                 ccpCurrFCO->IntAttrByName[static_cast<const BSTR>(bstrAttrName)] = ListItem.Value.intVal;
00511                                         }break;
00512 
00513                                 case ITEMDATA_DOUBLE:
00514                                         {
00515                                                 ccpCurrFCO->FloatAttrByName[static_cast<const BSTR>(bstrAttrName)] = ListItem.Value.doubleVal;
00516                                         }break;
00517 
00518                                 case ITEMDATA_BOOLEAN:
00519                                         {
00520                                                 ccpCurrFCO->BoolAttrByName[static_cast<const BSTR>(bstrAttrName)] = ListItem.Value.boolVal ? VARIANT_TRUE : VARIANT_FALSE;
00521                                         }break;
00522 
00523                                 default:
00524                                         {
00525                                                 // Invalid ItemData type found here
00526                                                 ASSERT(0);
00527                                         }
00528                         }                                       
00529                 }
00530         }       
00531 }
00532 
00533 void CAttribute::WriteItemToMga(CListItem ListItem, CComPtr<IMgaProject> ccpProject)
00534 {
00535         CString strValue;
00536         ListItem.Value.toString(strValue);
00537         strValue.Replace(_T("\r\n"), _T("\n"));
00538         CComBSTR bstrValue(strValue);
00539 
00540         switch (ListItem.dwKeyValue) {
00541         case PROJECT_AUTHOR:
00542                 COMTHROW(ccpProject->put_Author(bstrValue));
00543                 break;
00544         case PROJECT_VERSION:
00545                 COMTHROW(ccpProject->put_Version(bstrValue));
00546                 break;
00547         case PROJECT_COMMENT:
00548                 COMTHROW(ccpProject->put_Comment(bstrValue));
00549                 break;
00550         default:
00551                 break;
00552         }
00553 }