1 /* 2 * Copyright 2007 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.event.dom.client.ClickEvent; 19 import com.google.gwt.event.dom.client.ClickHandler; 20 import com.google.gwt.user.client.ui.Button; 21 import com.google.gwt.user.client.ui.CheckBox; 22 import com.google.gwt.user.client.ui.Composite; 23 import com.google.gwt.user.client.ui.HasAlignment; 24 import com.google.gwt.user.client.ui.HorizontalPanel; 25 import com.google.gwt.user.client.ui.VerticalPanel; 26 import com.google.gwt.user.client.ui.Widget; 27 28 /** 29 * A UI Widget that allows a user to filter the days being displayed in the 30 * dynamic table. 31 */ 32 public class DayFilterWidget extends Composite { 33 34 private class DayCheckBox extends CheckBox { 35 public final int day; 36 DayCheckBox(String caption, int day)37 public DayCheckBox(String caption, int day) { 38 super(caption); 39 40 // Remember custom data for this widget. 41 this.day = day; 42 43 // Use a shared handler to save memory. 44 addClickHandler(dayCheckBoxHandler); 45 46 // Initialize based on the calendar's current value. 47 setValue(calendar.getDayIncluded(day)); 48 } 49 } 50 51 private class DayCheckBoxHandler implements ClickHandler { onClick(ClickEvent event)52 public void onClick(ClickEvent event) { 53 onClick((DayCheckBox) event.getSource()); 54 } 55 onClick(DayCheckBox dayCheckBox)56 public void onClick(DayCheckBox dayCheckBox) { 57 calendar.setDayIncluded(dayCheckBox.day, dayCheckBox.getValue()); 58 } 59 } 60 61 private final SchoolCalendarWidget calendar; 62 63 private final VerticalPanel outer = new VerticalPanel(); 64 65 private final DayCheckBoxHandler dayCheckBoxHandler = new DayCheckBoxHandler(); 66 DayFilterWidget(SchoolCalendarWidget calendar)67 public DayFilterWidget(SchoolCalendarWidget calendar) { 68 this.calendar = calendar; 69 initWidget(outer); 70 setStyleName("DynaTable-DayFilterWidget"); 71 outer.add(new DayCheckBox("Sunday", 0)); 72 outer.add(new DayCheckBox("Monday", 1)); 73 outer.add(new DayCheckBox("Tuesday", 2)); 74 outer.add(new DayCheckBox("Wednesday", 3)); 75 outer.add(new DayCheckBox("Thursday", 4)); 76 outer.add(new DayCheckBox("Friday", 5)); 77 outer.add(new DayCheckBox("Saturday", 6)); 78 79 Button buttonAll = new Button("All", new ClickHandler() { 80 public void onClick(ClickEvent event) { 81 setAllCheckBoxes(true); 82 } 83 }); 84 85 Button buttonNone = new Button("None", new ClickHandler() { 86 public void onClick(ClickEvent event) { 87 setAllCheckBoxes(false); 88 } 89 }); 90 91 HorizontalPanel hp = new HorizontalPanel(); 92 hp.setHorizontalAlignment(HasAlignment.ALIGN_CENTER); 93 hp.add(buttonAll); 94 hp.add(buttonNone); 95 96 outer.add(hp); 97 outer.setCellVerticalAlignment(hp, HasAlignment.ALIGN_BOTTOM); 98 outer.setCellHorizontalAlignment(hp, HasAlignment.ALIGN_CENTER); 99 } 100 setAllCheckBoxes(boolean checked)101 private void setAllCheckBoxes(boolean checked) { 102 for (int i = 0, n = outer.getWidgetCount(); i < n; ++i) { 103 Widget w = outer.getWidget(i); 104 if (w instanceof DayCheckBox) { 105 ((DayCheckBox) w).setValue(checked); 106 dayCheckBoxHandler.onClick((DayCheckBox) w); 107 } 108 } 109 } 110 } 111