All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.mapway.document.ui.client.test.TestPanel Maven / Gradle / Ivy

There is a newer version: 2.3.0
Show newest version
package cn.mapway.document.ui.client.test;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsonUtils;
import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.event.logical.shared.HasCloseHandlers;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.*;
import cn.mapway.document.ui.client.component.Clients;
import cn.mapway.document.ui.client.main.JsonPanel;
import cn.mapway.document.ui.client.main.storage.LocalStorage;
import cn.mapway.document.ui.client.module.Entry;
import cn.mapway.document.ui.client.resource.SysResource;
import cn.mapway.document.ui.client.rpc.ApiDocProxy;
import cn.mapway.document.ui.client.rpc.IOnData;
import cn.mapway.document.ui.client.rpc.RpcContext;

import java.util.Date;

// TODO: Auto-generated Javadoc

/**
 * 测试面板.
 *
 * @author zhangjianshe
 */
public class TestPanel extends Composite implements HasCloseHandlers {

    /**
     * The constant GWT_USER_HEADER.
     */
    public static final String GWT_USER_HEADER = "gwt_user_token";

    /**
     * The ui binder.
     */
    private static TestPanelUiBinder uiBinder = GWT.create(TestPanelUiBinder.class);

    /**
     * The Interface TestPanelUiBinder.
     */
    interface TestPanelUiBinder extends UiBinder {
    }

    /**
     * Instantiates a new test panel.
     */
    public TestPanel() {
        initWidget(uiBinder.createAndBindUi(this));
        btnExecute.setStyleName(SysResource.INSTANCE.getCss().btn());
        btnClose.setStyleName(SysResource.INSTANCE.getCss().btn());
        imgLoadding.setUrl(SysResource.INSTANCE.loading().getSafeUri());
        btnHistory.setUrl(SysResource.INSTANCE.history().getSafeUri());
        txtHeader.addBlurHandler(new BlurHandler() {

            @Override
            public void onBlur(BlurEvent event) {
                saveHeader();
            }
        });
        txtHeaderValue.addBlurHandler(new BlurHandler() {

            @Override
            public void onBlur(BlurEvent event) {
                saveHeader();
            }
        });

    }

    /**
     * Save header.
     */
    protected void saveHeader() {
        String key = txtHeader.getValue();
        String value = txtHeaderValue.getValue();
        if (key != null && key.length() > 0 && value != null && value.length() > 0) {
            RpcContext context = RpcContext.get();
            context.KEY = key;
            context.VALUE = value;
            LocalStorage.save(GWT_USER_HEADER, context.KEY);
            LocalStorage.save(context.KEY, value);
        }
    }

    /**
     * 恢复请求头的数据
     */
    private void loadHeader() {
        String key = LocalStorage.val(GWT_USER_HEADER);
        if (key != null && key.length() > 0) {
            txtHeader.setValue(key);
            txtHeaderValue.setValue(LocalStorage.val(key));
        }
    }

    /**
     * The lb title.
     */
    @UiField
    Label lbTitle;

    /**
     * The lb url.
     */
    @UiField
    Label lbUrl;

    /**
     * The txt input.
     */
    @UiField
    TextArea txtInput;

    /**
     * The txt output.
     */
    @UiField
    JsonPanel txtOutput;

    /**
     * The m entry.
     */
    Entry mEntry;

    /**
     * Invoke.
     *
     * @param entry the entry
     */
    public void invoke(Entry entry) {
        loadHeader();
        mEntry = entry;
        lbTitle.setText(entry.title());
        lbUrl.setText(Clients.getHostPort() + entry.url());
        String his = readHistory();
        if (his.length() == 0) {
            his = entry.input().get(0).json();
        }
        txtInput.setValue(his);
        txtOutput.setString("");
    }

