Package com.google.api.checkout.client

Source Code of com.google.api.checkout.client.CheckoutButtonGenerator

package com.google.api.checkout.client;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FocusListener;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.ListBox;

/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class CheckoutButtonGenerator implements EntryPoint {

  static String HEADER = "item_name_1*|item_description_1*|item_quantity_1|item_price_1*|item_currency_1|item_hidden_data_1|ship_method_name_1|ship_method_price_1|ship_method_currency_1|ship_method_us_area_1|tax_us_state|tax_rate";
  static String SAMPLE = "Turkey|Nice Turkey for thanksgiving|1|10.50|USD||FedEx|10|USD|CONTINENTAL_48|CA|0.0875";
  static String[] FIELDS =  {"item_name_1",
                             "item_description_1",
                             "item_quantity_1",
                             "item_price_1",
                             "item_currency_1",
                             "item_hidden_data_1",
                             "ship_method_name_1",
                             "ship_method_price_1",
                             "ship_method_currency_1",
                             "ship_method_us_area_1",
                             "tax_us_state",
                             "tax_rate"};
  static String[] MANDATORY = {"item_name_1",
                     "item_description_1",
                   "item_price_1"};
  static List MANDATORY_FIELDS = Arrays.asList(MANDATORY);
 
  /**
   * This is the entry point method.
   */
  public void onModuleLoad() {
    final Button button = new Button("Generate Buttons");
    final Label label = new Label();
    // Let's make an 80x50 text area to go along with the other two.
    final TextArea catalogField = new TextArea();
    final VerticalPanel buttons = new VerticalPanel();
    final TextBox merchantId = new TextBox();
    final TextBox server = new TextBox();
    server.setText("checkout.google.com");
//    final VerticalPanel defaults = new VerticalPanel();
/*   
    ListBox item_currency_1 = new ListBox();
    item_currency_1.addItem("USD");
    TextBox item_hidden_data_1 = new TextBox();
    TextBox ship_method_name_1 = new TextBox();
    TextBox ship_method_price_1 = new TextBox();
    ListBox ship_method_currency_1 = new ListBox();
    ship_method_currency_1.addItem("USD");
    TextBox ship_method_us_area_1 = new TextBox();
    TextBox tax_us_state = new TextBox();
    TextBox tax_rate = new TextBox();
    defaults.add(item_currency_1);
    defaults.add(item_hidden_data_1);
    defaults.add(ship_method_name_1);
    defaults.add(ship_method_price_1);
    defaults.add(ship_method_currency_1);
    defaults.add(ship_method_us_area_1);
    defaults.add(tax_us_state);
    defaults.add(tax_rate);   
*/
   
    merchantId.setMaxLength(15);
    merchantId.setText("1234567890");
    catalogField.setCharacterWidth(80);
    //TODO set to 50
//    catalogField.setVisibleLines(50);
    catalogField.setVisibleLines(30);
    //catalogField.setText(HEADER);
    catalogField.setText(HEADER + "\n" + SAMPLE + "\n" + SAMPLE + "\n");
    button.addClickListener(new ClickListener() {
      public void onClick(Widget sender) {
      try {
        List items = parseCatalog(catalogField.getText());
          if (items != null) {
            label.setText(items.size() + " rows parsed successfully");
            //buttons.clear();
            generateButtons(items, buttons);
          } else {
            label.setText("The catalog field is empty: you need to copy your catalog data in the catalog field");
         
      } catch (Exception e) {
            label.setText("One line in the catalog is invalid:" + e.getMessage());
      }
      }
    });

    // Assume that the host HTML has elements defined whose
    // IDs are "slot1", "slot2".  In a real app, you probably would not want
    // to hard-code IDs.  Instead, you could, for example, search for all
    // elements with a particular CSS class and replace them with widgets.
    //
    //RootPanel.get("defaults").add(defaults);
    RootPanel.get("catalog").add(catalogField);
    RootPanel.get("status").add(label);
    RootPanel.get("merchantId").add(merchantId);
    RootPanel.get("server").add(server);
    RootPanel.get("createButton").add(button);
    RootPanel.get("buttons").add(buttons);
  }
  public static List parseCatalog(String text) throws Exception {
    ArrayList catalog = new ArrayList();
    if ((text == null)||(text == "")) return null;
    String[] lines = text.split("\n");
    if (lines.length == 1) return null;
    for (int i=1; i < lines.length; i++) {   
      String[] fields = lines[i].split("\\|");
      HashMap cart = new HashMap();
      for (int j=0; j < fields.length; j++) {
        checkField(i, j, fields[j]);
        if ("" != fields[j] ) {
          cart.put(FIELDS[j], fields[j]);
        }
      }
      catalog.add(cart);
    }
    return catalog;
  }
 
  public static void checkField(int row, int col, String value) throws Exception {
    if (("" == value) && (MANDATORY_FIELDS.contains(FIELDS[col]))) {
      throw new Exception("mandatory field row " + row + " " + FIELDS[col] + " not present");
    }
  }
 
  public void generateButtons(List catalog, VerticalPanel buttons) throws Exception {
    //if you call catalog.iterator() directly there's a bug and it goes into an infinite loop
    //clean existing buttons
    buttons.clear();
    Iterator it = catalog.iterator();
    while (it.hasNext()) {
      HashMap cart = (HashMap)it.next();
      buttons.add(generateButton(cart));
    }
  }
 
  public String getField(String fieldName) throws Exception {
    try {
    return ((TextBox)(RootPanel.get(fieldName).iterator().next())).getText();
    } catch (Exception e) {
      throw new Exception("field " + fieldName + " is incorrect:" + e.getMessage());
    }
  }
 
  public Widget generateButton(HashMap cart) throws Exception {
   
    StringBuffer form = new StringBuffer();
    form.append("<form method=\"POST\"  action=\"https://");
    form.append(getField("server"));
    form.append("/cws/v2/Merchant/");
    form.append(getField("merchantId"));
    form.append("/checkoutForm\" accept-charset=\"utf-8\">\n");
    Iterator it = cart.keySet().iterator();
    while (it.hasNext()) {
       String key = (String)it.next();
       String value = (String)cart.get(key);
       form.append("<input type=\"hidden\" name=\"");
       form.append(key);
       form.append("\" value=\"");
       form.append(value);
       form.append("\"/>\n");
    }
    form.append("<input type=\"hidden\" name=\"_charset_\"/>\n");
    form.append("<input type=\"image\" name=\"Google Checkout\" alt=\"Fast checkout through Google\" ");
    form.append("src=\"http://");
    form.append(getField("server"));
    form.append("/buttons/checkout.gif?merchant_id=");
    form.append(getField("merchantId"));
    form.append("&w=180&h=46&style=white&variant=text&loc=en_US\" height=\"46\" width=\"180\"/>\n</form>")

    HorizontalPanel panel = new HorizontalPanel();
    HTML html = new HTML(form.toString());
    panel.add(html);
    TextArea formText = new TextArea();
    formText.setCharacterWidth(60);
    formText.setVisibleLines(20);
    formText.setText(form.toString());
    formText.addFocusListener(new FocusListener() {
        public void onFocus(Widget sender) {
          try {
            ((TextArea)sender).selectAll();
          } catch (Exception e) {
                //debug("Problem: :" + e.getMessage());
          }
        }
        public void onLostFocus(Widget sender) {
        }
      });
    formText.addClickListener(new ClickListener() {
        public void onClick(Widget sender) {
          try {
            ((TextArea)sender).selectAll();
          } catch (Exception e) {
                //debug("Problem: :" + e.getMessage());
          }
        }
      });
    panel.add(formText);
    return panel;
  }
}
TOP

Related Classes of com.google.api.checkout.client.CheckoutButtonGenerator

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.