Skip to content

Commit 8585d29

Browse files
author
jossonsmith
committed
Support splitting swt-default.css into small *.css
1 parent 6192089 commit 8585d29

3 files changed

Lines changed: 160 additions & 3 deletions

File tree

sources/net.sf.j2s.java.core/src/java/lang/Class.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,6 +1302,12 @@ Clazz.makeConstructor = function (clazzThis, funBody, funParams) {
13021302
/* protected */
13031303
Clazz.allPackage = new Object ();
13041304

1305+
/**
1306+
* Will be used to keep value of whether the class is defined or not.
1307+
*/
1308+
/* protected */
1309+
Clazz.allClasses = new Object ();
1310+
13051311
Clazz.lastPackageName = null;
13061312
Clazz.lastPackage = null;
13071313

sources/net.sf.j2s.java.core/src/java/lang/ClassExt.js

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ Clazz.cloneFinals = function () {
204204
/* public */
205205
Clazz.isClassDefined = Clazz.isDefinedClass = function (clazzName) {
206206
if (clazzName != null && clazzName.length != 0) {
207+
if (Clazz.allClasses[clazzName]) {
208+
return true;
209+
}
207210
var pkgFrags = clazzName.split (/\./);
208211
var pkg = null;
209212
for (var i = 0; i < pkgFrags.length; i++) {
@@ -222,7 +225,13 @@ Clazz.isClassDefined = Clazz.isDefinedClass = function (clazzName) {
222225
}
223226
}
224227
//error (clazzName + " / " + (pkg != null));
225-
return pkg != null;
228+
//return pkg != null;
229+
if (pkg != null) {
230+
Clazz.allClasses[clazzName] = true;
231+
return true;
232+
} else {
233+
return false;
234+
}
226235
} else {
227236
/* consider null or empty name as non-defined class */
228237
return false;
@@ -735,6 +744,7 @@ Clazz.innerFunctions.newInstance = function () {
735744
}
736745
}
737746

747+
/* public */
738748
Clazz.forName = function (clazzName) {
739749
if (Clazz.isClassDefined (clazzName)) {
740750
return Clazz.evalType (clazzName);
@@ -747,3 +757,98 @@ Clazz.forName = function (clazzName) {
747757
alert ("[Java2Script] Error: No ClassLoader!");
748758
}
749759
};
760+
761+
/* protected */
762+
Clazz.cssAlreadyAggregated = false;
763+
Clazz.cssForcedUsingFile = true;
764+
765+
countxx = 0;
766+
/* private */
767+
Clazz.cssForIE = function (key) {
768+
var value = window[key];
769+
window[key] = true; // loaded and release css text
770+
return value;
771+
};
772+
773+
/**
774+
* Register css for the given class. If the given css text is null, it will
775+
* try to find relative *.css file instead loading css text directly.
776+
* @param clazzName Qualified name of a class
777+
* @param cssText Optional, if given, it will loaded into the page directly.
778+
*/
779+
/* public */
780+
Clazz.registerCSS = function (clazzName, cssText) {
781+
if (Clazz.cssAlreadyAggregated || window["ClazzLoader"] == null) {
782+
return ;
783+
}
784+
clazzName = ClazzLoader.unwrapArray ([clazzName])[0];
785+
var cssPath = ClazzLoader.getClasspathFor (clazzName, false, ".css");
786+
var basePath = ClazzLoader.getClasspathFor (clazzName, true);
787+
/*
788+
* Check whether the css resources is loaded or not
789+
*/
790+
if (!ClazzLoader.isResourceExisted (clazzName, cssPath, basePath)) {
791+
if (cssText == null || Clazz.cssForcedUsingFile) {
792+
var cssLink = document.createElement ("LINK");
793+
cssLink.rel = "stylesheet";
794+
cssLink.id = clazzName;
795+
cssLink.href = cssPath;
796+
document.getElementsByTagName ("HEAD")[0].appendChild (cssLink);
797+
} else {
798+
var prefix = "";
799+
var idx = cssPath.lastIndexOf ("/");
800+
if (idx != -1) {
801+
prefix = cssPath.substring (0, idx + 1);
802+
}
803+
if (document.createStyleSheet != null) {
804+
// prepare for createStyleSheet with "javascript:...";
805+
/*
806+
* TODO: Make more tests on the correctness of prefix!
807+
*/
808+
//var protocol = window.location.protocol;
809+
//var host = window.location.host;
810+
var location = window.location.href.toString ();
811+
//if (protocol == "file:" || host == "") {
812+
var idx = location.lastIndexOf ("/");
813+
if (idx != -1) {
814+
prefix = location.substring (0, idx + 1) + prefix;
815+
}
816+
//}
817+
}
818+
/*
819+
* Fix the css images location
820+
*/
821+
cssText = cssText.replace (/(url\s*\(\s*['"])(.*)(['"])/ig,
822+
//"
823+
function ($0, $1, $2, $3) {
824+
if ($2.indexOf ("/") == 0
825+
|| $2.indexOf ("http://") == 0
826+
|| $2.indexOf ("https://") == 0
827+
|| $2.indexOf ("file:/") == 0
828+
|| $2.indexOf ("ftp://") == 0
829+
|| $2.indexOf ("javascript:") == 0) {
830+
return $0;
831+
}
832+
return $1 + prefix + $2 + $3;
833+
});
834+
if (document.createStyleSheet != null) {
835+
/*
836+
* Internet Explorer does not support loading dynamic css styles
837+
* by <STYLE>!
838+
*/
839+
var cssID = "css." + clazzName;
840+
window[cssID] = cssText;
841+
/*
842+
* TODO: Keep eyes open on IE. Maybe IE won't initialize this
843+
* javascript-kind of CSS!
844+
*/
845+
document.createStyleSheet ("javascript:Clazz.cssForIE (\"" + cssID + "\");");
846+
} else {
847+
var cssStyle = document.createElement ("STYLE");
848+
cssStyle.id = clazzName;
849+
cssStyle.appendChild (document.createTextNode (cssText));
850+
document.getElementsByTagName ("HEAD")[0].appendChild (cssStyle);
851+
}
852+
}
853+
}
854+
};

sources/net.sf.j2s.java.core/src/java/lang/ClassLoader.js

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ ClazzLoader.unwrapArray = function (arr) {
296296
}
297297
last = arr[i];
298298
}
299+
return arr;
299300
};
300301

301302
/**
@@ -379,9 +380,11 @@ ClazzLoader.registerPackages = function (prefix, pkgs) {
379380
* be java.package, or java.lang.String
380381
* @param forRoot Optional argument, if true, the return path will be root
381382
* of the given classs' package root path.
383+
* @param ext Optional argument, if given, it will replace the default ".js"
384+
* extension.
382385
*/
383386
/* public */
384-
ClazzLoader.getClasspathFor = function (clazz, forRoot) {
387+
ClazzLoader.getClasspathFor = function (clazz, forRoot, ext) {
385388
//error ("check js path : " + clazz);
386389
var path = ClazzLoader.classpathMap["#" + clazz];
387390
if (path != null) {
@@ -417,7 +420,12 @@ ClazzLoader.getClasspathFor = function (clazz, forRoot) {
417420
if (clazz.lastIndexOf (".*") == clazz.length - 2) {
418421
return base + clazz.substring (0, idx + 1).replace (/\./g, "/");
419422
}
420-
var jsPath = base + clazz.replace (/\./g, "/") + ".js";
423+
if (ext == null) {
424+
ext = ".js";
425+
} else if (ext.charAt (0) != '.') {
426+
ext = "." + ext;
427+
}
428+
var jsPath = base + clazz.replace (/\./g, "/") + ext;
421429
return jsPath;
422430
};
423431

@@ -703,6 +711,44 @@ ClazzLoader.loadScript = function (file) {
703711
ClazzLoader.scriptLoading (file);
704712
};
705713

714+
/* protected */
715+
ClazzLoader.isResourceExisted = function (id, path, base) {
716+
if (id != null && document.getElementById (id) != null) {
717+
return true;
718+
}
719+
if (path != null) {
720+
var key = path;
721+
if (base != null) {
722+
if (path.indexOf (base) == 0) {
723+
key = path.substring (base.length);
724+
}
725+
}
726+
if (path.lastIndexOf (".css") == path.length - 4) {
727+
var resLinks = document.getElementsByTagName ("LINK");
728+
for (var i = 0; i < resLinks.length; i++) {
729+
var cssPath = resLinks[i].href;
730+
var idx = cssPath.lastIndexOf (key);
731+
if (idx != -1 && idx == cssPath.length - key.length) {
732+
return true;
733+
}
734+
}
735+
if (window["css." + id] == true) {
736+
return true;
737+
}
738+
} else if (path.lastIndexOf (".js") == path.length - 4) {
739+
var resScripts = document.getElementsByTagName ("SCRIPT");
740+
for (var i = 0; i < resScripts.length; i++) {
741+
var jsPath = resScripts[i].src;
742+
var idx = jsPath.lastIndexOf (key);
743+
if (idx != -1 && idx == jsPath.length - key.length) {
744+
return true;
745+
}
746+
}
747+
}
748+
}
749+
return false;
750+
};
751+
706752
/* public */
707753
ClazzLoader.loadCSS = function (cssName) {
708754
var cssKey = "";

0 commit comments

Comments
 (0)