/*
 * Smart GWT (GWT for SmartClient)
 * Copyright 2008 and beyond, Isomorphic Software, Inc.
 *
 * Smart GWT is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation.  Smart GWT is also
 * available under typical commercial license terms - see
 * http://smartclient.com/license
 *
 * This software 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
 * Lesser General Public License for more details.
 */
/* sgwtgen */
 
package com.smartgwt.client.browser;


import com.smartgwt.client.event.*;
import com.smartgwt.client.core.*;
import com.smartgwt.client.types.*;
import com.smartgwt.client.data.*;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.events.*;
import com.smartgwt.client.browser.window.*;
import com.smartgwt.client.rpc.*;
import com.smartgwt.client.ai.*;
import com.smartgwt.client.callbacks.*;
import com.smartgwt.client.tools.*;
import com.smartgwt.client.bean.*;
import com.smartgwt.client.widgets.*;
import com.smartgwt.client.widgets.ai.*;
import com.smartgwt.client.widgets.events.*;
import com.smartgwt.client.widgets.form.*;
import com.smartgwt.client.widgets.form.validator.*;
import com.smartgwt.client.widgets.form.fields.*;
import com.smartgwt.client.widgets.tile.*;
import com.smartgwt.client.widgets.tile.events.*;
import com.smartgwt.client.widgets.grid.*;
import com.smartgwt.client.widgets.grid.events.*;
import com.smartgwt.client.widgets.chart.*;
import com.smartgwt.client.widgets.layout.*;
import com.smartgwt.client.widgets.layout.events.*;
import com.smartgwt.client.widgets.menu.*;
import com.smartgwt.client.widgets.tour.*;
import com.smartgwt.client.widgets.notify.*;
import com.smartgwt.client.widgets.rte.*;
import com.smartgwt.client.widgets.rte.events.*;
import com.smartgwt.client.widgets.ace.*;
import com.smartgwt.client.widgets.ace.events.*;
import com.smartgwt.client.widgets.tab.*;
import com.smartgwt.client.widgets.toolbar.*;
import com.smartgwt.client.widgets.tree.*;
import com.smartgwt.client.widgets.tree.events.*;
import com.smartgwt.client.widgets.tableview.*;
import com.smartgwt.client.widgets.viewer.*;
import com.smartgwt.client.widgets.calendar.*;
import com.smartgwt.client.widgets.calendar.events.*;
import com.smartgwt.client.widgets.cube.*;
import com.smartgwt.client.widgets.notify.*;
import com.smartgwt.client.widgets.drawing.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.google.gwt.event.shared.*;
import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.core.client.JavaScriptObject;

import com.smartgwt.client.util.*;
import com.smartgwt.client.util.events.*;
import com.smartgwt.client.util.workflow.*;
import com.smartgwt.client.util.workflow.Process; // required to override java.lang.Process
import com.smartgwt.client.util.tour.*;


/**
 * This class allows developers to create, modify and load/unload custom CSS stylesheets with simple API calls.
 */
@BeanFactory.FrameworkClass
@BeanFactory.ScClassName("StyleSheetHandler")
public class StyleSheetHandler extends BaseClass {

    public static StyleSheetHandler getOrCreateRef(JavaScriptObject jsObj) {
        if(jsObj == null) return null;
        BaseClass obj = BaseClass.getRef(jsObj);
        if(obj != null) {
            return (StyleSheetHandler) obj;
        } else {
            return new StyleSheetHandler(jsObj);
        }
    }

    public void setJavaScriptObject(JavaScriptObject jsObj) {
        internalSetID(jsObj);
        JSOHelper.setObjectAttribute(jsObj, SC.REF, this);
        JSOHelper.setObjectAttribute(jsObj, SC.MODULE, BeanFactory.getSGWTModule());
        if (!JSOHelper.isScClassInstance(jsObj)) {
            setConfig(jsObj);
            return;
        }
        JSOHelper.setObjectAttribute(getConfig(), SC.REF, this);
        JSOHelper.setObjectAttribute(getConfig(), SC.MODULE, BeanFactory.getSGWTModule());
        this.jsObj = jsObj;
        onBind();
    }
        


    public StyleSheetHandler(){
        scClassName = "StyleSheetHandler";
    }

    public StyleSheetHandler(JavaScriptObject jsObj){
        scClassName = "StyleSheetHandler";
        setJavaScriptObject(jsObj);
    }


    public StyleSheetHandler(String cssText, boolean autoLoad) {
        setCssText(cssText);
		setAutoLoad(autoLoad);
        scClassName = "StyleSheetHandler"; if (autoLoad) injectSheet();
    }


    public StyleSheetHandler(String name, String cssText, boolean autoLoad) {
        setName(name);
		setCssText(cssText);
		setAutoLoad(autoLoad);
        scClassName = "StyleSheetHandler"; if (autoLoad) injectSheet();
    }

    public native JavaScriptObject create()/*-{
        var config = this.@com.smartgwt.client.core.BaseClass::getConfig()();
        var scClassName = this.@com.smartgwt.client.core.BaseClass::scClassName;
        return $wnd.isc[scClassName].create(config);
    }-*/;

    private JavaScriptObject jsObj;
    
    @Override
    public boolean isCreated(){
        return this.jsObj != null;
    }

    @Override
    public JavaScriptObject getJsObj(){
        return this.jsObj;
    }

    @Override
    public JavaScriptObject getOrCreateJsObj() {
        if (!isCreated()) {
            this.jsObj = createJsObj();
            doInit();
        }
        return this.jsObj;
    }



    // ********************* Properties / Attributes ***********************

    /**
     * When true, this handler will automatically {@link com.smartgwt.client.browser.StyleSheetHandler#injectSheet inject} 
     * it's stylesheet into the DOM.
     *
     * @param autoLoad New autoLoad value. Default value is false
     * @return {@link com.smartgwt.client.browser.StyleSheetHandler StyleSheetHandler} instance, for chaining setter calls
     * @throws IllegalStateException this property cannot be changed after the underlying component has been created
     */
    public StyleSheetHandler setAutoLoad(boolean autoLoad)  throws IllegalStateException {
        return (StyleSheetHandler)setAttribute("autoLoad", autoLoad, false);
    }

    /**
     * When true, this handler will automatically {@link com.smartgwt.client.browser.StyleSheetHandler#injectSheet inject} 
     * it's stylesheet into the DOM.
     *
     * @return Current autoLoad value. Default value is false
     */
    public boolean getAutoLoad()  {
        Boolean result = getAttributeAsBoolean("autoLoad");
        return result == null ? false : result;
    }
    

    /**
     * A string representation of all the CSS styles in this handler's  {@link
     * com.smartgwt.client.browser.StyleSheetHandler#getStyleSheet styleSheet}.
     *
     * <br><br>If this method is called after the component has been drawn/initialized:
     * Replace the entire CSS in this handler's {@link com.smartgwt.client.browser.StyleSheetHandler#getStyleSheet stylesheet}. <P> If the stylesheet is already in the DOM, it is first  {@link com.smartgwt.client.browser.StyleSheetHandler#unload unloaded}, the {@link com.smartgwt.client.browser.StyleSheetHandler#getCssText cssText} is updated and then the stylesheet is {@link com.smartgwt.client.browser.StyleSheetHandler#injectSheet injected} back into the DOM.  Otherwise, the handler's cssText is updated and no further action is  taken.
     *
     * @param cssText complete cssText to apply to the stylesheet. Default value is null
     * @return {@link com.smartgwt.client.browser.StyleSheetHandler StyleSheetHandler} instance, for chaining setter calls
     */
    public StyleSheetHandler setCssText(String cssText) {
        return (StyleSheetHandler)setAttribute("cssText", cssText, true);
    }

    /**
     * A string representation of all the CSS styles in this handler's  {@link
     * com.smartgwt.client.browser.StyleSheetHandler#getStyleSheet styleSheet}.
     *
     * @return Current cssText value. Default value is null
     */
    public String getCssText()  {
        return getAttributeAsString("cssText");
    }
    

    /**
     * The name for this handler.  This value is applied as both the 'id' and 'title' attributes on the handler's {@link
     * com.smartgwt.client.browser.StyleSheetHandler#getStyleSheet stylesheet}.
     *
     * @param name New name value. Default value is null
     * @return {@link com.smartgwt.client.browser.StyleSheetHandler StyleSheetHandler} instance, for chaining setter calls
     * @throws IllegalStateException this property cannot be changed after the underlying component has been created
     */
    public StyleSheetHandler setName(String name)  throws IllegalStateException {
        return (StyleSheetHandler)setAttribute("name", name, false);
    }

    /**
     * The name for this handler.  This value is applied as both the 'id' and 'title' attributes on the handler's {@link
     * com.smartgwt.client.browser.StyleSheetHandler#getStyleSheet stylesheet}.
     *
     * @return Current name value. Default value is null
     */
    public String getName()  {
        return getAttributeAsString("name");
    }
    

    /**
     * A reference to this handler's stylesheet-element in the DOM.  This attribute cannot be  set - see {@link
     * com.smartgwt.client.browser.StyleSheetHandler#injectSheet injectSheet()} and  {@link
     * com.smartgwt.client.browser.StyleSheetHandler#unload unload()} for details.
     * <p>
     * <b>Note :</b> This method should be called only after the underlying component has been created.
     *
     * @return Current styleSheet value. Default value is null
     * @throws IllegalStateException if the underlying component has not yet been created.
     */
    public Element getStyleSheet() throws IllegalStateException {
        errorIfNotCreated("styleSheet");
        return getAttributeAsElement("styleSheet");
    }
    

