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.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;
26 * A Generator for contact informations, output is in MeCard format.
28 * @author Yohann Coppel
30 public class ContactInfoGenerator implements GeneratorSource {
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};
42 public ContactInfoGenerator(ChangeListener changeListener,
43 KeyPressHandler keyListener) {
44 for (TextBox w: widgets) {
45 w.addChangeListener(changeListener);
46 w.addKeyPressHandler(keyListener);
50 public String getName() {
51 return "Contact information";
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();
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);
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 output.append("N:").append(name).append(';');
75 //maybeAppend(output, "ORG:", company); // Not standard; don't generate
76 maybeAppend(output, "TEL:", tel);
77 maybeAppend(output, "URL:", url);
78 maybeAppend(output, "EMAIL:", email);
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(String name, TextBox textBox) throws GeneratorException {
122 String input = textBox.getText();
123 if (input.length() < 1) {
126 if (input.contains("\n")) {
127 throw new GeneratorException(name + " field must not contain \\n characters.");
129 if (input.contains(";")) {
130 throw new GeneratorException(name + " field must not contains ; characters");
135 private String getNameField() throws GeneratorException {
136 String input = name.getText();
137 if (input.length() < 1) {
138 throw new GeneratorException("Name must be at least 1 character.");
140 return parseTextField("Name", name);
143 private String getCompanyField() throws GeneratorException {
144 return parseTextField("Company", company);
147 private String getTelField() throws GeneratorException {
148 String input = Validators.filterNumber(tel.getText());
149 if (input.length() < 1) {
152 Validators.validateNumber(input);
153 if (input.contains(";")) {
154 throw new GeneratorException("Tel must not contains ; characters");
159 private String getUrlField() throws GeneratorException {
160 String input = url.getText();
161 if (input != null && input.length() > 0) {
162 Validators.validateUrl(input);
167 private String getEmailField() throws GeneratorException {
168 String input = email.getText();
169 if (input.length() < 1) {
172 Validators.validateEmail(input);
173 if (input.contains(";")) {
174 throw new GeneratorException("Email must not contains ; characters");
179 private String getAddressField() throws GeneratorException {
180 return parseTextField("Address", address);
183 private String getAddress2Field() throws GeneratorException {
184 return parseTextField("Address 2", address2);
187 private String getMemoField() throws GeneratorException {
188 return parseTextField("Memo", memo);
191 public Grid getWidget() {
193 // early termination if the table has already been constructed
196 table = new Grid(8, 2);
198 table.setText(0, 0, "Name");
199 table.setWidget(0, 1, name);
200 table.setText(1, 0, "Company");
201 table.setWidget(1, 1, company);
202 table.setText(2, 0, "Phone number");
203 table.setWidget(2, 1, tel);
204 table.setText(3, 0, "Email");
205 table.setWidget(3, 1, email);
206 table.setText(4, 0, "Address");
207 table.setWidget(4, 1, address);
208 table.setText(5, 0, "Address 2");
209 table.setWidget(5, 1, address2);
210 table.setText(6, 0, "Website");
211 table.setWidget(6, 1, url);
212 table.setText(7, 0, "Memo");
213 table.setWidget(7, 1, memo);
215 name.addStyleName(StylesDefs.INPUT_FIELD_REQUIRED);
219 public void validate(Widget widget) throws GeneratorException {
220 if (widget == name) getNameField();
221 if (widget == company) getCompanyField();
222 if (widget == tel) getTelField();
223 if (widget == email) getEmailField();
224 if (widget == address) getAddressField();
225 if (widget == address2) getAddress2Field();
226 if (widget == url) getUrlField();
227 if (widget == memo) getMemoField();
230 public void setFocus() {