KickJava   Java API By Example, From Geeks To Geeks.

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


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.user.client.ui.Button;
19 import com.google.gwt.user.client.ui.CheckBox;
20 import com.google.gwt.user.client.ui.ClickListener;
21 import com.google.gwt.user.client.ui.Composite;
22 import com.google.gwt.user.client.ui.HasAlignment;
23 import com.google.gwt.user.client.ui.HorizontalPanel;
24 import com.google.gwt.user.client.ui.VerticalPanel;
25 import com.google.gwt.user.client.ui.Widget;
26
27 /**
28  * A UI Widget that allows a user to filter the days being displayed in the
29  * dynamic table.
30  */

31 public class DayFilterWidget extends Composite {
32
33   private class DayCheckBox extends CheckBox {
34     public final int day;
35
36     public DayCheckBox(String JavaDoc caption, int day) {
37       super(caption);
38
39       // Remember custom data for this widget.
40
this.day = day;
41
42       // Use a shared listener to save memory.
43
addClickListener(dayCheckBoxListener);
44
45       // Initialize based on the calendar's current value.
46
setChecked(calendar.getDayIncluded(day));
47     }
48   }
49
50   private class DayCheckBoxListener implements ClickListener {
51     public void onClick(Widget sender) {
52       DayCheckBox dayCheckBox = ((DayCheckBox) sender);
53       calendar.setDayIncluded(dayCheckBox.day, dayCheckBox.isChecked());
54     }
55   }
56
57   private final SchoolCalendarWidget calendar;
58
59   private final VerticalPanel outer = new VerticalPanel();
60
61   private final DayCheckBoxListener dayCheckBoxListener = new DayCheckBoxListener();
62
63   public DayFilterWidget(SchoolCalendarWidget calendar) {
64     this.calendar = calendar;
65     initWidget(outer);
66     setStyleName("DynaTable-DayFilterWidget");
67     outer.add(new DayCheckBox("Sunday", 0));
68     outer.add(new DayCheckBox("Monday", 1));
69     outer.add(new DayCheckBox("Tuesday", 2));
70     outer.add(new DayCheckBox("Wednesday", 3));
71     outer.add(new DayCheckBox("Thursday", 4));
72     outer.add(new DayCheckBox("Friday", 5));
73     outer.add(new DayCheckBox("Saturday", 6));
74
75     Button buttonAll = new Button("All", new ClickListener() {
76       public void onClick(Widget sender) {
77         setAllCheckBoxes(true);
78       }
79     });
80
81     Button buttonNone = new Button("None", new ClickListener() {
82       public void onClick(Widget sender) {
83         setAllCheckBoxes(false);
84       }
85     });
86
87     HorizontalPanel hp = new HorizontalPanel();
88     hp.setHorizontalAlignment(HasAlignment.ALIGN_CENTER);
89     hp.add(buttonAll);
90     hp.add(buttonNone);
91
92     outer.add(hp);
93     outer.setCellVerticalAlignment(hp, HasAlignment.ALIGN_BOTTOM);
94     outer.setCellHorizontalAlignment(hp, HasAlignment.ALIGN_CENTER);
95   }
96
97   private void setAllCheckBoxes(boolean checked) {
98     for (int i = 0, n = outer.getWidgetCount(); i < n; ++i) {
99       Widget w = outer.getWidget(i);
100       if (w instanceof DayCheckBox) {
101         ((DayCheckBox) w).setChecked(checked);
102         dayCheckBoxListener.onClick(w);
103       }
104     }
105   }
106 }
107
Popular Tags