Development/Cpp/Helper/ReflectionHelper

From Apache OpenOffice Wiki
< Development‎ | Cpp‎ | Helper
Revision as of 10:07, 11 April 2009 by SergeMoutou (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Here is the complete hpp File :

  1. include <rtl/ustring.hxx>
  2. include <com/sun/star/uno/Sequence.hxx>
  3. include <com/sun/star/uno/Any.hxx>
  4. include <com/sun/star/lang/XMultiServiceFactory.hpp>
  5. include <com/sun/star/beans/XIntrospection.hpp>
  6. include <com/sun/star/beans/XIntrospectionAccess.hpp>
  7. include <com/sun/star/lang/XTypeProvider.hpp>
  8. include <com/sun/star/beans/MethodConcept.hpp>
  9. include <com/sun/star/reflection/XIdlMethod.hpp>
  10. include <com/sun/star/uno/Type.hxx>
  11. include <com/sun/star/beans/Property.hpp>
  12. include <com/sun/star/lang/XServiceInfo.hpp>
  13. include <com/sun/star/reflection/ParamMode.hpp>

using rtl::OUString; using namespace com::sun::star::reflection; using namespace com::sun::star::beans; using namespace com::sun::star::lang; using namespace com::sun::star::uno;

class ReflectionHelper { public: ReflectionHelper(Any any,Reference< XMultiServiceFactory > oSVM);

Sequence < OUString > getMethods(), getTypes(), getServices(), getPropertiesWithoutValues(), getPropertiesWithValues(); // Print out void printOut();

private: Any toInspect; Reference< XMultiServiceFactory > xServiceManager; Reference< XIntrospection >xIntrospection; Reference< XIntrospectionAccess > xIntrospec; Reference< XTypeProvider > xTypeProvider; OUString getValueName(Any object); OUString getParamMode(ParamMode paramMode); // methods Sequence< Reference< XIdlMethod > > mMethods; // Interfaces Sequence< Type > types; // Services Reference< XServiceInfo > xServiceInfo; // Sequence< OUString > services; // Properties Sequence< Property > Properties; }; And now the complete cpp implementation : // C++ // Serge Moutou // with help of <OpenOffice1.1_SDK>/examples/java/Inspector // and Bernard Marcelly XRay tool // version 0.1 (22 Dec 2004) // version 0.2 (4 Jun 2005) added printout in a dialog // To do : Exception Handling, to go further with properties values

  1. include "/home/smoutou/OpenOffice.org1.1_SDK/examples/DevelopersGuide/ProfUNO/CppBinding/ReflectionHelper.hpp"
  2. include <com/sun/star/reflection/XIdlClass.hpp>
  3. include <com/sun/star/beans/PropertyConcept.hpp>
  4. include <com/sun/star/beans/XPropertySet.hpp>

//#include <com/sun/star/reflection/ParamMode.hpp> done in ReflectionHelper.hpp

  1. include <com/sun/star/container/XNameContainer.hpp>
  2. include <cppuhelper/implbase1.hxx>
  3. include <com/sun/star/awt/XTextComponent.hpp>
  4. include <com/sun/star/awt/XActionListener.hpp>
  5. include <com/sun/star/awt/XToolkit.hpp>
  6. include <com/sun/star/awt/XControlContainer.hpp>
  7. include <com/sun/star/awt/PushButtonType.hpp>
  8. include <com/sun/star/awt/XButton.hpp>
  9. include <com/sun/star/awt/XWindow.hpp>
  10. include <com/sun/star/awt/XDialog.hpp>

//#include <com/sun/star/lang/XMultiServiceFactory.hpp>

//#define DEBUG

using namespace com::sun::star::awt; //using namespace com::sun::star::frame; using namespace com::sun::star::lang; using namespace com::sun::star::container;

// Don't forget the #include <cppuhelper/implbase1.hxx> typedef ::cppu::WeakImplHelper1< ::com::sun::star::awt::XActionListener > ActionListenerHelper; /** action listener

  • /

class ActionListenerImpl : public ActionListenerHelper { private : sal_Int32 _nCounts; Reference< XControlContainer > _xControlCont; //XControlContainer _xControlCont; ReflectionHelper *refHelp; Sequence <OUString> methods,properties,services,types;

public :

ActionListenerImpl(const Reference< XControlContainer >& xControlCont,ReflectionHelper *rHelper) { _xControlCont = xControlCont; _nCounts = 0; refHelp = rHelper; methods = refHelp->getMethods(); properties = refHelp->getPropertiesWithValues(); services = refHelp->getServices(); types = refHelp->getTypes(); }

// XEventListener

virtual void SAL_CALL disposing (const com::sun::star::lang::EventObject& aEventObj ) throw(::com::sun::star::uno::RuntimeException) {

// _xControlCont = NULL;

  1. ifdef DEBUG

printf("object listened to will be disposed\n");

  1. endif

}

// XActionListener virtual void SAL_CALL actionPerformed (const ::com::sun::star::awt::ActionEvent& rEvent ) throw ( RuntimeException) { OUString OUStr; // set label text Reference< XControl > label = _xControlCont->getControl(OUString::createFromAscii("Label1"));

// Don't forget to add : #include <com/sun/star/awt/XTextComponent.hpp> // Don't forget to add "com.sun.star.awt.XTextComponent \" in the makefile Reference< XTextComponent > xLabel(label,UNO_QUERY); switch (_nCounts%4){ case 0 : for (int i=0;i<methods.getLength();i++){ if(i==0) xLabel->setText(methods[i] + OUString::createFromAscii("\n"));else xLabel->insertText (xLabel->getSelection(), methods[i] + OUString::createFromAscii("\n")); } xLabel->insertText (xLabel->getSelection(), OUString::createFromAscii("******** Methods : ") + OUStr.valueOf((sal_Int32)methods.getLength())+ OUString::createFromAscii("\n")); break; case 1 :for (int i=0;i<properties.getLength();i++){ if(i==0) xLabel->setText(properties[i] + OUString::createFromAscii("\n"));else xLabel->insertText (xLabel->getSelection(), properties[i] + OUString::createFromAscii("\n")); } xLabel->insertText (xLabel->getSelection(), OUString::createFromAscii("******** Properties : ") + OUStr.valueOf((sal_Int32)properties.getLength())+ OUString::createFromAscii("\n")); break; case 2 :for (int i=0;i<services.getLength();i++){ if(i==0) xLabel->setText(services[i] + OUString::createFromAscii("\n"));else xLabel->insertText (xLabel->getSelection(), services[i] + OUString::createFromAscii("\n")); } xLabel->insertText (xLabel->getSelection(), OUString::createFromAscii("******** Services : ") + OUStr.valueOf((sal_Int32)services.getLength())+ OUString::createFromAscii("\n")); break; case 3 :for (int i=0;i<types.getLength();i++){ if(i==0) xLabel->setText(types[i] + OUString::createFromAscii("\n"));else xLabel->insertText (xLabel->getSelection(), types[i] + OUString::createFromAscii("\n")); } xLabel->insertText (xLabel->getSelection(), OUString::createFromAscii("******** Types - Interfaces : ") + OUStr.valueOf((sal_Int32)types.getLength())+ OUString::createFromAscii("\n")); break; }

// increase click counter _nCounts++;

}

};


// constructor ReflectionHelper::ReflectionHelper(Any any,Reference< XMultiServiceFactory > oSVM) : toInspect(any), xServiceManager(oSVM){ xIntrospection = Reference< XIntrospection >( xServiceManager->createInstance(

                               OUString( RTL_CONSTASCII_USTRINGPARAM(
                               "com.sun.star.beans.Introspection" ))), UNO_QUERY );

