org.gk.client.GKEngine.java Source code

Java tutorial

Introduction

Here is the source code for org.gk.client.GKEngine.java

Source

/*
 * Copyright (C) 2000-2012  InfoChamp System Corporation
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.gk.client;

import jfreecode.gwt.event.client.bus.EventBus;

import org.gk.engine.client.Engine;
import org.gk.ui.client.gkComponent;

import com.extjs.gxt.ui.client.GXT;
import com.extjs.gxt.ui.client.util.Theme;
import com.extjs.gxt.ui.client.util.ThemeManager;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.RootPanel;

public class GKEngine extends gkComponent {
    private Engine gk;

    @Override
    public void start() {
        if (hasRenderPageFunction()) {
            RootPanel.get().add(viewport);
        }
        gk = Engine.get();
        Timer timer = new Timer() {
            @Override
            public void run() {
                if (gk.isReady()) {
                    cancel();
                    registry(GKEngine.this, gk);
                }
            }
        };
        timer.scheduleRepeating(200);
    }

    /**
     * <pre>
     * ?renderPage,renderPage
     * ?<gk>
     * </pre>
     * 
     * @return boolean
     */
    native boolean hasRenderPageFunction()/*-{
                                          return $wnd.renderPage != undefined;
                                          }-*/;

    /**
     * <pre>
     * render??JavaScript?? 
     * ?JavaScriptGK Engine???
     * registry? $wnd.renderPage() JavaScript
     * JavaScript GK Engine
     * </pre>
     * 
     * @param engineWrap
     * @param engine
     */
    public native void registry(GKEngine engineWrap, Engine engine)/*-{
                                                                   $wnd.gk.gul = function(gulCode) {
                                                                   if (arguments.length == 0) {
                                                                   return engineWrap.@org.gk.client.GKEngine::gul()();
                                                                   } else {
                                                                   $wnd.gk.render(gulCode);
                                                                   return gulCode;
                                                                   }
                                                                   }
                                                                   //?EventBus??
                                                                   $wnd.gk.bus = function(busName) {
                                                                   return engineWrap.@org.gk.client.GKEngine::eventBus(Ljava/lang/String;)(busName);
                                                                   }
                                                                   $wnd.gk.load = function(gulFilePath) {
                                                                   $wnd.$.ajax({
                                                                   type : 'GET',
                                                                   url : gulFilePath + "?render=true",
                                                                   dataType : 'text',
                                                                   success : function(gul) {
                                                                   $wnd.gk.render(gul);
                                                                   $wnd.gk.path = gulFilePath;
                                                                   }
                                                                   });
                                                                   }
                                                                   $wnd.gk.render = function(gulCode, id) {
                                                                   engineWrap.@org.gk.client.GKEngine::renderByJS(Ljava/lang/String;Ljava/lang/String;)(gulCode,id);
                                                                   }
                                                                   $wnd.gk.version = function() {
                                                                   return @org.gk.engine.client.Engine::getVersion()();
                                                                   }
                                                                   $wnd.gk.theme = function(theme, def) {
                                                                   return @org.gk.client.GKEngine::switchTheme(Ljava/lang/String;Ljava/lang/String;)(theme,''+def);
                                                                   }
                                                                   if ($wnd.renderPage == undefined) {
                                                                   $wnd.renderPage = function() {
                                                                   var gks = $wnd.document.getElementsByTagName('gk');
                                                                   while (gks.length > 0) {
                                                                   engineWrap.@org.gk.client.GKEngine::renderElement(Lcom/google/gwt/user/client/Element;)(gks[0]);
                                                                   }
                                                                   }
                                                                   }
                                                                   $wnd.renderPage();
                                                                   }-*/;

    private static String switchTheme(String themeId, String def) {
        themeId = themeId.toLowerCase();
        if (!themeId.equals(GXT.getThemeId())) {
            Theme t = ThemeManager.findTheme(themeId);
            if (Boolean.parseBoolean(def)) {
                GXT.setDefaultTheme(t, true);
            }
            GXT.switchTheme(t);
        }
        return themeId;
    }

    /**
     * <pre>
     * JavaScriptGK Engine?
     * id?null??element?
     * ??GK Enginebody?
     * <img width='280' src='http://icsclink.appspot.com/event/put/x/file.download.go?j={"i":"1281843983964_447249.png"}' />
     * </pre>
     * 
     * @param gulSyntax
     *            GUL
     * @param id
     *            ?null??element?
     */
    public void renderByJS(String gulSyntax, String id) {
        if (id != null && RootPanel.get(id) != null) {
            LayoutContainer gulPanel = new LayoutContainer();
            RootPanel.get(id).add(gulPanel);
            gk.render(gulSyntax, gulPanel, false);
        } else {
            gk.render(gulSyntax, viewport, true);
        }
    }

    public void renderElement(Element ele) {
        String gulSyntax = "<page>" + ele.getInnerHTML() + "</page>";
        Element div = DOM.createDiv();
        div.setId(ele.getId());
        ele.getParentElement().replaceChild(div, ele);
        if (div.getId().equals("")) {
            div.setId(DOM.createUniqueId());
        }
        LayoutContainer lc = new LayoutContainer();
        lc.setLayout(new FitLayout());
        RootPanel.get(div.getId()).add(lc);
        gk.render(gulSyntax, lc);
    }

    public String gul() {
        return gk.gul();
    }

    /**
     * ?? eventBus
     * 
     * @param busName
     * @return String
     */
    private String eventBus(String busName) {
        if (busName == null || busName.equals("")) {
            return EventBus.getEventBusList();
        } else {
            return "" + EventBus.get(busName);
        }
    }

    private native void setTitle(String title)/*-{
                                              $wnd.document.title = title;
                                              }-*/;
}