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.mail.client;
017:
018: import com.google.gwt.user.client.ui.AbstractImagePrototype;
019: import com.google.gwt.user.client.ui.ClickListener;
020: import com.google.gwt.user.client.ui.Composite;
021: import com.google.gwt.user.client.ui.HTML;
022: import com.google.gwt.user.client.ui.HorizontalPanel;
023: import com.google.gwt.user.client.ui.ImageBundle;
024: import com.google.gwt.user.client.ui.Label;
025: import com.google.gwt.user.client.ui.PopupPanel;
026: import com.google.gwt.user.client.ui.SimplePanel;
027: import com.google.gwt.user.client.ui.VerticalPanel;
028: import com.google.gwt.user.client.ui.Widget;
029:
030: /**
031: * A component that displays a list of contacts.
032: */
033: public class Contacts extends Composite {
034:
035: /**
036: * An image bundle for this widget and an example of the use of @gwt.resource.
037: */
038: public interface Images extends ImageBundle {
039: /**
040: * @gwt.resource default_photo.jpg
041: */
042: AbstractImagePrototype defaultPhoto();
043: }
044:
045: /**
046: * Simple data structure representing a contact.
047: */
048: private class Contact {
049: public String email;
050: public String name;
051:
052: public Contact(String name, String email) {
053: this .name = name;
054: this .email = email;
055: }
056: }
057:
058: /**
059: * A simple popup that displays a contact's information.
060: */
061: private class ContactPopup extends PopupPanel {
062:
063: public ContactPopup(Contact contact) {
064: // The popup's constructor's argument is a boolean specifying that it
065: // auto-close itself when the user clicks outside of it.
066: super (true);
067:
068: VerticalPanel inner = new VerticalPanel();
069: Label nameLabel = new Label(contact.name);
070: Label emailLabel = new Label(contact.email);
071: inner.add(nameLabel);
072: inner.add(emailLabel);
073:
074: HorizontalPanel hp = new HorizontalPanel();
075: hp.setSpacing(4);
076: hp.add(images.defaultPhoto().createImage());
077: hp.add(inner);
078:
079: add(hp);
080: setStyleName("mail-ContactPopup");
081: nameLabel.setStyleName("mail-ContactPopupName");
082: emailLabel.setStyleName("mail-ContactPopupEmail");
083: }
084: }
085:
086: private Contact[] contacts = new Contact[] {
087: new Contact("Benoit Mandelbrot", "benoit@example.com"),
088: new Contact("Albert Einstein", "albert@example.com"),
089: new Contact("Rene Descartes", "rene@example.com"),
090: new Contact("Bob Saget", "bob@example.com"),
091: new Contact("Ludwig von Beethoven", "ludwig@example.com"),
092: new Contact("Richard Feynman", "richard@example.com"),
093: new Contact("Alan Turing", "alan@example.com"),
094: new Contact("John von Neumann", "john@example.com") };
095:
096: private VerticalPanel panel = new VerticalPanel();
097: private final Images images;
098:
099: 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:;'>"
115: + contact.name + "</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: }
|