Announcement: You can find the guides for Commerce 7.5 and later on the new Elastic Path Documentation site. This Developer Center contains the guides for Commerce 6.13.0 through 7.4.1.Visit new site

This version of Elastic Path Commerce is no longer supported or maintained. To upgrade to the latest version, contact your Elastic Path representative.

Creating a State Policy aware dialog

Creating a State Policy aware dialog

Note: Before You Begin

Make sure that you have Creating a State Policy extension point in the plugin.

For a Commerce Manager dialog to be State Policies aware, it must extend AbstractPolicyAwareDialog and implement ObjectGuidReceiver.

The dialog must also have a zero-parameter constructor and must be able to perform all persistence operations (retrieve, update, save, etc.) by itself.

The following methods must be implemented:

  • getTargetIdentifier
  • setObjectGuid
  • createDialogContent
  • getOkButtonPolicyActionContainer
  • refreshLayout

getTargetIdentifier must return the unique identifier that will be used to map Creating a State Policy Contribution to this object.

         com.elasticpath.cmclient.catalog/src/main/java/com/elasticpath/cmclient/catalog/dialogs/catalog/VirtualCatalogDialog.java
	public String getTargetIdentifier() {
		if (this.virtualCatalog.isPersisted()) {
			return "virtualCatalogDialogTarget"; //$NON-NLS-1$
		}
		return "virualCatalogDialogEditableTarget"; //$NON-NLS-1$
	}
      

setObjectGuid must be implemented to retrieve the dialog's dependent object from its GUID.

         com.elasticpath.cmclient.catalog/src/main/java/com/elasticpath/cmclient/catalog/dialogs/catalog/VirtualCatalogDialog.java
public void setObjectGuid(final String objectGuid) {
	if (objectGuid == null) {
		virtualCatalog = getBeanFactory().getBean(ContextIdNames.CATALOG);
		virtualCatalog.setMaster(false);
	} else {
		virtualCatalog = catalogService.findByCode(objectGuid);
		if (virtualCatalog == null) {
			throw new IllegalArgumentException(
					NLS.bind(CoreMessages.Given_Object_Not_Exist, new String[] {"Virtual Catalog", objectGuid})); //$NON-NLS-1$
		}
	}
}
      

createDialogContent is responsible for creating and laying out the dialog's UI controls. The first step is usually to add a Policy Action Container. Then use the IPolicyTargetLayoutComposite's add methods to add UI controls. These methods take the PolicyActionContainer object as their final argument. This ensures that a StateChangeTarget is added to the PolicyActionContainer for each UI control that is added to the dialog.

         com.elasticpath.cmclient.catalog/src/main/java/com/elasticpath/cmclient/catalog/dialogs/catalog/VirtualCatalogDialog.java
protected void createDialogContent(final IPolicyTargetLayoutComposite dialogComposite) {
	
	// This name does not need to match anything necessarily
	readOnlyVirtualCatalogPolicyContainer = addPolicyActionContainer("readOnlyVirtualCatalogDialog"); //$NON-NLS-1$
	editableVirtualCatalogPolicyContainer = addPolicyActionContainer("virtualCatalogDialog"); //$NON-NLS-1$
	
	mainPolicyComposite = dialogComposite
			.addGridLayoutComposite(2, false, dialogComposite
					.createLayoutData(IEpLayoutData.FILL, IEpLayoutData.BEGINNING, true, false),
									  editableVirtualCatalogPolicyContainer);

	final IEpLayoutData mainCompositeFieldData = mainPolicyComposite
			.createLayoutData(IEpLayoutData.FILL, IEpLayoutData.BEGINNING, true, true);
	final IEpLayoutData mainCompositeLabelData = mainPolicyComposite
			.createLayoutData(IEpLayoutData.BEGINNING, IEpLayoutData.BEGINNING, false, true);

	if (isVirtualCatalogPersistent()) {
		mainPolicyComposite.addLabelBold(CatalogMessages.CatalogSummarySection_CatalogCode,
				                         mainCompositeLabelData, readOnlyVirtualCatalogPolicyContainer);
		
		this.catalogCode = mainPolicyComposite.addTextField(mainCompositeFieldData, readOnlyVirtualCatalogPolicyContainer);
	} else {
		mainPolicyComposite.addLabelBoldRequired(CatalogMessages.CatalogSummarySection_CatalogCode,
												 mainCompositeLabelData, editableVirtualCatalogPolicyContainer);
		this.catalogCode = mainPolicyComposite.addTextField(mainCompositeFieldData, editableVirtualCatalogPolicyContainer);
	}
      

getOkButtonPolicyActionContainer returns the Policy Action Container that will control the state of the dialog's OK or Save button.

         com.elasticpath.cmclient.catalog/src/main/java/com/elasticpath/cmclient/catalog/dialogs/catalog/VirtualCatalogDialog.java
protected PolicyActionContainer getOkButtonPolicyActionContainer() {
	return this.editableVirtualCatalogPolicyContainer;
}
      

refreshLayout can be implemented to refresh the layout of parent composites if required.

         com.elasticpath.cmclient.catalog/src/main/java/com/elasticpath/cmclient/catalog/dialogs/catalog/VirtualCatalogDialog.java
protected void refreshLayout() {
	if (mainPolicyComposite != null) {
		mainPolicyComposite.getSwtComposite().layout();
		mainPolicyComposite.getSwtComposite().getParent().layout();
	}
}
      

Registering the dialog for Change Sets

In order to edit an object from within a Change Sets editor, its dialog must be registered as a Change Set supported component. To do this, edit the plugin's plugin.xml and add a supportedComponents extension point (if one doesn't already exist). Inside the extension point, add a dialog element with a class attribute set to the name of your dialog class and an objectType attribute containing a display name for the dialog. The following example shows the registration for com.elasticpath.cmclient.catalog.dialogs.catalog.VirtualCatalogDialog.

<extension point="com.elasticpath.cmclient.changeset.supportedComponents">
    ...
    <dialog
        class="com.elasticpath.cmclient.catalog.dialogs.catalog.VirtualCatalogDialog"
        objectType="Catalog">
    </dialog>
</extension>