001/*
002 * (C) Copyright 2006-2009 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.platform.annotations.gwt.client;
021
022import org.nuxeo.ecm.platform.annotations.gwt.client.configuration.WebConfiguration;
023import org.nuxeo.ecm.platform.annotations.gwt.client.configuration.WebConfigurationService;
024import org.nuxeo.ecm.platform.annotations.gwt.client.configuration.WebConfigurationServiceAsync;
025
026import com.allen_sauer.gwt.log.client.Log;
027import com.google.gwt.core.client.EntryPoint;
028import com.google.gwt.core.client.GWT;
029import com.google.gwt.user.client.Timer;
030import com.google.gwt.user.client.rpc.AsyncCallback;
031
032/**
033 * Entry point classes define <code>onModuleLoad()</code>.
034 */
035public class AnnotationFrameModule implements EntryPoint {
036
037    private WebConfigurationServiceAsync webConfigurationService;
038
039    private WebConfiguration webConfiguration;
040
041    /**
042     * This is the entry point method.
043     */
044    public void onModuleLoad() {
045        waitForAnnoteaServerUrlRegistered();
046    }
047
048    private void waitForAnnoteaServerUrlRegistered() {
049        Timer timer = new Timer() {
050            @Override
051            public void run() {
052                if (isAnnoteaServerUrlRegistered()) {
053                    loadModule();
054                } else {
055                    schedule(200);
056                }
057            }
058        };
059        timer.schedule(200);
060    }
061
062    private void loadModule() {
063        webConfigurationService = GWT.create(WebConfigurationService.class);
064        String url = getParentWindowUrl();
065        webConfigurationService.getWebConfiguration(url, new AsyncCallback<WebConfiguration>() {
066            public void onFailure(Throwable throwable) {
067                Log.debug("onFailure: " + throwable);
068                webConfiguration = WebConfiguration.DEFAULT_WEB_CONFIGURATION;
069                initModule();
070            }
071
072            public void onSuccess(WebConfiguration result) {
073                webConfiguration = result == null ? WebConfiguration.DEFAULT_WEB_CONFIGURATION : result;
074                initModule();
075                Log.debug("Module initialization finished.");
076            }
077        });
078    }
079
080    private native boolean isAnnoteaServerUrlRegistered() /*-{
081                                                          if (top['annoteaServerUrlRegistered']) {
082                                                          return top['annoteaServerUrlRegistered'];
083                                                          }
084                                                          return false;
085                                                          }-*/;
086
087    private native String getParentWindowUrl() /*-{
088                                               return $wnd.parent.location.href;
089                                               }-*/;
090
091    private void initModule() {
092        AnnotationFrameApplication.build(webConfiguration);
093    }
094
095}