001 
002 /*
003  *  JScripter Standard 1.0 - To Script In Java
004  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.jscripter.org>
005  *  
006  *  This program is free software: you can redistribute it and/or modify
007  *  it under the terms of the GNU Affero General Public License as published by
008  *  the Free Software Foundation, either version 3 of the License, or
009  *  (at your option) any later version.
010  *  
011  *  This program 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
014  *  GNU Affero General Public License for more details.
015  *  
016  *  You should have received a copy of the GNU Affero General Public License
017  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
018  */
019 
020 package jsx.ui.css;
021 
022 import js.Disposable;
023 import js.Js;
024 import js.Vars;
025 import js.user.JsElement;
026 import js.user.JsHTMLLinkElement;
027 import jsx.Code;
028 import jsx.client.Document;
029 import jsx.client.Browser;
030 import jsx.core.ArrayLikes;
031 import jsx.dom.Nodes;
032 import jsx.ui.Widget;
033 
034 /**
035  * <p>A utility class providing static methods to manage external style sheets
036  * for the current HTML document.</p>
037  * 
038  * @author <a href="mailto:jianjunliu@126.com">J.J.Liu (Jianjun Liu)</a> at <a href="http://www.jscripter.org" target="_blank">http://www.jscripter.org</a>
039  */
040 public final class StyleSheets extends Disposable
041 {
042     private StyleSheets() {}
043 
044     /**
045      * <p>Appends a new external style sheet to the current HTML document.</p>
046      * @param url The resource locator for the external style sheet.
047      * @since 1.0
048      */
049     public static final void create(String url) {
050         Nodes.appendChild(
051                 Document.head(),
052                 createLink(url)
053         );
054     }
055 
056     /**
057      * <p>Creates the external file name of the style sheet for a class of widgets.</p>
058      * @param cls A class of widgets.
059      * @return The external file name of the style sheet for that class of widgets
060      * @since 1.0
061      */
062     public static final String css(Class<? extends Widget> cls) {
063         return Code.ref(cls.getName(), "css");
064     }
065 
066     /**
067      * <p>Links all style sheets under a named base folder.</p>
068      * @param base The name of a base folder.
069      * @since 1.0
070      */
071     public static final void link(String base) {
072         create(def(base, "all"));
073     }
074 
075     /**
076      * <p>Links a named external style sheet under a named base folder.</p>
077      * @param base The name of a base folder.
078      * @param css The file name of an external style sheet.
079      * @since 1.0
080      */
081     public static final void link(String base, String css) {
082         create(Code.sub(base, css));
083     }
084 
085     /**
086      * <p>Gets the default folder under a named base for system external style sheets.</p>
087      * @param base The name of a base folder.
088      * @return The default folder for system external style sheets.
089      * @since 1.0
090      */
091     public static final String def(String base) {
092         return ArrayLikes.join(
093                 new Vars<String>().add(base)
094                                   .add("css")
095                                   .add("def")
096                                   .var(),
097                 "/"
098         );
099     }
100 
101     /**
102      * <p>Links all the system default external style sheets.</p>
103      * @since 1.0
104      */
105     public static final void prelink() {
106         if (Js.not(prelinks)) {
107             prelink(".");
108 //          prelink("http://www.jscripter.org");
109             prelinks++;
110         }
111     }
112 
113     private static int prelinks = 0;
114 
115     private static final String def(String base, String css) {
116         return Code.sub(base, Code.ref(css, "css"));
117     }
118     private static final JsElement createLink(String url) {
119         JsElement e = Document.createElement("link");
120         JsHTMLLinkElement.rel.with(e, "stylesheet");
121         JsHTMLLinkElement.type.with(e, Code.sub("text", "css"));
122         JsHTMLLinkElement.href.with(e, url);
123         return e;
124     }
125     private static final void prelink(String base) {
126         String all = def(def(base), "all");
127         if (Browser.isIE) {
128             Document.createStyleSheet(all, 0);
129         } else {
130             Document.head().insertBefore(
131                     createLink(all),
132                     Nodes.firstChild(Document.head())
133             );
134         }
135     }
136 }