    /**
     * Read history.
     *
     * @return the string
     */
    private String readHistory() {
        String r = "";
        String v = LocalStorage.val(mEntry.relativePath());
        if (v == null || v.length() == 0) {
            return "";
        }

        String[] vs = v.split("\\|");

        if (vs.length > 0) {
            String[] itemdata = vs[0].split("`");
            r = itemdata[1];
            return r;
        }

        return r;
    }

    /**
     * The btn execute.
     */
    @UiField
    Button btnExecute;

    /**
     * The btn close.
     */
    @UiField
    Button btnClose;

    /**
     * The img loadding.
     */
    @UiField
    Image imgLoadding;

    /**
     * The btn history.
     */
    @UiField
    Image btnHistory;

    /**
     * On execute.
     *
     * @param ev the ev
     */
    @UiHandler("btnExecute")
    void onExecute(ClickEvent ev) {

        ///保存请求头数据
        //saveHeader();

        imgLoadding.setVisible(true);
        txtOutput.setText("");
        // 保存到本地
        String v = LocalStorage.val(mEntry.relativePath());
        Date d = new Date();
        String key =
                (d.getYear() + 1900) + "-" + (d.getMonth() + 1) + "-" + d.getDate() + " " + d.getHours()
                        + ":" + d.getMinutes() + ":" + d.getSeconds();
        String va = txtInput.getValue();

        if (v == null || v.length() == 0) {
            LocalStorage.save(mEntry.relativePath(), key + "`" + va);
        } else {
            LocalStorage.save(mEntry.relativePath(), key + "`" + va + "|" + v);
        }
        try {
            ApiDocProxy.fetchString(mEntry.url(), txtInput.getValue(), "", mEntry.invokeMethods().get(0),
                    new IOnData() {
                        @Override
                        public void onError(String url, String error) {
                            txtOutput.setString(error);
                            imgLoadding.setVisible(false);

                        }

                        @Override
                        public void onSuccess(String url, String data) {
                            JavaScriptObject jso = JsonUtils.unsafeEval(data);
                            txtOutput.setJson(JsonUtils.stringify(jso, "   "));
                            imgLoadding.setVisible(false);
                            processToken(url, data);
                        }

                        ;
                    });
        } catch (RequestException e) {
            txtOutput.setString(e.getMessage());
            imgLoadding.setVisible(false);
        }
        // }
    }

    /**
     * 处理TOKEN.
     *
     * @param url  the url
     * @param data the data
     */
    protected void processToken(String url, String data) {

    }

    /**
     * The txt custom token.
     */
    @UiField
    TextBox txtHeader;

    /**
     * The Txt header value.
     */
    @UiField
    TextBox txtHeaderValue;

    /**
     * On close.
     *
     * @param ev the ev
     */
    @UiHandler("btnClose")
    void onClose(ClickEvent ev) {
        CloseEvent.fire(this, null);
    }

    /*
     * (non-Javadoc)
     *
     * @see com.google.gwt.event.logical.shared.HasCloseHandlers#addCloseHandler(
     * com.google.gwt.event.logical.shared.CloseHandler)
     */
    @Override
    public HandlerRegistration addCloseHandler(CloseHandler handler) {
        return addHandler(handler, CloseEvent.getType());
    }

    /**
     * The pop.
     */
    PopupPanel pop = null;

    /**
     * The history panel.
     */
    InputHistoryPanel historyPanel;

    /**
     * The item selected.
     */
    private CloseHandler itemSelected = new CloseHandler() {

        @Override
        public void onClose(CloseEvent event) {
            txtInput.setValue(event.getTarget().value);
            pop.hide();
        }
    };

    /**
     * 显示历史记录.
     *
     * @param e the e
     */
    @UiHandler("btnHistory")
    void onHistory(ClickEvent e) {
        if (pop == null) {
            pop = new PopupPanel(true, true);
            historyPanel = new InputHistoryPanel();
            historyPanel.addCloseHandler(itemSelected);
            pop.add(historyPanel);
        }
        pop.showRelativeTo(btnHistory);
        historyPanel.render(mEntry.relativePath());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy