eu.maxschuster.vaadin.jquery.Callback.java Source code

Java tutorial

Introduction

Here is the source code for eu.maxschuster.vaadin.jquery.Callback.java

Source

/*
 * Copyright 2013 Max Schuster 
 * 
 * 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 eu.maxschuster.vaadin.jquery;

import org.json.JSONArray;
import org.json.JSONException;

import com.vaadin.annotations.JavaScript;
import com.vaadin.server.AbstractJavaScriptExtension;
import com.vaadin.ui.JavaScriptFunction;

import eu.maxschuster.vaadin.jquery.shared.CallbackState;

@JavaScript("js/callback.js")
public abstract class Callback extends AbstractJavaScriptExtension implements JavaScriptFunction {

    private static final long serialVersionUID = 24803562414458626L;
    public static final String REGEXP = "/^-jQ-Callback\\[(\\d+)\\]$/";
    private JQueryObject host;

    public Callback(JQueryObject host) {
        this.host = host;
        extend(host);
        addFuncs();
    }

    private void addFuncs() {
        final Callback self = this;
        addFunction("callToServer", new JavaScriptFunction() {
            private static final long serialVersionUID = -6135324536871720370L;

            @Override
            public void call(JSONArray arguments) throws JSONException {
                self.setRunning(false);
                self.call(arguments);
            }
        });
        addFunction("setRunning", new JavaScriptFunction() {
            private static final long serialVersionUID = 5142085261390695076L;

            @Override
            public void call(JSONArray arguments) throws JSONException {
                self.setRunning(arguments.getBoolean(0));
            }
        });
    }

    abstract public void call(JSONArray arguments);

    public void cancel() {
        callFunction("cancel");
    }

    public String toString() {
        return "-jQ-Callback[" + getConnectorId() + "]";
    }

    public int getDelay() {
        return getState().getDelay();
    }

    public Callback setDelay(int delay) {
        getState().setDelay(delay);
        return this;
    }

    public JQueryObject getHost() {
        return host;
    }

    protected boolean isRunning() {
        return getState().isRunning();
    }

    protected void setRunning(boolean running) {
        getState().setRunning(running);
    }

    @Override
    protected CallbackState getState() {
        return (CallbackState) super.getState();
    }

}