1 /*
2  * Copyright 2010 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.showcase.client.content.cell;
17 
18 import com.google.gwt.core.client.GWT;
19 import com.google.gwt.event.dom.client.ClickEvent;
20 import com.google.gwt.event.dom.client.ClickHandler;
21 import com.google.gwt.i18n.client.DateTimeFormat;
22 import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
23 import com.google.gwt.sample.showcase.client.content.cell.ContactDatabase.Category;
24 import com.google.gwt.sample.showcase.client.content.cell.ContactDatabase.ContactInfo;
25 import com.google.gwt.uibinder.client.UiBinder;
26 import com.google.gwt.uibinder.client.UiField;
27 import com.google.gwt.user.client.ui.Button;
28 import com.google.gwt.user.client.ui.Composite;
29 import com.google.gwt.user.client.ui.ListBox;
30 import com.google.gwt.user.client.ui.TextArea;
31 import com.google.gwt.user.client.ui.TextBox;
32 import com.google.gwt.user.client.ui.Widget;
33 import com.google.gwt.user.datepicker.client.DateBox;
34 
35 /**
36  * A form used for editing contacts.
37  */
38 public class ContactInfoForm extends Composite {
39 
40   private static Binder uiBinder = GWT.create(Binder.class);
41 
42   interface Binder extends UiBinder<Widget, ContactInfoForm> {
43   }
44 
45   @UiField
46   TextArea addressBox;
47   @UiField
48   DateBox birthdayBox;
49   @UiField
50   ListBox categoryBox;
51   @UiField
52   Button createButton;
53   @UiField
54   TextBox firstNameBox;
55   @UiField
56   TextBox lastNameBox;
57   @UiField
58   Button updateButton;
59 
60   private ContactInfo contactInfo;
61 
ContactInfoForm()62   public ContactInfoForm() {
63     initWidget(uiBinder.createAndBindUi(this));
64     DateTimeFormat dateFormat = DateTimeFormat.getFormat(
65         PredefinedFormat.DATE_LONG);
66     birthdayBox.setFormat(new DateBox.DefaultFormat(dateFormat));
67 
68     // Add the categories to the category box.
69     final Category[] categories = ContactDatabase.get().queryCategories();
70     for (Category category : categories) {
71       categoryBox.addItem(category.getDisplayName());
72     }
73 
74     // Initialize the contact to null.
75     setContact(null);
76 
77     // Handle events.
78     updateButton.addClickHandler(new ClickHandler() {
79       public void onClick(ClickEvent event) {
80         if (contactInfo == null) {
81           return;
82         }
83 
84         // Update the contact.
85         contactInfo.setFirstName(firstNameBox.getText());
86         contactInfo.setLastName(lastNameBox.getText());
87         contactInfo.setAddress(addressBox.getText());
88         contactInfo.setBirthday(birthdayBox.getValue());
89         int categoryIndex = categoryBox.getSelectedIndex();
90         contactInfo.setCategory(categories[categoryIndex]);
91 
92         // Update the views.
93         ContactDatabase.get().refreshDisplays();
94       }
95     });
96     createButton.addClickHandler(new ClickHandler() {
97       public void onClick(ClickEvent event) {
98         int categoryIndex = categoryBox.getSelectedIndex();
99         Category category = categories[categoryIndex];
100         contactInfo = new ContactInfo(category);
101         contactInfo.setFirstName(firstNameBox.getText());
102         contactInfo.setLastName(lastNameBox.getText());
103         contactInfo.setAddress(addressBox.getText());
104         contactInfo.setBirthday(birthdayBox.getValue());
105         ContactDatabase.get().addContact(contactInfo);
106         setContact(contactInfo);
107       }
108     });
109   }
110 
setContact(ContactInfo contact)111   public void setContact(ContactInfo contact) {
112     this.contactInfo = contact;
113     updateButton.setEnabled(contact != null);
114     if (contact != null) {
115       firstNameBox.setText(contact.getFirstName());
116       lastNameBox.setText(contact.getLastName());
117       addressBox.setText(contact.getAddress());
118       birthdayBox.setValue(contact.getBirthday());
119       Category category = contact.getCategory();
120       Category[] categories = ContactDatabase.get().queryCategories();
121       for (int i = 0; i < categories.length; i++) {
122         if (category == categories[i]) {
123           categoryBox.setSelectedIndex(i);
124           break;
125         }
126       }
127     }
128   }
129 }
130