Creating and Registering a New Perspective

The SAP Commerce Cloud Cockpit Framework enables you to easily create and register a new perspective. You can also create other cockpit elements such as areas and sections.

Precondition:

To make implementation changes described in this document, you need to have a valid ZK Studio Enterprise Edition license.

Creating New Perspective in Theory

A perspective in the SAP Commerce Cloud cockpit framework is an implementation of the interface de.hybris.platform.cockpit.session.UICockpitPerspective. A perspective can be treated as an overall view that holds four areas:

  1. Navigation area - this is an area that holds different navigation structures like element trees, undo section, favorite item collections, history, saved queries. Elements of navigation area can be highly customized.
  2. Browser area - this area is used to display elements as for instance a searchable list, grid or in our other customized view like, for example, in catalog perspective in the Product Cockpit module. In this area, we can use tabs for displaying many browsers in the same moment and also splittable view is available that makes drag and drop functionality easy to implement. In a browser area, you can also perform on-line item editing.
  3. Editor area - this area is used to display item for edit. It can contain many sections with all available item attributes. Here you can easily perform the editing of an item.
  4. Pop-up editor area - this area is basically the same as editor area, only difference is that this editor area is opened in the pop-up window, so does not occupy place inside the main window.

When you want to create a new perspective, follow the steps below:

  1. Create an implementation of the de.hybris.platform.cockpit.session.UICockpitPerspective interface or extend one of the existing implementations of this interface. de.hybris.platform.cockpit.session.impl.BaseUICockpitPerspective is the example base implementation that can be used.
  2. Register your implementation as a spring bean in UICockpitSesssion bean in the property availablePerspectives.
  3. Set up four areas described above for the new perspective. You can reuse or extend existing area implementations.

Creating New Perspective in Practice

As an example, you can create new perspective that displays for us items of type Media and next you customize it a little. You need a new cockpit extension created or you can do it in some existing extension as well.

Step One - Perspective Itself

You have several ways to create a new perspective bean:

  1. Create a new bean with a new id in spring configuration that has other perspective bean as a parent.
    <bean id="TestCockpitPerspective" scope="session" parent="BasePerspective">
    (...)
    </bean>

    In this case you have a new perspective bean without any custom underlaying java class, but still you can put there your custom areas.

  2. Create a new java class that extends the existing implementation of the de.hybris.platform.cockpit.session.UICockpitPerspective interface, for example de.hybris.platform.cockpit.session.impl.BaseUICockpitPerspective. In this case, you can override some methods and customize the class in the way you want to.
    import de.hybris.platform.cockpit.session.impl.BaseUICockpitPerspective;
    
    public class MediaPerspective extends BaseUICockpitPerspective
    {
    
    	@Override
    	public void closePopupEditor()
    	{
    		// Do some customized stuff
    		super.closePopupEditor();
    	}
    }

    You need to add this class to the spring configuration as a perspective in the same way as before.

  3. Create a new java class that implements the de.hybris.platform.cockpit.session.UICockpitPerspective interface. It is very generic way - you have to then implement many methods by yourself. That's why this way isn't used not so often, if ever.

In your example, you can continue using second way of creating perspective. You need to create your perspective java class as shown above and then register your perspective bean. You also add empty properties tags for four required perspective areas and a few additional fields:

<bean id="MediaPerspective" scope="session" class="(...).MediaPerspective" parent="BasePerspective">
	<property name="uid" value="TestCockpit.perspective.Media" /> 
	<property name="label" value="perspective.Media" /> 
	<property name="customCsaURI" value="/cockpit/commonCSA.zul" />  

	<property name="navigationArea"></property>
	<property name="browserArea"></property>
	<property name="editorArea"></property>
	<property name="popupEditorArea"></property>

	<property name="cockpitTypeService" ref="cockpitTypeService"/>		
</bean>

In the code snippet above you have the following properties:

  • uid - this is an identification for this perspective. Must be unique in the scope of application.
  • label - localization key for perspective label. Proper label value must be placed in the files i3_label.properties.
  • customCsaURI - this is path to a file with optional custom client-side actions. This file is included in the generated page.
  • cockpitTypeService - reference to the TypeService bean that must be implementation of the de.hybris.platform.cockpit.services.meta.TypeService interface.

In the next step, you create required areas.

Step Two - Create Areas

Navigation Area

You create simple navigation area that contains info box and undo section. In your spring configuration xml file you put the following code:

