/** Fills the chart with the content of the list of {@link HeapSegmentElement}. */
  private void showChart(ArrayList<HeapSegmentElement> list) {
    mAllocCountDataSet.clear();

    if (list != null) {
      String rowKey = "Alloc Count";

      long currentSize = -1;
      int currentCount = 0;
      for (HeapSegmentElement element : list) {
        if (element.getLength() != currentSize) {
          if (currentSize != -1) {
            ByteLong columnKey = new ByteLong(currentSize);
            mAllocCountDataSet.addValue(currentCount, rowKey, columnKey);
          }

          currentSize = element.getLength();
          currentCount = 1;
        } else {
          currentCount++;
        }
      }

      // add the last item
      if (currentSize != -1) {
        ByteLong columnKey = new ByteLong(currentSize);
        mAllocCountDataSet.addValue(currentCount, rowKey, columnKey);
      }
    }
  }
  private void fillDetailedTable(Client client, boolean forceRedraw) {
    // first check if the client is invalid or heap updates are not enabled.
    if (client == null || client.isHeapUpdateEnabled() == false) {
      mStatisticsTable.removeAll();
      showChart(null);
      return;
    }

    ClientData cd = client.getClientData();

    Map<Integer, ArrayList<HeapSegmentElement>> heapMap;

    // Atomically get and clear the heap data.
    synchronized (cd) {
      if (serializeHeapData(cd.getVmHeapData()) == false && forceRedraw == false) {
        // no change, we return.
        return;
      }

      heapMap = cd.getVmHeapData().getProcessedHeapMap();
    }

    // we have new data, lets display it.

    // First, get the current selection, and its key.
    int index = mStatisticsTable.getSelectionIndex();
    Integer selectedKey = null;
    if (index != -1) {
      selectedKey = (Integer) mStatisticsTable.getItem(index).getData();
    }

    // disable redraws and remove all from the table.
    mStatisticsTable.setRedraw(false);
    mStatisticsTable.removeAll();

    if (heapMap != null) {
      int selectedIndex = -1;
      ArrayList<HeapSegmentElement> selectedList = null;

      // get the keys
      Set<Integer> keys = heapMap.keySet();
      int iter = 0; // use a manual iter int because Set<?> doesn't have an index
      // based accessor.
      for (Integer key : keys) {
        ArrayList<HeapSegmentElement> list = heapMap.get(key);

        // check if this is the key that is supposed to be selected
        if (key.equals(selectedKey)) {
          selectedIndex = iter;
          selectedList = list;
        }
        iter++;

        TableItem item = new TableItem(mStatisticsTable, SWT.NONE);
        item.setData(key);

        // get the type
        item.setText(0, mMapLegend[key]);

        // set the count, smallest, largest
        int count = list.size();
        item.setText(1, addCommasToNumber(count));

        if (count > 0) {
          item.setText(3, prettyByteCount(list.get(0).getLength()));
          item.setText(4, prettyByteCount(list.get(count - 1).getLength()));

          int median = count / 2;
          HeapSegmentElement element = list.get(median);
          long size = element.getLength();
          item.setText(5, prettyByteCount(size));

          long totalSize = 0;
          for (int i = 0; i < count; i++) {
            element = list.get(i);

            size = element.getLength();
            totalSize += size;
          }

          // set the average and total
          item.setText(2, prettyByteCount(totalSize));
          item.setText(6, prettyByteCount(totalSize / count));
        }
      }

      mStatisticsTable.setRedraw(true);

      if (selectedIndex != -1) {
        mStatisticsTable.setSelection(selectedIndex);
        showChart(selectedList);
      } else {
        showChart(null);
      }
    } else {
      mStatisticsTable.setRedraw(true);
    }
  }