1 16 package com.google.gwt.sample.dynatable.client; 17 18 import com.google.gwt.core.client.GWT; 19 import com.google.gwt.user.client.Command; 20 import com.google.gwt.user.client.DeferredCommand; 21 import com.google.gwt.user.client.rpc.AsyncCallback; 22 import com.google.gwt.user.client.rpc.ServiceDefTarget; 23 import com.google.gwt.user.client.ui.Composite; 24 25 29 public class SchoolCalendarWidget extends Composite { 30 31 35 public class CalendarProvider implements DynaTableDataProvider { 36 37 private final SchoolCalendarServiceAsync calService; 38 39 private int lastMaxRows = -1; 40 41 private Person[] lastPeople; 42 43 private int lastStartRow = -1; 44 45 public CalendarProvider() { 46 calService = (SchoolCalendarServiceAsync) GWT.create(SchoolCalendarService.class); 49 50 ServiceDefTarget target = (ServiceDefTarget) calService; 55 56 String moduleRelativeURL = GWT.getModuleBaseURL() + "calendar"; 60 target.setServiceEntryPoint(moduleRelativeURL); 61 } 62 63 public void updateRowData(final int startRow, final int maxRows, 64 final RowDataAcceptor acceptor) { 65 if (startRow == lastStartRow) { 68 if (maxRows == lastMaxRows) { 69 pushResults(acceptor, startRow, lastPeople); 72 return; 73 } 74 } 75 76 if (USE_STATIC_RPC_ANSWERS) { 85 ServiceDefTarget target = (ServiceDefTarget) calService; 86 String staticResponseURL = GWT.getModuleBaseURL(); 87 staticResponseURL += "calendar" + startRow + ".txt"; 88 target.setServiceEntryPoint(staticResponseURL); 89 } 90 91 calService.getPeople(startRow, maxRows, new AsyncCallback() { 94 public void onFailure(Throwable caught) { 95 acceptor.failed(caught); 96 } 97 98 public void onSuccess(Object result) { 99 Person[] people = (Person[]) result; 100 lastStartRow = startRow; 101 lastMaxRows = maxRows; 102 lastPeople = people; 103 pushResults(acceptor, startRow, people); 104 } 105 106 }); 107 } 108 109 private void pushResults(RowDataAcceptor acceptor, int startRow, 110 Person[] people) { 111 String [][] rows = new String [people.length][]; 112 for (int i = 0, n = rows.length; i < n; i++) { 113 Person person = people[i]; 114 rows[i] = new String [3]; 115 rows[i][0] = person.getName(); 116 rows[i][1] = person.getDescription(); 117 rows[i][2] = person.getSchedule(daysFilter); 118 } 119 acceptor.accept(startRow, rows); 120 } 121 } 122 123 private static final boolean USE_STATIC_RPC_ANSWERS = true; 124 125 private final CalendarProvider calProvider = new CalendarProvider(); 126 127 private final boolean[] daysFilter = new boolean[] { 128 true, true, true, true, true, true, true}; 129 130 private final DynaTableWidget dynaTable; 131 132 private Command pendingRefresh; 133 134 public SchoolCalendarWidget(int visibleRows) { 135 String [] columns = new String [] {"Name", "Description", "Schedule"}; 136 String [] styles = new String [] {"name", "desc", "sched"}; 137 dynaTable = new DynaTableWidget(calProvider, columns, styles, visibleRows); 138 initWidget(dynaTable); 139 } 140 141 protected boolean getDayIncluded(int day) { 142 return daysFilter[day]; 143 } 144 145 protected void onLoad() { 146 dynaTable.refresh(); 147 } 148 149 protected void setDayIncluded(int day, boolean included) { 150 if (daysFilter[day] == included) { 151 return; 154 } 155 156 daysFilter[day] = included; 157 if (pendingRefresh == null) { 158 pendingRefresh = new Command() { 159 public void execute() { 160 pendingRefresh = null; 161 dynaTable.refresh(); 162 } 163 }; 164 DeferredCommand.addCommand(pendingRefresh); 165 } 166 } 167 } 168 | Popular Tags |