<bean id="MediaNavigationArea" scope="session" parent="BaseNavigationArea">
	<property name="sectionModel">
		<bean class="de.hybris.platform.cockpit.components.navigationarea.DefaultNavigationAreaModel" scope="session" />
	</property>		
	<property name="infoSlotRenderer">
		<bean class="de.hybris.platform.cockpit.components.navigationarea.renderer.InfoBoxRenderer"/>
	</property>
	<property name="sections">
		<list>
			<ref bean="MediaUndoSection" />
		</list>
	</property>
	<property name="cockpitTypeService" ref="cockpitTypeService"/>
</bean>

<bean id="MediaUndoSection" class="de.hybris.platform.cockpit.components.navigationarea.NavigationPanelSection" scope="prototype">
	<property name="localizedLabel" value="navigationarea.history" />
	<property name="visible" value="true" />
	<property name="renderer">
		<bean class="de.hybris.platform.cockpit.components.navigationarea.renderer.UndoSectionRenderer" />
	</property>
</bean>

You need to create two beans:

  • MediaNavigationArea that just extends existing BaseNavigationArea cockpit bean. Contains your navigation area section model and also sections that you want to put into this area.
  • MediaUndoSection - here you create a bean with default undo section renderer.

For the properties sectionModel and infoSlotRenderer of MediaNavigationArea bean, you can reuse the existing default cockpit classes. Now you can add this navigation area to your MediaPerspective bean:

<property name="navigationArea">
		<ref bean="MediaNavigationArea" />
	</property>

Browser Area

Add the default search browser area with root type set to Media. So in your MediaPerspective bean declaration you add:

<property name="browserArea">
		<bean class="de.hybris.platform.cockpit.session.impl.DefaultSearchBrowserArea">
			<property name="rootSearchTypeCode" value="Media"/>
			<property name="viewURI" value="/cockpit/baseSearchBrowserArea.zul"/>
		</bean>
	</property>

The DefaultSearchBrowserArea instance is responsible for searching for media and displaying results.

Editor Area

For this area you need to create additional bean:

<bean id="MediaEditorAreaController" class="de.hybris.platform.cockpit.session.impl.DefaultEditorAreaController" scope="session">
	<property name="sectionRowRenderer">
		<bean id="MediaEditorSectionRowRenderer" parent="EditorSectionRowRenderer"/>
	</property>
	<property name="editorFactory">
		<ref bean="EditorFactory"/>
	</property>
</bean>

No new java classes are required. You just created an instance of the DefaultEditorAreaController with two properties filled with default cockpit editor beans.

Now you must update your MediaPerspective with this new bean:

<property name="editorArea">
		<bean id="MediaEditorAreaModel" parent="BaseEditorAreaModel">
		<property name="editorAreaController">
				<ref bean="MediaEditorAreaController"/>
			</property>
		</bean>	
	</property>

Pop-Up Editor Area

For the last area, you have to create a java class that extends the DefaultEditorAreaController cockpit class and implements the PopupEditorAreaController interface.

import de.hybris.platform.cockpit.session.BrowserModel;
import de.hybris.platform.cockpit.session.PopupEditorAreaController;
import de.hybris.platform.cockpit.session.impl.DefaultEditorAreaController;

import java.util.HashMap;
import java.util.Map;

public class MediaPopupEditorAreaControllerImpl extends DefaultEditorAreaController implements PopupEditorAreaController
{
	private BrowserModel contextEditorBrowser = null;
	private final Map<String, Object> attributes = new HashMap<String, Object>();

	@Override
	public Map<String, Object> getAttributesMap()
	{
		return attributes;
	}

	@Override
	public void setContextEditorBrowser(final BrowserModel model)
	{
		this.contextEditorBrowser = model;
	}

	@Override
	public BrowserModel getContextEditorBrowserModel()
	{
		return contextEditorBrowser;
	}
}

This simple implementation is enough, and now you need to create spring bean with this class:

<bean id="MediaPopupEditorAreaController" class="(...).MediaPopupEditorAreaControllerImpl" scope="prototype">
	<property name="sectionRowRenderer">
		<bean id="MediaPopupEditorSectionRowRenderer" parent="EditorSectionRowRenderer"/>
	</property>
	<property name="editorFactory">
		<ref bean="EditorFactory"/>
	</property>
</bean>

and update your MediaPerspective with this bean:

<property name="popupEditorArea">		
		<bean id="MediaPopupEditor" parent="BasePopupEditor">
			<property name="editorAreaController">
				<ref bean="MediaPopupEditorAreaController"/>
			</property>
		</bean>
	</property>

After all these steps you can finally build your platform, start application server and check our new perspective. It should contain undo section and info box in the navigation area at the left, searchable Media browser in the center and after double-clicking Media item editor area should be opened at the right part of the page and should be filled with the proper data.