Implementing an Additional Editor

Editors are Cockpit components where you can change values of attributes. They are used within the editor area and browsers in list view mode. By default, the SAP Commerce Cloud Cockpit provides editors for all types of attributes such as text, date, or enumerations. In addition, you can write your own custom editors.

After creating a custom editor, you integrate it into the SAP Commerce Cloud Cockpit and adjust the configuration files to use it for some attributes.

Overview of Editors in SAP Commerce Cloud Cockpit

Cockpit editors should be free-of-logic and just change values; so they can be easily reused in different places. Therefore their view components have to communicate with the component from which they were created. This is done by the EditorListener interface containing the following methods:

  • valueChanged(Object value)
  • actionPerformed(String actionCode)

Make sure, that this methods are invoked at the right place. For editors that are used in the Editor Area only, it is sufficient to invoke valueChanged. But if they are also used somewhere else, for example in the listview, they should be a bit more verbose and at least notify the listener if some cancel operation was done.

Examples of editors.

Creating a Custom Editor Class

To create a custom editor class, first create a new Java class called WysiwygUIEditor that implements the UIEditor interface.

A more convenient way is to extend the class AbstractUIEditor, which already implements most of the methods.

Implement the following methods:

  • public boolean isInline(): Just return false, since you will create a pop-up editor
  • public String getEditorType(): Our editor will be used for text properties, so return PropertyDescriptor.TEXT
  • public HtmlBasedComponent createViewComponent(final Object initialValue, final Map<String, ? extends Object> parameters, final EditorListener listener): This method creates the ZK component for the editor and is responsible for the behavior, for example sending events on particular situations. Basically, almost all is done here.

Basics

Your WYSIWYG editor should display just an area of HTML formatted text. If you click on it, a pop-up window shall appear containing a FCKEditor.

Implement the createViewComponent(...) method as follows:

WysiwygUIEditor.java

public HtmlBasedComponent createViewComponent(final Object initialValue, 
                final Map<String, ? extends Object> parameters,
		final EditorListener listener)
{
	//create main container which will be returned*/
	final Div editorView = new Div();
	//set the style according to your preferences*/
	editorView.setStyle("max-height: 4em; min-height: 18px; overflow: hidden;" +
                            " border: 1px solid #cccccc; padding: 3px;");
	//the html container for the text*/
	final Html contentComponent = new Html();
	editorView.appendChild(contentComponent);

	//do some checks and set the value*/
	if (initialValue instanceof String)
	{
		contentComponent.setContent((String) initialValue);
	}
	else if (initialValue != null)
	{
		log.error("Initial value not of type String.");
	}

	return editorView;
}

Now you have an Editor, which just displays a string as HTML within a <div>. You can now go directly to the Register the Editor in the hybris Cockpit section below and try it out before going on with more advanced topics.

Advanced Coding

As mentioned above, the editor should open a pop-up window containing a FCKEditor when a user clicks on it. Thus, you have to add an event listener for the click event to our main container by adding the following code to createViewComponent:

WysiwygUIEditor.java

// add listener for click event 
	editorView.addEventListener(Events.ON_CLICK, new EventListener()
	{
		public void onEvent(final Event arg0) throws Exception
		{
			// create window containing the fck editor
			final Window win = createPopupWysiwyg(contentComponent, listener);
			// add it as child to root window 
			win.setParent(editorView.getRoot());
			// open it in highlight mode 
			win.doHighlighted();
		}
	});

The window is created by invoking createPopupWysiwyg(contentComponent, listener):

WysiwygUIEditor.java

protected Window createPopupWysiwyg(final Html html, final EditorListener listener)
{
	//Create the window
	final Window ret = new Window();
	ret.setTitle("Edit");

	//We use a vbox with two children, one is the fckeditor, 
        //the other one is for the buttons
	final Vbox box = new Vbox();
	ret.appendChild(box);
	box.setHeights("none,25px");

	//create and set up the fckeditor
	final FCKeditor fckEditor = new FCKeditor();
	fckEditor.setWidth("600px");
	fckEditor.setHeight("400px");
	fckEditor.setParent(box);
	fckEditor.setValue(html.getContent());

	//create horizontal box for the buttons
	final Hbox buttonBox = new Hbox();

	final Button okBtn = new Button("OK");
	//listener for the click event
	okBtn.addEventListener(Events.ON_CLICK, new EventListener()
	{
		public void onEvent(final Event arg0) throws Exception
		{
			//when the user clicks on ok, we take the current 
			//string from fckeditor...
			final String currentValue = fckEditor.getValue();
			//... set the editors value to it ...
			WysiwygUIEditor.this.setValue(currentValue);
			// ... notify the listener that the value has changed ...
			listener.valueChanged(currentValue);
			// ... notify the listener that someone has explicitly 
                        //clicked on a kind of "save" button ...
			listener.actionPerformed(EditorListener.FORCE_SAVE_CLICKED);
			// ... set the string of our preview to the current value ...
			html.setContent(currentValue);
			// ... and finally close the popup window
			ret.detach();
		}
	});

	final Button cancelBtn = new Button("Cancel");
	cancelBtn.addEventListener(Events.ON_CLICK, new EventListener()
	{
		public void onEvent(final Event arg0) throws Exception
		{
			//cancel just closes the window and notifies the editor listener
			ret.detach();
			listener.actionPerformed(EditorListener.CANCEL_CLICKED);
		}
	});

	buttonBox.appendChild(okBtn);
	buttonBox.appendChild(cancelBtn);
	buttonBox.setParent(box);

	return ret;
	}

Registering the Editor in the SAP Commerce Cloud Cockpit

  1. To register the new editor to be available in the SAP Commerce Cloud Cockpit, just locate the EditorFactory bean definition, usually located in the cockpit-spring-editors.xml file.
  2. In the file, search for the PropertyEditorDescriptor that has the editorType value TEXT.
  3. Add a new entry in the editors map with a unique qualifier as key and the fully qualified class as value.
<bean class="de.hybris.platform.cockpit.model.meta.DefaultPropertyEditorDescriptor">
  <property name="editorType" value="TEXT"/>
  <property name="defaultMode" value="single"/>
  <property name="defaultEditor" 
            value="de.hybris.platform.cockpit.model.editor.impl.DefaultTextUIEditor" />
  <property name="editors">
    <map>
      <entry key="multi" 
             value="de.hybris.platform.cockpit.model.editor.impl.GenericCollectionUIEditor"/>
      <entry key="range" 
             value="de.hybris.platform.cockpit.model.editor.impl.GenericRangeUIEditor"/>
      <entry key="imageEditor" 
             value="de.hybris.platform.cockpit.model.editor.impl.DefaultImageUIEditor"/>
<!-- this line registers our wysiwyg editor -->
      <entry key="wysiwyg" 
             value="de.hybris.platform.cockpit.model.editor.WysiwygUIEditor"/>
    </map>
  </property>
  <property name="label" value="textEditor"/>
</bean>

Customizing the Configuration Files to Use the Editor

Finally, get your new editor working by assigning it to an attribute in the configuration files of intended UI components, such as editorarea or listview. Just set the editor attribute for a row with the qualifier that you have defined in the EditorFactory above.

<editor>
    <section qualifier="General" visible="true" initially-opened="true">
        <label lang="de">Stammdaten</label>
	<label lang="en">Properties</label>
	<row property="product.code" />
	<row property="product.name" />
	<row property="product.catalogversion" />
	<row property="product.approvalStatus" />
	<row property="product.onlineDate" />
	<row property="product.offlineDate" />
	<row property="product.unit" />
        <!-- here we changed the editor from default to "wysiwyg" -->
	<row property="product.description" editor="wysiwyg"/>
    </section>
...