2 * Copyright (C) 2008 ZXing authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.web.generator.client;
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;
25 * A Generator for contact informations, output is in MeCard format.
27 * @author Yohann Coppel
29 public class ContactInfoGenerator implements GeneratorSource {
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 address2 = new TextBox();
38 TextBox memo = new TextBox();
39 TextBox[] widgets = {name, company, tel, url, email, address, address2, memo};
41 public ContactInfoGenerator(ChangeListener changeListener) {
42 for (TextBox w: widgets) {
43 w.addChangeListener(changeListener);
47 public String getName() {
48 return "Contact information";
51 public String getText() throws GeneratorException {
52 String name = getNameField();
53 String company = getCompanyField();
54 String tel = getTelField();
55 String url = getUrlField();
56 String email = getEmailField();
57 String address = getAddressField();
58 String address2 = getAddress2Field();
59 String memo = getMemoField();
61 // Build the output with obtained data.
62 // note that some informations may just be "" if they were not specified.
63 //return getVCard(name, company, tel, url, email, address, memo);
64 return getMeCard(name, company, tel, url, email, address, address2, memo);
67 private String getMeCard(String name, String company, String tel, String url,
68 String email, String address, String address2, String memo) {
69 StringBuilder output = new StringBuilder();
70 output.append("MECARD:");
71 output.append("N:").append(name).append(';');
72 maybeAppend(output, "ORG:", company);
73 maybeAppend(output, "TEL:", tel);
74 maybeAppend(output, "URL:", url);
75 maybeAppend(output, "EMAIL:", email);
76 maybeAppend(output, "ADR:", address);
77 if (address.length() > 0 || address2.length() > 0) {
78 output.append("ADR:");
79 if (address.length() > 0) {
80 output.append(address);
82 if (address2.length() > 0) {
83 if (address.length() > 0) {
86 output.append(address2);
90 maybeAppend(output, "NOTE:", memo);
92 return output.toString();
95 private static void maybeAppend(StringBuilder output, String prefix, String value) {
96 if (value.length() > 0) {
97 output.append(prefix).append(value).append(';');
101 /*// VCARD GENERATION. Keep this in case we want to go back to vcard format
102 // or have the option.
103 private String getVCard(String name, String company, String tel, String url,
104 String email, String address, String memo) {
105 String output = "BEGIN:VCARD\n";
106 output += "N:" + name + "\n";
107 output += company.length() > 0 ? "ORG:" + company + "\n" : "";
108 output += tel.length() > 0 ? "TEL:" + tel + "\n" : "";
109 output += url.length() > 0 ? "URL:" + url + "\n" : "";
110 output += email.length() > 0 ? "EMAIL:" + email + "\n" : "";
111 output += address.length() > 0 ? "ADR:" + address + "\n" : "";
112 output += memo.length() > 0 ? "NOTE:" + memo + "\n" : "";
113 output += "END:VCARD";
119 private static String parseTextField(TextBox textBox) throws GeneratorException {
120 String input = textBox.getText();
121 if (input.length() < 1) {
124 if (input.contains("\n")) {
125 throw new GeneratorException("Field must not contain \\n characters.");
127 if (input.contains(";")) {
128 throw new GeneratorException("Field must not contains ; characters");
133 private String getNameField() throws GeneratorException {
134 return parseTextField(name);
137 private String getCompanyField() throws GeneratorException {
138 return parseTextField(company);
141 private String getTelField() throws GeneratorException {
142 String input = Validators.filterNumber(tel.getText());
143 if (input.length() < 1) {
146 Validators.validateNumber(input);
147 if (input.contains(";")) {
148 throw new GeneratorException("Tel must not contains ; characters");
153 private String getUrlField() throws GeneratorException {
154 String input = url.getText();
155 Validators.validateUrl(input);
159 private String getEmailField() throws GeneratorException {
160 String input = email.getText();
161 if (input.length() < 1) {
164 Validators.validateEmail(input);
165 if (input.contains(";")) {
166 throw new GeneratorException("Email must not contains ; characters");
171 private String getAddressField() throws GeneratorException {
172 return parseTextField(address);
175 private String getAddress2Field() throws GeneratorException {
176 return parseTextField(address2);
179 private String getMemoField() throws GeneratorException {
180 return parseTextField(memo);
183 public Grid getWidget() {
185 // early termination if the table has already been constructed
188 table = new Grid(8, 2);
190 table.setText(0, 0, "Name");
191 table.setWidget(0, 1, name);
192 table.setText(1, 0, "Company");
193 table.setWidget(1, 1, company);
194 table.setText(2, 0, "Phone number");
195 table.setWidget(2, 1, tel);
196 table.setText(3, 0, "Email");
197 table.setWidget(3, 1, email);
198 table.setText(4, 0, "Address");
199 table.setWidget(4, 1, address);
200 table.setText(5, 0, "Address 2");
201 table.setWidget(5, 1, address2);
202 table.setText(6, 0, "Website");
203 table.setWidget(6, 1, url);
204 table.setText(7, 0, "Memo");
205 table.setWidget(7, 1, memo);
207 name.addStyleName(StylesDefs.INPUT_FIELD_REQUIRED);
211 public void validate(Widget widget) throws GeneratorException {
212 if (widget == name) getNameField();
213 if (widget == company) getCompanyField();
214 if (widget == tel) getTelField();
215 if (widget == email) getEmailField();
216 if (widget == address) getAddressField();
217 if (widget == address2) getAddress2Field();
218 if (widget == url) getUrlField();
219 if (widget == memo) getMemoField();
222 public void setFocus() {