Creating Custom UI Configuration for a Widget

In the Backoffice Framework, it is possible to customize a widget in such a way that it is displayed differently in the UI depending on the context.

Context

Create a widget displaying a tree with node icons, labels, and others. The tree structure should be configurable during run time and there should be different configurations, for example per user and other context parameters.

This tutorial starts from creating XSD schema, but in this case the order is not relevant and you can also start from creating JAXB classes.

Procedure

  1. Create Schema for XML. Create the following XSD file and store it in myextension/resources/schemas.

    example-config.xsd

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.hybris.com/cockpitng/example"
    	targetNamespace="http://www.hybris.com/cockpitng/example"
    	elementFormDefault="qualified">
    
    	<xs:element name="tree" type="tree"/>
    
    	<xs:complexType name="tree">
    		<xs:sequence minOccurs="1" maxOccurs="1">
    			<xs:element name="root" type="node"/>
    		</xs:sequence>
    	</xs:complexType>
    
    	<xs:complexType name="node">
    		<xs:sequence>
    			<xs:element name="node" type="node" minOccurs="0" maxOccurs="unbounded"/>
    		</xs:sequence>
    		<xs:attribute name="id" type="xs:string" use="required"/>
    		<xs:attribute name="label" type="xs:string"/>
    		<xs:attribute name="icon" type="xs:string"/>
    		<xs:attribute name="tooltip" type="xs:string"/>
    	</xs:complexType>
    
    </xs:schema>

    There are two elements:

    • tree: A root element that consists of node elements

    • node: An element that can consist of a list of node subelements. Each note element can contain a number of properties, like for example label.

  2. Generate JAXB Classes. Navigate to location of the XSD schema and use xjc command prompt tool like this:

    xjc example-config.xsd -p org.myextension.widgets.mywidget.jaxb

    The following classes are generated under org.myextension.widgets.mywidget.jaxb:

    • Tree.java: For tree element representation.

    • Node.java: For node element representation.

    • ObjectFactory.java

    • package-info.java

    1. Add @XmlRootElement annotation to the Tree class:

      Tree.java

      //
      
      @XmlAccessorType(XmlAccessType.FIELD)
      @XmlType(name = "tree", propOrder = { "root" })
      @XmlRootElement(name = "tree") // This is it.
      public class Tree
      
      //
    2. Ad example prefix for your namespace in the package-info class:

      package-info.java

      //
      
      @javax.xml.bind.annotation.XmlSchema(namespace = "http://www.hybris.com/cockpitng/example", 
          xmlns = { @javax.xml.bind.annotation.XmlNs(prefix = "example", namespaceURI = "http://www.hybris.com/cockpitng/example") }, 
          elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
      
      //
  3. Use Created Configuration. Now you can, for example, add two configurations, each for a different user (principal context parameter).
    <config xmlns="http://www.hybris.com/cockpit/config" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:ex="http://www.hybris.com/cockpitng/example"
    	xsi:schemaLocation="http://www.hybris.com/cockpit/config http://www.hybris.com/schema/cockpitng/config/cockpit-configuration.xsd
    	                    http://www.hybris.com/cockpitng/example example-config.xsd">
    
    	<context component="myTreeWidget" principal="cockpituser">
    		<ex:tree>
    			<ex:root id="theroot" icon="root.png" label="root">
    				<ex:node id="system" icon="system.png" label="system">
    					<ex:node id="settings" icon="default.png" label="setings" />
    					<ex:node id="import" icon="import.png" label="import" />
    					<ex:node id="export" icon="export.png" label="export" />
    				</ex:node>
    				<ex:node id="catalog" icon="catalog.png" label="catalog">
    					<ex:node id="category" icon="cat.png" label="categories" />
    					<ex:node id="product" icon="prod.png" label="products" />
    				</ex:node>
    			</ex:root>
    		</ex:tree>
    	</context>
    	
    	<context component="myTreeWidget" principal="printuser">
    		<ex:tree>
    			<ex:root id="theroot" icon="root.png" label="root">
    				<ex:node id="catalog" icon="catalog.png" label="catalog">
    					<ex:node id="category" icon="cat.png" label="categories" />
    					<ex:node id="product" icon="prod.png" label="products" />
    					<ex:node id="media" icon="med.png" label="media" />
    				</ex:node>
    				<ex:node id="print" icon="print.png" label="print">
    					<ex:node id="publication" icon="pub.png" label="publications" />
    					<ex:node id="chapters" icon="cha.png" label="chapters" />
    					<ex:node id="page" icon="pag.png" label="pages" />
    				</ex:node>
    			</ex:root>
    		</ex:tree>
    	</context>
    </config>