Skip to content

Commit 541fb70

Browse files
author
soheil_h_y
committed
1. Theme manager done!
1 parent a033bd0 commit 541fb70

File tree

5 files changed

+147
-1
lines changed

5 files changed

+147
-1
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,7 @@ Clazz.registerCSS = function (clazzName, cssText) {
816816
* Check whether the css resources is loaded or not
817817
*/
818818
if (!ClazzLoader.isResourceExisted (clazzName, cssPath, basePath)) {
819+
ClazzLoader.registeredCSSs[ClazzLoader.registeredCSSs.length] = clazzName;
819820
if (cssText == null || Clazz.cssForcedUsingFile) {
820821
var cssLink = document.createElement ("LINK");
821822
cssLink.rel = "stylesheet";
@@ -875,5 +876,16 @@ Clazz.registerCSS = function (clazzName, cssText) {
875876
document.getElementsByTagName ("HEAD")[0].appendChild (cssStyle);
876877
}
877878
}
879+
880+
var len = ClazzLoader.themes.length;
881+
for(var i = 0; i < len; i++){
882+
var themeName = ClazzLoader.themes[i];
883+
var themePath = ClazzLoader.themePaths[themeName] + "/" + clazzName.replace (/\./g, "/") + ".css";
884+
var cssLink = document.createElement ("LINK");
885+
cssLink.rel = "stylesheet";
886+
cssLink.id = cssID + themeName;
887+
cssLink.href = themePath;
888+
document.getElementsByTagName ("HEAD")[0].appendChild (cssLink);
889+
}
878890
}
879891
};

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,36 @@ ClazzLoader.loadCSS = function (cssName) {
876876
cssLink.href = path.substring (0, path.lastIndexOf (".js")) + ".css";
877877
document.getElementsByTagName ("HEAD")[0].appendChild (cssLink);
878878
};
879-
879+
/*
880+
* This array will preserves the themes order.
881+
*/
882+
ClazzLoader.themes = new Array();
883+
ClazzLoader.themePaths = new Object();
884+
ClazzLoader.registeredCSSs = new Array();
885+
/**
886+
* This mehtod register a theme for overriding the default theme mechanism.
887+
*
888+
* @param themeName The name of the theme that must be unique
889+
* @param themePath The path of the theme that must contains the CSS files
890+
*/
891+
ClazzLoader.registerTheme = function(themeName, themePath){
892+
ClazzLoader.themes[ClazzLoader.themes.length] = themeName;
893+
ClazzLoader.themePaths[themeName] = themePath;
894+
895+
var len = ClazzLoader.registeredCSSs.length;
896+
var cssID = "c$$." + clazzName;
897+
898+
for (var i = 0 ; i < len; i++) {
899+
var clazzName = ClazzLoader.registeredCSSs[i];
900+
var cssPath = themePath + "/" + clazzName.replace (/\./g, "/") + ".css";
901+
var cssLink = document.createElement ("LINK");
902+
cssLink.rel = "stylesheet";
903+
cssLink.id = cssID + themeName;
904+
cssLink.href = cssPath;
905+
document.getElementsByTagName ("HEAD")[0].appendChild (cssLink);
906+
}
907+
908+
};
880909
/**
881910
* After class is loaded, this method will be executed to check whether there
882911
* are classes in the dependency tree that need to be loaded.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*******************************************************************************
2+
* Java2Script Pacemaker (http://j2s.sourceforge.net)
3+
*
4+
* Copyright (c) 2006 ognize.com and others.
5+
* All rights reserved. This program and the accompanying materials
6+
* are made available under the terms of the Eclipse Public License v1.0
7+
* which accompanies this distribution, and is available at
8+
* http://www.eclipse.org/legal/epl-v10.html
9+
*
10+
* Contributors:
11+
* ognize.com - initial API and implementation
12+
*******************************************************************************/
13+
14+
package net.sf.j2s.test.swt.theme;
15+
16+
import org.eclipse.swt.SWT;
17+
import org.eclipse.swt.browser.Browser;
18+
import org.eclipse.swt.events.SelectionAdapter;
19+
import org.eclipse.swt.events.SelectionEvent;
20+
import org.eclipse.swt.graphics.Color;
21+
import org.eclipse.swt.graphics.Font;
22+
import org.eclipse.swt.graphics.Image;
23+
import org.eclipse.swt.layout.GridData;
24+
import org.eclipse.swt.layout.GridLayout;
25+
import org.eclipse.swt.widgets.Button;
26+
import org.eclipse.swt.widgets.Control;
27+
import org.eclipse.swt.widgets.Display;
28+
import org.eclipse.swt.widgets.Label;
29+
import org.eclipse.swt.widgets.Shell;
30+
31+
/**
32+
* @author Soheil Hassas Yeganeh
33+
*
34+
*/
35+
public class TestTheme {
36+
static final String[] themes = {
37+
"theme1",
38+
"theme2"};
39+
/**
40+
* @param args
41+
*/
42+
public static void main(String[] args) {
43+
Display display = new Display ();
44+
final Shell shell = new Shell(display);
45+
shell.setLayout(new GridLayout());
46+
47+
final Button button = new Button(shell, SWT.BORDER);
48+
button.setText("Change to theme1");
49+
button.addSelectionListener(new SelectionAdapter(){
50+
public void widgetSelected(SelectionEvent e){
51+
/**
52+
* @j2sNative
53+
* ClazzLoader.registerTheme("theme1", "theme1");
54+
*/{}
55+
}
56+
});
57+
final Button button2 = new Button(shell, SWT.BORDER);
58+
button2.setText("Change to theme2");
59+
button2.addSelectionListener(new SelectionAdapter(){
60+
public void widgetSelected(SelectionEvent e){
61+
/**
62+
* @j2sNative
63+
* ClazzLoader.registerTheme("theme2", "theme2");
64+
*/{}
65+
}
66+
});
67+
68+
shell.pack();
69+
shell.open ();
70+
while (!shell.isDisposed ()) {
71+
if (!display.readAndDispatch ()) display.sleep ();
72+
}
73+
display.dispose ();
74+
}
75+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
.button-push, .button-toggle, .button-arrow {
2+
position:absolute;
3+
white-space:nowrap;
4+
font-family:inherit;
5+
font-size:1em;
6+
font-size:inherit;
7+
8+
background-color:red;
9+
color:inherit;
10+
11+
margin:0;
12+
padding:0;
13+
width:100%;
14+
height:100%;
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
.button-push, .button-toggle, .button-arrow {
2+
position:absolute;
3+
white-space:nowrap;
4+
font-family:inherit;
5+
font-size:1em;
6+
font-size:inherit;
7+
8+
background-color:green;
9+
color:inherit;
10+
11+
margin:0;
12+
padding:0;
13+
width:100%;
14+
height:100%;
15+
}

0 commit comments

Comments
 (0)