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.core.client.EntryPoint;
019: import com.google.gwt.core.client.GWT;
020: import com.google.gwt.sample.kitchensink.client.Sink.SinkInfo;
021: import com.google.gwt.user.client.DOM;
022: import com.google.gwt.user.client.History;
023: import com.google.gwt.user.client.HistoryListener;
024: import com.google.gwt.user.client.ui.HTML;
025: import com.google.gwt.user.client.ui.RootPanel;
026: import com.google.gwt.user.client.ui.VerticalPanel;
027:
028: /**
029: * Application that demonstrates all of the built-in widgets.
030: */
031: public class KitchenSink implements EntryPoint, HistoryListener {
032:
033: private static final Sink.Images images = GWT
034: .create(Sink.Images.class);
035:
036: protected SinkList list = new SinkList(images);
037: private SinkInfo curInfo;
038: private Sink curSink;
039: private HTML description = new HTML();
040: private VerticalPanel panel = new VerticalPanel();
041:
042: public void onHistoryChanged(String token) {
043: // Find the SinkInfo associated with the history context. If one is
044: // found, show it (It may not be found, for example, when the user mis-
045: // types a URL, or on startup, when the first context will be "").
046: SinkInfo info = list.find(token);
047: if (info == null) {
048: showInfo();
049: return;
050: }
051: show(info, false);
052: }
053:
054: public void onModuleLoad() {
055: // Load all the sinks.
056: loadSinks();
057:
058: panel.add(list);
059: panel.add(description);
060: panel.setWidth("100%");
061:
062: description.setStyleName("ks-Info");
063:
064: History.addHistoryListener(this );
065: RootPanel.get().add(panel);
066:
067: // Show the initial screen.
068: String initToken = History.getToken();
069: if (initToken.length() > 0) {
070: onHistoryChanged(initToken);
071: } else {
072: showInfo();
073: }
074: }
075:
076: public void show(SinkInfo info, boolean affectHistory) {
077: // Don't bother re-displaying the existing sink. This can be an issue
078: // in practice, because when the history context is set, our
079: // onHistoryChanged() handler will attempt to show the currently-visible
080: // sink.
081: if (info == curInfo) {
082: return;
083: }
084: curInfo = info;
085:
086: // Remove the old sink from the display area.
087: if (curSink != null) {
088: curSink.onHide();
089: panel.remove(curSink);
090: }
091:
092: // Get the new sink instance, and display its description in the
093: // sink list.
094: curSink = info.getInstance();
095: list.setSinkSelection(info.getName());
096: description.setHTML(info.getDescription());
097:
098: // If affectHistory is set, create a new item on the history stack. This
099: // will ultimately result in onHistoryChanged() being called. It will call
100: // show() again, but nothing will happen because it will request the exact
101: // same sink we're already showing.
102: if (affectHistory) {
103: History.newItem(info.getName());
104: }
105:
106: // Change the description background color.
107: DOM.setStyleAttribute(description.getElement(),
108: "backgroundColor", info.getColor());
109:
110: // Display the new sink.
111: curSink.setVisible(false);
112: panel.add(curSink);
113: panel.setCellHorizontalAlignment(curSink,
114: VerticalPanel.ALIGN_CENTER);
115: curSink.setVisible(true);
116: curSink.onShow();
117: }
118:
119: /**
120: * Adds all sinks to the list. Note that this does not create actual instances
121: * of all sinks yet (they are created on-demand). This can make a significant
122: * difference in startup time.
123: */
124: protected void loadSinks() {
125: list.addSink(Info.init());
126: list.addSink(Widgets.init(images));
127: list.addSink(Panels.init(images));
128: list.addSink(Lists.init(images));
129: list.addSink(Text.init());
130: list.addSink(Popups.init());
131: }
132:
133: private void showInfo() {
134: show(list.find("Intro"), false);
135: }
136: }
|