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.core.client.GWT;
19 import com.google.gwt.resources.client.ImageResource;
20 import com.google.gwt.safehtml.shared.SafeHtml;
21 import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
22 import com.google.gwt.user.client.ui.AbstractImagePrototype;
23 import com.google.gwt.user.client.ui.Composite;
24 import com.google.gwt.user.client.ui.Tree;
25 import com.google.gwt.user.client.ui.TreeItem;
26 
27 /**
28  * A tree displaying a set of email folders.
29  */
30 public class Mailboxes extends Composite {
31 
32   /**
33    * Specifies the images that will be bundled for this Composite and specify
34    * that tree's images should also be included in the same bundle.
35    */
36   public interface Images extends Tree.Resources {
drafts()37     ImageResource drafts();
38 
home()39     ImageResource home();
40 
inbox()41     ImageResource inbox();
42 
sent()43     ImageResource sent();
44 
templates()45     ImageResource templates();
46 
trash()47     ImageResource trash();
48 
49     @Override
50     @Source("noimage.png")
treeLeaf()51     ImageResource treeLeaf();
52   }
53 
54   private Tree tree;
55 
56   /**
57    * Constructs a new mailboxes widget.
58    */
Mailboxes()59   public Mailboxes() {
60     Images images = GWT.create(Images.class);
61 
62     tree = new Tree(images);
63     TreeItem root = new TreeItem(
64         imageItemHTML(images.home(), "foo@example.com"));
65     tree.addItem(root);
66 
67     addImageItem(root, "Inbox", images.inbox());
68     addImageItem(root, "Drafts", images.drafts());
69     addImageItem(root, "Templates", images.templates());
70     addImageItem(root, "Sent", images.sent());
71     addImageItem(root, "Trash", images.trash());
72 
73     root.setState(true);
74     initWidget(tree);
75   }
76 
77   /**
78    * A helper method to simplify adding tree items that have attached images.
79    * {@link #addImageItem(TreeItem, String, ImageResource) code}
80    *
81    * @param root the tree item to which the new item will be added.
82    * @param title the text associated with this item.
83    */
addImageItem(TreeItem root, String title, ImageResource imageProto)84   private TreeItem addImageItem(TreeItem root, String title,
85       ImageResource imageProto) {
86     TreeItem item = new TreeItem(imageItemHTML(imageProto, title));
87     root.addItem(item);
88     return item;
89   }
90 
91   /**
92    * Generates HTML for a tree item with an attached icon.
93    *
94    * @param imageProto the image prototype to use
95    * @param title the title of the item
96    * @return the resultant HTML
97    */
imageItemHTML(ImageResource imageProto, String title)98   private SafeHtml imageItemHTML(ImageResource imageProto, String title) {
99     SafeHtmlBuilder builder = new SafeHtmlBuilder();
100     builder.append(AbstractImagePrototype.create(imageProto).getSafeHtml());
101     builder.appendHtmlConstant(" ");
102     builder.appendEscaped(title);
103     return builder.toSafeHtml();
104   }
105 }
106