// rest of javadoc inherited
  public DeviceDefinitionPoliciesSection(Composite parent, int style, DeviceEditorContext context) {
    super(parent, style);

    setMinWidth(DEFAULT_MIN_WIDTH);
    this.context = context;
    DeviceRepositoryAccessorManager dram = context.getDeviceRepositoryAccessorManager();

    Section section = SectionFactory.createSection(this, SWT.NONE, TITLE, MESSAGE);
    GridData data = new GridData(GridData.FILL_BOTH);
    section.setLayoutData(data);

    categoriesComposite = new CategoriesComposite(section, CategoriesComposite.POLICIES, dram);
    section.setClient(categoriesComposite);

    // Set up a focus listener for maintaining global actions.
    categoriesComposite
        .getTreeViewer()
        .getControl()
        .addFocusListener(
            new FocusListener() {
              public void focusGained(FocusEvent event) {
                IActionBars actionBars =
                    DeviceDefinitionPoliciesSection.this.context.getActionBars();
                origDelete = actionBars.getGlobalActionHandler(IWorkbenchActionConstants.DELETE);
                actionBars.setGlobalActionHandler(
                    IWorkbenchActionConstants.DELETE, deletePolicyAction);
                actionBars.updateActionBars();
              }

              public void focusLost(FocusEvent event) {
                IActionBars actionBars =
                    DeviceDefinitionPoliciesSection.this.context.getActionBars();
                actionBars.setGlobalActionHandler(IWorkbenchActionConstants.DELETE, origDelete);
                actionBars.updateActionBars();
              }
            });

    // We use the selection manager for event handling because it allows
    // use to use filters which we need for resolving category elements
    // for use with ODOMAction enablement.
    categoriesComposite.addSelectionChangedListener(context.getODOMSelectionManager());

    data = new GridData(GridData.FILL_BOTH);
    categoriesComposite.setLayoutData(data);

    createActions();
    createContextMenu();

    createButtons(this, SWT.NONE);
  }
  /**
   * Add a new policy to the device repository in use. This will add the policy to the definitions
   * document and to the master device file.
   *
   * @param policyName the name of the new policy. Cannot be null.
   * @param composition the PolicyTypeComposition of the new policy
   * @param type the PolicyType of the new policy
   * @throws IllegalArgumentException if the named policy already exists or if any of the arguments
   *     are null.
   */
  private void addNewPolicyToRepository(
      String policyName, PolicyTypeComposition composition, PolicyType type) {
    if (policyName == null) {
      throw new IllegalArgumentException("Cannot be null: " + policyName);
    }
    if (composition == null) {
      throw new IllegalArgumentException("Cannot be null: " + composition);
    }
    if (type == null) {
      throw new IllegalArgumentException("Cannot be null: " + type);
    }
    String masterDeviceName = context.getDeviceRepositoryAccessorManager().retrieveRootDeviceName();
    boolean policyExists =
        context.getDeviceRepositoryAccessorManager().retrievePolicy(masterDeviceName, policyName)
            != null;

    if (policyExists) {
      throw new IllegalArgumentException(
          "Policy " + policyName + " already exists. Aborting new policy.");
    }

    // Add the policy to the definitions document
    Element category = categoriesComposite.getSelectedCategoryElement();
    Element policy =
        context
            .getODOMFactory()
            .element(DeviceRepositorySchemaConstants.POLICY_ELEMENT_NAME, category.getNamespace());
    policy.setAttribute(DeviceRepositorySchemaConstants.POLICY_NAME_ATTRIBUTE, policyName);
    category.addContent(policy);
    composition.addTypeElement(policy, type, context.getODOMFactory());

    // Add the policy to the master device
    Element masterDevice =
        context.getDeviceRepositoryAccessorManager().retrieveDeviceElement(masterDeviceName);

    StringBuffer xPathBuffer = new StringBuffer();
    xPathBuffer
        .append("//")
        . //$NON-NLS-1$
        append(MCSNamespace.DEVICE.getPrefix())
        .append(':')
        .append(DeviceRepositorySchemaConstants.POLICIES_ELEMENT_NAME);
    XPath policiesXPath = new XPath(xPathBuffer.toString(), new Namespace[] {MCSNamespace.DEVICE});

    try {
      Element policies = policiesXPath.selectSingleElement(masterDevice);
      composition.addDefaultPolicyValue(
          policies,
          policyName,
          type,
          context.getODOMFactory(),
          context.getDeviceRepositoryAccessorManager());
    } catch (XPathException e) {
      EclipseCommonPlugin.handleError(ABPlugin.getDefault(), e);
    }
  }
  /**
   * Select's the new named policy element in the tree.
   *
   * @param newPolicyName the name of the new policy to select.
   */
  private void selectNewPolicy(final String newPolicyName) {
    // Create the XPath for selecting the new policy element just created
    // from the definitions document.
    final StringBuffer newPolicyXPathBuffer = new StringBuffer();
    newPolicyXPathBuffer
        .append("//")
        .append(MCSNamespace.DEVICE_DEFINITIONS.getPrefix())
        .append(':')
        .append(DeviceRepositorySchemaConstants.POLICY_ELEMENT_NAME)
        .append("[@")
        .append(DeviceRepositorySchemaConstants.POLICY_NAME_ATTRIBUTE)
        .append("=\"")
        .append(newPolicyName)
        .append("\"]");
    final XPath newPolicyXPath =
        new XPath(
            newPolicyXPathBuffer.toString(), new Namespace[] {MCSNamespace.DEVICE_DEFINITIONS});

    Element newPolicyElement = null;
    try {
      // Get the definitions root for the XPath search.
      final Element definitionsRoot =
          context
              .getDeviceRepositoryAccessorManager()
              .getDeviceDefinitionsDocument()
              .getRootElement();
      // Retrieve the new policy element.
      newPolicyElement = newPolicyXPath.selectSingleElement(definitionsRoot);
    } catch (XPathException e) {
      EclipseCommonPlugin.handleError(ABPlugin.getDefault(), e);
    }

    TreeViewer treeViewer = categoriesComposite.getTreeViewer();

    // Expand the tree to the new policy element's level and select it.
    treeViewer.expandToLevel(newPolicyElement, 1);
    treeViewer.setSelection(new StructuredSelection(newPolicyElement), true);
  }
  /**
   * Add the restore button and the standard element handling facility.
   *
   * @param parent the parent composite for the resotre button.
   */
  private void createRestoreMechanism(Composite parent) {
    restoreButton = new Button(parent, SWT.NONE);
    restoreButton.setText(RESTORE_DEFAULTS_TEXT);
    restoreButton.setEnabled(!context.isAdminProject());
    restoreButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
    restoreButton.addSelectionListener(
        new SelectionListener() {
          public void widgetSelected(SelectionEvent e) {
            widgetDefaultSelected(e);
          }

          public void widgetDefaultSelected(SelectionEvent event) {
            if (deviceIDElement != null) {
              ((DeviceODOMElement) deviceIDElement).restore();
            }
          }
        });

    // Add a listener to the selection manager which responds to device
    // element selections.
    context
        .getODOMSelectionManager()
        .addSelectionListener(
            new ODOMElementSelectionListener() {

              public void selectionChanged(ODOMElementSelectionEvent event) {
                ODOMElementSelection selection = event.getSelection();

                if (!selection.isEmpty()) {
                  Element deviceElement = (Element) selection.getFirstElement();
                  // Retrieve the device name.
                  final String deviceName =
                      deviceElement.getAttributeValue(
                          DeviceRepositorySchemaConstants.DEVICE_NAME_ATTRIBUTE);

                  deviceIDElement =
                      context
                          .getDeviceRepositoryAccessorManager()
                          .retrieveDeviceIdentification(deviceName);

                  if (deviceIDElement == null) {
                    Object params =
                        new Object[] {
                          deviceName,
                          context.getDeviceRepositoryAccessorManager().getDeviceRepositoryName()
                        };
                    LOGGER.error("device-not-found", params);
                    String message = EXCEPTION_LOCALIZER.format("device-not-found", params);
                    throw new UndeclaredThrowableException(new RepositoryException(message));
                  } else {
                    DeviceODOMElement parent = (DeviceODOMElement) deviceIDElement.getParent();
                    if (parent == null) {
                      LOGGER.error("device-no-parent", deviceName);
                      String message = EXCEPTION_LOCALIZER.format("device-no-parent", deviceName);
                      throw new UndeclaredThrowableException(new RepositoryException(message));
                    } else {
                      parent.submitRestorableName(deviceName);
                    }
                  }
                } else {
                  deviceIDElement = null;
                }
              }
            },
            DEVICE_FILTER);
  }