    // ********************* Methods ***********************
	/**
     * Returns all styles for the passed className or array of classNames.  The output  is a single compound string in the
     * format  "<i>.class1 { css settings; } .class2 { css settings; } ... </i>". <P> This string may then be imported directly
     * into another stylesheet, either  {@link com.smartgwt.client.browser.StyleSheetHandler#setCssText replacing content} or 
     * {@link com.smartgwt.client.browser.StyleSheetHandler#injectCssText adding to it}.
     * @param className one or more CSS-class names to get the                                              declaration for
     *
     * @return the CSS declaration for the passed className
     */
    public native String filterCssText(String className) /*-{
        if (this.@com.smartgwt.client.core.BaseClass::isConfigOnly()()) {
            @com.smartgwt.client.util.ConfigUtil::warnOfPostConfigInstantiation(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/String;)(this.@java.lang.Object::getClass()(), "filterCssText", "String");
        }
        var self = this.@com.smartgwt.client.core.BaseClass::getOrCreateJsObj()();
        var ret = self.filterCssText(className);
        return ret;
    }-*/;

	/**
     * Returns all styles for the passed className or array of classNames.  The output  is a single compound string in the
     * format  "<i>.class1 { css settings; } .class2 { css settings; } ... </i>". <P> This string may then be imported directly
     * into another stylesheet, either  {@link com.smartgwt.client.browser.StyleSheetHandler#setCssText replacing content} or 
     * {@link com.smartgwt.client.browser.StyleSheetHandler#injectCssText adding to it}.
     * @param className one or more CSS-class names to get the                                              declaration for
     *
     * @return the CSS declaration for the passed className
     */
    public native String filterCssText(String... className) /*-{
        if (this.@com.smartgwt.client.core.BaseClass::isConfigOnly()()) {
            @com.smartgwt.client.util.ConfigUtil::warnOfPostConfigInstantiation(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/String;)(this.@java.lang.Object::getClass()(), "filterCssText", "String...");
        }
        var self = this.@com.smartgwt.client.core.BaseClass::getOrCreateJsObj()();
        var ret = self.filterCssText(@com.smartgwt.client.util.JSOHelper::convertToJavaScriptArray([Ljava/lang/Object;)(className));
        return ret;
    }-*/;

	/**
     * Returns the list of CSS class-names defined in this handler's {@link
     * com.smartgwt.client.browser.StyleSheetHandler#getStyleSheet stylesheet}.
     *
     * @return the list of CSS class-names defined in the stylesheet
     */
    public native String[] getClassList() /*-{
        if (this.@com.smartgwt.client.core.BaseClass::isConfigOnly()()) {
            @com.smartgwt.client.util.ConfigUtil::warnOfPostConfigInstantiation(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/String;)(this.@java.lang.Object::getClass()(), "getClassList", "");
        }
        var self = this.@com.smartgwt.client.core.BaseClass::getOrCreateJsObj()();
        var ret = self.getClassList();
        if(ret == null) return null;
        return @com.smartgwt.client.util.ConvertTo::arrayOfString(Lcom/google/gwt/core/client/JavaScriptObject;)(ret);
    }-*/;

	/**
     * Injects text defining a block of selectors into this handler's  {@link
     * com.smartgwt.client.browser.StyleSheetHandler#getStyleSheet stylesheet}. <P> The passed 'cssText' string should contain
     * one or more selectors, each in the format  '.className [, className ...] { css settings; }'.  Use  {@link
     * com.smartgwt.client.browser.StyleSheetHandler#modifyClass modifyClass(className, cssText-without-braces)}  to apply
     * styles to a specific CSS class.
     * @param cssText string of css selectors and their settings to apply to this                         handler's {@link
     * com.smartgwt.client.browser.StyleSheetHandler#getStyleSheet stylesheet}.
     *
     * @return returns true if CSS was injected, false otherwise
     */
    public native boolean injectCssText(String cssText) /*-{
        if (this.@com.smartgwt.client.core.BaseClass::isConfigOnly()()) {
            @com.smartgwt.client.util.ConfigUtil::warnOfPostConfigInstantiation(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/String;)(this.@java.lang.Object::getClass()(), "injectCssText", "String");
        }
        var self = this.@com.smartgwt.client.core.BaseClass::getOrCreateJsObj()();
        var ret = self.injectCssText(cssText);
        return ret == null ? false : ret;
    }-*/;

