Back to top

vogella training Training Books

Eclipse 4 CSS Styling- Tutorial

Based on Eclipse 4.2 and Eclipse 4.3

Lars Vogel

Version 6.1

10.04.2013

Revision History
Revision 0.1 14.02.2009 Lars
Vogel
created
Revision 0.2 - 6.1 16.02.2009 - 10.04.2013 Lars
Vogel
bug fixes and enhancements

Eclipse 4 and CSS Styling

This tutorial gives an introduction into the CSS styling capabilities of Eclipse 4.


Table of Contents

1. Eclipse and declarative styling
1.1. Styling Eclipse applications
1.2. Static vs. dynamic styling
1.3. Limitations
2. Fixed styling via the applicationCSS parameter
3. Using the Theme Manager
4. Styling Possibilities
4.1. CSS Attributes
4.2. Styling based on ids and classes
4.3. Colors and Gradients
4.4. CSS support for custom widgets
5. CSS Tools
5.1. CSS Spy
5.2. CSS Scratchpad
6. Tutorial: Styling an application
6.1. Create CSS file
6.2. Define applicationCSS property
7. Tutorial: Using the Theme Manager
7.1. Prepare project
7.2. Remove the applicationCSS property
7.3. Create theme extensions
7.4. Implement a new menu entry
8. Thank you
9. Questions and Discussion
10. Links and Literature
10.1. Source Code
10.2. CSS Styling
10.3. vogella Resources

1. Eclipse and declarative styling

1.1. Styling Eclipse applications

You can configure the visual appearance of your Eclipse RCP application. For example you can set the background color of your parts or the text size used for Label widgets. This configuration can be via a CSS file.

In this CSS file you use selectors and style rules. Selectors are identifiers, which relate to widgets or other elements, for example predefined pseudo classes.

The styling is similar to the CSS 2.1 standard. Non-standard properties are prefixed with either swt- or eclipse-.

The following code shows a CSS file which defines an example styling for an Eclipse application.

Label {
  font: Verdana 8px;
  color: black;
}

Composite Label {
  color: black;
}

Text {
  font: Verdana 8px;
}

/* Identifies only those SWT Text elements 
   appearing within a Composite */

Composite Text {
  background-color: white;  
  color: black;
}

SashForm {
  background-color: #c1d5ef;
}


/* background uses a gradient */

.MTrimBar {

  background-color: #e3efff #c1d5ef;
  color: white;
  font: Verdana 8px;
}

Shell {
    background-color: #e3efff #c1d5ef 60%;
}

CTabItem, ToolBar, Button, CBanner, CoolBar {
  font-size: 9;
  background-color: #e3efff;
} 

1.2. Static vs. dynamic styling

To define the styling for your application, you have two options:

  • static styling: Using the a property on the product extension in the plugin.xml file

  • Dynamic or fixed styling: The Theme Manager allows you to register themes and supports switching styles at runtime

1.3. Limitations

SWT currently has some limitations for styling. For example it is not possible to style menus and table headers. In addition some platforms do not allow the styling of certain widgets, e.g. the Button or the ScrollBar widget.

2. Fixed styling via the applicationCSS parameter

You can specify a fixed styling of your application via the org.eclipse.core.runtime.products extension point in the plugin.xml file. For this you set the applicationCSS property to the CSS file.

The value of the applicationCSS property should point to your CSS file via an URI. The URI follows the platform:/plugin/BundleSymbolicName/path/file convention. For example:

platform:/plugin/com.example.e4.rcp.todo/css/default.css 

The following listing shows an example for the plugin.xml file which the applicationCSS property defined.

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>

 <extension
   id="product"
   point="org.eclipse.core.runtime.products">
  <product
     application="org.eclipse.e4.ui.workbench.swt.E4Application"
      name="to-do">
   <property
       name="applicationXMI"
       value="com.example.e4.rcp.todo/Application.e4xmi">
   </property>
   <property
       name="appName"
       value="to-do">
   </property>
   <property
     name="applicationCSS"
     value="platform:/plugin/com.example.e4.rcp.todo/css/default.css">
   </property>
  </product>
 </extension>

</plugin> 

3. Using the Theme Manager

The Theme Manager allows you to define several themes which define different styles for the application and to change these themes at runtime.

The org.eclipse.e4.ui.css.swt.theme extension point supports the creation of extensions for themes. This extension defines an ID for the style and a pointer to the CSS file.

You can also define the default theme via the cssTheme property in your org.eclipse.core.runtime.products extension. This can also be used to define a fixed styling.

To switch the styling you use the IThemeEngine.

package com.example.e4.rcp.todo.handlers;

import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.e4.ui.css.swt.theme.IThemeEngine;

public class ThemeSwitchHandler {
  // Remember the state
  static boolean defaulttheme= true;
  @Execute
  public void switchTheme(IThemeEngine engine) {
    System.out.println("ThemeSwitchHandler called");
    // The last argument controls
    // whether the change should be persisted and
    // restored on restart
    if (!defaulttheme) {
      engine.setTheme("com.vogella.e4.todo.defaulttheme", true);
      
    } else {
      engine.setTheme("com.vogella.e4.todo.redtheme", true);
    }
    defaulttheme= !defaulttheme;
  }
} 

4. Styling Possibilities

4.1. CSS Attributes

The CSS attributes for SWT which can be styled are listed under the following link:

http://wiki.eclipse.org/E4/CSS/SWT_Mapping 

Eclipse application model elements, e.g. MPartStack or MPart can also be styled. The CSS selectors are based on the Java classes for example the following:

Table 1. Model elements and CSS selectors

Model Element CSS Selector
MPart .MPart
MPartStack .MPartStack
MPartSashContainer .MPartSashContainer


For example you could hide the minimize and maximize button of a MPartStack via the following CSS rule.

.MPartStack {
  swt-maximize-visible: false;
  swt-minimize-visible: false;
} 

Eclipse supports also certain CSS pseudo classes for example the following.

Table 2. CSS pseudo classes in Eclipse

CSS pseudo classes Description
Button:checked Selects checked buttons
:active For example shell:active selects the active shell\
:selected Allows to style a selected element, e.g. a part in a PartStack.


4.2. Styling based on ids and classes

You can specify tags on widgets in your source code and use these tags in your CSS file to style these widgets. You can specify an ID or a class. In CSS an ID is supposed to be unique across all of the elements in a page (or window in our case) while a class can be assigned to several elements.

The following example shows how to set the ID and the class for specific SWT widgets.

// IStylingEngine is injected
@Inject IStylingEngine engine;

// More code....

Label label = new Label(parent, SWT.NONE);
Text text = new Text(parent, SWT.BORDER);

// Set the ID, must be unique in the same window
engine.setID(label, "MyCSSTagForLabel"); 

// Set the class, can be used several times
engine.setClassname(text, "error"); 

These elements can be addressed in the CSS file via:

#MyCSSTagForLabel{
  color:#blue;
}

.error { 
  border: 1px red; 
} 

4.3. Colors and Gradients

Colors can be defined in different ways, e.g. the color white can be described as #white, rgb(255,255,2555) or #ffffff.

Styling supports gradients for the background color of UI controls. Linear and radial gradients are supported. The definition of gradients is demonstrated in the following listing.

// linear gradient
background-color: gradient linear color_1 [color_2]* [percentage]*

// For example
background-color: gradient linear white black 
background-color: gradient linear white black blue
background-color: gradient linear white black 50%
background-color: gradient linear white black 50% 20%

// radial gradient
background-color: gradient linear color_1 color_2 [percentage]

// For example
background-color: gradient radial white black
background-color: gradient radial white black 50% 

If you use the optional percentage in the definition, then the color change will be done after the defined percentage. The following picture shows the result of different settings.

Different Gradient settings

Please note that the current gradient specification is not compliant with the CSS specification and that it is planned to change this definition (after the Eclipse 4.2 release).

4.4. CSS support for custom widgets

It is also possible to implement CSS support for custom widgets. See the Eclipse 4 CSS Wiki for further details description. This can be found under the following URL.

http://wiki.eclipse.org/Eclipse4/RCP/CSS 

5. CSS Tools

5.1. CSS Spy

CSS spy is a very useful tool to see the possible styling options. CSS spy is part of the e4 tooling project and can be installed from its update site. See the Eclipse 4 installation description for details.

You can open the CSS spy via the Shift+Alt+F5 shortcut.

CSS spy allows you to see the available properties and the current style settings.

The CSS Spy in action

5.2. CSS Scratchpad

The CSS Scratchpad allows to change CSS rules interactively. is also part of the e4 tooling project.

You open it via the Ctrl+Shift+Alt+F6 shortcut.

If you click the Apply button, the entered CSS will be applied at runtime.

6. Tutorial: Styling an application

6.1. Create CSS file

Create a folder called css in your com.example.e4.rcp.todo project and create the following default.css file.

Label {
  font: Verdana 8px;
  color: black;
}

Composite Label {
  color: black;
}

Text {
  font: Verdana 8px;
}

/* Identifies only those SWT Text elements 
   appearing within a Composite */

Composite Text {
  background-color: white;  
  color: black;
}

SashForm {
  background-color: #c1d5ef;
}


/* background uses a gradient */

.MTrimBar {

  background-color: #e3efff #c1d5ef;
  color: white;
  font: Verdana 8px;
}

Shell {
    background-color: #e3efff #c1d5ef 60%;
}

CTabItem, ToolBar, Button, CBanner, CoolBar {
  font-size: 9;
  background-color: #e3efff;
} 

6.2. Define applicationCSS property

Open the plugin.xml file in your com.example.e4.rcp.todo plug-in. Select the Extensions tab.

Selecting the extension

Right-click on your product and select Newproperty.

Adding a property

Add the applicationCSS property to the org.eclipse.core.runtime.products extension. The value of this property should use the following identifier.

platform:/plugin/com.example.e4.rcp.todo/css/default.css 

Setting the CSS property

Start your application. The application should be styled.

7. Tutorial: Using the Theme Manager

7.1. Prepare project

The following exercises assumes that you already implemented CSS support for your application based on the last exercise.

You will now use the Theme Manager to introduce the ability to switch the application styling at runtime.

Add the org.eclipse.e4.ui.css.swt.theme as dependency to your todo application plug-in.

Create the following css/red.css file.

CTabItem, ToolBar, Button, CBanner, CoolBar {
  font-size: 9;
  background-color: red;
} 

7.2. Remove the applicationCSS property

Remove the applicationCSS property from your product extension.

Remove applicationCSS

7.3. Create theme extensions

The theme engines uses extension which you can define in your plugin.xml file.

Select the Extensions tab in the editor for the plugin.xml file and create two org.eclipse.e4.ui.css.swt.theme extensions.

You create an extension in the Extensions tab by pressing the Add button.

Add button in extension points

Selecting

Right mouse click on the org.eclipse.e4.ui.css.swt.theme extension to create a new theme. The following screenshots show the extensions which you should create.

The relevant part in the plugin.xml file should look like the following.

<extension
         point="org.eclipse.e4.ui.css.swt.theme">
      <theme
            basestylesheeturi="css/default.css"
            id="com.vogella.e4.todo.defaulttheme"
            label="Default Theme">
      </theme>
      <theme
            basestylesheeturi="css/red.css"
            id="com.vogella.e4.todo.redtheme"
            label="Red Theme">
      </theme>
 </extension> 

After adding the two extensions add the cssTheme property to your product pointing to one of the themes you created.

7.4. Implement a new menu entry

Create a new handler class, which allows you to switch between the themes.

package com.example.e4.rcp.todo.handlers;

import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.e4.ui.css.swt.theme.IThemeEngine;

public class ThemeSwitchHandler {
  // Remember the state
  static boolean defaulttheme= true;
  @Execute
  public void switchTheme(IThemeEngine engine) {
    System.out.println("ThemeSwitchHandler called");
    // The last argument controls
    // whether the change should be persisted and
    // restored on restart
    if (!defaulttheme) {
      engine.setTheme("com.vogella.e4.todo.defaulttheme", true);
      
    } else {
      engine.setTheme("com.vogella.e4.todo.redtheme", true);
    }
    defaulttheme= !defaulttheme;
  }
} 

Define a new command and a handler for switching the style. Assign the above class to the handler.

Add a menu entry to your application model which uses your handler for switching the style.

If you run the application and select your new menu entry, the styling of your application should use the red color similarly to the following screenshot.

Note

Changes in styling may require a restart of your application if your new style does not override all properties of the old style.

8. Thank you

Please help me to support this article:

Flattr this

9. Questions and Discussion

Before posting questions, please see the vogella FAQ. If you have questions or find an error in this article please use the www.vogella.com Google Group. I have created a short list how to create good questions which might also help you.

10. Links and Literature

10.1. Source Code

Source Code of Examples

10.3. vogella Resources

vogella Training Android and Eclipse Training from the vogella team

Android Tutorial Introduction to Android Programming

GWT Tutorial Program in Java and compile to JavaScript and HTML

Eclipse RCP Tutorial Create native applications in Java

JUnit Tutorial Test your application

Git Tutorial Put everything you have under distributed version control system