001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2012 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      public final void onModuleLoad() {
038        export(GWT.getModuleName(), this);
039        load();
040        onResourceLoad();
041      }
042    
043      private void load() {
044        Widget widget = doOnModuleLoad();
045        if (widget != null) {
046          getRootPanel().add(widget);
047        }
048      }
049    
050      protected Widget doOnModuleLoad() {
051        return null;
052      }
053    
054      public final void onResourceLoad() {
055        JavaScriptObject json = loadResource();
056        if (json != null) {
057          if (WSUtils.getINSTANCE() == null) {
058            WSUtils.setInstance(new GwtUtils()); // TODO dirty hack to initialize WSUtils
059          }
060          String jsonStr = (new JSONObject(json)).toString();
061          Resource resource = RESOURCE_UNMARSHALLER.toModel(jsonStr);
062    
063          RootPanel container = getRootPanel();
064          container.clear();
065    
066          Widget currentWidget = doOnResourceLoad(resource);
067          if (currentWidget != null) {
068            container.add(currentWidget);
069          }
070        }
071      }
072    
073      protected Widget doOnResourceLoad(Resource resource) {
074        return null;
075      }
076    
077      protected final RootPanel getRootPanel() {
078        RootPanel result = RootPanel.get("gwtpage-" + GWT.getModuleName());
079        if (result == null) {
080          result = RootPanel.get("gwtpage");
081        }
082        return result;
083      }
084    
085      private native JavaScriptObject loadResource()/*-{
086                                                    return $wnd.config['resource'];
087                                                    }-*/;
088    
089      private native void export(String gwtId, Page page)/*-{
090                                                         $wnd.modules[gwtId]=function() {page.@org.sonar.gwt.ui.Page::onResourceLoad()()};
091                                                         }-*/;
092    }