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.ui.RootPanel;
027    import com.google.gwt.user.client.ui.Widget;
028    import org.sonar.wsclient.gwt.GwtUtils;
029    import org.sonar.wsclient.services.Resource;
030    import org.sonar.wsclient.services.WSUtils;
031    import org.sonar.wsclient.unmarshallers.ResourceUnmarshaller;
032    
033    public abstract class Page implements EntryPoint {
034    
035      private static final ResourceUnmarshaller RESOURCE_UNMARSHALLER = new ResourceUnmarshaller();
036    
037      private Widget currentWidget = null;
038      private Integer currentResourceId = null;
039    
040      public final void onModuleLoad() {
041        export(GWT.getModuleName(), this);
042        load();
043        onResourceLoad();
044      }
045    
046      private void load() {
047        Widget widget = doOnModuleLoad();
048        if (widget != null) {
049          getRootPanel().add(widget);
050        }
051      }
052    
053      protected Widget doOnModuleLoad() {
054        return null;
055      }
056    
057      public final void onResourceLoad() {
058        JavaScriptObject json = loadResource();
059        if (json != null) {
060          if (WSUtils.getINSTANCE() == null) {
061            WSUtils.setInstance(new GwtUtils()); // TODO dirty hack to initialize WSUtils
062          }
063          String jsonStr = (new JSONObject(json)).toString();
064          Resource resource = RESOURCE_UNMARSHALLER.toModel(jsonStr);
065    
066          RootPanel container = getRootPanel();
067          container.clear();
068          if (resource.getId().equals(currentResourceId) && currentWidget != null) {
069            container.add(currentWidget);
070    
071          } else {
072            currentWidget = doOnResourceLoad(resource);
073            currentResourceId = resource.getId();
074            if (currentWidget != null) {
075              container.add(currentWidget);
076            }
077          }
078        }
079      }
080    
081      protected Widget doOnResourceLoad(Resource resource) {
082        return null;
083      }
084    
085      protected final RootPanel getRootPanel() {
086        RootPanel result = RootPanel.get("gwtpage-" + GWT.getModuleName());
087        if (result == null) {
088          result = RootPanel.get("gwtpage");
089        }
090        return result;
091      }
092    
093      private native JavaScriptObject loadResource()/*-{
094                                                    return $wnd.config['resource'];
095                                                    }-*/;
096    
097      private native void export(String gwtId, Page page)/*-{
098                                                         $wnd.modules[gwtId]=function() {page.@org.sonar.gwt.ui.Page::onResourceLoad()()};
099                                                         }-*/;
100    }