xIntrospec = xIntrospection->inspect(toInspect); mMethods = xIntrospec -> getMethods(MethodConcept::ALL); xTypeProvider = Reference< XTypeProvider> (toInspect,UNO_QUERY); types = xTypeProvider->getTypes(); xServiceInfo = Reference< XServiceInfo>(toInspect,UNO_QUERY); Properties = xIntrospec -> getProperties(PropertyConcept::ALL); }

//OK Sequence < OUString > ReflectionHelper::getServices() { return xServiceInfo->getSupportedServiceNames(); }

//OK Sequence < OUString > ReflectionHelper::getMethods(){ Sequence< OUString > methods(mMethods.getLength()); for (int i=0;i<mMethods.getLength();i++){ OUString params; params=OUString::createFromAscii("("); Sequence< ParamInfo > ParamInfos = mMethods[i]->getParameterInfos(); if (ParamInfos.getLength() > 0) { for (int j=0;j<ParamInfos.getLength();j++){ Reference< XIdlClass > xIdlClass = ParamInfos[j].aType; if (j == 0) // first parameter has no leading comma params += OUString::createFromAscii("[") + getParamMode(ParamInfos[j].aMode)+ OUString::createFromAscii("]") + xIdlClass->getName() + OUString::createFromAscii(" ") + ParamInfos[j].aName; else params += OUString::createFromAscii(",[") + getParamMode(ParamInfos[j].aMode)+ OUString::createFromAscii("]")+ xIdlClass->getName() + OUString::createFromAscii(" ") + ParamInfos[j].aName; } } params += OUString::createFromAscii(")"); methods[i] = mMethods[i]->getReturnType()->getName()+OUString::createFromAscii(" ")+ mMethods[i]->getName()+params; } return methods; }

