Issue 507 remove company
[zxing.git] / zxing.appspot.com / generator / src / com / google / zxing / web / generator / client / ContactInfoGenerator.java
index fd4029b..4cde3a9 100644 (file)
@@ -16,6 +16,7 @@
 
 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;
@@ -29,17 +30,20 @@ import com.google.gwt.user.client.ui.Widget;
 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, 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);
     }
   }
   
@@ -49,31 +53,52 @@ public class ContactInfoGenerator implements GeneratorSource {
 
   public String getText() throws GeneratorException {
     String name = getNameField();
-    String company = getCompanyField();
+    //String company = getCompanyField();
     String tel = getTelField();
     String url = getUrlField();
     String email = getEmailField();
     String address = getAddressField();
+    String address2 = getAddress2Field();
     String memo = getMemoField();
     
     // 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, memo);
+    return getMeCard(name, tel, url, email, address, address2, memo);
   }
 
-  private String getMeCard(String name, String company, String tel, String url,
-      String email, String address, String memo) {
-    String output = "MECARD:";
-    output += "N:" + name + ";";
-    output += company.length() > 0 ? "ORG:" + company + ";" : "";
-    output += tel.length() > 0 ? "TEL:" + tel + ";" : "";
-    output += url.length() > 0 ? "URL:" + url + ";" : "";
-    output += email.length() > 0 ? "EMAIL:" + email + ";" : "";
-    output += address.length() > 0 ? "ADR:" + address + ";" : "";
-    output += memo.length() > 0 ? "NOTE:" + memo + ";" : "";
-    output += ";";    
-    return output;    
+  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); // Not standard; don't generate
+    maybeAppend(output, "TEL:", tel);
+    maybeAppend(output, "URL:", url);
+    maybeAppend(output, "EMAIL:", email);
+    if (address.length() > 0 || address2.length() > 0) {
+      output.append("ADR:");
+      if (address.length() > 0) {
+        output.append(address);
+      }
+      if (address2.length() > 0) {
+        if (address.length() > 0) {
+          output.append(' ');
+        }
+        output.append(address2);
+      }
+      output.append(';');
+    }
+    maybeAppend(output, "NOTE:", memo);
+    output.append(';');
+    return output.toString();
+  }
+
+  private static void maybeAppend(StringBuilder output, String prefix, String value) {
+    if (value.length() > 0) {
+      output.append(prefix).append(value).append(';');
+    }
   }
   
   /*// VCARD GENERATION. Keep this in case we want to go back to vcard format
@@ -93,35 +118,32 @@ public class ContactInfoGenerator implements GeneratorSource {
     return output;    
   }
   */
-  
-  private String getNameField() throws GeneratorException {
-    String inputName = name.getText();
-    if (inputName.length() < 1) {
-      throw new GeneratorException("Name must be at least 1 character.");
-    }
-    if (inputName.contains("\n")) {
-      throw new GeneratorException("Name should not contanains \\n characters.");
-    }
-    if (inputName.contains(";")) {
-      throw new GeneratorException("Name must not contains ; characters");
-    }
-    return inputName;
-  }
-  
-  private String getCompanyField() throws GeneratorException {
-    String input = company.getText();
+
+  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("Company should not contanains \\n characters.");
+      throw new GeneratorException(name + " field must not contain \\n characters.");
     }
     if (input.contains(";")) {
-      throw new GeneratorException("Company must not contains ; characters");
+      throw new GeneratorException(name + " field must not contains ; characters");
     }
-    // the input contains some informations. 
     return input;
   }
+  
+  private String getNameField() throws GeneratorException {
+    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", company);
+  //}
 
   private String getTelField() throws GeneratorException {
     String input = Validators.filterNumber(tel.getText());
@@ -137,12 +159,8 @@ public class ContactInfoGenerator implements GeneratorSource {
   
   private String getUrlField() throws GeneratorException {
     String input = url.getText();
-    if (input.length() < 1) {
-      return "";
-    }
-    Validators.validateUrl(input);
-    if (input.contains(";")) {
-      throw new GeneratorException("URL must not contains ; characters");
+    if (input != null && input.length() > 0) {
+      Validators.validateUrl(input);
     }
     return input;
   }
@@ -160,31 +178,15 @@ public class ContactInfoGenerator implements GeneratorSource {
   }
   
   private String getAddressField() throws GeneratorException {
-    String input = address.getText();
-    if (input.length() < 1) {
-      return "";
-    }
-    if (input.contains("\n")) {
-      throw new GeneratorException("Address must not contain \\n characters.");
-    }
-    if (input.contains(";")) {
-      throw new GeneratorException("Address must not contains ; characters");
-    }
-    return input;
+    return parseTextField("Address", address);
+  }
+
+  private String getAddress2Field() throws GeneratorException {
+    return parseTextField("Address 2", address2);
   }
   
   private String getMemoField() throws GeneratorException {
-    String input = memo.getText();
-    if (input.length() < 1) {
-      return "";
-    }
-    if (input.contains("\n")) {
-      throw new GeneratorException("Memo must not contain \\n characters.");
-    }
-    if (input.contains(";")) {
-      throw new GeneratorException("Memo must not contains ; characters");
-    }
-    return input;
+    return parseTextField("Memo", memo);
   }
   
   public Grid getWidget() {
@@ -196,14 +198,14 @@ public class ContactInfoGenerator implements GeneratorSource {
     
     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(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");
@@ -215,10 +217,11 @@ public class ContactInfoGenerator implements GeneratorSource {
 
   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();
+    if (widget == address2) getAddress2Field();
     if (widget == url) getUrlField();
     if (widget == memo) getMemoField();
   }