TK-UI provide several CSS engine implementation which are enable to apply CSS styles to SWT widget . Please read Download section to download CSS SWT engine distribution and SWT samples.
This section explains how use CSS SWT engine with simple samples. You can find more complex samples into test folder of the org.akrogen.tkui.css.swt* distribution. There are tow ways to apply CSS styles to SWT widgets :
Selectors section describe all type of selectors that you can write with SWT widgets.
CSS SWT engines are configurables, please read CSSEngine API section for more informations.
Label { color:red; } Text { background-color:green; }
Display display = new Display(); // Create SWT CSS Engine CSSEngine engine = new CSSSWTEngineImpl(display); // Parse style sheet engine.parseStyleSheet(new StringReader( "Label {color:red;} Text {background-color:green;}")); /*--- Start UI SWT ---*/ Shell shell = new Shell(display, SWT.SHELL_TRIM); FillLayout layout = new FillLayout(); shell.setLayout(layout); Composite panel1 = new Composite(shell, SWT.NONE); panel1.setLayout(new FillLayout()); // Label Label label1 = new Label(panel1, SWT.NONE); label1.setText("Label 0"); // Text Text text1 = new Text(panel1, SWT.NONE); text1.setText("bla bla bla..."); /*--- End UI SWT ---*/ // Apply Styles engine.applyStyles(shell, true); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose();
label { color:red; } input { background-color:green; }
Display display = new Display(); // Create SWT CSS Engine CSSEngine engine = new CSSSWTHTMLEngineImpl(display); // Parse style sheet engine.parseStyleSheet(new StringReader( "label {color:red;} input {background-color:green;}")); /*--- Start UI SWT ---*/ Shell shell = new Shell(display, SWT.SHELL_TRIM); FillLayout layout = new FillLayout(); shell.setLayout(layout); Composite panel1 = new Composite(shell, SWT.NONE); panel1.setLayout(new FillLayout()); // Label Label label1 = new Label(panel1, SWT.NONE); label1.setText("Label 0"); // Text Text text1 = new Text(panel1, SWT.NONE); text1.setText("bla bla bla..."); /*--- End UI SWT ---*/ // Apply Styles engine.applyStyles(shell, true); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose();
Pour gérer les noms HTML en tant que selector, un mapping entre la widget SWT et le nom HTML est effectué dans la méthode getLocalName de l'instance CSSStylableElement qui wrappe la widget SWT. Voici un tableau du mapping nom HTML/widget SWT: To manage HTML name as selector, a mapping between SWT widget and HTML name is done. with getLocalName method of the CSSStylableElement instance which wrap the SWT widget. Here mapping array of the HTML name/SWT widget :
HTML name | SWT widget |
---|---|
body | org.eclipse.swt.widgets.Shell |
textarea | org.eclipse.swt.widgets.Text : style=SWT.MULTI |
input[type=password] | org.eclipse.swt.widgets.Text : style=SWT.PASSWORD |
input[type=text] | org.eclipse.swt.widgets.Text |
input[type=radio] | org.eclipse.swt.widgets.Button : style=SWT.RADIO |
input[type=checkbox] | org.eclipse.swt.widgets.Button : style=SWT.CHECK |
input[type=button] | org.eclipse.swt.widgets.Button |
select | org.eclipse.swt.widgets.Combo, org.eclipse.swt.custom.CCombo |
label | org.eclipse.swt.widgets.Label |
canva | org.eclipse.swt.widgets.Canvas |
a | org.eclipse.swt.widgets.Link |
div | org.eclipse.swt.widgets.Composite, org.eclipse.swt.widgets.Scrollable |
tree | org.eclipse.swt.widgets.Tree |
listbox | org.eclipse.swt.widgets.Table |
<input type="text" id="MyId" style="color:red;background-color:yellow;" />
var input = document.getElementById('MyId'); input.style.color='red'; input.style.backgroundColor='yellow';
Text text = new Text(parent, SWT.NONE); text.setText("bla bla bla"); text.setData("style", "color:red;background-color:yellow;"); ... engine.applyStyles(shell, true);
Text text = new Text(parent, SWT.NONE); text.setText("bla bla bla"); CSSStylableElement stylableElement = (CSSStylableElement) engine.getElement(text); CSS2Properties style = stylableElement.getStyle(); style.setColor("red"); style.setBackgroundColor("yellow");
This section follow spécification w3c selector and adapt it for the CSS SWT engine.
Pattern | Meaning | Described in section |
---|---|---|
* | Matches any SWT widget. | |
E | Matches any SWT widget E. E is defined with class name of the SWT widget (CSSSWTEngineImpl) or HTML name of the SWT widget (CSSSWTHTMLEngineImpl). | |
E F | Matches any SWT widget SWT F that is a descendant of a SWT widget E (ex : JLabel into JPanel). | Descendant selectors |
E:focus, E:hover | Matches any SWT widget during certain user actions. | The dynamic pseudo-classes |
E[foo="warning"] | Matches any SWT widget E whose "foo" attribute value is exactly equal to "warning".. | Attribute selectors |
E.className | Matches any SWT widget E which have class with className name. | Class selectors |
E#myid | Matches any SWT widget which are id equals to "myid". | ID selectors |
It's possible to use attributes into CSS styles with SWT. It exist several kind of attributes :
Button[style='SWT.CHECK'] { color:red; } Button[style='SWT.RADIO'] { color:green; }
Text[editable=true] { color:red; }
Text text = ... text.setData("foo", "warning");
Text[foo="warning"] { color:red; }
By default class information is managed with setData method of SWT widget :
.redColor { color:red; } .greenColor { color:green; }
Label label = new Label(parent, SWT.NONE); label.setData("class", "redClass");
Label#MyId { color:red; } Label { color:green; }
Label label = new Label(parent, SWT.NONE); label.setData("id", "MyId");
Text:focus { color:red; } Text:hover { color:green; }