001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2009 SonarSource SA
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.user.client.ui.RootPanel;
026    import com.google.gwt.user.client.ui.Widget;
027    import org.sonar.wsclient.gwt.unmarshallers.ResourceUnmarshaller;
028    import org.sonar.wsclient.services.Resource;
029    
030    public abstract class Page implements EntryPoint {
031    
032      private Widget currentWidget = null;
033      private Integer currentResourceId = null;
034    
035      public final void onModuleLoad() {
036        export(GWT.getModuleName(), this);
037        load();
038        onResourceLoad();
039      }
040    
041      private void load() {
042        Widget widget = doOnModuleLoad();
043        if (widget!=null) {
044          getRootPanel().add(widget);
045        }
046      }
047    
048      protected Widget doOnModuleLoad() {
049        return null;
050      }
051    
052      public final void onResourceLoad() {
053        JavaScriptObject json = loadResource();
054        if (json != null) {
055          Resource resource = ResourceUnmarshaller.getInstance().toModel(json);
056    
057          RootPanel container = getRootPanel();
058          container.clear();
059          if (resource.getId().equals(currentResourceId) && currentWidget != null) {
060            container.add(currentWidget);
061    
062          } else {
063            currentWidget = doOnResourceLoad(resource);
064            currentResourceId = resource.getId();
065            if (currentWidget != null) {
066              container.add(currentWidget);
067            }
068          }
069        }
070      }
071    
072      protected Widget doOnResourceLoad(Resource resource) {
073        return null;
074      }
075    
076      protected final RootPanel getRootPanel() {
077        RootPanel result = RootPanel.get("gwtpage-" + GWT.getModuleName());
078        if (result == null) {
079          result = RootPanel.get("gwtpage");
080        }
081        return result;
082      }
083    
084      private native JavaScriptObject loadResource()/*-{
085        return $wnd.config['resource'];
086      }-*/;
087    
088      private native void export(String gwtId, Page page)/*-{
089        $wnd.modules[gwtId]=function() {page.@org.sonar.gwt.ui.Page::onResourceLoad()()};
090      }-*/;
091    }
092