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 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);
85 if (address2.length() > 0) {
86 if (address.length() > 0) {
89 output.append(address2);
93 maybeAppend(output, "NOTE:", memo);
95 return output.toString();
98 private static void maybeAppend(StringBuilder output, String prefix, String value) {
99 if (value.length() > 0) {
100 output.append(prefix).append(value).append(';');
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";
122 private static String parseTextField(String name, TextBox textBox) throws GeneratorException {
123 String input = textBox.getText();
124 if (input.length() < 1) {
127 if (input.contains("\n")) {
128 throw new GeneratorException(name + " field must not contain \\n characters.");
130 if (input.contains(";")) {
131 throw new GeneratorException(name + " field must not contains ; characters");
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.");
141 return parseTextField("Name", name);
144 private String getCompanyField() throws GeneratorException {
145 return parseTextField("Company", company);
148 private String getTelField() throws GeneratorException {
149 String input = Validators.filterNumber(tel.getText());
150 if (input.length() < 1) {
153 Validators.validateNumber(input);
154 if (input.contains(";")) {
155 throw new GeneratorException("Tel must not contains ; characters");
160 private String getUrlField() throws GeneratorException {
161 String input = url.getText();
162 if (input != null && input.length() > 0) {
163 Validators.validateUrl(input);
168 private String getEmailField() throws GeneratorException {
169 String input = email.getText();
170 if (input.length() < 1) {
173 Validators.validateEmail(input);
174 if (input.contains(";")) {
175 throw new GeneratorException("Email must not contains ; characters");
180 private String getAddressField() throws GeneratorException {
181 return parseTextField("Address", address);
184 private String getAddress2Field() throws GeneratorException {
185 return parseTextField("Address 2", address2);
188 private String getMemoField() throws GeneratorException {
189 return parseTextField("Memo", memo);
192 public Grid getWidget() {
194 // early termination if the table has already been constructed
197 table = new Grid(8, 2);
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);
216 name.addStyleName(StylesDefs.INPUT_FIELD_REQUIRED);
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();
231 public void setFocus() {