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.user.client.ui.Button;
019: import com.google.gwt.user.client.ui.CheckBox;
020: import com.google.gwt.user.client.ui.ClickListener;
021: import com.google.gwt.user.client.ui.Composite;
022: import com.google.gwt.user.client.ui.HasAlignment;
023: import com.google.gwt.user.client.ui.HorizontalPanel;
024: import com.google.gwt.user.client.ui.VerticalPanel;
025: import com.google.gwt.user.client.ui.Widget;
026:
027: /**
028: * A UI Widget that allows a user to filter the days being displayed in the
029: * dynamic table.
030: */
031: public class DayFilterWidget extends Composite {
032:
033: private class DayCheckBox extends CheckBox {
034: public final int day;
035:
036: public DayCheckBox(String caption, int day) {
037: super (caption);
038:
039: // Remember custom data for this widget.
040: this .day = day;
041:
042: // Use a shared listener to save memory.
043: addClickListener(dayCheckBoxListener);
044:
045: // Initialize based on the calendar's current value.
046: setChecked(calendar.getDayIncluded(day));
047: }
048: }
049:
050: private class DayCheckBoxListener implements ClickListener {
051: public void onClick(Widget sender) {
052: DayCheckBox dayCheckBox = ((DayCheckBox) sender);
053: calendar.setDayIncluded(dayCheckBox.day, dayCheckBox
054: .isChecked());
055: }
056: }
057:
058: private final SchoolCalendarWidget calendar;
059:
060: private final VerticalPanel outer = new VerticalPanel();
061:
062: private final DayCheckBoxListener dayCheckBoxListener = new DayCheckBoxListener();
063:
064: public DayFilterWidget(SchoolCalendarWidget calendar) {
065: this .calendar = calendar;
066: initWidget(outer);
067: setStyleName("DynaTable-DayFilterWidget");
068: outer.add(new DayCheckBox("Sunday", 0));
069: outer.add(new DayCheckBox("Monday", 1));
070: outer.add(new DayCheckBox("Tuesday", 2));
071: outer.add(new DayCheckBox("Wednesday", 3));
072: outer.add(new DayCheckBox("Thursday", 4));
073: outer.add(new DayCheckBox("Friday", 5));
074: outer.add(new DayCheckBox("Saturday", 6));
075:
076: Button buttonAll = new Button("All", new ClickListener() {
077: public void onClick(Widget sender) {
078: setAllCheckBoxes(true);
079: }
080: });
081:
082: Button buttonNone = new Button("None", new ClickListener() {
083: public void onClick(Widget sender) {
084: setAllCheckBoxes(false);
085: }
086: });
087:
088: HorizontalPanel hp = new HorizontalPanel();
089: hp.setHorizontalAlignment(HasAlignment.ALIGN_CENTER);
090: hp.add(buttonAll);
091: hp.add(buttonNone);
092:
093: outer.add(hp);
094: outer.setCellVerticalAlignment(hp, HasAlignment.ALIGN_BOTTOM);
095: outer.setCellHorizontalAlignment(hp, HasAlignment.ALIGN_CENTER);
096: }
097:
098: private void setAllCheckBoxes(boolean checked) {
099: for (int i = 0, n = outer.getWidgetCount(); i < n; ++i) {
100: Widget w = outer.getWidget(i);
101: if (w instanceof DayCheckBox) {
102: ((DayCheckBox) w).setChecked(checked);
103: dayCheckBoxListener.onClick(w);
104: }
105: }
106: }
107: }
|