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.kitchensink.client;
017:
018: import com.google.gwt.user.client.ui.ChangeListener;
019: import com.google.gwt.user.client.ui.HorizontalPanel;
020: import com.google.gwt.user.client.ui.Label;
021: import com.google.gwt.user.client.ui.ListBox;
022: import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
023: import com.google.gwt.user.client.ui.SuggestBox;
024: import com.google.gwt.user.client.ui.Tree;
025: import com.google.gwt.user.client.ui.TreeItem;
026: import com.google.gwt.user.client.ui.TreeListener;
027: import com.google.gwt.user.client.ui.VerticalPanel;
028: import com.google.gwt.user.client.ui.Widget;
029:
030: /**
031: * Demonstrates {@link com.google.gwt.user.client.ui.ListBox}.
032: */
033: public class Lists extends Sink implements ChangeListener, TreeListener {
034:
035: private static class PendingItem extends TreeItem {
036: public PendingItem() {
037: super ("Please wait...");
038: }
039: }
040:
041: private static class Proto {
042: public Proto[] children;
043: public TreeItem item;
044: public String text;
045:
046: public Proto(String text) {
047: this .text = text;
048: }
049:
050: public Proto(String text, Proto[] children) {
051: this (text);
052: this .children = children;
053: }
054: }
055:
056: private static final String[][] stringLists = new String[][] {
057: new String[] { "foo0", "bar0", "baz0", "toto0", "tintin0" },
058: new String[] { "foo1", "bar1", "baz1", "toto1", "tintin1" },
059: new String[] { "foo2", "bar2", "baz2", "toto2", "tintin2" },
060: new String[] { "foo3", "bar3", "baz3", "toto3", "tintin3" },
061: new String[] { "foo4", "bar4", "baz4", "toto4", "tintin4" }, };
062:
063: private static final String[] words = new String[] { "1337",
064: "apple", "about", "ant", "bruce", "banana", "bobv",
065: "canada", "coconut", "compiler", "donut",
066: "deferred binding", "dessert topping", "eclair", "ecc",
067: "frog attack", "floor wax", "fitz", "google", "gosh",
068: "gwt", "hollis", "haskell", "hammer", "in the flinks",
069: "internets", "ipso facto", "jat", "jgw", "java", "jens",
070: "knorton", "kaitlyn", "kangaroo", "la grange", "lars",
071: "love", "morrildl", "max", "maddie", "mloofle", "mmendez",
072: "nail", "narnia", "null", "optimizations", "obfuscation",
073: "original", "ping pong", "polymorphic", "pleather",
074: "quotidian", "quality", "qu'est-ce que c'est",
075: "ready state", "ruby", "rdayal", "subversion",
076: "superclass", "scottb", "tobyr", "the dans", "~ tilde",
077: "undefined", "unit tests", "under 100ms", "vtbl",
078: "vidalia", "vector graphics", "w3c", "web experience",
079: "work around", "w00t!", "xml", "xargs", "xeno", "yacc",
080: "yank (the vi command)", "zealot", "zoe", "zebra" };
081:
082: private static Proto[] fProto = new Proto[] {
083: new Proto(
084: "Beethoven",
085: new Proto[] {
086: new Proto(
087: "Concertos",
088: new Proto[] {
089: new Proto("No. 1 - C"),
090: new Proto(
091: "No. 2 - B-Flat Major"),
092: new Proto("No. 3 - C Minor"),
093: new Proto("No. 4 - G Major"),
094: new Proto(
095: "No. 5 - E-Flat Major"), }),
096: new Proto(
097: "Quartets",
098: new Proto[] {
099: new Proto(
100: "Six String Quartets"),
101: new Proto(
102: "Three String Quartets"),
103: new Proto(
104: "Grosse Fugue for String Quartets"), }),
105: new Proto("Sonatas", new Proto[] {
106: new Proto("Sonata in A Minor"),
107: new Proto("Sonata in F Major"), }),
108: new Proto("Symphonies", new Proto[] {
109: new Proto("No. 1 - C Major"),
110: new Proto("No. 2 - D Major"),
111: new Proto("No. 3 - E-Flat Major"),
112: new Proto("No. 4 - B-Flat Major"),
113: new Proto("No. 5 - C Minor"),
114: new Proto("No. 6 - F Major"),
115: new Proto("No. 7 - A Major"),
116: new Proto("No. 8 - F Major"),
117: new Proto("No. 9 - D Minor"), }), }),
118: new Proto(
119: "Brahms",
120: new Proto[] {
121: new Proto(
122: "Concertos",
123: new Proto[] {
124: new Proto("Violin Concerto"),
125: new Proto(
126: "Double Concerto - A Minor"),
127: new Proto(
128: "Piano Concerto No. 1 - D Minor"),
129: new Proto(
130: "Piano Concerto No. 2 - B-Flat Major"), }),
131: new Proto(
132: "Quartets",
133: new Proto[] {
134: new Proto(
135: "Piano Quartet No. 1 - G Minor"),
136: new Proto(
137: "Piano Quartet No. 2 - A Major"),
138: new Proto(
139: "Piano Quartet No. 3 - C Minor"),
140: new Proto(
141: "String Quartet No. 3 - B-Flat Minor"), }),
142: new Proto(
143: "Sonatas",
144: new Proto[] {
145: new Proto(
146: "Two Sonatas for Clarinet - F Minor"),
147: new Proto(
148: "Two Sonatas for Clarinet - E-Flat Major"), }),
149: new Proto("Symphonies", new Proto[] {
150: new Proto("No. 1 - C Minor"),
151: new Proto("No. 2 - D Minor"),
152: new Proto("No. 3 - F Major"),
153: new Proto("No. 4 - E Minor"), }), }),
154: new Proto("Mozart", new Proto[] { new Proto("Concertos",
155: new Proto[] { new Proto("Piano Concerto No. 12"),
156: new Proto("Piano Concerto No. 17"),
157: new Proto("Clarinet Concerto"),
158: new Proto("Violin Concerto No. 5"),
159: new Proto("Violin Concerto No. 4"), }), }), };
160:
161: public static SinkInfo init(final Sink.Images images) {
162: return new SinkInfo(
163: "Lists",
164: "<h2>Lists and Trees</h2>"
165: + "<p>GWT provides a number of ways to display lists and trees. This "
166: + "includes the browser's built-in list and drop-down boxes, as well as "
167: + "the more advanced suggestion combo-box and trees.</p><p>Try typing "
168: + "some text in the SuggestBox below to see what happens!</p>") {
169:
170: @Override
171: public Sink createInstance() {
172: return new Lists(images);
173: }
174: };
175: }
176:
177: private ListBox combo = new ListBox();
178: private ListBox list = new ListBox();
179:
180: private MultiWordSuggestOracle oracle = new MultiWordSuggestOracle();
181:
182: private SuggestBox suggestBox = new SuggestBox(oracle);
183:
184: private Tree tree;
185:
186: public Lists(Sink.Images images) {
187: combo.setVisibleItemCount(1);
188: combo.addChangeListener(this );
189: list.setVisibleItemCount(10);
190: list.setMultipleSelect(true);
191:
192: for (int i = 0; i < stringLists.length; ++i) {
193: combo.addItem("List " + i);
194: }
195: combo.setSelectedIndex(0);
196: fillList(0);
197:
198: list.addChangeListener(this );
199:
200: for (int i = 0; i < words.length; ++i) {
201: oracle.add(words[i]);
202: }
203:
204: VerticalPanel suggestPanel = new VerticalPanel();
205: suggestPanel.add(new Label("Suggest box:"));
206: suggestPanel.add(suggestBox);
207:
208: HorizontalPanel horz = new HorizontalPanel();
209: horz.setVerticalAlignment(HorizontalPanel.ALIGN_TOP);
210: horz.setSpacing(8);
211: horz.add(combo);
212: horz.add(list);
213: horz.add(suggestPanel);
214:
215: VerticalPanel panel = new VerticalPanel();
216: panel.setHorizontalAlignment(VerticalPanel.ALIGN_LEFT);
217: panel.add(horz);
218: initWidget(panel);
219:
220: tree = new Tree(images);
221: for (int i = 0; i < fProto.length; ++i) {
222: createItem(fProto[i]);
223: tree.addItem(fProto[i].item);
224: }
225:
226: tree.addTreeListener(this );
227: tree.setWidth("20em");
228: horz.add(tree);
229: }
230:
231: public void onChange(Widget sender) {
232: if (sender == combo) {
233: fillList(combo.getSelectedIndex());
234: } else if (sender == list) {
235: }
236: }
237:
238: @Override
239: public void onShow() {
240: }
241:
242: public void onTreeItemSelected(TreeItem item) {
243: }
244:
245: public void onTreeItemStateChanged(TreeItem item) {
246: TreeItem child = item.getChild(0);
247: if (child instanceof PendingItem) {
248: item.removeItem(child);
249:
250: Proto proto = (Proto) item.getUserObject();
251: for (int i = 0; i < proto.children.length; ++i) {
252: createItem(proto.children[i]);
253: item.addItem(proto.children[i].item);
254: }
255: }
256: }
257:
258: private void createItem(Proto proto) {
259: proto.item = new TreeItem(proto.text);
260: proto.item.setUserObject(proto);
261: if (proto.children != null) {
262: proto.item.addItem(new PendingItem());
263: }
264: }
265:
266: private void fillList(int idx) {
267: // Set the contents of the list box to reflect the combo selection.
268: list.clear();
269: String[] strings = stringLists[idx];
270: for (int i = 0; i < strings.length; ++i) {
271: list.addItem(strings[i]);
272: }
273: }
274: }
|