FileDocCategorySizeDatePackage
MailList.javaAPI DocExample6133Mon Aug 27 20:12:46 BST 2007com.google.gwt.sample.mail.client

MailList

public class MailList extends com.google.gwt.user.client.ui.Composite implements com.google.gwt.user.client.ui.TableListener, com.google.gwt.user.client.ui.ClickListener
A composite that displays a list of emails that can be selected.

Fields Summary
private static final int
VISIBLE_EMAIL_COUNT
private com.google.gwt.user.client.ui.HTML
countLabel
private com.google.gwt.user.client.ui.HTML
newerButton
private com.google.gwt.user.client.ui.HTML
olderButton
private int
startIndex
private int
selectedRow
private com.google.gwt.user.client.ui.FlexTable
table
private com.google.gwt.user.client.ui.HorizontalPanel
navBar
Constructors Summary
public MailList()


    
    // Setup the table.
    table.setCellSpacing(0);
    table.setCellPadding(0);
    table.setWidth("100%");

    // Hook up events.
    table.addTableListener(this);
    newerButton.addClickListener(this);
    olderButton.addClickListener(this);

    // Create the 'navigation' bar at the upper-right.
    HorizontalPanel innerNavBar = new HorizontalPanel();
    navBar.setStyleName("mail-ListNavBar");
    innerNavBar.add(newerButton);
    innerNavBar.add(countLabel);
    innerNavBar.add(olderButton);

    navBar.setHorizontalAlignment(HorizontalPanel.ALIGN_RIGHT);
    navBar.add(innerNavBar);
    navBar.setWidth("100%");

    initWidget(table);
    setStyleName("mail-List");

    initTable();
    update();
  
Methods Summary
private voidinitTable()
Initializes the table so that it contains enough rows for a full page of emails. Also creates the images that will be used as 'read' flags.

    // Create the header row.
    table.setText(0, 0, "Sender");
    table.setText(0, 1, "Email");
    table.setText(0, 2, "Subject");
    table.setWidget(0, 3, navBar);
    table.getRowFormatter().setStyleName(0, "mail-ListHeader");

    // Initialize the rest of the rows.
    for (int i = 0; i < VISIBLE_EMAIL_COUNT; ++i) {
      table.setText(i + 1, 0, "");
      table.setText(i + 1, 1, "");
      table.setText(i + 1, 2, "");
      table.getCellFormatter().setWordWrap(i + 1, 0, false);
      table.getCellFormatter().setWordWrap(i + 1, 1, false);
      table.getCellFormatter().setWordWrap(i + 1, 2, false);
      table.getFlexCellFormatter().setColSpan(i + 1, 2, 2);
    }
  
public voidonCellClicked(com.google.gwt.user.client.ui.SourcesTableEvents sender, int row, int cell)

    // Select the row that was clicked (-1 to account for header row).
    if (row > 0) {
      selectRow(row - 1);
    }
  
public voidonClick(com.google.gwt.user.client.ui.Widget sender)

    if (sender == olderButton) {
      // Move forward a page.
      startIndex += VISIBLE_EMAIL_COUNT;
      if (startIndex >= MailItems.getMailItemCount()) {
        startIndex -= VISIBLE_EMAIL_COUNT;
      } else {
        styleRow(selectedRow, false);
        selectedRow = -1;
        update();
      }
    } else if (sender == newerButton) {
      // Move back a page.
      startIndex -= VISIBLE_EMAIL_COUNT;
      if (startIndex < 0) {
        startIndex = 0;
      } else {
        styleRow(selectedRow, false);
        selectedRow = -1;
        update();
      }
    }
  
private voidselectRow(int row)
Selects the given row (relative to the current page).

param
row the row to be selected

    // When a row (other than the first one, which is used as a header) is
    // selected, display its associated MailItem.
    MailItem item = MailItems.getMailItem(startIndex + row);
    if (item == null) {
      return;
    }

    styleRow(selectedRow, false);
    styleRow(row, true);

    item.read = true;
    selectedRow = row;
    Mail.get().displayItem(item);
  
private voidstyleRow(int row, boolean selected)

    if (row != -1) {
      if (selected) {
        table.getRowFormatter().addStyleName(row + 1, "mail-SelectedRow");
      } else {
        table.getRowFormatter().removeStyleName(row + 1, "mail-SelectedRow");
      }
    }
  
private voidupdate()

    // Update the older/newer buttons & label.
    int count = MailItems.getMailItemCount();
    int max = startIndex + VISIBLE_EMAIL_COUNT;
    if (max > count) {
      max = count;
    }

    newerButton.setVisible(startIndex != 0);
    olderButton.setVisible(startIndex + VISIBLE_EMAIL_COUNT < count);
    countLabel.setText("" + (startIndex + 1) + " - " + max + " of " + count);

    // Show the selected emails.
    int i = 0;
    for (; i < VISIBLE_EMAIL_COUNT; ++i) {
      // Don't read past the end.
      if (startIndex + i >= MailItems.getMailItemCount()) {
        break;
      }

      MailItem item = MailItems.getMailItem(startIndex + i);

      // Add a new row to the table, then set each of its columns to the
      // email's sender and subject values.
      table.setText(i + 1, 0, item.sender);
      table.setText(i + 1, 1, item.email);
      table.setText(i + 1, 2, item.subject);
    }

    // Clear any remaining slots.
    for (; i < VISIBLE_EMAIL_COUNT; ++i) {
      table.setHTML(i + 1, 0, " ");
      table.setHTML(i + 1, 1, " ");
      table.setHTML(i + 1, 2, " ");
    }

    // Select the first row if none is selected.
    if (selectedRow == -1) {
      selectRow(0);
    }