Modifying the Widget View

Modify the widget view.

Context

We have created a QuickEdit widget with the following definition:

definition.xml

<widget-definition xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:noNamespaceSchemaLocation="http://www.hybris.com/schema/cockpitng/widget-definition.xsd"
                   id="org.training.widgets.quickEdit">
    <name>QuickEdit widget</name>
    <description>QuickEdit Widget</description>
    <defaultTitle>QuickEdit Widget</defaultTitle>
    <author>hybris</author>
    <version>1.0</version>
    <view src="quickedit.zul" />
    <keywords>
        <keyword>moduleB</keyword>
    </keywords>
    <sockets>
        <input id="in" type="java.lang.Object"/>
    </sockets>
    <controller class="org.training.widgets.QuickEditWidgetController" />

</widget-definition>

Procedure

  1. Configure the ZUL file of QuickEdit widget:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <widget xmlns="http://www.zkoss.org/2005/zul"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:h="http://www.w3.org/1999/xhtml"
            xmlns:w="http://www.zkoss.org/2005/zk/client"
            xmlns:zk="http://www.zkoss.org/2005/zk"
            xsi:schemaLocation="http://www.zkoss.org/2005/zul http://www.hybris.com/schema/cockpitng/zul/zul.xsd"
            height="100%">
        <actions id="actions" viewMode="iconAndText" group="common"/>
        <editors id="editors" group="common"/>
        <button id="save" label="save"/>
    
    </widget>

    In the ZUL file, we have defined actions component, editors component, and the Save button.

  2. Notice how the widget controller is defined in the following way:
    QuickEditWidgetController
    import com.hybris.cockpitng.annotations.SocketEvent;
    import com.hybris.cockpitng.annotations.ViewEvent;
    import com.hybris.cockpitng.components.Actions;
    import com.hybris.cockpitng.components.Editors;
    import com.hybris.cockpitng.dataaccess.facades.object.ObjectFacade;
    import com.hybris.cockpitng.dataaccess.facades.object.exceptions.ObjectSavingException;
    import com.hybris.cockpitng.dataaccess.facades.type.TypeFacade;
    import com.hybris.cockpitng.util.DefaultWidgetController;
    
    import org.zkoss.zk.ui.Component;
    import org.zkoss.zk.ui.event.Events;
    import org.zkoss.zul.Button;
    
    public class QuickEditWidgetController extends DefaultWidgetController
    {
        private final String MODEL_CURRENT_OBJECT = "currentObject";
        private TypeFacade typeFacade;
        private ObjectFacade objectFacade;
        private Actions actions;
        private Editors editors;
        private Button save;
        @Override
        public void initialize(final Component comp) {
            super.initialize(comp);
        }
        @ViewEvent(eventName = Events.ON_CLICK,componentID = "save")
        public void onSave()
        {
            try {
                objectFacade.save(getModel().getValue(MODEL_CURRENT_OBJECT,Object.class));
            } catch (ObjectSavingException e) {
                e.printStackTrace();
            }
        }
        @SocketEvent(socketId = "in")
        public void inSocket(final Object obj)
        {
            getModel().put(MODEL_CURRENT_OBJECT,obj);
            reloadActions(obj);
            reloadEditors(obj);
        }
        private void reloadActions(final Object obj)
        {
            final String type = typeFacade.getType(obj);
            actions.setConfig("component=previewActions,type=" + type);
            actions.reload();
        }
        private void reloadEditors(final Object obj)
        {
            final String type = typeFacade.getType(obj);
            editors.setConfig("component=previewEditors,type=" + type);
            editors.reload();
        }
    }

    The two most important methods are:

    • inSocket: it reacts on the incoming socket event. In this method, we store the model of the incoming object and reload configuration for actions and editors. For example, in the reloadActions method we dynamically calculate the type of the current object and change the actions config tag - as a result of that, we can have different actions for different types. reloadEditors method has similar implementation.
    • onSave: it reacts on click of the Save button and simply uses the object façade to persist the changes.