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.dynatable.client;
017:
018: import com.google.gwt.core.client.GWT;
019: import com.google.gwt.user.client.Command;
020: import com.google.gwt.user.client.DeferredCommand;
021: import com.google.gwt.user.client.rpc.AsyncCallback;
022: import com.google.gwt.user.client.rpc.ServiceDefTarget;
023: import com.google.gwt.user.client.ui.Composite;
024:
025: /**
026: * A Composite widget that abstracts a DynaTableWidget and a data provider tied
027: * to the <@link SchoolCalendarService> RPC endpoint.
028: */
029: public class SchoolCalendarWidget extends Composite {
030:
031: /**
032: * A data provider that bridges the provides row level updates from the data
033: * available through a <@link SchoolCalendarService>.
034: */
035: public class CalendarProvider implements DynaTableDataProvider {
036:
037: private final SchoolCalendarServiceAsync calService;
038:
039: private int lastMaxRows = -1;
040:
041: private Person[] lastPeople;
042:
043: private int lastStartRow = -1;
044:
045: public CalendarProvider() {
046: // Initialize the service.
047: //
048: calService = (SchoolCalendarServiceAsync) GWT
049: .create(SchoolCalendarService.class);
050:
051: // By default, we assume we'll make RPCs to a servlet, but see
052: // updateRowData(). There is special support for canned RPC responses.
053: // (Which is a totally demo hack, by the way :-)
054: //
055: ServiceDefTarget target = (ServiceDefTarget) calService;
056:
057: // Use a module-relative URLs to ensure that this client code can find
058: // its way home, even when the URL changes (as might happen when you
059: // deploy this as a webapp under an external servlet container).
060: String moduleRelativeURL = GWT.getModuleBaseURL()
061: + "calendar";
062: target.setServiceEntryPoint(moduleRelativeURL);
063: }
064:
065: public void updateRowData(final int startRow,
066: final int maxRows, final RowDataAcceptor acceptor) {
067: // Check the simple cache first.
068: //
069: if (startRow == lastStartRow) {
070: if (maxRows == lastMaxRows) {
071: // Use the cached batch.
072: //
073: pushResults(acceptor, startRow, lastPeople);
074: return;
075: }
076: }
077:
078: // Fetch the data remotely.
079: //
080: calService.getPeople(startRow, maxRows,
081: new AsyncCallback<Person[]>() {
082: public void onFailure(Throwable caught) {
083: acceptor.failed(caught);
084: }
085:
086: public void onSuccess(Person[] result) {
087: lastStartRow = startRow;
088: lastMaxRows = maxRows;
089: lastPeople = result;
090: pushResults(acceptor, startRow, result);
091: }
092:
093: });
094: }
095:
096: private void pushResults(RowDataAcceptor acceptor,
097: int startRow, Person[] people) {
098: String[][] rows = new String[people.length][];
099: for (int i = 0, n = rows.length; i < n; i++) {
100: Person person = people[i];
101: rows[i] = new String[3];
102: rows[i][0] = person.getName();
103: rows[i][1] = person.getDescription();
104: rows[i][2] = person.getSchedule(daysFilter);
105: }
106: acceptor.accept(startRow, rows);
107: }
108: }
109:
110: private final CalendarProvider calProvider = new CalendarProvider();
111:
112: private final boolean[] daysFilter = new boolean[] { true, true,
113: true, true, true, true, true };
114:
115: private final DynaTableWidget dynaTable;
116:
117: private Command pendingRefresh;
118:
119: public SchoolCalendarWidget(int visibleRows) {
120: String[] columns = new String[] { "Name", "Description",
121: "Schedule" };
122: String[] styles = new String[] { "name", "desc", "sched" };
123: dynaTable = new DynaTableWidget(calProvider, columns, styles,
124: visibleRows);
125: initWidget(dynaTable);
126: }
127:
128: protected boolean getDayIncluded(int day) {
129: return daysFilter[day];
130: }
131:
132: @Override
133: protected void onLoad() {
134: dynaTable.refresh();
135: }
136:
137: protected void setDayIncluded(int day, boolean included) {
138: if (daysFilter[day] == included) {
139: // No change.
140: //
141: return;
142: }
143:
144: daysFilter[day] = included;
145: if (pendingRefresh == null) {
146: pendingRefresh = new Command() {
147: public void execute() {
148: pendingRefresh = null;
149: dynaTable.refresh();
150: }
151: };
152: DeferredCommand.addCommand(pendingRefresh);
153: }
154: }
155: }
|