Add address line 2 support to generator
[zxing.git] / zxing.appspot.com / generator / src / com / google / zxing / web / generator / client / ContactInfoGenerator.java
1 /*
2  * Copyright (C) 2008 ZXing authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of 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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.zxing.web.generator.client;
18
19 import com.google.gwt.user.client.ui.ChangeListener;
20 import com.google.gwt.user.client.ui.Grid;
21 import com.google.gwt.user.client.ui.TextBox;
22 import com.google.gwt.user.client.ui.Widget;
23
24 import java.net.URI;
25
26 /**
27  * A Generator for contact informations, output is in MeCard format.
28  * 
29  * @author Yohann Coppel
30  */
31 public class ContactInfoGenerator implements GeneratorSource {
32   Grid table = null;
33   TextBox name = new TextBox();
34   TextBox company = new TextBox();
35   TextBox tel = new TextBox();
36   TextBox url = new TextBox();
37   TextBox email = new TextBox();
38   TextBox address = new TextBox();
39   TextBox address2 = new TextBox();
40   TextBox memo = new TextBox();
41   TextBox[] widgets = {name, company, tel, url, email, address, address2, memo};
42   
43   public ContactInfoGenerator(ChangeListener changeListener) {
44     for (TextBox w: widgets) {
45       w.addChangeListener(changeListener);
46     }
47   }
48   
49   public String getName() {
50     return "Contact information";
51   }
52
53   public String getText() throws GeneratorException {
54     String name = getNameField();
55     String company = getCompanyField();
56     String tel = getTelField();
57     String url = getUrlField();
58     String email = getEmailField();
59     String address = getAddressField();
60     String address2 = getAddress2Field();
61     String memo = getMemoField();
62     
63     // Build the output with obtained data.
64     // note that some informations may just be "" if they were not specified.
65     //return getVCard(name, company, tel, url, email, address, memo);
66     return getMeCard(name, company, tel, url, email, address, address2, memo);
67   }
68
69   private String getMeCard(String name, String company, String tel, String url,
70       String email, String address, String address2, String memo) {
71     StringBuilder output = new StringBuilder();
72     output.append("MECARD:");
73     output.append("N:").append(name).append(';');
74     maybeAppend(output, "ORG:", company);
75     maybeAppend(output, "TEL:", tel);
76     maybeAppend(output, "URL:", url);
77     maybeAppend(output, "EMAIL:", email);
78     maybeAppend(output, "ADR:", address);
79     if (address.length() > 0 || address2.length() > 0) {
80       output.append("ADR:");
81       if (address.length() > 0) {
82         output.append(address);
83       }
84       if (address2.length() > 0) {
85         if (address.length() > 0) {
86           output.append(' ');
87         }
88         output.append(address2);
89       }
90       output.append(';');
91     }
92     maybeAppend(output, "NOTE:", memo);
93     output.append(';');
94     return output.toString();
95   }
96
97   private static void maybeAppend(StringBuilder output, String prefix, String value) {
98     if (value.length() > 0) {
99       output.append(prefix).append(value).append(';');
100     }
101   }
102   
103   /*// VCARD GENERATION. Keep this in case we want to go back to vcard format
104     // or have the option.
105   private String getVCard(String name, String company, String tel, String url,
106       String email, String address, String memo) {
107     String output = "BEGIN:VCARD\n";
108     output += "N:" + name + "\n";
109     output += company.length() > 0 ? "ORG:" + company + "\n" : "";
110     output += tel.length() > 0 ? "TEL:" + tel + "\n" : "";
111     output += url.length() > 0 ? "URL:" + url + "\n" : "";
112     output += email.length() > 0 ? "EMAIL:" + email + "\n" : "";
113     output += address.length() > 0 ? "ADR:" + address + "\n" : "";
114     output += memo.length() > 0 ? "NOTE:" + memo + "\n" : "";
115     output += "END:VCARD";
116     
117     return output;    
118   }
119   */
120
121   private static String parseTextField(TextBox textBox) throws GeneratorException {
122     String input = textBox.getText();
123     if (input.length() < 1) {
124       return "";
125     }
126     if (input.contains("\n")) {
127       throw new GeneratorException("Field must not contain \\n characters.");
128     }
129     if (input.contains(";")) {
130       throw new GeneratorException("Field must not contains ; characters");
131     }
132     return input;
133   }
134   
135   private String getNameField() throws GeneratorException {
136     return parseTextField(name);
137   }
138   
139   private String getCompanyField() throws GeneratorException {
140     return parseTextField(company);
141   }
142
143   private String getTelField() throws GeneratorException {
144     String input = Validators.filterNumber(tel.getText());
145     if (input.length() < 1) {
146       return "";
147     }
148     Validators.validateNumber(input);
149     if (input.contains(";")) {
150       throw new GeneratorException("Tel must not contains ; characters");
151     }
152     return input;
153   }
154   
155   private String getUrlField() throws GeneratorException {
156     String input = url.getText();
157     Validators.validateUrl(input);
158     return input;
159   }
160   
161   private String getEmailField() throws GeneratorException {
162     String input = email.getText();
163     if (input.length() < 1) {
164       return "";
165     }
166     Validators.validateEmail(input);
167     if (input.contains(";")) {
168       throw new GeneratorException("Email must not contains ; characters");
169     }
170     return input;
171   }
172   
173   private String getAddressField() throws GeneratorException {
174     return parseTextField(address);
175   }
176
177   private String getAddress2Field() throws GeneratorException {
178     return parseTextField(address2);
179   }
180   
181   private String getMemoField() throws GeneratorException {
182     return parseTextField(memo);
183   }
184   
185   public Grid getWidget() {
186     if (table != null) {
187       // early termination if the table has already been constructed
188       return table;
189     }
190     table = new Grid(8, 2);
191     
192     table.setText(0, 0, "Name");
193     table.setWidget(0, 1, name);
194     table.setText(1, 0, "Company");
195     table.setWidget(1, 1, company);
196     table.setText(2, 0, "Phone number");
197     table.setWidget(2, 1, tel);
198     table.setText(3, 0, "Email");
199     table.setWidget(3, 1, email);
200     table.setText(4, 0, "Address");
201     table.setWidget(4, 1, address);
202     table.setText(5, 0, "Address 2");
203     table.setWidget(5, 1, address2);
204     table.setText(6, 0, "Website");
205     table.setWidget(6, 1, url);
206     table.setText(7, 0, "Memo");
207     table.setWidget(7, 1, memo);
208     
209     name.addStyleName(StylesDefs.INPUT_FIELD_REQUIRED);
210     return table;
211   }
212
213   public void validate(Widget widget) throws GeneratorException {
214     if (widget == name) getNameField();
215     if (widget == company) getCompanyField();
216     if (widget == tel) getTelField();
217     if (widget == email) getEmailField();
218     if (widget == address) getAddressField();
219     if (widget == address2) getAddress2Field();
220     if (widget == url) getUrlField();
221     if (widget == memo) getMemoField();
222   }
223
224   public void setFocus() {
225     name.setFocus(true);
226   }
227 }