KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > sample > dynatable > client > DynaTableWidget


1 /*
2  * Copyright 2006 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.dynatable.client;
17
18 import com.google.gwt.sample.dynatable.client.DynaTableDataProvider.RowDataAcceptor;
19 import com.google.gwt.user.client.ui.Button;
20 import com.google.gwt.user.client.ui.ClickListener;
21 import com.google.gwt.user.client.ui.Composite;
22 import com.google.gwt.user.client.ui.DockPanel;
23 import com.google.gwt.user.client.ui.Grid;
24 import com.google.gwt.user.client.ui.HasAlignment;
25 import com.google.gwt.user.client.ui.HorizontalPanel;
26 import com.google.gwt.user.client.ui.HTML;
27 import com.google.gwt.user.client.ui.Widget;
28
29 /**
30  * A composite Widget that implements the main interface for the dynamic table,
31  * including the data table, status indicators, and paging buttons.
32  */

33 public class DynaTableWidget extends Composite {
34
35   private class NavBar extends Composite implements ClickListener {
36
37     public final DockPanel bar = new DockPanel();
38
39     public final Button gotoFirst = new Button("<<", this);
40
41     public final Button gotoNext = new Button(">", this);
42     public final Button gotoPrev = new Button("<", this);
43     public final HTML status = new HTML();
44
45     public NavBar() {
46       initWidget(bar);
47       bar.setStyleName("navbar");
48       status.setStyleName("status");
49
50       HorizontalPanel buttons = new HorizontalPanel();
51       buttons.add(gotoFirst);
52       buttons.add(gotoPrev);
53       buttons.add(gotoNext);
54       bar.add(buttons, DockPanel.EAST);
55       bar.setCellHorizontalAlignment(buttons, DockPanel.ALIGN_RIGHT);
56       bar.add(status, DockPanel.CENTER);
57       bar.setVerticalAlignment(DockPanel.ALIGN_MIDDLE);
58       bar.setCellHorizontalAlignment(status, HasAlignment.ALIGN_RIGHT);
59       bar.setCellVerticalAlignment(status, HasAlignment.ALIGN_MIDDLE);
60       bar.setCellWidth(status, "100%");
61
62       // Initialize prev & first button to disabled.
63
//
64
gotoPrev.setEnabled(false);
65       gotoFirst.setEnabled(false);
66     }
67
68     public void onClick(Widget sender) {
69       if (sender == gotoNext) {
70         startRow += getDataRowCount();
71         refresh();
72       } else if (sender == gotoPrev) {
73         startRow -= getDataRowCount();
74         if (startRow < 0) {
75           startRow = 0;
76         }
77         refresh();
78       } else if (sender == gotoFirst) {
79         startRow = 0;
80         refresh();
81       }
82     }
83   }
84
85   private class RowDataAcceptorImpl implements RowDataAcceptor {
86     public void accept(int startRow, String JavaDoc[][] data) {
87
88       int destRowCount = getDataRowCount();
89       int destColCount = grid.getCellCount(0);
90       assert (data.length <= destRowCount) : "Too many rows";
91
92       int srcRowIndex = 0;
93       int srcRowCount = data.length;
94       int destRowIndex = 1; // skip navbar row
95
for (; srcRowIndex < srcRowCount; ++srcRowIndex, ++destRowIndex) {
96         String JavaDoc[] srcRowData = data[srcRowIndex];
97         assert (srcRowData.length == destColCount) : " Column count mismatch";
98         for (int srcColIndex = 0; srcColIndex < destColCount; ++srcColIndex) {
99           String JavaDoc cellHTML = srcRowData[srcColIndex];
100           grid.setText(destRowIndex, srcColIndex, cellHTML);
101         }
102       }
103
104       // Clear remaining table rows.
105
//
106
boolean isLastPage = false;
107       for (; destRowIndex < destRowCount + 1; ++destRowIndex) {
108         isLastPage = true;
109         for (int destColIndex = 0; destColIndex < destColCount; ++destColIndex) {
110           grid.clearCell(destRowIndex, destColIndex);
111         }
112       }
113
114       // Synchronize the nav buttons.
115
//
116
navbar.gotoNext.setEnabled(!isLastPage);
117       navbar.gotoFirst.setEnabled(startRow > 0);
118       navbar.gotoPrev.setEnabled(startRow > 0);
119
120       // Update the status message.
121
//
122
setStatusText((startRow + 1) + " - " + (startRow + srcRowCount));
123     }
124
125     public void failed(Throwable JavaDoc caught) {
126       String JavaDoc msg = "Failed to access data";
127       if (caught != null) {
128         msg += ": " + caught.getMessage();
129       }
130       setStatusText(msg);
131     }
132   }
133
134   private final RowDataAcceptor acceptor = new RowDataAcceptorImpl();
135
136   private final NavBar navbar = new NavBar();
137
138   private final DockPanel outer = new DockPanel();
139
140   private final DynaTableDataProvider provider;
141
142   private int startRow = 0;
143
144   private final Grid grid = new Grid();
145
146   public DynaTableWidget(DynaTableDataProvider provider, String JavaDoc[] columns,
147       String JavaDoc[] columnStyles, int rowCount) {
148
149     if (columns.length == 0) {
150       throw new IllegalArgumentException JavaDoc(
151           "expecting a positive number of columns");
152     }
153
154     if (columnStyles != null && columns.length != columnStyles.length) {
155       throw new IllegalArgumentException JavaDoc("expecting as many styles as columns");
156     }
157
158     this.provider = provider;
159     initWidget(outer);
160     grid.setStyleName("table");
161     outer.add(navbar, DockPanel.NORTH);
162     outer.add(grid, DockPanel.CENTER);
163     initTable(columns, columnStyles, rowCount);
164     setStyleName("DynaTable-DynaTableWidget");
165   }
166
167   public void clearStatusText() {
168     navbar.status.setHTML("&nbsp;");
169   }
170
171   public void refresh() {
172     // Disable buttons temporarily to stop the user from running off the end.
173
//
174
navbar.gotoFirst.setEnabled(false);
175     navbar.gotoPrev.setEnabled(false);
176     navbar.gotoNext.setEnabled(false);
177
178     setStatusText("Please wait...");
179     provider.updateRowData(startRow, grid.getRowCount() - 1, acceptor);
180   }
181
182   public void setRowCount(int rows) {
183     grid.resizeRows(rows);
184   }
185
186   public void setStatusText(String JavaDoc text) {
187     navbar.status.setText(text);
188   }
189
190   private int getDataRowCount() {
191     return grid.getRowCount() - 1;
192   }
193
194   private void initTable(String JavaDoc[] columns, String JavaDoc[] columnStyles, int rowCount) {
195     // Set up the header row. It's one greater than the number of visible rows.
196
//
197
grid.resize(rowCount + 1, columns.length);
198     for (int i = 0, n = columns.length; i < n; i++) {
199       grid.setText(0, i, columns[i]);
200       if (columnStyles != null) {
201         grid.getCellFormatter().setStyleName(0, i, columnStyles[i] + " header");
202       }
203     }
204   }
205 }
206
Popular Tags