a4d77c563283a6d29d9522b3aba0025c1712be83
[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     name = name.replace(",", ""); // remove commas -- reserved char in MECARD
75     output.append("N:").append(name).append(';');
76     //maybeAppend(output, "ORG:", company); // Not standard; don't generate
77     maybeAppend(output, "TEL:", tel);
78     maybeAppend(output, "URL:", url);
79     maybeAppend(output, "EMAIL:", email);
80     if (address.length() > 0 || address2.length() > 0) {
81       output.append("ADR:");
82       if (address.length() > 0) {
83         output.append(address);
84       }
85       if (address2.length() > 0) {
86         if (address.length() > 0) {
87           output.append(' ');
88         }
89         output.append(address2);
90       }
91       output.append(';');
92     }
93     maybeAppend(output, "NOTE:", memo);
94     output.append(';');
95     return output.toString();
96   }
97
98   private static void maybeAppend(StringBuilder output, String prefix, String value) {
99     if (value.length() > 0) {
100       output.append(prefix).append(value).append(';');
101     }
102   }
103   
104   /*// VCARD GENERATION. Keep this in case we want to go back to vcard format
105     // or have the option.
106   private String getVCard(String name, String company, String tel, String url,
107       String email, String address, String memo) {
108     String output = "BEGIN:VCARD\n";
109     output += "N:" + name + "\n";
110     output += company.length() > 0 ? "ORG:" + company + "\n" : "";
111     output += tel.length() > 0 ? "TEL:" + tel + "\n" : "";
112     output += url.length() > 0 ? "URL:" + url + "\n" : "";
113     output += email.length() > 0 ? "EMAIL:" + email + "\n" : "";
114     output += address.length() > 0 ? "ADR:" + address + "\n" : "";
115     output += memo.length() > 0 ? "NOTE:" + memo + "\n" : "";
116     output += "END:VCARD";
117     
118     return output;    
119   }
120   */
121
122   private static String parseTextField(String name, TextBox textBox) throws GeneratorException {
123     String input = textBox.getText();
124     if (input.length() < 1) {
125       return "";
126     }
127     if (input.contains("\n")) {
128       throw new GeneratorException(name + " field must not contain \\n characters.");
129     }
130     if (input.contains(";")) {
131       throw new GeneratorException(name + " field must not contains ; characters");
132     }
133     return input;
134   }
135   
136   private String getNameField() throws GeneratorException {
137     String input = name.getText();
138     if (input.length() < 1) {
139       throw new GeneratorException("Name must be at least 1 character.");
140     }
141     return parseTextField("Name", name);
142   }
143   
144   private String getCompanyField() throws GeneratorException {
145     return parseTextField("Company", company);
146   }
147
148   private String getTelField() throws GeneratorException {
149     String input = Validators.filterNumber(tel.getText());
150     if (input.length() < 1) {
151       return "";
152     }
153     Validators.validateNumber(input);
154     if (input.contains(";")) {
155       throw new GeneratorException("Tel must not contains ; characters");
156     }
157     return input;
158   }
159   
160   private String getUrlField() throws GeneratorException {
161     String input = url.getText();
162     if (input != null && input.length() > 0) {
163       Validators.validateUrl(input);
164     }
165     return input;
166   }
167   
168   private String getEmailField() throws GeneratorException {
169     String input = email.getText();
170     if (input.length() < 1) {
171       return "";
172     }
173     Validators.validateEmail(input);
174     if (input.contains(";")) {
175       throw new GeneratorException("Email must not contains ; characters");
176     }
177     return input;
178   }
179   
180   private String getAddressField() throws GeneratorException {
181     return parseTextField("Address", address);
182   }
183
184   private String getAddress2Field() throws GeneratorException {
185     return parseTextField("Address 2", address2);
186   }
187   
188   private String getMemoField() throws GeneratorException {
189     return parseTextField("Memo", memo);
190   }
191   
192   public Grid getWidget() {
193     if (table != null) {
194       // early termination if the table has already been constructed
195       return table;
196     }
197     table = new Grid(8, 2);
198     
199     table.setText(0, 0, "Name");
200     table.setWidget(0, 1, name);
201     table.setText(1, 0, "Company");
202     table.setWidget(1, 1, company);
203     table.setText(2, 0, "Phone number");
204     table.setWidget(2, 1, tel);
205     table.setText(3, 0, "Email");
206     table.setWidget(3, 1, email);
207     table.setText(4, 0, "Address");
208     table.setWidget(4, 1, address);
209     table.setText(5, 0, "Address 2");
210     table.setWidget(5, 1, address2);
211     table.setText(6, 0, "Website");
212     table.setWidget(6, 1, url);
213     table.setText(7, 0, "Memo");
214     table.setWidget(7, 1, memo);
215     
216     name.addStyleName(StylesDefs.INPUT_FIELD_REQUIRED);
217     return table;
218   }
219
220   public void validate(Widget widget) throws GeneratorException {
221     if (widget == name) getNameField();
222     if (widget == company) getCompanyField();
223     if (widget == tel) getTelField();
224     if (widget == email) getEmailField();
225     if (widget == address) getAddressField();
226     if (widget == address2) getAddress2Field();
227     if (widget == url) getUrlField();
228     if (widget == memo) getMemoField();
229   }
230
231   public void setFocus() {
232     name.setFocus(true);
233   }
234 }