KickJava   Java API By Example, From Geeks To Geeks.

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


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.core.client.EntryPoint;
19 import com.google.gwt.core.client.GWT;
20 import com.google.gwt.sample.kitchensink.client.Sink.SinkInfo;
21 import com.google.gwt.user.client.DOM;
22 import com.google.gwt.user.client.History;
23 import com.google.gwt.user.client.HistoryListener;
24 import com.google.gwt.user.client.ui.HTML;
25 import com.google.gwt.user.client.ui.RootPanel;
26 import com.google.gwt.user.client.ui.VerticalPanel;
27
28 /**
29  * Application that demonstrates all of the built-in widgets.
30  */

31 public class KitchenSink implements EntryPoint, HistoryListener {
32
33   private static final Sink.Images images = (Sink.Images) GWT.create(Sink.Images.class);
34
35   protected SinkList list = new SinkList(images);
36   private SinkInfo curInfo;
37   private Sink curSink;
38   private HTML description = new HTML();
39   private VerticalPanel panel = new VerticalPanel();
40
41   public void onHistoryChanged(String JavaDoc token) {
42     // Find the SinkInfo associated with the history context. If one is
43
// found, show it (It may not be found, for example, when the user mis-
44
// types a URL, or on startup, when the first context will be "").
45
SinkInfo info = list.find(token);
46     if (info == null) {
47       showInfo();
48       return;
49     }
50     show(info, false);
51   }
52
53   public void onModuleLoad() {
54     // Load all the sinks.
55
loadSinks();
56
57     panel.add(list);
58     panel.add(description);
59     panel.setWidth("100%");
60
61     description.setStyleName("ks-Info");
62
63     History.addHistoryListener(this);
64     RootPanel.get().add(panel);
65
66     // Show the initial screen.
67
String JavaDoc initToken = History.getToken();
68     if (initToken.length() > 0) {
69       onHistoryChanged(initToken);
70     } else {
71       showInfo();
72     }
73   }
74
75   public void show(SinkInfo info, boolean affectHistory) {
76     // Don't bother re-displaying the existing sink. This can be an issue
77
// in practice, because when the history context is set, our
78
// onHistoryChanged() handler will attempt to show the currently-visible
79
// sink.
80
if (info == curInfo) {
81       return;
82     }
83     curInfo = info;
84
85     // Remove the old sink from the display area.
86
if (curSink != null) {
87       curSink.onHide();
88       panel.remove(curSink);
89     }
90
91     // Get the new sink instance, and display its description in the
92
// sink list.
93
curSink = info.getInstance();
94     list.setSinkSelection(info.getName());
95     description.setHTML(info.getDescription());
96
97     // If affectHistory is set, create a new item on the history stack. This
98
// will ultimately result in onHistoryChanged() being called. It will call
99
// show() again, but nothing will happen because it will request the exact
100
// same sink we're already showing.
101
if (affectHistory) {
102       History.newItem(info.getName());
103     }
104
105     // Change the description background color.
106
DOM.setStyleAttribute(description.getElement(), "backgroundColor",
107         info.getColor());
108
109     // Display the new sink.
110
curSink.setVisible(false);
111     panel.add(curSink);
112     panel.setCellHorizontalAlignment(curSink, VerticalPanel.ALIGN_CENTER);
113     curSink.setVisible(true);
114     curSink.onShow();
115   }
116
117   /**
118    * Adds all sinks to the list. Note that this does not create actual instances
119    * of all sinks yet (they are created on-demand). This can make a significant
120    * difference in startup time.
121    */

122   protected void loadSinks() {
123     list.addSink(Info.init());
124     list.addSink(Widgets.init(images));
125     list.addSink(Panels.init(images));
126     list.addSink(Lists.init(images));
127     list.addSink(Text.init());
128     list.addSink(Popups.init());
129   }
130
131   private void showInfo() {
132     show(list.find("Intro"), false);
133   }
134 }
135
Popular Tags