001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * Sonar is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019     */
020    package org.sonar.gwt.ui;
021    
022    import com.google.gwt.core.client.EntryPoint;
023    import com.google.gwt.core.client.GWT;
024    import com.google.gwt.core.client.JavaScriptObject;
025    import com.google.gwt.json.client.JSONObject;
026    import com.google.gwt.user.client.Window;
027    import com.google.gwt.user.client.ui.RootPanel;
028    import com.google.gwt.user.client.ui.Widget;
029    import org.sonar.wsclient.gwt.GwtUtils;
030    import org.sonar.wsclient.services.Resource;
031    import org.sonar.wsclient.services.WSUtils;
032    import org.sonar.wsclient.unmarshallers.ResourceUnmarshaller;
033    
034    public abstract class Page implements EntryPoint {
035    
036      private static final ResourceUnmarshaller RESOURCE_UNMARSHALLER = new ResourceUnmarshaller();
037    
038      public final void onModuleLoad() {
039        export(GWT.getModuleName(), this);
040        load();
041        onResourceLoad();
042      }
043    
044      private void load() {
045        Widget widget = doOnModuleLoad();
046        if (widget != null) {
047          getRootPanel().add(widget);
048        }
049      }
050    
051      protected Widget doOnModuleLoad() {
052        return null;
053      }
054    
055      public final void onResourceLoad() {
056        JavaScriptObject json = loadResource();
057        if (json != null) {
058          if (WSUtils.getINSTANCE() == null) {
059            WSUtils.setInstance(new GwtUtils()); // TODO dirty hack to initialize WSUtils
060          }
061          String jsonStr = (new JSONObject(json)).toString();
062          Resource resource = RESOURCE_UNMARSHALLER.toModel(jsonStr);
063    
064          RootPanel container = getRootPanel();
065          container.clear();
066    
067          Widget currentWidget = doOnResourceLoad(resource);
068          if (currentWidget != null) {
069            container.add(currentWidget);
070          }
071        }
072      }
073    
074      protected Widget doOnResourceLoad(Resource resource) {
075        return null;
076      }
077    
078      protected final RootPanel getRootPanel() {
079        RootPanel result = RootPanel.get("gwtpage-" + GWT.getModuleName());
080        if (result == null) {
081          result = RootPanel.get("gwtpage");
082        }
083        return result;
084      }
085    
086      private native JavaScriptObject loadResource()/*-{
087                                                    return $wnd.config['resource'];
088                                                    }-*/;
089    
090      private native void export(String gwtId, Page page)/*-{
091                                                         $wnd.modules[gwtId]=function() {page.@org.sonar.gwt.ui.Page::onResourceLoad()()};
092                                                         }-*/;
093    }