/**
   * Creates the context menu that is associated with the tree.
   *
   * <p><strong>The {@link #createActions} method must have been invoked prior to this
   * method</strong>
   */
  private void createContextMenu() {
    // Create menu manager.
    MenuManager menuManager = new MenuManager();
    menuManager.add(newPolicyAction);
    menuManager.add(new Separator());
    menuManager.add(deletePolicyAction);

    // Create the menu and add it to the tree.
    final Tree tree = categoriesComposite.getTreeViewer().getTree();
    Menu menu = menuManager.createContextMenu(tree);
    tree.setMenu(menu);
  }
  // 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);
  }
  /**
   * 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);
  }