KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > sample > mail > client > MailList


1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.sample.mail.client;
17
18 import com.google.gwt.user.client.ui.ClickListener;
19 import com.google.gwt.user.client.ui.Composite;
20 import com.google.gwt.user.client.ui.FlexTable;
21 import com.google.gwt.user.client.ui.HTML;
22 import com.google.gwt.user.client.ui.HorizontalPanel;
23 import com.google.gwt.user.client.ui.SourcesTableEvents;
24 import com.google.gwt.user.client.ui.TableListener;
25 import com.google.gwt.user.client.ui.Widget;
26
27 /**
28  * A composite that displays a list of emails that can be selected.
29  */

30 public class MailList extends Composite implements TableListener, ClickListener {
31
32   private static final int VISIBLE_EMAIL_COUNT = 10;
33
34   private HTML countLabel = new HTML();
35   private HTML newerButton = new HTML("<a HREF='javascript:;'>&lt; newer</a>",
36       true);
37   private HTML olderButton = new HTML("<a HREF='javascript:;'>older &gt;</a>",
38       true);
39   private int startIndex, selectedRow = -1;
40   private FlexTable table = new FlexTable();
41   private HorizontalPanel navBar = new HorizontalPanel();
42
43   public MailList() {
44     // Setup the table.
45
table.setCellSpacing(0);
46     table.setCellPadding(0);
47     table.setWidth("100%");
48
49     // Hook up events.
50
table.addTableListener(this);
51     newerButton.addClickListener(this);
52     olderButton.addClickListener(this);
53
54     // Create the 'navigation' bar at the upper-right.
55
HorizontalPanel innerNavBar = new HorizontalPanel();
56     navBar.setStyleName("mail-ListNavBar");
57     innerNavBar.add(newerButton);
58     innerNavBar.add(countLabel);
59     innerNavBar.add(olderButton);
60
61     navBar.setHorizontalAlignment(HorizontalPanel.ALIGN_RIGHT);
62     navBar.add(innerNavBar);
63     navBar.setWidth("100%");
64
65     initWidget(table);
66     setStyleName("mail-List");
67
68     initTable();
69     update();
70   }
71
72   public void onCellClicked(SourcesTableEvents sender, int row, int cell) {
73     // Select the row that was clicked (-1 to account for header row).
74
if (row > 0) {
75       selectRow(row - 1);
76     }
77   }
78
79   public void onClick(Widget sender) {
80     if (sender == olderButton) {
81       // Move forward a page.
82
startIndex += VISIBLE_EMAIL_COUNT;
83       if (startIndex >= MailItems.getMailItemCount()) {
84         startIndex -= VISIBLE_EMAIL_COUNT;
85       } else {
86         styleRow(selectedRow, false);
87         selectedRow = -1;
88         update();
89       }
90     } else if (sender == newerButton) {
91       // Move back a page.
92
startIndex -= VISIBLE_EMAIL_COUNT;
93       if (startIndex < 0) {
94         startIndex = 0;
95       } else {
96         styleRow(selectedRow, false);
97         selectedRow = -1;
98         update();
99       }
100     }
101   }
102
103   /**
104    * Initializes the table so that it contains enough rows for a full page of
105    * emails. Also creates the images that will be used as 'read' flags.
106    */

107   private void initTable() {
108     // Create the header row.
109
table.setText(0, 0, "Sender");
110     table.setText(0, 1, "Email");
111     table.setText(0, 2, "Subject");
112     table.setWidget(0, 3, navBar);
113     table.getRowFormatter().setStyleName(0, "mail-ListHeader");
114
115     // Initialize the rest of the rows.
116
for (int i = 0; i < VISIBLE_EMAIL_COUNT; ++i) {
117       table.setText(i + 1, 0, "");
118       table.setText(i + 1, 1, "");
119       table.setText(i + 1, 2, "");
120       table.getCellFormatter().setWordWrap(i + 1, 0, false);
121       table.getCellFormatter().setWordWrap(i + 1, 1, false);
122       table.getCellFormatter().setWordWrap(i + 1, 2, false);
123       table.getFlexCellFormatter().setColSpan(i + 1, 2, 2);
124     }
125   }
126
127   /**
128    * Selects the given row (relative to the current page).
129    *
130    * @param row the row to be selected
131    */

132   private void selectRow(int row) {
133     // When a row (other than the first one, which is used as a header) is
134
// selected, display its associated MailItem.
135
MailItem item = MailItems.getMailItem(startIndex + row);
136     if (item == null) {
137       return;
138     }
139
140     styleRow(selectedRow, false);
141     styleRow(row, true);
142
143     item.read = true;
144     selectedRow = row;
145     Mail.get().displayItem(item);
146   }
147
148   private void styleRow(int row, boolean selected) {
149     if (row != -1) {
150       if (selected) {
151         table.getRowFormatter().addStyleName(row + 1, "mail-SelectedRow");
152       } else {
153         table.getRowFormatter().removeStyleName(row + 1, "mail-SelectedRow");
154       }
155     }
156   }
157
158   private void update() {
159     // Update the older/newer buttons & label.
160
int count = MailItems.getMailItemCount();
161     int max = startIndex + VISIBLE_EMAIL_COUNT;
162     if (max > count) {
163       max = count;
164     }
165
166     newerButton.setVisible(startIndex != 0);
167     olderButton.setVisible(startIndex + VISIBLE_EMAIL_COUNT < count);
168     countLabel.setText("" + (startIndex + 1) + " - " + max + " of " + count);
169
170     // Show the selected emails.
171
int i = 0;
172     for (; i < VISIBLE_EMAIL_COUNT; ++i) {
173       // Don't read past the end.
174
if (startIndex + i >= MailItems.getMailItemCount()) {
175         break;
176       }
177
178       MailItem item = MailItems.getMailItem(startIndex + i);
179
180       // Add a new row to the table, then set each of its columns to the
181
// email's sender and subject values.
182
table.setText(i + 1, 0, item.sender);
183       table.setText(i + 1, 1, item.email);
184       table.setText(i + 1, 2, item.subject);
185     }
186
187     // Clear any remaining slots.
188
for (; i < VISIBLE_EMAIL_COUNT; ++i) {
189       table.setHTML(i + 1, 0, "&nbsp;");
190       table.setHTML(i + 1, 1, "&nbsp;");
191       table.setHTML(i + 1, 2, "&nbsp;");
192     }
193
194     // Select the first row if none is selected.
195
if (selectedRow == -1) {
196       selectRow(0);
197     }
198   }
199 }
200
Popular Tags