1 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 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 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 [][] 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; for (; srcRowIndex < srcRowCount; ++srcRowIndex, ++destRowIndex) { 96 String [] srcRowData = data[srcRowIndex]; 97 assert (srcRowData.length == destColCount) : " Column count mismatch"; 98 for (int srcColIndex = 0; srcColIndex < destColCount; ++srcColIndex) { 99 String cellHTML = srcRowData[srcColIndex]; 100 grid.setText(destRowIndex, srcColIndex, cellHTML); 101 } 102 } 103 104 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 navbar.gotoNext.setEnabled(!isLastPage); 117 navbar.gotoFirst.setEnabled(startRow > 0); 118 navbar.gotoPrev.setEnabled(startRow > 0); 119 120 setStatusText((startRow + 1) + " - " + (startRow + srcRowCount)); 123 } 124 125 public void failed(Throwable caught) { 126 String 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 [] columns, 147 String [] columnStyles, int rowCount) { 148 149 if (columns.length == 0) { 150 throw new IllegalArgumentException ( 151 "expecting a positive number of columns"); 152 } 153 154 if (columnStyles != null && columns.length != columnStyles.length) { 155 throw new IllegalArgumentException ("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(" "); 169 } 170 171 public void refresh() { 172 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 text) { 187 navbar.status.setText(text); 188 } 189 190 private int getDataRowCount() { 191 return grid.getRowCount() - 1; 192 } 193 194 private void initTable(String [] columns, String [] columnStyles, int rowCount) { 195 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 |