Skip to content

Commit 0ccf1c2

Browse files
author
jossonsmith
committed
Move CSS related methods from Class and ClassLoader into SWT
package.js with namespace of "$WTC$$" for SWT only.
1 parent b4bf99b commit 0ccf1c2

File tree

25 files changed

+208
-24
lines changed

25 files changed

+208
-24
lines changed

sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt/browser/Browser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
* @since 3.0
3434
*
3535
* @j2sPrefix
36-
* Clazz.registerCSS ("$wt.browser.Browser");
36+
* $WTC$$.registerCSS ("$wt.browser.Browser");
3737
*/
3838
public class Browser extends Composite {
3939
/*

sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt/custom/CTabFolder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
* </p>
7070
*
7171
* @j2sPrefix
72-
* Clazz.registerCSS ("$wt.custom.CTabFolder");
72+
* $WTC$$.registerCSS ("$wt.custom.CTabFolder");
7373
*/
7474

7575
public class CTabFolder extends Composite {

sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt/package.js

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,187 @@
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+
1185
Clazz.declarePackage ("org.eclipse.swt");
2186
$wt = org.eclipse.swt;
3187

sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt/widgets/Button.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
* </p>
5252
*
5353
* @j2sPrefix
54-
* Clazz.registerCSS ("$wt.widgets.Button");
54+
* $WTC$$.registerCSS ("$wt.widgets.Button");
5555
*/
5656

5757
public class Button extends Control {

sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt/widgets/ColorDialog.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
* </p>
4545
*
4646
* @j2sPrefix
47-
* Clazz.registerCSS ("$wt.widgets.ColorDialog");
47+
* $WTC$$.registerCSS ("$wt.widgets.ColorDialog");
4848
*/
4949

5050
public class ColorDialog extends Dialog {

sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt/widgets/Combo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
* @see List
6767
*
6868
* @j2sPrefix
69-
* Clazz.registerCSS ("$wt.widgets.Combo");
69+
* $WTC$$.registerCSS ("$wt.widgets.Combo");
7070
*/
7171

7272
public class Combo extends Composite {

sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt/widgets/Composite.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
* @see Canvas
4747
*
4848
* @j2sPrefix
49-
* Clazz.registerCSS ("$wt.widgets.Composite");
49+
* $WTC$$.registerCSS ("$wt.widgets.Composite");
5050
*/
5151
public class Composite extends Scrollable {
5252
Layout layout;

sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt/widgets/CoolBar.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
* </p>
4747
*
4848
* @j2sPrefix
49-
* Clazz.registerCSS ("$wt.widgets.CoolBar");
49+
* $WTC$$.registerCSS ("$wt.widgets.CoolBar");
5050
*/
5151

5252
public class CoolBar extends Composite {

sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt/widgets/Group.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
* </p>
4242
*
4343
* @j2sPrefix
44-
* Clazz.registerCSS ("$wt.widgets.Group");
44+
* $WTC$$.registerCSS ("$wt.widgets.Group");
4545
*/
4646

4747
public class Group extends Composite {

sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt/widgets/Label.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
* </p>
4646
*
4747
* @j2sPrefix
48-
* Clazz.registerCSS ("$wt.widgets.Label");
48+
* $WTC$$.registerCSS ("$wt.widgets.Label");
4949
*/
5050
public class Label extends Control {
5151
String text = "";

0 commit comments

Comments
 (0)