forked from gwtproject/gwt
/
TimeSlotListWidget.java
278 lines (234 loc) · 8.11 KB
/
TimeSlotListWidget.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* Copyright 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.gwt.sample.dynatablerf.client.widgets;
import com.google.gwt.cell.client.ClickableTextCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.EditorDelegate;
import com.google.gwt.editor.client.ValueAwareEditor;
import com.google.gwt.sample.dynatablerf.shared.DynaTableRequestFactory;
import com.google.gwt.sample.dynatablerf.shared.DynaTableRequestFactory.ScheduleRequest;
import com.google.gwt.sample.dynatablerf.shared.TimeSlotProxy;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.web.bindery.requestfactory.shared.Receiver;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
/**
* Edits a list of time slots.
*/
public class TimeSlotListWidget extends Composite implements ValueAwareEditor<List<TimeSlotProxy>> {
interface TableResources extends CellTable.Resources {
@Override
@Source(value = {CellTable.Style.DEFAULT_CSS, "CellTablePatch.css"})
CellTable.Style cellTableStyle();
}
interface TimeSlotListWidgetUiBinder extends UiBinder<Widget, TimeSlotListWidget> {
}
private class ScheduleRow {
int hour;
ScheduleRow(int hour) {
this.hour = hour;
}
public int getHour() {
return hour;
}
public boolean isInUse(WeekDay day) {
return currentSchedule.contains(new TimeSlotKey(day, hour));
}
public void toggleInUse(WeekDay day) {
final TimeSlotKey key = new TimeSlotKey(day, hour);
if (currentSchedule.contains(key)) {
currentSchedule.remove(key);
table.redraw();
} else if (!existingSlots.containsKey(key)) {
acceptClicks = false;
ScheduleRequest context = factory.scheduleRequest();
context.createTimeSlot(day.ordinal(), hour * 60, hour * 60 + 50).fire(
new Receiver<TimeSlotProxy>() {
@Override
public void onSuccess(TimeSlotProxy slot) {
existingSlots.put(key, slot);
backing.add(slot);
currentSchedule.add(key);
table.redraw();
acceptClicks = true;
}
});
} else {
currentSchedule.add(key);
table.redraw();
}
}
}
private static class TimeSlotKey {
private int hour;
private WeekDay day;
TimeSlotKey(WeekDay day, int hour) {
this.day = day;
this.hour = hour;
}
TimeSlotKey(TimeSlotProxy slot) {
day = WeekDay.fromInt(slot.getDayOfWeek());
hour = slot.getStartMinutes() / 60;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
TimeSlotKey other = (TimeSlotKey) obj;
if (day == null) {
if (other.day != null) {
return false;
}
} else if (!day.equals(other.day)) {
return false;
}
if (hour != other.hour) {
return false;
}
return true;
}
@Override
public int hashCode() {
return 31 * (31 + ((day == null) ? 0 : day.hashCode())) + hour;
}
}
private class WeekDayColumn extends Column<ScheduleRow, String> {
private WeekDay day;
public WeekDayColumn(WeekDay day) {
super(new ClickableTextCell());
this.day = day;
}
@Override
public String getValue(ScheduleRow row) {
if (day == null) {
int hour = row.getHour();
return Integer.toString(hour <= 12 ? hour : hour - 12) + ":00"
+ ((hour < 12) ? "AM" : "PM");
}
return row.isInUse(day) ? "X" : ".";
}
}
private static final int ROWS_IN_A_DAY = 9;
private static final int FIRST_HOUR = 8;
private static TimeSlotListWidgetUiBinder uiBinder = GWT.create(
TimeSlotListWidgetUiBinder.class);
@UiField(provided = true)
CellTable<ScheduleRow> table;
private enum WeekDay {
SUNDAY("Su"), MONDAY("Mo"), TUESDAY("Tu"), WEDNESDAY("We"),
THURSDAY("Th"), FRIDAY("Fr"), SATURDAY("Sa");
public static WeekDay fromInt(int ordinal) {
return values()[ordinal];
}
private String shortName;
WeekDay(String shortName) {
this.shortName = shortName;
}
public String getShortName() {
return shortName;
}
}
private List<TimeSlotProxy> backing;
private HashSet<TimeSlotKey> currentSchedule;
private HashMap<TimeSlotKey, TimeSlotProxy> existingSlots;
private DynaTableRequestFactory factory;
private HashSet<TimeSlotKey> initialSchedule;
private boolean acceptClicks = true;
public TimeSlotListWidget(DynaTableRequestFactory factory) {
this.factory = factory;
table = new CellTable<TimeSlotListWidget.ScheduleRow>(ROWS_IN_A_DAY,
GWT.<TableResources> create(TableResources.class));
table.addColumn(new WeekDayColumn(null), "Hour");
for (WeekDay day : WeekDay.values()) {
WeekDayColumn col = new WeekDayColumn(day);
class Updater implements FieldUpdater<ScheduleRow, String> {
private WeekDay columnDay;
public Updater(WeekDay day) {
columnDay = day;
}
@Override
public void update(int index, ScheduleRow row, String value) {
if (acceptClicks) {
row.toggleInUse(columnDay);
}
}
}
FieldUpdater<ScheduleRow, String> fieldUpdater = new Updater(day);
col.setFieldUpdater(fieldUpdater);
table.addColumn(col, day.getShortName());
}
table.setRowCount(ROWS_IN_A_DAY, false);
table.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.DISABLED);
initWidget(uiBinder.createAndBindUi(this));
}
@Override
public void flush() {
HashMap<TimeSlotProxy, TimeSlotKey> index = new HashMap<TimeSlotProxy, TimeSlotKey>();
for (TimeSlotProxy slot : backing) {
index.put(slot, new TimeSlotKey(slot));
}
// Compute slots that need to be removed from the backing
initialSchedule.removeAll(currentSchedule);
for (Iterator<TimeSlotProxy> iterator = backing.iterator(); iterator.hasNext();) {
TimeSlotProxy slot = iterator.next();
TimeSlotKey key = index.get(slot);
if (initialSchedule.contains(key)) {
iterator.remove();
}
}
}
@Override
public void onPropertyChange(String... paths) {
}
@Override
public void setDelegate(EditorDelegate<List<TimeSlotProxy>> delegate) {
}
@Override
public void setValue(List<TimeSlotProxy> value) {
backing = value;
currentSchedule = new HashSet<TimeSlotKey>();
existingSlots = new HashMap<TimeSlotKey, TimeSlotProxy>();
initialSchedule = new HashSet<TimeSlotKey>();
for (TimeSlotProxy slot : backing) {
TimeSlotKey key = new TimeSlotKey(slot);
currentSchedule.add(key);
existingSlots.put(key, slot);
initialSchedule.add(new TimeSlotKey(slot));
}
ArrayList<ScheduleRow> rows = new ArrayList<ScheduleRow>(ROWS_IN_A_DAY);
for (int i = 0; i < ROWS_IN_A_DAY; i++) {
rows.add(new ScheduleRow(FIRST_HOUR + i));
}
table.setRowData(rows);
}
}