/*
   * Handle the set data event. Set the item data of the requested item to the
   * corresponding proposal in the proposal cache.
   */
  private void handleSetData(Event event) {
    TableItem item = (TableItem) event.item;
    int index = proposalTable.indexOf(item);

    int proposalIndex = 0;
    for (String provider : proposalList.getProviderList()) {
      if (index == proposalIndex) {
        int count = proposalList.getCount(provider);
        String text = provider + " (" + count + " matching items)";
        item.setText(text);
        // Data == null => not selectable
        item.setData(null);

        Display display = Display.getCurrent();
        Color color = display.getSystemColor(SWT.COLOR_GRAY);
        FontData fontData = item.getFont().getFontData()[0];
        Font font =
            new Font(
                display,
                new FontData(fontData.getName(), fontData.getHeight(), SWT.ITALIC | SWT.BOLD));
        item.setBackground(color);
        item.setFont(font);

        return;
      }
      proposalIndex++;
      for (IContentProposal proposal : proposalList.getProposals(provider)) {
        if (index == proposalIndex) {
          item.setText("  " + getString(proposal));
          item.setImage(getImage(proposal));
          item.setData(proposal);
          return;
        }
        proposalIndex++;
      }
    }
  }
  /*
   * Caches the specified proposals and repopulates the table if it has been
   * created.
   */
  private void setProposals(ContentProposalList newProposalList) {
    if (newProposalList == null || newProposalList.length() == 0) {
      newProposalList = getEmptyProposalArray();
    }
    this.proposalList = newProposalList;
    if (!isValid()) return;

    // If there is a table
    if (isValid()) {
      if (USE_VIRTUAL) {
        // Set and clear the virtual table. Data will be
        // provided in the SWT.SetData event handler.
        proposalTable.setItemCount(getTableLength());
        proposalTable.clearAll();
      } else {
        // Populate the table manually
        proposalTable.setRedraw(false);

        int itemCount = newProposalList.length() + newProposalList.getProviderList().size();
        proposalTable.setItemCount(itemCount);
        TableItem[] items = proposalTable.getItems();

        int index = 0;
        for (String provider : newProposalList.getProviderList()) {
          TableItem item = items[index];
          int count = newProposalList.getCount(provider);
          String text = provider + " (" + count + " matching items)";
          item.setText(text);
          // Data == null => not selectable
          item.setData(null);

          Display display = Display.getCurrent();
          Color color = display.getSystemColor(SWT.COLOR_GRAY);
          FontData fontData = item.getFont().getFontData()[0];
          Font font =
              new Font(
                  display,
                  new FontData(fontData.getName(), fontData.getHeight(), SWT.ITALIC | SWT.BOLD));
          item.setBackground(color);
          item.setFont(font);

          index++;
          for (IContentProposal proposal : newProposalList.getProposals(provider)) {
            item.setText("  " + getString(proposal));
            item.setImage(getImage(proposal));
            item.setData(proposal);
            index++;
          }
        }

        proposalTable.setRedraw(true);
      }
      // Default to the first selection if there is content.
      if (newProposalList.length() > 0) {
        int index = 0;
        boolean selected = false;
        for (String provider : newProposalList.getProviderList()) {
          index++;
          if (!selected && newProposalList.getCount(provider) > 0) {
            selectProposal(index);
            selected = true;
          }
        }
      } else {
        // No selection, close the secondary popup if it was open
        if (infoPopup != null) {
          infoPopup.close();
        }
      }
    }
    footer.setText("");
  }