Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "E4/CSS/Add Styleable Property"

< E4‎ | CSS
(Updated some of the details, but push people to use the extension point instead — we shouldn't be encouraging modifying the actual source code.)
Line 1: Line 1:
 
This wiki is a "how-to" that will explain the steps needed to add a styleable CSS property in E4.
 
This wiki is a "how-to" that will explain the steps needed to add a styleable CSS property in E4.
  
 +
{{warning|An easier approach than what is described here is to simply provide an extension to the
 +
<tt>org.eclipse.e4.ui.css.core.propertyHandler</tt> extension point.}}
  
 +
== Create the Property Handler Class ==
  
- Create a class in org.eclipse.e4.ui.css.swt.properties.custom and name it "CSSPropertyXXXXSWTHandler", where XXXX is the name of the property
+
Create a class in org.eclipse.e4.ui.css.swt.properties.custom and name it "CSSPropertyXXXXSWTHandler", where XXXX is the name of the property
  
  
 
- Make this new class extend "AbstractCSSPropertySWTHandler"
 
- Make this new class extend "AbstractCSSPropertySWTHandler"
 
     public class CSSPropertyXXXXSWTHandler extends AbstractCSSPropertySWTHandler
 
     public class CSSPropertyXXXXSWTHandler extends AbstractCSSPropertySWTHandler
 
 
- Within the class, create a ICSSPropertyHandler and set it equal to an instance of CSSPropertyXXXXSWTHandler
 
      public static final ICSSPropertyHandler INSTANCE = new CSSPropertyXXXXSWTHandler();
 
  
  
Line 19: Line 18:
 
       public String retrieveCSSProperty(Control control, String property, String pseudo, CSSEngine engine) throws Exception {}
 
       public String retrieveCSSProperty(Control control, String property, String pseudo, CSSEngine engine) throws Exception {}
  
 +
== Implement Property Setting ==
  
- Within the applyCSSProperty method, write the code needed to apply the wanted styles. For example, for the border-visible property (CTabFolder) we write the following:
+
Within the applyCSSProperty method, write the code needed to apply the wanted styles. For example, for the border-visible property (CTabFolder) we write the following:
 
       public void applyCSSProperty(Control control, String property, CSSValue value, String pseudo, CSSEngine engine) throws Exception {
 
       public void applyCSSProperty(Control control, String property, CSSValue value, String pseudo, CSSEngine engine) throws Exception {
 
             boolean isBorderVisible = (Boolean)engine.convert(value, Boolean.class, null);
 
             boolean isBorderVisible = (Boolean)engine.convert(value, Boolean.class, null);
Line 29: Line 29:
 
       }
 
       }
  
- Within the retrieveCSSProperty method, write the code needed to retrieve the value of the applied property. Again, for border-visible, we write the following:
+
== Implement Property Retrieval ==
 +
 
 +
Within the retrieveCSSProperty method, write the code needed to retrieve the value of the applied property. Again, for border-visible, we write the following:
 
       public String retrieveCSSProperty(Control control, String property, String pseudo, CSSEngine engine) throws Exception {
 
       public String retrieveCSSProperty(Control control, String property, String pseudo, CSSEngine engine) throws Exception {
 
             if (control instanceof CTabFolder) {
 
             if (control instanceof CTabFolder) {
Line 37: Line 39:
 
       }
 
       }
  
- Now, we must go in org.eclipse.e4.ui.css.swt.engine.CSSSWTEngineImpl#initializeCSSPropertyHandlers , and register the name of the property and the above class just created. We must also register the CSSPropertyHandler created in the class:
+
== Expose the Property ==
 +
 
 +
Finally we must wire in the property handler to the CSS Engine.  There are two approaches:
 +
 
 +
* Configure the property using the <tt>org.eclipse.e4.ui.css.core.propertyHandler</tt> extension point.
 +
* Explicitly configure the CSS engine instance in the <tt>initializeCSSPropertyHandlers()</tt> method.  In this case, we must first register the name of the property and the above class just created. We must also register the CSSPropertyHandler created in the class:
 
       super.registerCSSProperty("xxxx", CSSPropertyXXXXSWTHandler.class);
 
       super.registerCSSProperty("xxxx", CSSPropertyXXXXSWTHandler.class);
       super.registerCSSPropertyHandler(CSSPropertyXXXXSWTHandler.class, CSSPropertyXXXXSWTHandler.INSTANCE);
+
       super.registerCSSPropertyHandler(CSSPropertyXXXXSWTHandler.class, new CSSPropertyXXXXSWTHandler());

Revision as of 14:50, 27 July 2015

This wiki is a "how-to" that will explain the steps needed to add a styleable CSS property in E4.

Warning2.png
An easier approach than what is described here is to simply provide an extension to the org.eclipse.e4.ui.css.core.propertyHandler extension point.


Create the Property Handler Class

Create a class in org.eclipse.e4.ui.css.swt.properties.custom and name it "CSSPropertyXXXXSWTHandler", where XXXX is the name of the property


- Make this new class extend "AbstractCSSPropertySWTHandler"

    public class CSSPropertyXXXXSWTHandler extends AbstractCSSPropertySWTHandler


- Add the following two methods:

     public void applyCSSProperty(Control control, String property, CSSValue value, String pseudo, CSSEngine engine) throws Exception {}
     public String retrieveCSSProperty(Control control, String property, String pseudo, CSSEngine engine) throws Exception {}

Implement Property Setting

Within the applyCSSProperty method, write the code needed to apply the wanted styles. For example, for the border-visible property (CTabFolder) we write the following:

     public void applyCSSProperty(Control control, String property, CSSValue value, String pseudo, CSSEngine engine) throws Exception {
            boolean isBorderVisible = (Boolean)engine.convert(value, Boolean.class, null);
            if (control instanceof CTabFolder) {
                CTabFolder folder = (CTabFolder) control;
                folder.setBorderVisible(isBorderVisible);
            }
     }

Implement Property Retrieval

Within the retrieveCSSProperty method, write the code needed to retrieve the value of the applied property. Again, for border-visible, we write the following:

     public String retrieveCSSProperty(Control control, String property, String pseudo, CSSEngine engine) throws Exception {
            if (control instanceof CTabFolder) {
                CTabFolder folder = (CTabFolder) control;
                return Boolean.toString( folder.getBorderVisible() );
            }
     }

Expose the Property

Finally we must wire in the property handler to the CSS Engine. There are two approaches:

  • Configure the property using the org.eclipse.e4.ui.css.core.propertyHandler extension point.
  • Explicitly configure the CSS engine instance in the initializeCSSPropertyHandlers() method. In this case, we must first register the name of the property and the above class just created. We must also register the CSSPropertyHandler created in the class:
     super.registerCSSProperty("xxxx", CSSPropertyXXXXSWTHandler.class);
     super.registerCSSPropertyHandler(CSSPropertyXXXXSWTHandler.class, new CSSPropertyXXXXSWTHandler());

Copyright © Eclipse Foundation, Inc. All Rights Reserved.