Package org.fusesource.restygwt.examples.client

Source Code of org.fusesource.restygwt.examples.client.UI

/**
* Copyright (C) 2009-2012 the original author or authors.
* See the notice.md file distributed with this work for additional
* information regarding copyright ownership.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.fusesource.restygwt.examples.client;

import org.fusesource.restygwt.client.JsonCallback;
import org.fusesource.restygwt.client.Method;
import org.fusesource.restygwt.client.OverlayCallback;
import org.fusesource.restygwt.client.Resource;
import org.fusesource.restygwt.client.RestServiceProxy;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;

/**
* Entry point classes define <code>onModuleLoad()</code>.
*
* @author <a href="http://hiramchirino.com">Hiram Chirino</a>
*/
public class UI implements EntryPoint {

    /**
     * This is the entry point method.
     */
    public void onModuleLoad() {
        Button button = new Button("Get Greeting");
        button.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                getGreeting();
            }
        });
        RootPanel.get().add(button);
        RootPanel.get().add(new Label("Name:"));
        final TextBox nameInput = new TextBox();
        RootPanel.get().add(nameInput);
        Button customButton = new Button("Get Custom Greeting");
        customButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                getCustomGreeting(nameInput.getValue());
            }
        });
        RootPanel.get().add(customButton);
    }

    private void getGreeting() {
        GreetingService service = GWT.create(GreetingService.class);
        Resource resource = new Resource(GWT.getModuleBaseURL() + "greeting-service");
        ((RestServiceProxy) service).setResource(resource);

        service.getGreeting(new OverlayCallback<Greeting>() {
            public void onSuccess(Method method, Greeting greeting) {
                RootPanel.get().add(new Label("server said: " + greeting.getGreeting()));
            }

            public void onFailure(Method method, Throwable exception) {
                Window.alert("Error: " + exception);
            }
        });
    }

    private void getCustomGreeting(String name) {
        GreetingService service = GWT.create(GreetingService.class);
        Resource resource = new Resource(GWT.getModuleBaseURL() + "greeting-service");
        ((RestServiceProxy) service).setResource(resource);
        NameObject arg = (NameObject) JavaScriptObject.createObject();
        arg.setName(name);

        service.getCustomGreeting(arg, new OverlayCallback<Greeting>() {
            public void onSuccess(Method method, Greeting greeting) {
                RootPanel.get().add(new Label("server said: " + greeting.getGreeting()));
            }

            public void onFailure(Method method, Throwable exception) {
                Window.alert("Error: " + exception);
            }
        });
    }
}
TOP

Related Classes of org.fusesource.restygwt.examples.client.UI

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.