KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > sample > kitchensink > client > Lists


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.kitchensink.client;
17
18 import com.google.gwt.user.client.ui.ChangeListener;
19 import com.google.gwt.user.client.ui.HorizontalPanel;
20 import com.google.gwt.user.client.ui.Label;
21 import com.google.gwt.user.client.ui.ListBox;
22 import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
23 import com.google.gwt.user.client.ui.SuggestBox;
24 import com.google.gwt.user.client.ui.Tree;
25 import com.google.gwt.user.client.ui.TreeItem;
26 import com.google.gwt.user.client.ui.TreeListener;
27 import com.google.gwt.user.client.ui.VerticalPanel;
28 import com.google.gwt.user.client.ui.Widget;
29
30 /**
31  * Demonstrates {@link com.google.gwt.user.client.ui.ListBox}.
32  */

33 public class Lists extends Sink implements ChangeListener, TreeListener {
34
35   private static class PendingItem extends TreeItem {
36     public PendingItem() {
37       super("Please wait...");
38     }
39   }
40
41   private static class Proto {
42     public Proto[] children;
43     public TreeItem item;
44     public String JavaDoc text;
45
46     public Proto(String JavaDoc text) {
47       this.text = text;
48     }
49
50     public Proto(String JavaDoc text, Proto[] children) {
51       this(text);
52       this.children = children;
53     }
54   }
55
56   private static final String JavaDoc[][] stringLists = new String JavaDoc[][] {
57       new String JavaDoc[] {"foo0", "bar0", "baz0", "toto0", "tintin0"},
58       new String JavaDoc[] {"foo1", "bar1", "baz1", "toto1", "tintin1"},
59       new String JavaDoc[] {"foo2", "bar2", "baz2", "toto2", "tintin2"},
60       new String JavaDoc[] {"foo3", "bar3", "baz3", "toto3", "tintin3"},
61       new String JavaDoc[] {"foo4", "bar4", "baz4", "toto4", "tintin4"},};
62
63   private static final String JavaDoc[] words = new String JavaDoc[] {
64       "1337", "apple", "about", "ant", "bruce", "banana", "bobv", "canada",
65       "coconut", "compiler", "donut", "deferred binding", "dessert topping",
66       "eclair", "ecc", "frog attack", "floor wax", "fitz", "google", "gosh",
67       "gwt", "hollis", "haskell", "hammer", "in the flinks", "internets",
68       "ipso facto", "jat", "jgw", "java", "jens", "knorton", "kaitlyn",
69       "kangaroo", "la grange", "lars", "love", "morrildl", "max", "maddie",
70       "mloofle", "mmendez", "nail", "narnia", "null", "optimizations",
71       "obfuscation", "original", "ping pong", "polymorphic", "pleather",
72       "quotidian", "quality", "qu'est-ce que c'est", "ready state", "ruby",
73       "rdayal", "subversion", "superclass", "scottb", "tobyr", "the dans",
74       "~ tilde", "undefined", "unit tests", "under 100ms", "vtbl", "vidalia",
75       "vector graphics", "w3c", "web experience", "work around", "w00t!",
76       "xml", "xargs", "xeno", "yacc", "yank (the vi command)", "zealot", "zoe",
77       "zebra"};
78
79   private static Proto[] fProto = new Proto[]{
80     new Proto("Beethoven", new Proto[]{
81       new Proto("Concertos", new Proto[]{
82         new Proto("No. 1 - C"), new Proto("No. 2 - B-Flat Major"),
83         new Proto("No. 3 - C Minor"), new Proto("No. 4 - G Major"),
84         new Proto("No. 5 - E-Flat Major"),}),
85       new Proto("Quartets", new Proto[]{
86         new Proto("Six String Quartets"), new Proto("Three String Quartets"),
87         new Proto("Grosse Fugue for String Quartets"),}),
88       new Proto("Sonatas", new Proto[]{
89         new Proto("Sonata in A Minor"), new Proto("Sonata in F Major"),}),
90       new Proto("Symphonies", new Proto[]{
91         new Proto("No. 1 - C Major"), new Proto("No. 2 - D Major"),
92         new Proto("No. 3 - E-Flat Major"), new Proto("No. 4 - B-Flat Major"),
93         new Proto("No. 5 - C Minor"), new Proto("No. 6 - F Major"),
94         new Proto("No. 7 - A Major"), new Proto("No. 8 - F Major"),
95         new Proto("No. 9 - D Minor"),}),}),
96     new Proto("Brahms", new Proto[]{
97       new Proto("Concertos", new Proto[]{
98         new Proto("Violin Concerto"), new Proto("Double Concerto - A Minor"),
99         new Proto("Piano Concerto No. 1 - D Minor"),
100         new Proto("Piano Concerto No. 2 - B-Flat Major"),}),
101       new Proto("Quartets", new Proto[]{
102         new Proto("Piano Quartet No. 1 - G Minor"),
103         new Proto("Piano Quartet No. 2 - A Major"),
104         new Proto("Piano Quartet No. 3 - C Minor"),
105         new Proto("String Quartet No. 3 - B-Flat Minor"),}),
106       new Proto("Sonatas", new Proto[]{
107         new Proto("Two Sonatas for Clarinet - F Minor"),
108         new Proto("Two Sonatas for Clarinet - E-Flat Major"),}),
109       new Proto("Symphonies", new Proto[]{
110         new Proto("No. 1 - C Minor"), new Proto("No. 2 - D Minor"),
111         new Proto("No. 3 - F Major"), new Proto("No. 4 - E Minor"),}),}),
112     new Proto("Mozart", new Proto[]{new Proto("Concertos", new Proto[]{
113       new Proto("Piano Concerto No. 12"), new Proto("Piano Concerto No. 17"),
114       new Proto("Clarinet Concerto"), new Proto("Violin Concerto No. 5"),
115       new Proto("Violin Concerto No. 4"),}),}),};
116
117   public static SinkInfo init(final Sink.Images images) {
118     return new SinkInfo("Lists",
119         "<h2>Lists and Trees</h2>" +
120         "<p>GWT provides a number of ways to display lists and trees. This " +
121         "includes the browser's built-in list and drop-down boxes, as well as " +
122         "the more advanced suggestion combo-box and trees.</p><p>Try typing " +
123         "some text in the SuggestBox below to see what happens!</p>") {
124
125       public Sink createInstance() {
126         return new Lists(images);
127       }
128     };
129   }
130   private ListBox combo = new ListBox();
131   private ListBox list = new ListBox();
132
133   private MultiWordSuggestOracle oracle = new MultiWordSuggestOracle();
134
135   private SuggestBox suggestBox = new SuggestBox(oracle);
136
137   private Tree tree;
138
139   public Lists(Sink.Images images) {
140     combo.setVisibleItemCount(1);
141     combo.addChangeListener(this);
142     list.setVisibleItemCount(10);
143     list.setMultipleSelect(true);
144
145     for (int i = 0; i < stringLists.length; ++i) {
146       combo.addItem("List " + i);
147     }
148     combo.setSelectedIndex(0);
149     fillList(0);
150
151     list.addChangeListener(this);
152
153     for (int i = 0; i < words.length; ++i) {
154       oracle.add(words[i]);
155     }
156     
157     VerticalPanel suggestPanel = new VerticalPanel();
158     suggestPanel.add(new Label("Suggest box:"));
159     suggestPanel.add(suggestBox);
160
161     HorizontalPanel horz = new HorizontalPanel();
162     horz.setVerticalAlignment(HorizontalPanel.ALIGN_TOP);
163     horz.setSpacing(8);
164     horz.add(combo);
165     horz.add(list);
166     horz.add(suggestPanel);
167
168     VerticalPanel panel = new VerticalPanel();
169     panel.setHorizontalAlignment(VerticalPanel.ALIGN_LEFT);
170     panel.add(horz);
171     initWidget(panel);
172
173     tree = new Tree(images);
174     for (int i = 0; i < fProto.length; ++i) {
175       createItem(fProto[i]);
176       tree.addItem(fProto[i].item);
177     }
178
179     tree.addTreeListener(this);
180     tree.setWidth("20em");
181     horz.add(tree);
182   }
183
184   public void onChange(Widget sender) {
185     if (sender == combo) {
186       fillList(combo.getSelectedIndex());
187     } else if (sender == list) {
188     }
189   }
190
191   public void onShow() {
192   }
193
194   public void onTreeItemSelected(TreeItem item) {
195   }
196
197   public void onTreeItemStateChanged(TreeItem item) {
198     TreeItem child = item.getChild(0);
199     if (child instanceof PendingItem) {
200       item.removeItem(child);
201
202       Proto proto = (Proto) item.getUserObject();
203       for (int i = 0; i < proto.children.length; ++i) {
204         createItem(proto.children[i]);
205         item.addItem(proto.children[i].item);
206       }
207     }
208   }
209
210   private void createItem(Proto proto) {
211     proto.item = new TreeItem(proto.text);
212     proto.item.setUserObject(proto);
213     if (proto.children != null) {
214       proto.item.addItem(new PendingItem());
215     }
216   }
217
218   private void fillList(int idx) {
219     // Set the contents of the list box to reflect the combo selection.
220
list.clear();
221     String JavaDoc[] strings = stringLists[idx];
222     for (int i = 0; i < strings.length; ++i) {
223       list.addItem(strings[i]);
224     }
225   }
226 }
227
Popular Tags