Time for action – adding themes

Eclipse 4 ships with a theme manager which can be used to swap between themes (in essence, separate CSS files). The theme manager is available for inclusion in Eclipse 4-based applications by adding the org.eclipse.e4.ui.css.swt plug-in as a dependency to the application, and by adding one or more org.eclipse.e4.ui.css.swt.theme extension points.

  1. Add the org.eclipse.e4.ui.css.swt.theme plug-in to the Dependencies tab of the plugin.xml file of the com.packtpub.e4.application project.
  2. Go to the plugin.xml tab of the plugin.xml file (or edit the file as text) and add the following content to add blue and green themes:
    <extension point="org.eclipse.e4.ui.css.swt.theme">
      <theme label="Blue Theme"
        id="com.packtpub.e4.ui.css.theme.blue"
        basestylesheeturi="css/blue.css"/>
      <theme label="Green Theme"
        id="com.packtpub.e4.ui.css.theme.green"
        basestylesheeturi="css/green.css"/>
    </extension>
  3. Create a text file with the name blue.css in the css folder of the project with the following content:
    .MPart {
      background-color: blue;
    }
  4. Create another text file green.css in the css folder, substituting the background color for green instead of blue.

    Tip

    Ensure that the css folder is present in the build.properties file if it isn't added already, so that all the stylesheets are available in the running application.

  5. In the plugin.xml file, comment or remove the applicationCSS property from the product extension and replace it with a property cssTheme with the ID of the initial theme:
    <property name="cssTheme"
     value="com.packtpub.e4.ui.css.theme.blue"/>
    <!-- 
     property name="applicationCSS"
     value="platform:/plugin/com.packtpub.e4.application/css/default.css"
     -->
  6. The extensions tab will look like this:
    Time for action – adding themes
  7. Run the application and the default theme should be displayed. Test that it works by changing the cssTheme to point to the green theme (com.packtpub.e4.ui.css.theme.green) instead.

What just happened?

Two different CSS files were added to the product, and defined as themes, each with their own label and ID. Each theme can have its own definitions, and the themes can use @import (describe in earlier sections of this chapter) to include the content of another style file.

Instead of passing a CSS to the product when it launches, the theme identifier can be used. This permits the style to be refactored or the CSS file to be renamed at a later stage without affecting any existing products. In addition, it is possible to use the themes to dynamically switch the style of the runtime, as is shown in the next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.67.26