	/**
     * Create this handler's {@link com.smartgwt.client.browser.StyleSheetHandler#getStyleSheet stylesheet} element in the DOM.
     */
    public native void injectSheet() /*-{
        if (this.@com.smartgwt.client.core.BaseClass::isConfigOnly()()) {
            @com.smartgwt.client.util.ConfigUtil::warnOfPostConfigInstantiation(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/String;)(this.@java.lang.Object::getClass()(), "injectSheet", "");
        }
        var self = this.@com.smartgwt.client.core.BaseClass::getOrCreateJsObj()();
        self.injectSheet();
    }-*/;

	/**
     * Has this handler's {@link com.smartgwt.client.browser.StyleSheetHandler#getStyleSheet styleSheet} been loaded into the
     * DOM?
     *
     * @return true if this handler's stylesheet is currently loaded into the DOM
     */
    public native boolean loaded() /*-{
        if (this.@com.smartgwt.client.core.BaseClass::isConfigOnly()()) {
            @com.smartgwt.client.util.ConfigUtil::warnOfPostConfigInstantiation(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/String;)(this.@java.lang.Object::getClass()(), "loaded", "");
        }
        var self = this.@com.smartgwt.client.core.BaseClass::getOrCreateJsObj()();
        var ret = self.loaded();
        return ret == null ? false : ret;
    }-*/;

	/**
     * Inject a string of css settings into a single CSS class, via a new rule in this handler's  {@link
     * com.smartgwt.client.browser.StyleSheetHandler#getStyleSheet stylesheet}. <P> The passed 'cssText' should contain
     * semi-colon -separated CSS settings only, not the selector/className or enclosing braces - { }.
     * @param className name of the CSS class to apply the cssText to
     * @param cssText string of css settings to apply to the passed className in this                         handler's {@link
     * com.smartgwt.client.browser.StyleSheetHandler#getStyleSheet stylesheet}.
     *
     * @return returns true if CSS was applied, false otherwise
     */
    public native boolean modifyClass(String className, String cssText) /*-{
        if (this.@com.smartgwt.client.core.BaseClass::isConfigOnly()()) {
            @com.smartgwt.client.util.ConfigUtil::warnOfPostConfigInstantiation(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/String;)(this.@java.lang.Object::getClass()(), "modifyClass", "String,String");
        }
        var self = this.@com.smartgwt.client.core.BaseClass::getOrCreateJsObj()();
        var ret = self.modifyClass(className, cssText);
        return ret == null ? false : ret;
    }-*/;

	/**
     * Remove the passed CSS class from the {@link com.smartgwt.client.browser.StyleSheetHandler#getStyleSheet stylesheet}, by
     * deleting it's rules and pruning it from multi-class selectors.
     * @param className name of the CSS class to remove from the stylesheet
     *
     * @return returns true if CSS was removed, false otherwise
     */
    public native boolean removeClass(String className) /*-{
        if (this.@com.smartgwt.client.core.BaseClass::isConfigOnly()()) {
            @com.smartgwt.client.util.ConfigUtil::warnOfPostConfigInstantiation(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/String;)(this.@java.lang.Object::getClass()(), "removeClass", "String");
        }
        var self = this.@com.smartgwt.client.core.BaseClass::getOrCreateJsObj()();
        var ret = self.removeClass(className);
        return ret == null ? false : ret;
    }-*/;

	/**
     * Rename the passed <i>oldClass</i> to <i>newClass</i> in all rules that reference it.
     * @param oldName name of the CSS class to rename in the stylesheet
     * @param newName new name for the CSS class
     *
     * @return returns true if a class was renamed, false otherwise
     */
    public native boolean renameClass(String oldName, String newName) /*-{
        if (this.@com.smartgwt.client.core.BaseClass::isConfigOnly()()) {
            @com.smartgwt.client.util.ConfigUtil::warnOfPostConfigInstantiation(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/String;)(this.@java.lang.Object::getClass()(), "renameClass", "String,String");
        }
        var self = this.@com.smartgwt.client.core.BaseClass::getOrCreateJsObj()();
        var ret = self.renameClass(oldName, newName);
        return ret == null ? false : ret;
    }-*/;

	/**
     * Remove this handler's {@link com.smartgwt.client.browser.StyleSheetHandler#getStyleSheet stylesheet} from the DOM and 
     * destroy it.
     */
    public native void unload() /*-{
        if (this.@com.smartgwt.client.core.BaseClass::isConfigOnly()()) {
            @com.smartgwt.client.util.ConfigUtil::warnOfPostConfigInstantiation(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/String;)(this.@java.lang.Object::getClass()(), "unload", "");
        }
        var self = this.@com.smartgwt.client.core.BaseClass::getOrCreateJsObj()();
        self.unload();
    }-*/;


    // ********************* Static Methods ***********************

    // ***********************************************************

}
