KickJava   Java API By Example, From Geeks To Geeks.

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


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.ui.AbstractImagePrototype;
19 import com.google.gwt.user.client.ui.ClickListener;
20 import com.google.gwt.user.client.ui.Composite;
21 import com.google.gwt.user.client.ui.HTML;
22 import com.google.gwt.user.client.ui.HorizontalPanel;
23 import com.google.gwt.user.client.ui.ImageBundle;
24 import com.google.gwt.user.client.ui.Label;
25 import com.google.gwt.user.client.ui.PopupPanel;
26 import com.google.gwt.user.client.ui.SimplePanel;
27 import com.google.gwt.user.client.ui.VerticalPanel;
28 import com.google.gwt.user.client.ui.Widget;
29
30 /**
31  * A component that displays a list of contacts.
32  */

33 public class Contacts extends Composite {
34
35   /**
36    * An image bundle for this widget and an example of the use of @gwt.resource.
37    */

38   public interface Images extends ImageBundle {
39     /**
40      * @gwt.resource default_photo.jpg
41      */

42     AbstractImagePrototype defaultPhoto();
43   }
44
45   /**
46    * Simple data structure representing a contact.
47    */

48   private class Contact {
49     public String JavaDoc email;
50     public String JavaDoc name;
51
52     public Contact(String JavaDoc name, String JavaDoc email) {
53       this.name = name;
54       this.email = email;
55     }
56   }
57
58   /**
59    * A simple popup that displays a contact's information.
60    */

61   private class ContactPopup extends PopupPanel {
62
63     public ContactPopup(Contact contact) {
64       // The popup's constructor's argument is a boolean specifying that it
65
// auto-close itself when the user clicks outside of it.
66
super(true);
67
68       VerticalPanel inner = new VerticalPanel();
69       Label nameLabel = new Label(contact.name);
70       Label emailLabel = new Label(contact.email);
71       inner.add(nameLabel);
72       inner.add(emailLabel);
73
74       HorizontalPanel hp = new HorizontalPanel();
75       hp.setSpacing(4);
76       hp.add(images.defaultPhoto().createImage());
77       hp.add(inner);
78
79       add(hp);
80       setStyleName("mail-ContactPopup");
81       nameLabel.setStyleName("mail-ContactPopupName");
82       emailLabel.setStyleName("mail-ContactPopupEmail");
83     }
84   }
85
86   private Contact[] contacts = new Contact[] {
87       new Contact("Benoit Mandelbrot", "benoit@example.com"),
88       new Contact("Albert Einstein", "albert@example.com"),
89       new Contact("Rene Descartes", "rene@example.com"),
90       new Contact("Bob Saget", "bob@example.com"),
91       new Contact("Ludwig von Beethoven", "ludwig@example.com"),
92       new Contact("Richard Feynman", "richard@example.com"),
93       new Contact("Alan Turing", "alan@example.com"),
94       new Contact("John von Neumann", "john@example.com")};
95
96   private VerticalPanel panel = new VerticalPanel();
97   private final Images images;
98
99   public Contacts(Images images) {
100     SimplePanel outer = new SimplePanel();
101     outer.setWidget(panel);
102
103     this.images = images;
104     // Add all the contacts to the list.
105
for (int i = 0; i < contacts.length; ++i) {
106       addContact(contacts[i]);
107     }
108
109     initWidget(outer);
110     setStyleName("mail-Contacts");
111   }
112
113   private void addContact(final Contact contact) {
114     final HTML link = new HTML("<a HREF='javascript:;'>" + contact.name
115         + "</a>");
116     panel.add(link);
117
118     // Add a click listener that displays a ContactPopup when it is clicked.
119
link.addClickListener(new ClickListener() {
120       public void onClick(Widget sender) {
121         ContactPopup popup = new ContactPopup(contact);
122         int left = link.getAbsoluteLeft() + 14;
123         int top = link.getAbsoluteTop() + 14;
124         popup.setPopupPosition(left, top);
125         popup.show();
126       }
127     });
128   }
129 }
130
Popular Tags