001/*
002 * (C) Copyright 2006-2009 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.platform.annotations.gwt.client;
023
024import org.nuxeo.ecm.platform.annotations.gwt.client.configuration.WebConfiguration;
025import org.nuxeo.ecm.platform.annotations.gwt.client.configuration.WebConfigurationService;
026import org.nuxeo.ecm.platform.annotations.gwt.client.configuration.WebConfigurationServiceAsync;
027
028import com.allen_sauer.gwt.log.client.Log;
029import com.google.gwt.core.client.EntryPoint;
030import com.google.gwt.core.client.GWT;
031import com.google.gwt.user.client.Timer;
032import com.google.gwt.user.client.rpc.AsyncCallback;
033
034/**
035 * Entry point classes define <code>onModuleLoad()</code>.
036 */
037public class AnnotationFrameModule implements EntryPoint {
038
039    private WebConfigurationServiceAsync webConfigurationService;
040
041    private WebConfiguration webConfiguration;
042
043    /**
044     * This is the entry point method.
045     */
046    public void onModuleLoad() {
047        waitForAnnoteaServerUrlRegistered();
048    }
049
050    private void waitForAnnoteaServerUrlRegistered() {
051        Timer timer = new Timer() {
052            @Override
053            public void run() {
054                if (isAnnoteaServerUrlRegistered()) {
055                    loadModule();
056                } else {
057                    schedule(200);
058                }
059            }
060        };
061        timer.schedule(200);
062    }
063
064    private void loadModule() {
065        webConfigurationService = GWT.create(WebConfigurationService.class);
066        String url = getParentWindowUrl();
067        webConfigurationService.getWebConfiguration(url, new AsyncCallback<WebConfiguration>() {
068            public void onFailure(Throwable throwable) {
069                Log.debug("onFailure: " + throwable);
070                webConfiguration = WebConfiguration.DEFAULT_WEB_CONFIGURATION;
071                initModule();
072            }
073
074            public void onSuccess(WebConfiguration result) {
075                webConfiguration = result == null ? WebConfiguration.DEFAULT_WEB_CONFIGURATION : result;
076                initModule();
077                Log.debug("Module initialization finished.");
078            }
079        });
080    }
081
082    private native boolean isAnnoteaServerUrlRegistered() /*-{
083                                                          if (top['annoteaServerUrlRegistered']) {
084                                                          return top['annoteaServerUrlRegistered'];
085                                                          }
086                                                          return false;
087                                                          }-*/;
088
089    private native String getParentWindowUrl() /*-{
090                                               return $wnd.parent.location.href;
091                                               }-*/;
092
093    private void initModule() {
094        AnnotationFrameApplication.build(webConfiguration);
095    }
096
097}