Parametrizing a Widget
Parametrizing a widget means that you can customize the widget using defined settings. You can access the settings from a view file or a controller of your widget.
This example focuses on extending simple Chat widget and adding two widget settings. The first setting is responsible for defining whether the textbox can be expanded by a user. The second one is responsible for storing sent text in the history.
From a technical point of view, the Chat widget should have the two additional settings:
-
multiline: you can access this setting from the view file of the Chat widget.
-
history: you can access this setting from the controller of the Chat widget.
Modifing a Widget Definition
To the existing definition.xml file of the Chat widget add two settings that make it possible to add one, additional, widget.
-
Add the following settings to your definition.xml file:
definition.xml
<!-- ... --> <settings> <setting key="multi" type="Boolean" default-value="false"/> <setting key="history" type="Boolean" default-value="false"/> </settings> <widget-definition>
-
key: it is the name of the setting, displayed in the application orchestrator, and also an identifier that you use to refer to a setting.
-
type: supported types are: String, Boolean, Integer, Double, ENUM()
-
-
In the end, your definition.xml file should look like this:
definition.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <widget-definition id="org.myextension.widgets.mychat" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.hybris.com/schema/cockpitng/widget-definition.xsd"> <name>My Chat</name> <description>My chat widget.</description> <sockets> <input type="java.lang.String" id="incomingMsg"/> <output type="java.lang.String" id="outgoingMsg"/> </sockets> <keywords> <keyword>Chat</keyword> </keywords> <controller class="org.myextension.widgets.mychat.MyChatController"/> <settings> <setting key="multi" type="Boolean" default-value="false"/> <setting key="history" type="Boolean" default-value="false"/> </settings> </widget-definition>
Accessing Settings from View File
In the existing ZUL file, add an additional textbox's attribute, called multiline. It enables or disables the possibility to expand the textbox. This attribute holds a reference to multi setting.
-
Modify the textbox element in your widget view:
widgetid.zul
<!-- ... --> <textbox multiline="${widgetSettings.multi}" id="msgInput"/> </widget>For more information on ZK components, see the List of ZK Components

-
In the end, the view definition of your Chat widget should look like this:
widgetid.zul
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <widget xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.zkoss.org/2005/zul"> <div> <textbox multiline="${widgetSettings.multi}" id="msgInput"/> <button id="sendBtn" label="Send"/> </div> <div> <label id="lastMsgLabel" value="No message."></label> </div> <widgetslot slotID="additionalArea" width="100%" height="100%"/> </widget>
Accessing Settings from Controller
In the existing MyChatController, modify a method that is responsible for sending out a written massage. First, access the setting and then you trigger storing the message in the history. Don't define any real logic, but just display a message. It is only to demonstrate the functionality.
-
Modify the sendMsg method in the controller:
MyChatController.java
if (this.getWidgetSettings().getBoolean("history")) { // put some logic here; Messagebox.show("Saved to history"); } -
In the end, the controller should look like this:
MyChatController.java
public class MyChatController extends DefaultWidgetController { @WireVariable private Label lastMsgLabel; @WireVariable private Textbox msgInput; @ViewEvent(componentID = "sendBtn", eventName = Events.ON_CLICK) public void sendMsg() { sendOutput("outgoingMsg", msgInput.getText()); if (this.getWidgetSettings().getBoolean("history")) { // put some logic here; Messagebox.show("Saved to history"); } } @SocketEvent(socketId = "incomingMsg") public void updateTranscript(final String msg) { lastMsgLabel.setValue(msg); } }
Starting SAP Commerce Cloud Server
Normal operation mode:
- Navigate to the <HYBRIS_BIN_DIR>/platform directory.
- To start the SAP Commerce Cloud server:
- On Windows systems call the hybrisserver.bat file.
- On Unix systems call the hybrisserver.sh file, such as: ./hybrisserver.sh
Debug operation mode, requiring develop configuration template:
- Navigate to the <HYBRIS_BIN_DIR>/platform directory.
- To start the SAP Commerce Cloud server:
- On Windows, systems run the hybrisserver.bat file with the debug parameter, such as hybrisserver.bat debug.
- On Unix, systems call the hybrisserver.sh file with the debug parameter, such as ./hybrisserver.sh debug.
Settings in Application Orchestrator
Newly defined settings are now available to use in the application orchestrator: