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 memo = new TextBox();
38 TextBox[] widgets = {name, company, tel, url, email, address, memo};
40 public ContactInfoGenerator(ChangeListener changeListener) {
41 for (TextBox w: widgets) {
42 w.addChangeListener(changeListener);
46 public String getName() {
47 return "Contact information";
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();
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);
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 + ";" : "";
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";
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.");
102 if (inputName.contains("\n")) {
103 throw new GeneratorException("Name should not contanains \\n characters.");
105 if (inputName.contains(";")) {
106 throw new GeneratorException("Name must not contains ; characters");
111 private String getCompanyField() throws GeneratorException {
112 String input = company.getText();
113 if (input.length() < 1) {
116 if (input.contains("\n")) {
117 throw new GeneratorException("Company should not contanains \\n characters.");
119 if (input.contains(";")) {
120 throw new GeneratorException("Company must not contains ; characters");
122 // the input contains some informations.
126 private String getTelField() throws GeneratorException {
127 String input = Validators.filterNumber(tel.getText());
128 if (input.length() < 1) {
131 Validators.validateNumber(input);
132 if (input.contains(";")) {
133 throw new GeneratorException("Tel must not contains ; characters");
138 private String getUrlField() throws GeneratorException {
139 String input = url.getText();
140 if (input.length() < 1) {
143 Validators.validateUrl(input);
144 if (input.contains(";")) {
145 throw new GeneratorException("URL must not contains ; characters");
150 private String getEmailField() throws GeneratorException {
151 String input = email.getText();
152 if (input.length() < 1) {
155 Validators.validateEmail(input);
156 if (input.contains(";")) {
157 throw new GeneratorException("Email must not contains ; characters");
162 private String getAddressField() throws GeneratorException {
163 String input = address.getText();
164 if (input.length() < 1) {
167 if (input.contains("\n")) {
168 throw new GeneratorException("Address must not contain \\n characters.");
170 if (input.contains(";")) {
171 throw new GeneratorException("Address must not contains ; characters");
176 private String getMemoField() throws GeneratorException {
177 String input = memo.getText();
178 if (input.length() < 1) {
181 if (input.contains("\n")) {
182 throw new GeneratorException("Memo must not contain \\n characters.");
184 if (input.contains(";")) {
185 throw new GeneratorException("Memo must not contains ; characters");
190 public Grid getWidget() {
192 // early termination if the table has already been constructed
195 table = new Grid(7, 2);
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);
212 name.addStyleName(StylesDefs.INPUT_FIELD_REQUIRED);
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();
226 public void setFocus() {