// OK Sequence < OUString > ReflectionHelper::getTypes(){ Sequence< OUString > interfaces(types.getLength()); for (int i=0;i<types.getLength();i++){ interfaces[i] = types[i].getTypeName(); } return interfaces; }

// to improve : change all the tests with getCppuType : probably quicker than a string test OUString ReflectionHelper::getValueName(Any object){ OUString OUStr; OUStr = OUString::createFromAscii("!! No Computed value !!"); if (object.hasValue()) { if (object.isExtractableTo(getCppuBooleanType())){ sal_Bool MyBool; object >>= MyBool; return OUStr.valueOf((sal_Bool) MyBool); } else if (object.getValueTypeName() == OUString::createFromAscii("string")) { OUString *MyOUStr; MyOUStr = (OUString *) object.getValue(); OUStr = OUString::createFromAscii("\""); return OUStr + *MyOUStr + OUString::createFromAscii("\""); } else if (object.getValueTypeName() == OUString::createFromAscii("long")) { sal_Int32 *MyLong; MyLong = (sal_Int32*) object.getValue(); return OUStr.valueOf((sal_Int32) *MyLong); } else if (object.getValueTypeName() == OUString::createFromAscii("short")) { sal_Int16 *MyShort; MyShort = (sal_Int16*) object.getValue(); return OUStr.valueOf((sal_Int32) *MyShort); } else if (object.getValueTypeName() == OUString::createFromAscii("[]byte")) { Sequence< sal_Int8 > SeqByte; object >>= SeqByte; OUStr = OUString::createFromAscii("Length:"); OUStr=OUStr.concat(OUStr.valueOf((sal_Int32) SeqByte.getLength())); OUStr=OUStr.concat(OUString::createFromAscii(" : ")); // comments because cannot use such constructed name as shapes's name -- comments removed for (sal_Int32 i=0; i<SeqByte.getLength(); i++){ OUStr=OUStr.concat(OUStr.valueOf((sal_Int32) SeqByte[i])); OUStr=OUStr.concat(OUString::createFromAscii(" ")); } return OUStr; } else if (object.getValueTypeName() == OUString::createFromAscii("[]string")) { Sequence< OUString > SeqOUStr; object >>= SeqOUStr; OUStr = OUString::createFromAscii("Length:"); OUStr=OUStr.concat(OUStr.valueOf((sal_Int32) SeqOUStr.getLength())+ OUString::createFromAscii(" : ")); for (sal_Int32 i=0; i<SeqOUStr.getLength(); i++){ OUStr=OUStr.concat(OUString::createFromAscii("\"") +SeqOUStr[i] + OUString::createFromAscii("\"")); } return OUStr; } else return OUStr; } else return OUStr; }

