Issue 295 and Issue 294, ADR was getting generated twice!
[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.event.dom.client.KeyPressHandler;
20 import com.google.gwt.user.client.ui.ChangeListener;
21 import com.google.gwt.user.client.ui.Grid;
22 import com.google.gwt.user.client.ui.TextBox;
23 import com.google.gwt.user.client.ui.Widget;
24
25 /**
26  * A Generator for contact informations, output is in MeCard format.
27  * 
28  * @author Yohann Coppel
29  */
30 public class ContactInfoGenerator implements GeneratorSource {
31   Grid table = null;
32   TextBox name = new TextBox();
33   TextBox company = new TextBox();
34   TextBox tel = new TextBox();
35   TextBox url = new TextBox();
36   TextBox email = new TextBox();
37   TextBox address = new TextBox();
38   TextBox address2 = new TextBox();
39   TextBox memo = new TextBox();
40   TextBox[] widgets = {name, company, tel, url, email, address, address2, memo};
41   
42   public ContactInfoGenerator(ChangeListener changeListener,
43       KeyPressHandler keyListener) {
44     for (TextBox w: widgets) {
45       w.addChangeListener(changeListener);
46       w.addKeyPressHandler(keyListener);
47     }
48   }
49   
50   public String getName() {
51     return "Contact information";
52   }
53
54   public String getText() throws GeneratorException {
55     String name = getNameField();
56     //String company = getCompanyField();
57     String tel = getTelField();
58     String url = getUrlField();
59     String email = getEmailField();
60     String address = getAddressField();
61     String address2 = getAddress2Field();
62     String memo = getMemoField();
63     
64     // Build the output with obtained data.
65     // note that some informations may just be "" if they were not specified.
66     //return getVCard(name, company, tel, url, email, address, memo);
67     return getMeCard(name, tel, url, email, address, address2, memo);
68   }
69
70   private String getMeCard(String name, String tel, String url,
71       String email, String address, String address2, String memo) {
72     StringBuilder output = new StringBuilder();
73     output.append("MECARD:");
74     output.append("N:").append(name).append(';');
75     //maybeAppend(output, "ORG:", company); // Not standard; don't generate
76     maybeAppend(output, "TEL:", tel);
77     maybeAppend(output, "URL:", url);
78     maybeAppend(output, "EMAIL:", email);
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(String name, 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(name + " field must not contain \\n characters.");
128     }
129     if (input.contains(";")) {
130       throw new GeneratorException(name + " field must not contains ; characters");
131     }
132     return input;
133   }
134   
135   private String getNameField() throws GeneratorException {
136     String input = name.getText();
137     if (input.length() < 1) {
138       throw new GeneratorException("Name must be at least 1 character.");
139     }
140     return parseTextField("Name", name);
141   }
142   
143   private String getCompanyField() throws GeneratorException {
144     return parseTextField("Company", company);
145   }
146
147   private String getTelField() throws GeneratorException {
148     String input = Validators.filterNumber(tel.getText());
149     if (input.length() < 1) {
150       return "";
151     }
152     Validators.validateNumber(input);
153     if (input.contains(";")) {
154       throw new GeneratorException("Tel must not contains ; characters");
155     }
156     return input;
157   }
158   
159   private String getUrlField() throws GeneratorException {
160     String input = url.getText();
161     if (input != null && input.length() > 0) {
162       Validators.validateUrl(input);
163     }
164     return input;
165   }
166   
167   private String getEmailField() throws GeneratorException {
168     String input = email.getText();
169     if (input.length() < 1) {
170       return "";
171     }
172     Validators.validateEmail(input);
173     if (input.contains(";")) {
174       throw new GeneratorException("Email must not contains ; characters");
175     }
176     return input;
177   }
178   
179   private String getAddressField() throws GeneratorException {
180     return parseTextField("Address", address);
181   }
182
183   private String getAddress2Field() throws GeneratorException {
184     return parseTextField("Address 2", address2);
185   }
186   
187   private String getMemoField() throws GeneratorException {
188     return parseTextField("Memo", memo);
189   }
190   
191   public Grid getWidget() {
192     if (table != null) {
193       // early termination if the table has already been constructed
194       return table;
195     }
196     table = new Grid(8, 2);
197     
198     table.setText(0, 0, "Name");
199     table.setWidget(0, 1, name);
200     table.setText(1, 0, "Company");
201     table.setWidget(1, 1, company);
202     table.setText(2, 0, "Phone number");
203     table.setWidget(2, 1, tel);
204     table.setText(3, 0, "Email");
205     table.setWidget(3, 1, email);
206     table.setText(4, 0, "Address");
207     table.setWidget(4, 1, address);
208     table.setText(5, 0, "Address 2");
209     table.setWidget(5, 1, address2);
210     table.setText(6, 0, "Website");
211     table.setWidget(6, 1, url);
212     table.setText(7, 0, "Memo");
213     table.setWidget(7, 1, memo);
214     
215     name.addStyleName(StylesDefs.INPUT_FIELD_REQUIRED);
216     return table;
217   }
218
219   public void validate(Widget widget) throws GeneratorException {
220     if (widget == name) getNameField();
221     if (widget == company) getCompanyField();
222     if (widget == tel) getTelField();
223     if (widget == email) getEmailField();
224     if (widget == address) getAddressField();
225     if (widget == address2) getAddress2Field();
226     if (widget == url) getUrlField();
227     if (widget == memo) getMemoField();
228   }
229
230   public void setFocus() {
231     name.setFocus(true);
232   }
233 }