package com.google.zxing.web.generator.client;
+import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.TextBox;
public class ContactInfoGenerator implements GeneratorSource {
Grid table = null;
TextBox name = new TextBox();
- TextBox company = new TextBox();
+ //TextBox company = new TextBox();
TextBox tel = new TextBox();
TextBox url = new TextBox();
TextBox email = new TextBox();
TextBox address = new TextBox();
TextBox address2 = new TextBox();
TextBox memo = new TextBox();
- TextBox[] widgets = {name, company, tel, url, email, address, address2, memo};
+ TextBox[] widgets = {name, tel, url, email, address, address2, memo};
- public ContactInfoGenerator(ChangeListener changeListener) {
+ public ContactInfoGenerator(ChangeListener changeListener,
+ KeyPressHandler keyListener) {
for (TextBox w: widgets) {
w.addChangeListener(changeListener);
+ w.addKeyPressHandler(keyListener);
}
}
public String getText() throws GeneratorException {
String name = getNameField();
- String company = getCompanyField();
+ //String company = getCompanyField();
String tel = getTelField();
String url = getUrlField();
String email = getEmailField();
// Build the output with obtained data.
// note that some informations may just be "" if they were not specified.
//return getVCard(name, company, tel, url, email, address, memo);
- return getMeCard(name, company, tel, url, email, address, address2, memo);
+ return getMeCard(name, tel, url, email, address, address2, memo);
}
- private String getMeCard(String name, String company, String tel, String url,
+ private String getMeCard(String name, String tel, String url,
String email, String address, String address2, String memo) {
StringBuilder output = new StringBuilder();
output.append("MECARD:");
+ name = name.replace(",", ""); // remove commas -- reserved char in MECARD
output.append("N:").append(name).append(';');
- maybeAppend(output, "ORG:", company);
+ //maybeAppend(output, "ORG:", company); // Not standard; don't generate
maybeAppend(output, "TEL:", tel);
maybeAppend(output, "URL:", url);
maybeAppend(output, "EMAIL:", email);
- maybeAppend(output, "ADR:", address);
if (address.length() > 0 || address2.length() > 0) {
output.append("ADR:");
if (address.length() > 0) {
}
*/
- private static String parseTextField(TextBox textBox) throws GeneratorException {
+ private static String parseTextField(String name, TextBox textBox) throws GeneratorException {
String input = textBox.getText();
if (input.length() < 1) {
return "";
}
if (input.contains("\n")) {
- throw new GeneratorException("Field must not contain \\n characters.");
+ throw new GeneratorException(name + " field must not contain \\n characters.");
}
if (input.contains(";")) {
- throw new GeneratorException("Field must not contains ; characters");
+ throw new GeneratorException(name + " field must not contains ; characters");
}
return input;
}
private String getNameField() throws GeneratorException {
- return parseTextField(name);
+ String input = name.getText();
+ if (input.length() < 1) {
+ throw new GeneratorException("Name must be at least 1 character.");
+ }
+ return parseTextField("Name", name);
}
- private String getCompanyField() throws GeneratorException {
- return parseTextField(company);
- }
+ //private String getCompanyField() throws GeneratorException {
+ // return parseTextField("Company", company);
+ //}
private String getTelField() throws GeneratorException {
String input = Validators.filterNumber(tel.getText());
}
private String getAddressField() throws GeneratorException {
- return parseTextField(address);
+ return parseTextField("Address", address);
}
private String getAddress2Field() throws GeneratorException {
- return parseTextField(address2);
+ return parseTextField("Address 2", address2);
}
private String getMemoField() throws GeneratorException {
- return parseTextField(memo);
+ return parseTextField("Memo", memo);
}
public Grid getWidget() {
// early termination if the table has already been constructed
return table;
}
- table = new Grid(8, 2);
+ table = new Grid(7, 2);
table.setText(0, 0, "Name");
table.setWidget(0, 1, name);
- table.setText(1, 0, "Company");
- table.setWidget(1, 1, company);
- table.setText(2, 0, "Phone number");
- table.setWidget(2, 1, tel);
- table.setText(3, 0, "Email");
- table.setWidget(3, 1, email);
- table.setText(4, 0, "Address");
- table.setWidget(4, 1, address);
- table.setText(5, 0, "Address 2");
- table.setWidget(5, 1, address2);
- table.setText(6, 0, "Website");
- table.setWidget(6, 1, url);
- table.setText(7, 0, "Memo");
- table.setWidget(7, 1, memo);
+ table.setText(1, 0, "Phone number");
+ table.setWidget(1, 1, tel);
+ table.setText(2, 0, "Email");
+ table.setWidget(2, 1, email);
+ table.setText(3, 0, "Address");
+ table.setWidget(3, 1, address);
+ table.setText(4, 0, "Address 2");
+ table.setWidget(4, 1, address2);
+ table.setText(5, 0, "Website");
+ table.setWidget(5, 1, url);
+ table.setText(6, 0, "Memo");
+ table.setWidget(6, 1, memo);
name.addStyleName(StylesDefs.INPUT_FIELD_REQUIRED);
return table;
public void validate(Widget widget) throws GeneratorException {
if (widget == name) getNameField();
- if (widget == company) getCompanyField();
+ //if (widget == company) getCompanyField();
if (widget == tel) getTelField();
if (widget == email) getEmailField();
if (widget == address) getAddressField();