Adding gwt QR Code generator, and appspot configuration files.
[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 /**
25  * A Generator for contact informations, output is in MeCard format.
26  * 
27  * @author Yohann Coppel
28  */
29 public class ContactInfoGenerator implements GeneratorSource {
30   Grid table = null;
31   TextBox name = new TextBox();
32   TextBox company = new TextBox();
33   TextBox tel = new TextBox();
34   TextBox url = new TextBox();
35   TextBox email = new TextBox();
36   TextBox address = new TextBox();
37   TextBox memo = new TextBox();
38   TextBox[] widgets = {name, company, tel, url, email, address, memo};
39   
40   public ContactInfoGenerator(ChangeListener changeListener) {
41     for (TextBox w: widgets) {
42       w.addChangeListener(changeListener);
43     }
44   }
45   
46   public String getName() {
47     return "Contact information";
48   }
49
50   public String getText() throws GeneratorException {
51     String name = getNameField();
52     String company = getCompanyField();
53     String tel = getTelField();
54     String url = getUrlField();
55     String email = getEmailField();
56     String address = getAddressField();
57     String memo = getMemoField();
58     
59     // Build the output with obtained data.
60     // note that some informations may just be "" if they were not specified.
61     //return getVCard(name, company, tel, url, email, address, memo);
62     return getMeCard(name, company, tel, url, email, address, memo);
63   }
64
65   private String getMeCard(String name, String company, String tel, String url,
66       String email, String address, String memo) {
67     String output = "MECARD:";
68     output += "N:" + name + ";";
69     output += company.length() > 0 ? "ORG:" + company + ";" : "";
70     output += tel.length() > 0 ? "TEL:" + tel + ";" : "";
71     output += url.length() > 0 ? "URL:" + url + ";" : "";
72     output += email.length() > 0 ? "EMAIL:" + email + ";" : "";
73     output += address.length() > 0 ? "ADR:" + address + ";" : "";
74     output += memo.length() > 0 ? "NOTE:" + memo + ";" : "";
75     output += ";";    
76     return output;    
77   }
78   
79   /*// VCARD GENERATION. Keep this in case we want to go back to vcard format
80     // or have the option.
81   private String getVCard(String name, String company, String tel, String url,
82       String email, String address, String memo) {
83     String output = "BEGIN:VCARD\n";
84     output += "N:" + name + "\n";
85     output += company.length() > 0 ? "ORG:" + company + "\n" : "";
86     output += tel.length() > 0 ? "TEL:" + tel + "\n" : "";
87     output += url.length() > 0 ? "URL:" + url + "\n" : "";
88     output += email.length() > 0 ? "EMAIL:" + email + "\n" : "";
89     output += address.length() > 0 ? "ADR:" + address + "\n" : "";
90     output += memo.length() > 0 ? "NOTE:" + memo + "\n" : "";
91     output += "END:VCARD";
92     
93     return output;    
94   }
95   */
96   
97   private String getNameField() throws GeneratorException {
98     String inputName = name.getText();
99     if (inputName.length() < 1) {
100       throw new GeneratorException("Name must be at least 1 character.");
101     }
102     if (inputName.contains("\n")) {
103       throw new GeneratorException("Name should not contanains \\n characters.");
104     }
105     if (inputName.contains(";")) {
106       throw new GeneratorException("Name must not contains ; characters");
107     }
108     return inputName;
109   }
110   
111   private String getCompanyField() throws GeneratorException {
112     String input = company.getText();
113     if (input.length() < 1) {
114       return "";
115     }
116     if (input.contains("\n")) {
117       throw new GeneratorException("Company should not contanains \\n characters.");
118     }
119     if (input.contains(";")) {
120       throw new GeneratorException("Company must not contains ; characters");
121     }
122     // the input contains some informations. 
123     return input;
124   }
125
126   private String getTelField() throws GeneratorException {
127     String input = Validators.filterNumber(tel.getText());
128     if (input.length() < 1) {
129       return "";
130     }
131     Validators.validateNumber(input);
132     if (input.contains(";")) {
133       throw new GeneratorException("Tel must not contains ; characters");
134     }
135     return input;
136   }
137   
138   private String getUrlField() throws GeneratorException {
139     String input = url.getText();
140     if (input.length() < 1) {
141       return "";
142     }
143     Validators.validateUrl(input);
144     if (input.contains(";")) {
145       throw new GeneratorException("URL must not contains ; characters");
146     }
147     return input;
148   }
149   
150   private String getEmailField() throws GeneratorException {
151     String input = email.getText();
152     if (input.length() < 1) {
153       return "";
154     }
155     Validators.validateEmail(input);
156     if (input.contains(";")) {
157       throw new GeneratorException("Email must not contains ; characters");
158     }
159     return input;
160   }
161   
162   private String getAddressField() throws GeneratorException {
163     String input = address.getText();
164     if (input.length() < 1) {
165       return "";
166     }
167     if (input.contains("\n")) {
168       throw new GeneratorException("Address must not contain \\n characters.");
169     }
170     if (input.contains(";")) {
171       throw new GeneratorException("Address must not contains ; characters");
172     }
173     return input;
174   }
175   
176   private String getMemoField() throws GeneratorException {
177     String input = memo.getText();
178     if (input.length() < 1) {
179       return "";
180     }
181     if (input.contains("\n")) {
182       throw new GeneratorException("Memo must not contain \\n characters.");
183     }
184     if (input.contains(";")) {
185       throw new GeneratorException("Memo must not contains ; characters");
186     }
187     return input;
188   }
189   
190   public Grid getWidget() {
191     if (table != null) {
192       // early termination if the table has already been constructed
193       return table;
194     }
195     table = new Grid(7, 2);
196     
197     table.setText(0, 0, "Name");
198     table.setWidget(0, 1, name);
199     table.setText(1, 0, "Company");
200     table.setWidget(1, 1, company);
201     table.setText(2, 0, "Phone number");
202     table.setWidget(2, 1, tel);
203     table.setText(3, 0, "Email");
204     table.setWidget(3, 1, email);
205     table.setText(4, 0, "Address");
206     table.setWidget(4, 1, address);
207     table.setText(5, 0, "Website");
208     table.setWidget(5, 1, url);
209     table.setText(6, 0, "Memo");
210     table.setWidget(6, 1, memo);
211     
212     name.addStyleName(StylesDefs.INPUT_FIELD_REQUIRED);
213     return table;
214   }
215
216   public void validate(Widget widget) throws GeneratorException {
217     if (widget == name) getNameField();
218     if (widget == company) getCompanyField();
219     if (widget == tel) getTelField();
220     if (widget == email) getEmailField();
221     if (widget == address) getAddressField();
222     if (widget == url) getUrlField();
223     if (widget == memo) getMemoField();
224   }
225
226   public void setFocus() {
227     name.setFocus(true);
228   }
229 }