Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    How to use Reflection in SmartGWT

    Hello everyone,
    I need help about reflection in SmartGWT, I have googling around and nothing really match what I need. What I really need is something like this :
    1. User clicked on a menu (a tree node)
    2. Based on menu that user clicked, I need to load the right panel/view class using just the class name in string value and add the loaded panel to content area in the center.
    Please help me how to achieve this, if reflection not available, then how can I do this task?
    Thanks.

    Best Regards,
    the.wizard

    #2
    Hi,

    I believe there will be a limited amount of panel classes. Use an enumeration.

    Zdary

    Comment


      #3
      You cannot use Reflection!
      The is a javascript based framework and this could result in security breaching.

      What you can do is:

      1) get the actionString when you click on the menu
      2) give the String to a function that makes a
      if("xxx".equals(actionString)) {
      loadSomething
      } esle if ............ {
      load another thing
      }

      Comment


        #4
        Originally posted by kiril_bhd
        You cannot use Reflection!
        The is a javascript based framework and this could result in security breaching.

        What you can do is:

        1) get the actionString when you click on the menu
        2) give the String to a function that makes a
        if("xxx".equals(actionString)) {
        loadSomething
        } esle if ............ {
        load another thing
        }
        the problem is, the class I want to load is not located in the same project, it is in a jar files... and I know what class to load in the runtime not in compile time...
        is there another way to achieve this things?
        thanks

        Comment


          #5
          hi,
          Thinking about things I have done before, I wonder if you could set an attribute on your TreeNode at run time where you can use:
          setAttribute(String property, Object value) and value will be an instance of the panel you want to load in your content (whether a singleton or not).
          And to use it:
          PanelToLoadInterface panelToLoad ;
          panelToLoad = (PanelToLoadInterface ) myTreeGrid.getSelectedRecord().getAttributeAsObject("panelToLoad");
          Canvas[] contentMembers= variablePanel.getMembers();
          variablePanel.removeMembers(contentMembers);
          variablePanel.addMember(panelToLoad);

          Hoping it could help

          Regards

          Alain

          Comment


            #6
            Originally posted by albu77
            hi,
            Thinking about things I have done before, I wonder if you could set an attribute on your TreeNode at run time where you can use:
            setAttribute(String property, Object value) and value will be an instance of the panel you want to load in your content (whether a singleton or not).
            And to use it:
            PanelToLoadInterface panelToLoad ;
            panelToLoad = (PanelToLoadInterface ) myTreeGrid.getSelectedRecord().getAttributeAsObject("panelToLoad");
            Canvas[] contentMembers= variablePanel.getMembers();
            variablePanel.removeMembers(contentMembers);
            variablePanel.addMember(panelToLoad);

            Hoping it could help

            Regards

            Alain
            sounds interesting... but I think before we do that, we still need to load all the panel we will use in treegrid, am I right? and the second thing that still a question for me is how can I load the other panel class in another jar (let say it is in other project and the project output is some jars)? I just wonder why is something that so important is not available in smartgwt?...
            I hope someone expert in smartgwt can help me out...
            Thanks.

            Regards,
            the.wizard

            Comment


              #7
              anyone please help me.... :(

              Comment


                #8
                Hi,
                I don't understand your probleme with jars, the smartGwt widget are in a jar also, you just include the lib in your project path and do the right import...
                Is it something else?

                Regards

                Alain

                Comment


                  #9
                  Originally posted by albu77
                  Hi,
                  I don't understand your probleme with jars, the smartGwt widget are in a jar also, you just include the lib in your project path and do the right import...
                  Is it something else?

                  Regards

                  Alain
                  Dear Alain, I will explain it more clearly about what I am trying to achieve.
                  I want to build a single project application that will act as a main container of others project applications. This main application is responsible for generating navigation pane (at the right side) and context area (at the left side), then everytime user click the menu in the navigation pane, the context area will be displaying the view that related to the selected menu.
                  Now, all the menu is in other project, and will be build pararel by other teams. What I want to achieve is, we just create the main application once, and all the other application that provided the right context area just need to compiled as jars and put in some directory that the main application can access and load all the class in the jars. The main application will know what class to be loaded when the user click the menu from the database.
                  Previously, we are doing this with URL in ASP.NET, but I see it is not possible to do it in smartGWT. Please kindly advice me or give me some suggestions how can I achieve my goal.
                  Thanks a lot.

                  Regards,
                  the.wizard

                  Comment


                    #10
                    Ok, I understand better what is your need but sadely I'm not able to answer to your problem.
                    One remark, this requirement and search of solution is related to Gwt,
                    Smartgwt is only about the presentation and not the basic principles which are still Gwt.
                    So perhaps you could have a look to:
                    http://code.google.com/p/gwt-ent/wiki/UseReflection
                    http://gwtreflection.sourceforge.net/

                    I would be pleased if you can give me some news about your progress on this..

                    Regards

                    Alain

                    Comment


                      #11
                      Originally posted by albu77
                      Ok, I understand better what is your need but sadely I'm not able to answer to your problem.
                      One remark, this requirement and search of solution is related to Gwt,
                      Smartgwt is only about the presentation and not the basic principles which are still Gwt.
                      So perhaps you could have a look to:
                      http://code.google.com/p/gwt-ent/wiki/UseReflection
                      http://gwtreflection.sourceforge.net/

                      I would be pleased if you can give me some news about your progress on this..

                      Regards

                      Alain
                      Hi Alain,
                      Thanks for replying... I will take a look at the link you gave me...
                      Hope someone that ever achieve this and can share it with us in this thread...

                      Regards,
                      the.wizard

                      Comment


                        #12
                        Originally posted by the.wizard
                        Dear Alain, I will explain it more clearly about what I am trying to achieve.
                        I want to build a single project application that will act as a main container of others project applications. This main application is responsible for generating navigation pane (at the right side) and context area (at the left side), then everytime user click the menu in the navigation pane, the context area will be displaying the view that related to the selected menu.
                        Now, all the menu is in other project, and will be build pararel by other teams. What I want to achieve is, we just create the main application once, and all the other application that provided the right context area just need to compiled as jars and put in some directory that the main application can access and load all the class in the jars. The main application will know what class to be loaded when the user click the menu from the database.
                        Previously, we are doing this with URL in ASP.NET, but I see it is not possible to do it in smartGWT. Please kindly advice me or give me some suggestions how can I achieve my goal.
                        Thanks a lot.

                        Regards,
                        the.wizard
                        Hello everyone...
                        Is there anyone can help me to achieve this goal? Any advice is really appreciated... Please share your thoughts and ideas to achieve this goal...
                        Thanks a lot...

                        Regards,
                        the.wizard
                        Last edited by the.wizard; 6 Nov 2011, 23:55.

                        Comment


                          #13
                          Originally posted by the.wizard
                          Hello everyone...
                          Is there anyone can help me to achieve this goal? Any advice is really appreciated... Please share your thoughts and ideas to achieve this goal...
                          Thanks a lot...

                          Regards,
                          the.wizard
                          Hello everyone... any ideas?...

                          Comment


                            #14
                            Originally posted by the.wizard
                            Hello everyone...
                            Is there anyone can help me to achieve this goal? Any advice is really appreciated... Please share your thoughts and ideas to achieve this goal...
                            Thanks a lot...

                            Regards,
                            the.wizard
                            Still no one to answer or give some advice about this...

                            Comment


                              #15
                              Below are the steps to implement GWT Reflection

                              Step I: Download GWT-ENT (gwtent2.0 RC1(GWT2.3).zip) from "http://code.google.com/p/gwt-ent/downloads/list" or "http://code.google.com/p/gwt-ent"

                              Step II: Copy "gwtent.jar" into WEB-INF/lib folder of your project

                              Step III: Add "gwtent.jar" to your eclipse Project Build Path (Add jar files to Build Path in your favorite choice of Editor)

                              Step IV: Edit .gwt.xml file to add below line
                              <inherits name="com.gwtent.GwtEnt" />

                              Step V: Let's write a utility which can be invoked from anywhere who wants to use reflection

                              FWUtil.java
                              package com.your.package;

                              import com.gwtent.reflection.client.ClassHelper;
                              import com.gwtent.reflection.client.ClassType;
                              import com.gwtent.reflection.client.TypeOracle;

                              public class FWUtil {
                              /**
                              * To invoke a specific java method
                              * @param istrMethodName method name including class name
                              * @param iobjarrtMethodParams method parameters
                              */
                              public static void invokeJavaMethod(String istrMethodName, Object[] iobjarrtMethodParams)
                              {
                              //Get Class Name
                              String lstrClassName = istrMethodName.substring(0,istrMethodName.lastIndexOf("."));
                              //Get Method Name
                              String lstrMethodName = istrMethodName.substring((istrMethodName.lastIndexOf(".")+1),istrMethodName.length());
                              try
                              {
                              //Get Class
                              Class lobjClass = ClassHelper.forName(lstrClassName);
                              //Get Class Type
                              ClassType lobjClassType = TypeOracle.Instance.getClassType(lobjClass);
                              //Invoke Method including Method Params (Assumption: Method its calling is Static)
                              lobjClassType.invoke(null, lstrMethodName, iobjarrtMethodParams);
                              }
                              catch (ClassNotFoundException e)
                              {
                              e.printStackTrace();
                              }
                              }//end of method
                              }

                              Step VI: Add "implements Reflection" to the class whose method(s) you want to invoke at runtime using reflection. Below is an example of testClass

                              testClass.java
                              package com.your.package;
                              import com.gwtent.reflection.client.Reflection;

                              public class testClass implements Reflection
                              {
                              public static void testMethod()
                              {
                              System.out.println("TEST WITH NO INPUT PARAM");
                              }

                              public static void testMethod(String istrTestMsg)
                              {
                              System.out.println("TEST "+istrTestMsg);
                              }

                              public static void testMethod(String istrTestMsg, int iintTestNumber)
                              {
                              System.out.println("TEST "+istrTestMsg+" "+iintTestNumber);
                              }

                              }


                              Step VII: Using Reflection
                              package com.your.package;

                              import com.smartgwt.client.widgets.Canvas;
                              import com.smartgwt.client.widgets.IButton;
                              import com.smartgwt.client.widgets.events.ClickEvent;
                              import com.smartgwt.client.widgets.events.ClickHandler;

                              import com.google.gwt.core.client.EntryPoint;

                              public class FWInitializer implements EntryPoint {

                              public void onModuleLoad() {

                              Canvas lobjCanvas = new Canvas();

                              //Create Button to invoke testMethod with no inputs
                              final IButton lobjButton0 = new IButton("No Input Params");
                              lobjButton0.addClickHandler(new ClickHandler() {
                              public void onClick(ClickEvent event) {
                              FWUtil.invokeMethod("com.your.package.testClass.testMethod", null);
                              }
                              });
                              lobjCanvas.addChild(lobjButton0);

                              //Create Button to invoke testMethod with 1 inputs
                              final IButton lobjButton1 = new IButton("One Input Param");
                              lobjButton1.addClickHandler(new ClickHandler() {
                              public void onClick(ClickEvent event) {
                              Object[] lobjarrParams = new Object[]{"me"};
                              FWUtil.invokeMethod("com.your.package.testClass.testMethod", lobjarrParams);
                              }
                              });
                              lobjCanvas.addChild(lobjButton1);

                              //Create Button to invoke testMethod with 2 inputs
                              final IButton lobjButton2 = new IButton("Two Input Param");
                              lobjButton2.addClickHandler(new ClickHandler() {
                              public void onClick(ClickEvent event) {
                              Object[] lobjarrParams = new Object[]{"me", new Integer(2).intValue()};
                              FWUtil.invokeMethod("com.your.package.testClass.testMethod", lobjarrParams);
                              }
                              });
                              lobjCanvas.addChild(lobjButton2);

                              lobjCanvas.draw();
                              }

                              Good Luck and Enjoy...

                              Comment

                              Working...
                              X