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;
27 * A Generator for contact informations, output is in MeCard format.
29 * @author Yohann Coppel
31 public class ContactInfoGenerator implements GeneratorSource {
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};
43 public ContactInfoGenerator(ChangeListener changeListener) {
44 for (TextBox w: widgets) {
45 w.addChangeListener(changeListener);
49 public String getName() {
50 return "Contact information";
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();
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);
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);
84 if (address2.length() > 0) {
85 if (address.length() > 0) {
88 output.append(address2);
92 maybeAppend(output, "NOTE:", memo);
94 return output.toString();
97 private static void maybeAppend(StringBuilder output, String prefix, String value) {
98 if (value.length() > 0) {
99 output.append(prefix).append(value).append(';');
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";
121 private static String parseTextField(TextBox textBox) throws GeneratorException {
122 String input = textBox.getText();
123 if (input.length() < 1) {
126 if (input.contains("\n")) {
127 throw new GeneratorException("Field must not contain \\n characters.");
129 if (input.contains(";")) {
130 throw new GeneratorException("Field must not contains ; characters");
135 private String getNameField() throws GeneratorException {
136 return parseTextField(name);
139 private String getCompanyField() throws GeneratorException {
140 return parseTextField(company);
143 private String getTelField() throws GeneratorException {
144 String input = Validators.filterNumber(tel.getText());
145 if (input.length() < 1) {
148 Validators.validateNumber(input);
149 if (input.contains(";")) {
150 throw new GeneratorException("Tel must not contains ; characters");
155 private String getUrlField() throws GeneratorException {
156 String input = url.getText();
157 Validators.validateUrl(input);
161 private String getEmailField() throws GeneratorException {
162 String input = email.getText();
163 if (input.length() < 1) {
166 Validators.validateEmail(input);
167 if (input.contains(";")) {
168 throw new GeneratorException("Email must not contains ; characters");
173 private String getAddressField() throws GeneratorException {
174 return parseTextField(address);
177 private String getAddress2Field() throws GeneratorException {
178 return parseTextField(address2);
181 private String getMemoField() throws GeneratorException {
182 return parseTextField(memo);
185 public Grid getWidget() {
187 // early termination if the table has already been constructed
190 table = new Grid(8, 2);
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);
209 name.addStyleName(StylesDefs.INPUT_FIELD_REQUIRED);
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();
224 public void setFocus() {