private Widget createButtonPanel(final FlexTable mainPanel, final boolean forceOpen) {
    final IconButton reloadImageButton =
        new IconButton(
            "Refresh",
            true,
            BaseImageBundle.images.refresh_16(),
            BaseImageBundle.images.refresh_16(),
            BaseImageBundle.images.refresh_16(),
            BaseImageBundle.images.refresh_disabled_16());
    reloadImageButton.setSTYLE("commentToolBarButton");
    reloadImageButton.setTitle("Refresh comments");
    reloadImageButton.addClickHandler(
        new ClickHandler() {
          public void onClick(ClickEvent event) {
            BaseServiceCache.getService()
                .getPageInfo(
                    permissibleObject,
                    Comment.class.getName(),
                    pageSize,
                    new AsyncCallback<PageInfo>() {
                      public void onFailure(Throwable caught) {};

                      public void onSuccess(PageInfo pageInfo) {
                        numComments = pageInfo.getTotalRowCount();
                        lastPageNumber = pageInfo.getLastPageNumber();
                        pageCache.clear();
                        fetchPage();
                      };
                    });
          }
        });

    final IconButton sortImageButton =
        new IconButton(
            "Sort " + (sortDescending ? "Ascending" : "Descending"),
            true,
            BaseImageBundle.images.sort(),
            BaseImageBundle.images.sort(),
            BaseImageBundle.images.sort(),
            BaseImageBundle.images.sort());
    sortImageButton.setSTYLE("commentToolBarButton");
    sortImageButton.setTitle(
        sortDescending ? "Show oldest comments first" : "Show most recent comments first");
    sortImageButton.addClickHandler(
        new ClickHandler() {
          public void onClick(ClickEvent event) {
            sortDescending = !sortDescending;
            // this could be optimized if we have all the pages, then we have all the data
            // we could do it all on the client
            pageCache.clear();
            fetchPage();
          }
        });

    IconButton flattenImageButton = null;
    if (flatten) {
      flattenImageButton =
          new IconButton(
              "Hierarchy",
              true,
              BaseImageBundle.images.hierarchy(),
              BaseImageBundle.images.hierarchy(),
              BaseImageBundle.images.hierarchy(),
              BaseImageBundle.images.hierarchy());
      flattenImageButton.setTitle("Build a comment hierarchy");
    } else {
      flattenImageButton =
          new IconButton(
              "Flatten",
              true,
              BaseImageBundle.images.flatten(),
              BaseImageBundle.images.flatten(),
              BaseImageBundle.images.flatten(),
              BaseImageBundle.images.flatten());
      flattenImageButton.setTitle("Flatten the comment hierarchy");
    }
    flattenImageButton.setSTYLE("commentToolBarButton");
    flattenImageButton.addClickHandler(
        new ClickHandler() {
          public void onClick(ClickEvent event) {
            flatten = !flatten;
            loadCommentWidget(forceOpen);
          }
        });

    HorizontalPanel buttonPanel = new HorizontalPanel();
    buttonPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
    buttonPanel.add(createPageControllerPanel(mainPanel));
    Label spacer1 = new Label();
    buttonPanel.add(spacer1);
    buttonPanel.setCellWidth(spacer1, "20px");
    buttonPanel.add(reloadImageButton);
    buttonPanel.add(sortImageButton);
    buttonPanel.add(flattenImageButton);
    Label maxCommentDepthLabel = new Label("Max Depth", false);
    maxCommentDepthLabel.setTitle("Set the maximum depth of comments to show");
    Label spacer2 = new Label();
    buttonPanel.add(spacer2);
    buttonPanel.setCellWidth(spacer2, "20px");
    buttonPanel.add(maxCommentDepthLabel);
    buttonPanel.add(maxCommentDepthListBox);
    return buttonPanel;
  }
  private Widget createPageControllerPanel(final FlexTable mainPanel) {
    final IconButton nextPageImageButton =
        new IconButton(
            null,
            true,
            BaseImageBundle.images.next(),
            BaseImageBundle.images.next(),
            BaseImageBundle.images.next(),
            BaseImageBundle.images.next());
    nextPageImageButton.setSTYLE("commentToolBarButton");
    nextPageImageButton.addClickHandler(
        new ClickHandler() {
          public void onClick(ClickEvent event) {
            if (pageNumber == lastPageNumber) {
              return;
            }
            pageNumber++;
            fetchPage();
          }
        });
    final IconButton previousPageImageButton =
        new IconButton(
            null,
            false,
            BaseImageBundle.images.previous(),
            BaseImageBundle.images.previous(),
            BaseImageBundle.images.previous(),
            BaseImageBundle.images.previous());
    previousPageImageButton.setSTYLE("commentToolBarButton");
    previousPageImageButton.addClickHandler(
        new ClickHandler() {
          public void onClick(ClickEvent event) {
            if (pageNumber == 0) {
              return;
            }
            pageNumber--;
            fetchPage();
          }
        });
    final IconButton lastPageImageButton =
        new IconButton(
            null,
            false,
            BaseImageBundle.images.last(),
            BaseImageBundle.images.last(),
            BaseImageBundle.images.last(),
            BaseImageBundle.images.last());
    lastPageImageButton.setSTYLE("commentToolBarButton");
    lastPageImageButton.addClickHandler(
        new ClickHandler() {
          public void onClick(ClickEvent event) {
            pageNumber = (int) lastPageNumber;
            fetchPage();
          }
        });
    final IconButton firstPageImageButton =
        new IconButton(
            null,
            false,
            BaseImageBundle.images.first(),
            BaseImageBundle.images.first(),
            BaseImageBundle.images.first(),
            BaseImageBundle.images.first());
    firstPageImageButton.setSTYLE("commentToolBarButton");
    firstPageImageButton.addClickHandler(
        new ClickHandler() {
          public void onClick(ClickEvent event) {
            pageNumber = 0;
            fetchPage();
          }
        });
    if (lastPageNumber < 0) {
      firstPageImageButton.setEnabled(false);
      previousPageImageButton.setEnabled(false);
      nextPageImageButton.setEnabled(false);
      lastPageImageButton.setEnabled(false);
    }

    HorizontalPanel buttonPanel = new HorizontalPanel();
    buttonPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
    buttonPanel.add(firstPageImageButton);
    buttonPanel.add(previousPageImageButton);
    Label pageLabel = new Label("Page " + (pageNumber + 1) + " of " + (lastPageNumber + 1), false);
    if (lastPageNumber < 0) {
      pageLabel.setText("Page 1 of 1");
    }
    DOM.setStyleAttribute(pageLabel.getElement(), "margin", "0 5px 0 5px");
    buttonPanel.add(pageLabel);
    buttonPanel.add(nextPageImageButton);
    buttonPanel.add(lastPageImageButton);
    return buttonPanel;
  }