// OK // Get properties with values : only those computed in getValueName // améliorer avec : // voir SDK2.3.1 portable IUT <SDK>/examples/cpp/extensions/source/objectbrowser/uno_lang.cxx Sequence < OUString > ReflectionHelper::getPropertiesWithValues(){ Sequence< OUString > propWithVal(Properties.getLength()); for (int i=0;i<Properties.getLength();i++){ Type typ = getCppuType( (const Reference< XPropertySet > *)0); Reference< XPropertySet > rPropertySet(xIntrospec->queryAdapter(typ),UNO_QUERY); Reference< XPropertySetInfo > rPropertySetInfo=rPropertySet->getPropertySetInfo(); Any object; if (rPropertySetInfo->hasPropertyByName(Properties[i].Name)){ object <<= rPropertySet->getPropertyValue(Properties[i].Name); //if (object.hasValue()) printf("Valeur trouvee : \n"); propWithVal[i] = Properties[i].Name + OUString::createFromAscii(" = (")+ Properties[i].Type.getTypeName() + OUString::createFromAscii(") ") + getValueName(object); //+ object.getValueTypeName(); } } return propWithVal; }

// OK // Get properties without values but types Sequence < OUString > ReflectionHelper::getPropertiesWithoutValues(){ Sequence< OUString > propWithVal(Properties.getLength()); for (int i=0;i<Properties.getLength();i++){ Type typ = getCppuType( (const Reference< XPropertySet > *)0); Reference< XPropertySet > xPropertySet(xIntrospec->queryAdapter(typ),UNO_QUERY); Reference< XPropertySetInfo > xPropertySetInfo=xPropertySet->getPropertySetInfo(); if (xPropertySetInfo->hasPropertyByName(Properties[i].Name)){ propWithVal[i] = Properties[i].Name + OUString::createFromAscii(" = (")+ Properties[i].Type.getTypeName() + OUString::createFromAscii(")"); } } return propWithVal; }

// Don't forget to add : #include <com/sun/star/reflection/ParamMode.hpp> // Don't forget to add "com.sun.star.reflection.ParamMode \" in the makefile OUString ReflectionHelper::getParamMode(ParamMode paramMode) {

 OUString toReturn;
 toReturn = OUString::createFromAscii("");
 if (paramMode == ParamMode_IN) toReturn = OUString::createFromAscii("IN"); else
   if (paramMode == ParamMode_OUT) toReturn = OUString::createFromAscii("OUT"); else
     if (paramMode == ParamMode_INOUT) toReturn = OUString::createFromAscii("INOUT");
 return toReturn;

}

