001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.sample.mail.client;
017:
018: import com.google.gwt.user.client.ui.ClickListener;
019: import com.google.gwt.user.client.ui.Composite;
020: import com.google.gwt.user.client.ui.FlexTable;
021: import com.google.gwt.user.client.ui.HTML;
022: import com.google.gwt.user.client.ui.HorizontalPanel;
023: import com.google.gwt.user.client.ui.SourcesTableEvents;
024: import com.google.gwt.user.client.ui.TableListener;
025: import com.google.gwt.user.client.ui.Widget;
026:
027: /**
028: * A composite that displays a list of emails that can be selected.
029: */
030: public class MailList extends Composite implements TableListener,
031: ClickListener {
032:
033: private static final int VISIBLE_EMAIL_COUNT = 10;
034:
035: private HTML countLabel = new HTML();
036: private HTML newerButton = new HTML(
037: "<a href='javascript:;'>< newer</a>", true);
038: private HTML olderButton = new HTML(
039: "<a href='javascript:;'>older ></a>", true);
040: private int startIndex, selectedRow = -1;
041: private FlexTable table = new FlexTable();
042: private HorizontalPanel navBar = new HorizontalPanel();
043:
044: public MailList() {
045: // Setup the table.
046: table.setCellSpacing(0);
047: table.setCellPadding(0);
048: table.setWidth("100%");
049:
050: // Hook up events.
051: table.addTableListener(this );
052: newerButton.addClickListener(this );
053: olderButton.addClickListener(this );
054:
055: // Create the 'navigation' bar at the upper-right.
056: HorizontalPanel innerNavBar = new HorizontalPanel();
057: navBar.setStyleName("mail-ListNavBar");
058: innerNavBar.add(newerButton);
059: innerNavBar.add(countLabel);
060: innerNavBar.add(olderButton);
061:
062: navBar.setHorizontalAlignment(HorizontalPanel.ALIGN_RIGHT);
063: navBar.add(innerNavBar);
064: navBar.setWidth("100%");
065:
066: initWidget(table);
067: setStyleName("mail-List");
068:
069: initTable();
070: update();
071: }
072:
073: public void onCellClicked(SourcesTableEvents sender, int row,
074: int cell) {
075: // Select the row that was clicked (-1 to account for header row).
076: if (row > 0) {
077: selectRow(row - 1);
078: }
079: }
080:
081: public void onClick(Widget sender) {
082: if (sender == olderButton) {
083: // Move forward a page.
084: startIndex += VISIBLE_EMAIL_COUNT;
085: if (startIndex >= MailItems.getMailItemCount()) {
086: startIndex -= VISIBLE_EMAIL_COUNT;
087: } else {
088: styleRow(selectedRow, false);
089: selectedRow = -1;
090: update();
091: }
092: } else if (sender == newerButton) {
093: // Move back a page.
094: startIndex -= VISIBLE_EMAIL_COUNT;
095: if (startIndex < 0) {
096: startIndex = 0;
097: } else {
098: styleRow(selectedRow, false);
099: selectedRow = -1;
100: update();
101: }
102: }
103: }
104:
105: /**
106: * Initializes the table so that it contains enough rows for a full page of
107: * emails. Also creates the images that will be used as 'read' flags.
108: */
109: private void initTable() {
110: // Create the header row.
111: table.setText(0, 0, "Sender");
112: table.setText(0, 1, "Email");
113: table.setText(0, 2, "Subject");
114: table.setWidget(0, 3, navBar);
115: table.getRowFormatter().setStyleName(0, "mail-ListHeader");
116:
117: // Initialize the rest of the rows.
118: for (int i = 0; i < VISIBLE_EMAIL_COUNT; ++i) {
119: table.setText(i + 1, 0, "");
120: table.setText(i + 1, 1, "");
121: table.setText(i + 1, 2, "");
122: table.getCellFormatter().setWordWrap(i + 1, 0, false);
123: table.getCellFormatter().setWordWrap(i + 1, 1, false);
124: table.getCellFormatter().setWordWrap(i + 1, 2, false);
125: table.getFlexCellFormatter().setColSpan(i + 1, 2, 2);
126: }
127: }
128:
129: /**
130: * Selects the given row (relative to the current page).
131: *
132: * @param row the row to be selected
133: */
134: private void selectRow(int row) {
135: // When a row (other than the first one, which is used as a header) is
136: // selected, display its associated MailItem.
137: MailItem item = MailItems.getMailItem(startIndex + row);
138: if (item == null) {
139: return;
140: }
141:
142: styleRow(selectedRow, false);
143: styleRow(row, true);
144:
145: item.read = true;
146: selectedRow = row;
147: Mail.get().displayItem(item);
148: }
149:
150: private void styleRow(int row, boolean selected) {
151: if (row != -1) {
152: if (selected) {
153: table.getRowFormatter().addStyleName(row + 1,
154: "mail-SelectedRow");
155: } else {
156: table.getRowFormatter().removeStyleName(row + 1,
157: "mail-SelectedRow");
158: }
159: }
160: }
161:
162: private void update() {
163: // Update the older/newer buttons & label.
164: int count = MailItems.getMailItemCount();
165: int max = startIndex + VISIBLE_EMAIL_COUNT;
166: if (max > count) {
167: max = count;
168: }
169:
170: newerButton.setVisible(startIndex != 0);
171: olderButton
172: .setVisible(startIndex + VISIBLE_EMAIL_COUNT < count);
173: countLabel.setText("" + (startIndex + 1) + " - " + max + " of "
174: + count);
175:
176: // Show the selected emails.
177: int i = 0;
178: for (; i < VISIBLE_EMAIL_COUNT; ++i) {
179: // Don't read past the end.
180: if (startIndex + i >= MailItems.getMailItemCount()) {
181: break;
182: }
183:
184: MailItem item = MailItems.getMailItem(startIndex + i);
185:
186: // Add a new row to the table, then set each of its columns to the
187: // email's sender and subject values.
188: table.setText(i + 1, 0, item.sender);
189: table.setText(i + 1, 1, item.email);
190: table.setText(i + 1, 2, item.subject);
191: }
192:
193: // Clear any remaining slots.
194: for (; i < VISIBLE_EMAIL_COUNT; ++i) {
195: table.setHTML(i + 1, 0, " ");
196: table.setHTML(i + 1, 1, " ");
197: table.setHTML(i + 1, 2, " ");
198: }
199:
200: // Select the first row if none is selected.
201: if (selectedRow == -1) {
202: selectRow(0);
203: }
204: }
205: }
|