Java tutorial
/* * 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 java.lang.reflect.Method; import java.util.ArrayList; import java.util.EventObject; import java.util.HashMap; import org.json.JSONArray; import org.json.JSONException; import com.vaadin.annotations.JavaScript; import com.vaadin.server.AbstractClientConnector; import com.vaadin.server.AbstractJavaScriptExtension; import com.vaadin.server.ClientConnector; import com.vaadin.ui.JavaScriptFunction; import com.vaadin.util.ReflectTools; import eu.maxschuster.vaadin.jquery.shared.JQueryObjectState; @JavaScript({ "js/jquery-1.9.1.min.js", "js/jquery-object.js" }) public class JQueryObject extends AbstractJavaScriptExtension { private static final long serialVersionUID = -6859468566863980128L; private AbstractClientConnector target; private int length = 0; private HashMap<String, Callback> callbacks = new HashMap<String, Callback>(); public JQueryObject(AbstractClientConnector target) { this.target = target; this.getState().setId(hashCode()); addFuncs(); extend(this.target); setSelector(target); } private void addFuncs() { final JQueryObject self = this; addFunction("targetRefreshed", new JavaScriptFunction() { private static final long serialVersionUID = 8978871967352479460L; @Override public void call(JSONArray arguments) throws JSONException { int length = arguments.getInt(0); self.setLength(length); self.fireEvent(new TargetRefreshedEvent(self)); } }); addFunction("callCallback", new JavaScriptFunction() { private static final long serialVersionUID = -197344735063198908L; @Override public void call(JSONArray arguments) throws JSONException { int callbackId = arguments.getInt(0); Callback callback = self.callbacks.get(callbackId); if (callback != null) { callback.call(arguments); } } }); } @Override protected JQueryObjectState getState() { return (JQueryObjectState) super.getState(); } @Override protected JQueryObjectState getState(boolean markAsDirty) { return (JQueryObjectState) super.getState(markAsDirty); } public JQueryObject setSelector(String selector) { this.getState().setSelector(selector); this.getState().setConnector(false); return this; } public JQueryObject setSelector(ClientConnector connector) { this.getState().setSelector(connector.getConnectorId()); this.getState().setConnector(true); return this; } public JQueryObject setParentObject(JQueryObject parent) { this.getState().setParentId(parent.getId()); return this; } public int getId() { return this.getState().getId(); } public JQueryObject find(String selector) { JQueryObject child = new JQueryObject(target); child.setParentObject(this); child.setSelector(selector); return child; } public JQueryObject fn(String name, Object... args) { for (Object arg : args) { if (arg instanceof Callback) { registerCallback((Callback) arg); } else if (arg instanceof PlainObject) { ArrayList<Callback> callbacks = ((PlainObject) arg).getCallbacks(); for (Callback callback : callbacks) { registerCallback(callback); } } } callFunction("fn", name, args); return this; } public JQueryObject css(String propertyName, String value) { return fn("css", propertyName, value); } public JQueryObject css(PlainObject properties) { return fn("css", properties); } public JQueryObject animate(PlainObject properties, Integer duration, String easing, Callback complete) { return fn("animate", properties, duration, easing, complete); } public JQueryObject animate(PlainObject properties, PlainObject options) { return fn("animate", properties, options); } public JQueryObject on(String events, String selector, PlainObject data, Callback handler) { return fn("on", events, selector, data, handler); } public JQueryObject off(String events, String selector, Callback handler) { return fn("off", events, selector, handler); } public void registerCallback(Callback callback) { String id = callback.getConnectorId(); if (callbacks.get(id) == null) { this.callbacks.put(id, callback); callFunction("registerCallback", id); } } public void removeCallback(Callback callback) { String id = callback.getConnectorId(); this.callbacks.remove(id); callFunction("removeCallback", id); } public int getLength() { return length; } private void setLength(int length) { this.length = length; } // TODO do some attach/detach actions to prevent memory leaks on client side... public void addListener(TargetRefreshedListener listener) { addListener(TargetRefreshedEvent.EVENT_ID, TargetRefreshedEvent.class, listener, TargetRefreshedListener.targetRefreshedMethod); } public void removeListener(TargetRefreshedListener listener) { removeListener(TargetRefreshedEvent.EVENT_ID, TargetRefreshedEvent.class, listener); } public interface TargetRefreshedListener { public final static Method targetRefreshedMethod = ReflectTools.findMethod(TargetRefreshedListener.class, "ontargetRefreshed", TargetRefreshedEvent.class); public void onTargetRefreshed(TargetRefreshedEvent event); } public class TargetRefreshedEvent extends EventObject { private static final long serialVersionUID = 4965371442816196545L; public static final String EVENT_ID = "jqueryr-target-refreshed"; public TargetRefreshedEvent(JQueryObject jQueryObject) { super(jQueryObject); } public JQueryObject getJQueryObject() { return (JQueryObject) getSource(); } } }