|
| 1 | +/* |
| 2 | + * The following codes are moved from ClassExt.js, with modifications |
| 3 | + */ |
| 4 | + |
| 5 | +/* |
| 6 | + * Standing for SWT CSS, with all "S" into "$". We love dollors. -zhou renjian |
| 7 | + * |
| 8 | + * ATTENTION: Should only be used for SWT only. |
| 9 | + */ |
| 10 | +/* protected */ |
| 11 | +$WTC$$ = {}; |
| 12 | + |
| 13 | +/* protected */ |
| 14 | +$WTC$$.cssAlreadyAggregated = false; |
| 15 | +$WTC$$.cssForcedUsingFile = false; |
| 16 | + |
| 17 | +/** |
| 18 | + * Register css for the given class. If the given css text is null, it will |
| 19 | + * try to find relative *.css file instead loading css text directly. |
| 20 | + * @param clazzName Qualified name of a class |
| 21 | + * @param cssText Optional, if given, it will loaded into the page directly. |
| 22 | + */ |
| 23 | +/* public */ |
| 24 | +$WTC$$.registerCSS = function (clazzName, cssText) { |
| 25 | + if ($WTC$$.cssAlreadyAggregated || window["ClazzLoader"] == null) { |
| 26 | + return ; |
| 27 | + } |
| 28 | + clazzName = ClazzLoader.unwrapArray ([clazzName])[0]; |
| 29 | + var cssPath = ClazzLoader.getClasspathFor (clazzName, false, ".css"); |
| 30 | + |
| 31 | + var basePath = ClazzLoader.getClasspathFor (clazzName, true); |
| 32 | + var cssID = "c$$." + clazzName; |
| 33 | + /* |
| 34 | + * Check whether the css resources is loaded or not |
| 35 | + */ |
| 36 | + if (!ClazzLoader.isResourceExisted (clazzName, cssPath, basePath)) { |
| 37 | + $WTC$$.registeredCSSs[$WTC$$.registeredCSSs.length] = clazzName; |
| 38 | + if (cssText == null || $WTC$$.cssForcedUsingFile) { |
| 39 | + var cssLink = document.createElement ("LINK"); |
| 40 | + cssLink.rel = "stylesheet"; |
| 41 | + cssLink.id = cssID; |
| 42 | + cssLink.href = cssPath; |
| 43 | + document.getElementsByTagName ("HEAD")[0].appendChild (cssLink); |
| 44 | + } else { |
| 45 | + var prefix = ""; |
| 46 | + var idx = cssPath.lastIndexOf ("/"); |
| 47 | + if (idx != -1) { |
| 48 | + prefix = cssPath.substring (0, idx + 1); |
| 49 | + } |
| 50 | + if (document.createStyleSheet != null) { |
| 51 | + // prepare for createStyleSheet with "javascript:..."; |
| 52 | + /* |
| 53 | + * TODO: Make more tests on the correctness of prefix! |
| 54 | + */ |
| 55 | + //var protocol = window.location.protocol; |
| 56 | + //var host = window.location.host; |
| 57 | + var location = window.location.href.toString (); |
| 58 | + //if (protocol == "file:" || host == "") { |
| 59 | + var idx = location.lastIndexOf ("/"); |
| 60 | + if (idx != -1) { |
| 61 | + prefix = location.substring (0, idx + 1) + prefix; |
| 62 | + } |
| 63 | + //} |
| 64 | + } |
| 65 | + /* |
| 66 | + * Fix the css images location |
| 67 | + */ |
| 68 | + cssText = cssText.replace (/(url\s*\(\s*['"])(.*)(['"])/ig, |
| 69 | + //" |
| 70 | + function ($0, $1, $2, $3) { |
| 71 | + if ($2.indexOf ("/") == 0 |
| 72 | + || $2.indexOf ("http://") == 0 |
| 73 | + || $2.indexOf ("https://") == 0 |
| 74 | + || $2.indexOf ("file:/") == 0 |
| 75 | + || $2.indexOf ("ftp://") == 0 |
| 76 | + || $2.indexOf ("javascript:") == 0) { |
| 77 | + return $0; |
| 78 | + } |
| 79 | + return $1 + prefix + $2 + $3; |
| 80 | + }); |
| 81 | + if (document.createStyleSheet != null) { |
| 82 | + /* |
| 83 | + * Internet Explorer does not support loading dynamic css styles |
| 84 | + * by creating <STYLE> element! |
| 85 | + */ |
| 86 | + var sheet = document.createStyleSheet (); |
| 87 | + sheet.cssText = cssText; |
| 88 | + //sheet.id = cssID; // No ID support the this element for IE |
| 89 | + window[cssID] = true; |
| 90 | + } else { |
| 91 | + var cssStyle = document.createElement ("STYLE"); |
| 92 | + cssStyle.id = cssID; |
| 93 | + cssStyle.appendChild (document.createTextNode (cssText)); |
| 94 | + document.getElementsByTagName ("HEAD")[0].appendChild (cssStyle); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + var len = $WTC$$.themes.length; |
| 99 | + for(var i = 0; i < len; i++){ |
| 100 | + var themeName = $WTC$$.themes[i]; |
| 101 | + var themePath = $WTC$$.themePaths[themeName] + "/" + clazzName.replace (/\./g, "/") + ".css"; |
| 102 | + var cssLink = document.createElement ("LINK"); |
| 103 | + cssLink.rel = "stylesheet"; |
| 104 | + cssLink.id = cssID + themeName; |
| 105 | + cssLink.href = themePath; |
| 106 | + document.getElementsByTagName ("HEAD")[0].appendChild (cssLink); |
| 107 | + } |
| 108 | + } |
| 109 | +}; |
| 110 | + |
| 111 | +/* |
| 112 | + * The above codes are moved from ClassExt.js |
| 113 | + */ |
| 114 | + |
| 115 | +/* |
| 116 | + * The following codes are moved from ClassLoader.js |
| 117 | + */ |
| 118 | + |
| 119 | +/* |
| 120 | + * loadCSS may be considered part of SWT library. Should be packed with |
| 121 | + * SWT not with Java core. |
| 122 | + * |
| 123 | + * Not used in other *.js yet. |
| 124 | + * - Nov 8, 2006 |
| 125 | + */ |
| 126 | +/* public */ |
| 127 | +$WTC$$.loadCSS = function (cssName) { |
| 128 | + var cssKey = ""; |
| 129 | + var idx = cssName.lastIndexOf ("."); |
| 130 | + if (idx == -1) { |
| 131 | + cssKey = cssName + ".css"; |
| 132 | + } else { |
| 133 | + cssKey = cssName.substring (idx + 1) + ".css"; |
| 134 | + } |
| 135 | + var resLinks = document.getElementsByTagName ("LINK"); |
| 136 | + for (var i = 0; i < resLinks.length; i++) { |
| 137 | + var cssPath = resLinks[i].href; |
| 138 | + if (cssPath.lastIndexOf (cssKey) == cssPath.length - cssKey.length) { |
| 139 | + return ; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + /*-# cssLink -> rel #-*/ |
| 144 | + var cssLink = document.createElement ("LINK"); |
| 145 | + cssLink.rel = "stylesheet"; |
| 146 | + var path = ClazzLoader.getClasspathFor (cssName); |
| 147 | + cssLink.href = path.substring (0, path.lastIndexOf (".js")) + ".css"; |
| 148 | + document.getElementsByTagName ("HEAD")[0].appendChild (cssLink); |
| 149 | +}; |
| 150 | +/* |
| 151 | + * This array will preserves the themes order. |
| 152 | + */ |
| 153 | +$WTC$$.themes = new Array(); |
| 154 | +$WTC$$.themePaths = new Object(); |
| 155 | +$WTC$$.registeredCSSs = new Array(); |
| 156 | +/** |
| 157 | + * This mehtod register a theme for overriding the default theme mechanism. |
| 158 | + * |
| 159 | + * @param themeName The name of the theme that must be unique |
| 160 | + * @param themePath The path of the theme that must contains the CSS files |
| 161 | + */ |
| 162 | +$WTC$$.registerTheme = function(themeName, themePath){ |
| 163 | + $WTC$$.themes[$WTC$$.themes.length] = themeName; |
| 164 | + $WTC$$.themePaths[themeName] = themePath; |
| 165 | + |
| 166 | + var len = $WTC$$.registeredCSSs.length; |
| 167 | + var cssID = "c$$." + clazzName; |
| 168 | + |
| 169 | + for (var i = 0 ; i < len; i++) { |
| 170 | + var clazzName = $WTC$$.registeredCSSs[i]; |
| 171 | + var cssPath = themePath + "/" + clazzName.replace (/\./g, "/") + ".css"; |
| 172 | + var cssLink = document.createElement ("LINK"); |
| 173 | + cssLink.rel = "stylesheet"; |
| 174 | + cssLink.id = cssID + themeName; |
| 175 | + cssLink.href = cssPath; |
| 176 | + document.getElementsByTagName ("HEAD")[0].appendChild (cssLink); |
| 177 | + } |
| 178 | + |
| 179 | +}; |
| 180 | + |
| 181 | +/* |
| 182 | + * The above codes are moved from ClassLoader.js |
| 183 | + */ |
| 184 | + |
1 | 185 | Clazz.declarePackage ("org.eclipse.swt"); |
2 | 186 | $wt = org.eclipse.swt; |
3 | 187 |
|
|
0 commit comments