// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
package dk.vajhoej.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.HTTPRequest;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.user.client.ResponseTextHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Widget;

public class Demo implements EntryPoint, ClickListener, ChangeListener {
    private Button btn1;
    private Button btn2;
    private Label lbl;
    private TextArea ta;
    private TAHandler tah;
    private ListBox lb1;
    private LBHandler lbh1;
    private ListBox lb2;
    private LBHandler lbh2;
    public void onModuleLoad() {
         btn1 = new Button("Load text #1");
         btn1.addClickListener(this);
         RootPanel.get("btn1").add(btn1);
         btn2 = new Button("Load text #2");
         btn2.addClickListener(this);
         RootPanel.get("btn2").add(btn2);
         lbl = new Label();
         RootPanel.get("lbl").add(lbl);
         ta = new TextArea();
         tah = new TAHandler(ta);
         RootPanel.get("ta").add(ta);
         lb1 = new ListBox(false);
         lbh1 = new LBHandler(lb1, "mfg");
         lb1.addChangeListener(this);
         lb1.setEnabled(false);
         HTTPRequest.asyncGet("http://localhost/mfg.php", lbh1);
         RootPanel.get("lb1").add(lb1);
         lb2 = new ListBox(false);
         lbh2 = new LBHandler(lb2, "mod");
         lb2.addChangeListener(this);
         lb2.setEnabled(false);
         RootPanel.get("lb2").add(lb2);
    }
    public void onClick(Widget sender) {
        if(sender == btn1) {
            lbl.setText("Text #1:");
            HTTPRequest.asyncGet("text1.txt", tah);
        } else if(sender == btn2) {
            lbl.setText("Text #2:");
            HTTPRequest.asyncGet("text2.txt", tah);
        }
    }
    public void onChange(Widget sender) {
        if(sender == lb1) {
            lb2.clear();
            HTTPRequest.asyncGet("http://localhost/mod.php?mfg=" + lb1.getItemText(lb1.getSelectedIndex()), lbh2);
        } else if(sender == lb2) {
            Window.alert("Value=" + lb2.getItemText(lb2.getSelectedIndex()));
        }
    }
}

class TAHandler implements ResponseTextHandler {
    private TextArea ta;
    public TAHandler(TextArea ta) {
        this.ta = ta;
    }
    public void onCompletion(String txt) {
        ta.setText(txt);
    }
}

class LBHandler implements ResponseTextHandler {
    private ListBox lb;
    private String key;
    public LBHandler(ListBox lb, String key) {
        this.lb = lb;
        this.key = key;
    }
    public void onCompletion(String txt) {
        JSONArray a = (JSONArray)JSONParser.parse(txt);
        for(int i = 0; i < a.size(); i++) {
            JSONObject o = (JSONObject)a.get(i);
            JSONString s = (JSONString)o.get(key);
            lb.addItem(s.stringValue());
        }
        lb.setEnabled(true);
    }
}
