KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > sample > mail > client > Shortcuts


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.mail.client;
17
18 import com.google.gwt.user.client.DOM;
19 import com.google.gwt.user.client.Element;
20 import com.google.gwt.user.client.Event;
21 import com.google.gwt.user.client.ui.AbstractImagePrototype;
22 import com.google.gwt.user.client.ui.Composite;
23 import com.google.gwt.user.client.ui.StackPanel;
24 import com.google.gwt.user.client.ui.Widget;
25
26 /**
27  * A composite that contains the shortcut stack panel on the left side. The
28  * mailbox tree and shortcut lists don't actually do anything, but serve to show
29  * how you can construct an interface using
30  * {@link com.google.gwt.user.client.ui.StackPanel},
31  * {@link com.google.gwt.user.client.ui.Tree}, and other custom widgets.
32  */

33 public class Shortcuts extends Composite {
34
35   /**
36    * An image bundle specifying the images for this Widget and aggragating
37    * images needed in child widgets.
38    */

39   public interface Images extends Contacts.Images, Mailboxes.Images {
40     AbstractImagePrototype contactsgroup();
41
42     AbstractImagePrototype leftCorner();
43
44     AbstractImagePrototype mailgroup();
45
46     AbstractImagePrototype rightCorner();
47
48     AbstractImagePrototype tasksgroup();
49   }
50   
51   private int nextHeaderIndex = 0;
52   
53   private StackPanel stackPanel = new StackPanel() {
54     public void onBrowserEvent(Event event) {
55       int oldIndex = getSelectedIndex();
56       super.onBrowserEvent(event);
57       int newIndex = getSelectedIndex();
58       if (oldIndex != newIndex) {
59         updateSelectedStyles(oldIndex, newIndex);
60       }
61     }
62   };
63
64   /**
65    * Constructs a new shortcuts widget using the specified images.
66    *
67    * @param images a bundle that provides the images for this widget
68    */

69   public Shortcuts(Images images) {
70     // Create the groups within the stack panel.
71
add(images, new Mailboxes(images), images.mailgroup(), "Mail");
72     add(images, new Tasks(), images.tasksgroup(), "Tasks");
73     add(images, new Contacts(images), images.contactsgroup(), "Contacts");
74     
75     initWidget(stackPanel);
76   }
77   
78   protected void onLoad() {
79     // Show the mailboxes group by default.
80
stackPanel.showStack(0);
81     updateSelectedStyles(-1, 0);
82   }
83
84   private void add(Images images, Widget widget, AbstractImagePrototype imageProto,
85       String JavaDoc caption) {
86     widget.addStyleName("mail-StackContent");
87     stackPanel.add(widget, createHeaderHTML(images, imageProto, caption), true);
88   }
89
90   private String JavaDoc computeHeaderId(int index) {
91     return "header-" + this.hashCode() + "-" + index;
92   }
93   
94   /**
95    * Creates an HTML fragment that places an image & caption together, for use
96    * in a group header.
97    *
98    * @param imageProto an image prototype for an image
99    * @param caption the group caption
100    * @return the header HTML fragment
101    */

102   private String JavaDoc createHeaderHTML(Images images,
103       AbstractImagePrototype imageProto, String JavaDoc caption) {
104     
105     boolean isTop = (nextHeaderIndex == 0);
106     String JavaDoc cssId = computeHeaderId(nextHeaderIndex);
107     nextHeaderIndex++;
108     
109     String JavaDoc captionHTML = "<table class='caption' cellpadding='0' cellspacing='0'>"
110         + "<tr><td class='lcaption'>" + imageProto.getHTML()
111         + "</td><td class='rcaption'><b style='white-space:nowrap'>" + caption
112         + "</b></td></tr></table>";
113     
114     return "<table id='" + cssId + "' align='left' cellpadding='0' cellspacing='0'"
115         + (isTop ? " class='is-top'" : "" ) + "><tbody>"
116         + "<tr><td class='box-00'>" + images.leftCorner().getHTML() + "</td>"
117         + "<td class='box-10'>&nbsp;</td>"
118         + "<td class='box-20'>" + images.rightCorner().getHTML() + "</td>"
119         + "</tr><tr>"
120         + "<td class='box-01'>&nbsp;</td>"
121         + "<td class='box-11'>" + captionHTML + "</td>"
122         + "<td class='box-21'>&nbsp;</td>"
123         + "</tr></tbody></table>";
124   }
125   
126   /**
127    * Example of using the DOM class to do CSS class name tricks that have
128    * become common to AJAX apps. In this case we add CSS class name for the
129    * stack panel item that is below the selected item.
130    */

131   private void updateSelectedStyles(int oldIndex, int newIndex) {
132     oldIndex++;
133     if (oldIndex > 0 && oldIndex < stackPanel.getWidgetCount()) {
134       Element elem = DOM.getElementById(computeHeaderId(oldIndex));
135       DOM.setElementProperty(elem, "className", "");
136     }
137     
138     newIndex++;
139     if (newIndex > 0 && newIndex < stackPanel.getWidgetCount()) {
140       Element elem = DOM.getElementById(computeHeaderId(newIndex));
141       DOM.setElementProperty(elem, "className", "is-beneath-selected");
142     }
143   }
144 }
145
Popular Tags