void ReflectionHelper::printOut(){ Reference< XInterface > xdialogModel = xServiceManager->createInstance( OUString::createFromAscii("com.sun.star.awt.UnoControlDialogModel")); #ifdef DEBUG if (xdialogModel.is()) printf("OK XDialogModel\n"); else printf("Error ... XDialodModel\n"); #endif Any val; Reference< XPropertySet > xPSetDialog(xdialogModel,UNO_QUERY); #ifdef DEBUG if (xPSetDialog.is()) printf("OK XPropertySet\n"); else printf("Error ... XPropertySet\n"); #endif sal_Int32 value=100; val<<=value; xPSetDialog->setPropertyValue(OUString::createFromAscii("PositionX"),val); xPSetDialog->setPropertyValue(OUString::createFromAscii("PositionY"),val); value=300;val<<=value; xPSetDialog->setPropertyValue(OUString::createFromAscii("Width"),val); value=200;val<<=value; xPSetDialog->setPropertyValue(OUString::createFromAscii("Height"),val);

val <<=OUString::createFromAscii("Runtime Dialog Demo"); xPSetDialog->setPropertyValue(OUString::createFromAscii("Title"),val);

Reference< XMultiServiceFactory > xMultiServiceFactory( xdialogModel,UNO_QUERY);

///***************** //******** in the above line xMultiServiceFactory instead xServiceManager !!!!!! //Reference< XInterface > xbuttonModel = xServiceManager>createInstance( .... Reference< XInterface > xbuttonModel = xMultiServiceFactory->createInstance( OUString::createFromAscii("com.sun.star.awt.UnoControlButtonModel")); #ifdef DEBUG if (xbuttonModel.is()) printf("OK UnoControlButtonModel\n"); else printf("Error ... UnoControlButtonModel\n"); #endif Reference< XPropertySet > xPSetButton(xbuttonModel,UNO_QUERY); #ifdef DEBUG if (xPSetButton.is()) printf("OK XPropertySet\n"); else printf("Error ... XPropertySet\n"); #endif value=20; val <<= value; xPSetButton->setPropertyValue(OUString::createFromAscii("PositionX"),val); value=170; val <<= value; xPSetButton->setPropertyValue(OUString::createFromAscii("PositionY"),val); value=50; val <<= value; xPSetButton->setPropertyValue(OUString::createFromAscii("Width"),val); value=14; val <<= value; xPSetButton->setPropertyValue(OUString::createFromAscii("Height"),val); val <<=OUString::createFromAscii("Button1"); xPSetButton->setPropertyValue(OUString::createFromAscii("Name"),val); xPSetButton->setPropertyValue(OUString::createFromAscii("TabIndex"),makeAny((short)0)); val <<=OUString::createFromAscii(">>"); xPSetButton->setPropertyValue(OUString::createFromAscii("Label"),val);

// second button : OK button // we need a second button modedl Reference< XInterface > xbuttonModel2 = xMultiServiceFactory->createInstance( OUString::createFromAscii("com.sun.star.awt.UnoControlButtonModel")); #ifdef DEBUG if (xbuttonModel.is()) printf("OK UnoControlButtonModel\n"); else printf("Error ... UnoControlButtonModel\n"); #endif Reference< XPropertySet > xPSetButton2(xbuttonModel2,UNO_QUERY); #ifdef DEBUG if (xPSetButton2.is()) printf("OK XPropertySet\n"); else printf("Error ... XPropertySet\n"); #endif // *** The following property is not position-independant !!!!! // Don't forget to add : #include <com/sun/star/awt/PushButtonType.hpp> // Don't forget to add "com.sun.star.awt.PushButtonType \" in the makefile // short is found with Inspector val <<= (short)PushButtonType_OK; xPSetButton2->setPropertyValue(OUString::createFromAscii("PushButtonType"),val);

value=220; val <<= value; xPSetButton2->setPropertyValue(OUString::createFromAscii("PositionX"),val); value=170; val <<= value; xPSetButton2->setPropertyValue(OUString::createFromAscii("PositionY"),val); value=50; val <<= value; xPSetButton2->setPropertyValue(OUString::createFromAscii("Width"),val); value=14; val <<= value; xPSetButton2->setPropertyValue(OUString::createFromAscii("Height"),val); val <<=OUString::createFromAscii("Button2"); xPSetButton2->setPropertyValue(OUString::createFromAscii("Name"),val); xPSetButton2->setPropertyValue(OUString::createFromAscii("TabIndex"),makeAny((short)1)); val <<=OUString::createFromAscii("OK"); xPSetButton2->setPropertyValue(OUString::createFromAscii("Label"),val);

Reference< XInterface > xlabelModel = xMultiServiceFactory->createInstance( OUString::createFromAscii("com.sun.star.awt.UnoControlEditModel")); #ifdef DEBUG if (xlabelModel.is()) printf("OK EditModel\n"); else printf("Error ... EditModel\n"); #endif Reference< XPropertySet > xPSetLabel(xlabelModel,UNO_QUERY); #ifdef DEBUG if (xPSetLabel.is()) printf("OK XPropertySet2\n"); else printf("Error ... XPropertySet2\n"); #endif value=10; val <<= value; xPSetLabel->setPropertyValue(OUString::createFromAscii("PositionX"),val); value=10; val <<= value; xPSetLabel->setPropertyValue(OUString::createFromAscii("PositionY"),val); value=280; val <<= value; xPSetLabel->setPropertyValue(OUString::createFromAscii("Width"),val); value=150; val <<= value; xPSetLabel->setPropertyValue(OUString::createFromAscii("Height"),val); xPSetLabel->setPropertyValue(OUString::createFromAscii("HScroll"),makeAny((sal_Bool)true)); xPSetLabel->setPropertyValue(OUString::createFromAscii("VScroll"),makeAny((sal_Bool)true)); xPSetLabel->setPropertyValue(OUString::createFromAscii("MultiLine"),makeAny((sal_Bool)true)); xPSetLabel->setPropertyValue(OUString::createFromAscii("HardLineBreaks"),makeAny((sal_Bool)true)); val <<=OUString::createFromAscii("Label1"); xPSetLabel->setPropertyValue(OUString::createFromAscii("Name"),val); xPSetLabel->setPropertyValue(OUString::createFromAscii("TabIndex"),makeAny((short)2)); //val <<=OUString::createFromAscii("Text1"); //xPSetLabel->setPropertyValue(OUString::createFromAscii("Label"),val);


// insert all the control in container Reference< XNameContainer > xNameCont(xdialogModel,UNO_QUERY); #ifdef DEBUG if (xNameCont.is()) printf("OK XNameContainer\n"); else printf("Error ... XNameContainer\n"); #endif val <<= xbuttonModel; // We insert first the button xNameCont->insertByName(OUString::createFromAscii("Button1") ,val); #ifdef DEBUG printf("First\n"); #endif val <<= xbuttonModel2; xNameCont->insertByName(OUString::createFromAscii("Button2") ,val); //printf("First\n"); // We insert now the text control val <<= xlabelModel; xNameCont->insertByName(OUString::createFromAscii("Label1") , val); // create the dialog control and set the model Reference< XInterface >dialog = xServiceManager->createInstance( OUString::createFromAscii("com.sun.star.awt.UnoControlDialog")); #ifdef DEBUG if (dialog.is()) printf("OK dialog\n"); else printf("Error ... dialog\n"); #endif Reference< XControl > xControl(dialog,UNO_QUERY); #ifdef DEBUG if (xControl.is()) printf("OK XControl\n"); else printf("Error ... XControl\n"); #endif Reference< XControlModel > xControlModel(xdialogModel,UNO_QUERY); #ifdef DEBUG if (xControlModel.is()) printf("OK xControlModel\n"); else printf("Error ... xControlModel\n"); #endif xControl->setModel(xControlModel);

// add an action listener to the button control Reference< XControlContainer > xControlCont(dialog,UNO_QUERY); #ifdef DEBUG if (xControlCont.is()) printf("OK xControlContainer\n"); else printf("Error ... xControlContainer\n"); #endif Reference< XInterface > objectButton=xControlCont->getControl(OUString::createFromAscii("Button1")); #ifdef DEBUG if (objectButton.is()) printf("OK objectButton\n"); else printf("Error ... objectButton\n"); #endif Reference< XButton > xButton(objectButton,UNO_QUERY); ActionListenerImpl *xListener = new ActionListenerImpl( xControlCont,this ); Reference< XActionListener > xActionListener = static_cast< XActionListener* > ( xListener ); xButton->addActionListener( xActionListener );

// create a peer Reference< XToolkit >xToolkit = Reference< XToolkit >( xServiceManager->createInstance(

                                       OUString( RTL_CONSTASCII_USTRINGPARAM(
                                           "com.sun.star.awt.Toolkit" ))), UNO_QUERY );

#ifdef DEBUG if (xToolkit.is()) printf ("XToolkit OK...\n"); else printf("XToolkit Error\n"); #endif Reference< XWindow > xWindow(xControl,UNO_QUERY); xWindow->setVisible(true); xControl->createPeer(xToolkit,NULL);

Reference< XDialog > xDialog(dialog,UNO_QUERY); xDialog->execute(); Reference< XComponent > xComponent(dialog,UNO_QUERY); xComponent->dispose();

} If you want know how to use this snippet have look at Reflection Helper. Here is also a screen shot : MyInspector.png

Personal tools