KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 /**
26  * A Composite widget that abstracts a DynaTableWidget and a data provider tied
27  * to the <@link SchoolCalendarService> RPC endpoint.
28  */

29 public class SchoolCalendarWidget extends Composite {
30
31   /**
32    * A data provider that bridges the provides row level updates from the data
33    * available through a <@link SchoolCalendarService>.
34    */

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       // Initialize the service.
47
//
48
calService = (SchoolCalendarServiceAsync) GWT.create(SchoolCalendarService.class);
49
50       // By default, we assume we'll make RPCs to a servlet, but see
51
// updateRowData(). There is special support for canned RPC responses.
52
// (Which is a totally demo hack, by the way :-)
53
//
54
ServiceDefTarget target = (ServiceDefTarget) calService;
55
56       // Use a module-relative URLs to ensure that this client code can find
57
// its way home, even when the URL changes (as might happen when you
58
// deploy this as a webapp under an external servlet container).
59
String JavaDoc 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       // Check the simple cache first.
66
//
67
if (startRow == lastStartRow) {
68         if (maxRows == lastMaxRows) {
69           // Use the cached batch.
70
//
71
pushResults(acceptor, startRow, lastPeople);
72           return;
73         }
74       }
75
76       // Decide where to get the data.
77
// This is very unusual. Normally, you'd set the service entry point
78
// only once. But since this demo runs without servlet support, we
79
// play a game with URLs to fetch canned RPC responses from static files.
80
// So, as you might guess, this is really fragile: changing the number
81
// of visible rows in the table will cause different batch sizes, and
82
// the URL naming trick below will *definitely* break.
83
//
84
if (USE_STATIC_RPC_ANSWERS) {
85         ServiceDefTarget target = (ServiceDefTarget) calService;
86         String JavaDoc staticResponseURL = GWT.getModuleBaseURL();
87         staticResponseURL += "calendar" + startRow + ".txt";
88         target.setServiceEntryPoint(staticResponseURL);
89       }
90
91       // Actually fetch the data remotely.
92
//
93
calService.getPeople(startRow, maxRows, new AsyncCallback() {
94         public void onFailure(Throwable JavaDoc caught) {
95           acceptor.failed(caught);
96         }
97
98         public void onSuccess(Object JavaDoc 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 JavaDoc[][] rows = new String JavaDoc[people.length][];
112       for (int i = 0, n = rows.length; i < n; i++) {
113         Person person = people[i];
114         rows[i] = new String JavaDoc[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 JavaDoc[] columns = new String JavaDoc[] {"Name", "Description", "Schedule"};
136     String JavaDoc[] styles = new String JavaDoc[] {"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       // No change.
152
//
153
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