Show TOC

Example: Creating Factory and Adapter Classes for Custom ControlsLocate this document in the navigation structure

Use

The sample code outlined below is an example of factory and adapter classes for custom controls.

Factory Class

               
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.widgets.Composite;

import com.sap.tc.mobile.wdlite.progmodel.api.IEvent;
import com.sap.tc.mobile.wdlite.progmodel.api.IEventSource;
import com.sap.tc.mobile.wdlite.progmodel.api.IWDNode;
import com.sap.tc.mobile.wdlite.renderer.api.BaseWdliteUiElementMethodCallAdapter;
import com.sap.tc.mobile.wdlite.renderer.api.IUIElementFactory;
import com.sap.tc.mobile.wdlite.renderer.api.UIContainer;
import com.sap.tc.mobile.wdlite.renderer.api.UIElement;
import com.sap.tc.mobile.wdlite.renderer.api.UIEvent;
import com.sap.tc.mobile.wdlite.renderer.api.UiGenericElement;
import com.sap.tc.mobile.wdlite.renderer.swt.SWTUtils;
import com.sap.tc.mobile.wdlite.renderer.swt.customcontrols.GenericSwtControlWrapper;

public class SampleFlatButtonCustomWidgetFactory implements IUIElementFactory {

        /**
         * Sample Implementation for an IUIElementFactory 
         */
        public UIElement createNewInstance(int type, String subType, UIContainer parent,
                        Map style) {

                //Create the native eSWT control and specify parent container. Apply all the style parameters.
                Button button = new Button(SWTUtils.getSwtComposite(parent),
                                SWT.BORDER);


                //The custom control has to provide an
                //implementation of a UiElementMethodCallAdapter. It contains all code for interaction between the custom control and the Mobile Client
                FlatButtonAdapter flatButtonAdapter = new FlatButtonAdapter(button);
                //Adds the UiGenericElement which the Mobile Client uses for communication with the custom control.
                //This UiGenericElement needs a UiElementMethodCallAdapter as argument. 
                UiGenericElement wdliteElement = new UiGenericElement(parent,flatButtonAdapter);
                flatButtonAdapter.setUiGenericElement(wdliteElement);

                //The GenericSwtControlWrapper is used to store the control, listen to events and forward them
                //Please see Javadocs of UiElementFactory to see how events work
                GenericSwtControlWrapper wrappedSwtControl = new GenericSwtControlWrapper(
                                parent, wdliteElement, button);
                wdliteElement.setUiWidgetAndParent(wrappedSwtControl, parent);

                return wdliteElement;
        }

        public UIElement createNewInstance(int type, String subType,
                        UIContainer container, Map style, String glyph) {
                return createNewInstance(type, subType, container, style);
        }

        
        }
            

Adapter Class

               import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Button;

import com.sap.tc.mobile.wdlite.progmodel.api.IEvent;
import com.sap.tc.mobile.wdlite.progmodel.api.IEventSource;
import com.sap.tc.mobile.wdlite.progmodel.api.IWDNode;
import com.sap.tc.mobile.wdlite.renderer.api.BaseWdliteUiElementMethodCallAdapter;
import com.sap.tc.mobile.wdlite.renderer.api.UIEvent;
import com.sap.tc.mobile.wdlite.renderer.api.UiGenericElement;
/**
 * 
* This class is a sample adapter class. It allows to set the button texts.
 * 
 * 
*/
public class FlatButtonAdapter extends BaseWdliteUiElementMethodCallAdapter implements SelectionListener, IEventSource {

                /**
                 * The native eSWT widget for interaction with Mobile Client, registering events, and so on
                 */
                private final Button button;
                
                /**
                 * Used to forward events between the framework and custom control
                 */
                private UiGenericElement wdliteUiGenericElement;

                private IWDNode node;

                private String buttonProb = "";
         
                /**
                 * The constructor stores the eSWT custom control and adds an event listener
                 * 
                 */
                public FlatButtonAdapter(Button b) {
                        this.button = b;
                        b.addSelectionListener(this);
                }

                
                /**
                 * Called whenever a property value gets changed in the wdDoModifyView() or in any method in the View
           */
                public void onPropertyChange(String name,Object value){                 
                        System.out.println("property changed: " + name +  ", value: " + value);
                        if ("textProp".equals(name) && value != null){
                                button.setText(value.toString());
                                buttonProb = value.toString();
                        }
                }

                //Setter method
                public void setUiGenericElement(UiGenericElement wdliteUiGenericElement) {
                        this.wdliteUiGenericElement = wdliteUiGenericElement;
                }

                //Store the context node and write code on what you want to do with it.
                public void setContextNode(IWDNode node) {
                        this.node = node;
                }

                //Translates a call from Mobile Client to the native control. For example, setText
                        public void setText(String text) {
                        button.setText(text);

                }

                public void widgetDefaultSelected(SelectionEvent arg0) {
                }
                
                public void setWidth(int width) {
                        System.out.println("width: " + width);
                }

                        }
            

Event Handling of the Custom Control

You need to write the SWT code in the implementation of the adapter class of the control. Events that need to be forwarded to the UI must also be wrapped in UIEvent and forwarded to the doHandleCustomControlEvent() method of the UIGenericElement class. Only one event type (onSubmit) is supported, therefore, the application developer has to add additional data to distinguish between multiple events of a custom control.

               /**
                 * SWT Event Listener: Translate the native SWT event into a UI event and forwards it. 
                 */
                public void widgetSelected(SelectionEvent arg0) {
                        UIEvent wdliteUiEvent = new UIEvent(this, IEvent.EVENT_ONACTION);
                        //add any data to the event, can be accessed by application developers in the event handling code of the View
                        wdliteUiEvent.setEventData("EventData", "CreateEmployee");
                        //allows to distinguish between multiple events of a custom control
                        wdliteUiEvent.setEventData("EventType", "ButtonClick");
                        wdliteUiGenericElement.doHandleCustomControlEvent(wdliteUiEvent);
//                      wdliteUiGenericElement.updateProperty("textProp", buttonProb + "updated");
                        
                }