}
public String getThemeUri() {
- return vaadinDirUrl + "themes/" + getThemeName();
+ return getVaadinDirUrl() + "themes/" + getThemeName();
+ }
+
+ /**
+ * Gets the URL of the VAADIN directory on the server.
+ *
+ * @return the URL of the VAADIN directory
+ */
+ public String getVaadinDirUrl() {
+ return vaadinDirUrl;
}
public void setAppId(String appId) {
*/
public void setPushEnabled(boolean enabled) {
if (enabled && push == null) {
- /*
- * TODO support for loading atmosphere.js on demand will be added in
- * another commit.
- */
- push = GWT.create(PushConnection.class);
- push.init(this);
- final String pushUri = addGetParameters(
- translateVaadinUri(ApplicationConstants.APP_PROTOCOL_PREFIX
- + ApplicationConstants.PUSH_PATH + '/'),
- UIConstants.UI_ID_PARAMETER + "="
- + getConfiguration().getUIId());
+ final PushConnection push = GWT.create(PushConnection.class);
+ push.init(this);
- Scheduler.get().scheduleDeferred(new Command() {
+ push.runWhenAtmosphereLoaded(new Command() {
@Override
public void execute() {
- push.connect(pushUri);
+ ApplicationConnection.this.push = push;
+
+ final String pushUri = addGetParameters(
+ translateVaadinUri(ApplicationConstants.APP_PROTOCOL_PREFIX
+ + ApplicationConstants.PUSH_PATH + '/'),
+ UIConstants.UI_ID_PARAMETER + "="
+ + getConfiguration().getUIId());
+
+ Scheduler.get().scheduleDeferred(new Command() {
+ @Override
+ public void execute() {
+ push.connect(pushUri);
+ }
+ });
}
});
} else if (!enabled && push != null) {
import java.util.ArrayList;
import com.google.gwt.core.client.JavaScriptObject;
+import com.google.gwt.user.client.Command;
import com.vaadin.client.ApplicationConnection;
+import com.vaadin.client.ResourceLoader;
+import com.vaadin.client.ResourceLoader.ResourceLoadEvent;
+import com.vaadin.client.ResourceLoader.ResourceLoadListener;
import com.vaadin.client.VConsole;
+import com.vaadin.shared.ApplicationConstants;
/**
* Represents the client-side endpoint of a bidirectional ("push") communication
/*-{
$wnd.jQueryVaadin.atmosphere.unsubscribeUrl(url);
}-*/;
+
+ private static native boolean isAtmosphereLoaded()
+ /*-{
+ return $wnd.jQueryVaadin != undefined;
+ }-*/;
+
+ /**
+ * Runs the provided command when the Atmosphere javascript has been loaded.
+ * If the script has already been loaded, the command is run immediately.
+ *
+ * @param command
+ * the command to run when Atmosphere has been loaded.
+ */
+ public void runWhenAtmosphereLoaded(final Command command) {
+ assert command != null;
+
+ if (isAtmosphereLoaded()) {
+ command.execute();
+ } else {
+ VConsole.log("Loading " + ApplicationConstants.VAADIN_PUSH_JS);
+ ResourceLoader.get().loadScript(
+ connection.getConfiguration().getVaadinDirUrl()
+ + ApplicationConstants.VAADIN_PUSH_JS,
+ new ResourceLoadListener() {
+ @Override
+ public void onLoad(ResourceLoadEvent event) {
+ VConsole.log(ApplicationConstants.VAADIN_PUSH_JS
+ + " loaded");
+ command.execute();
+ }
+
+ @Override
+ public void onError(ResourceLoadEvent event) {
+ VConsole.log(event.getResourceUrl()
+ + " could not be loaded. Push will not work.");
+ }
+ });
+ }
+ }
}
// Load client-side dependencies for push support
fragmentNodes.add(new Element(Tag.valueOf("script"), "").attr(
"type", "text/javascript").attr("src",
- vaadinLocation + "vaadinPush.js"));
+ vaadinLocation + ApplicationConstants.VAADIN_PUSH_JS));
}
String bootstrapLocation = vaadinLocation + "vaadinBootstrap.js";
* </p>
*/
public static final String VAADIN_DIR_URL = "vaadinDir";
+
+ /**
+ * The name of the javascript containing push support. The file is located
+ * in the VAADIN directory.
+ */
+ public static final String VAADIN_PUSH_JS = "vaadinPush.js";
}