Jump to: navigation, search

E4/CSS/Add Selector

< E4‎ | CSS

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


- Create a class in org.eclipse.e4.ui.css.swt.selectors, and name it "DynamicPseudoClassesSWTxxxxHandler" where "xxxx" is the name of the selector


- Make this new class extend "AbstractDynamicPseudoClassesControlHandler"

     public class DynamicPseudoClassesSWTActiveHandler extends AbstractDynamicPseudoClassesControlHandler


- Within the class, create a IDynamicPseudoClassesHandler and set it equal to an instance of DynamicPseudoClassesSWTxxxxHandler

     public static final IDynamicPseudoClassesHandler INSTANCE = new DynamicPseudoClassesSWTxxxxHandler();


- Add the following two methods:

     protected void intialize(final Control control, final CSSEngine engine) {}
     protected void dispose(Control control, CSSEngine engine) {}
     Note: method name is intilize is not initialize


- In the intialize method, add the code needed (most likely listeners to look for change of state). For an example, see org.eclipse.e4.ui.css.swt.selectors.DynamicPseudoClassesSWTActiveHandler


- Make use of the setData() method on the widget (to get information about the widget in another class), as well as applying the styles to the engine. For example, in a listener's method, you can do the following:

     try {
          control.setData("Some Qualified String", Boolean.TRUE);
          engine.applyStyles(control, false, true);
     } catch (Exception ex) {
          engine.handleExceptions(ex);
     }

    

- It is preferable to use a qualified string, and to keep it in org.eclipse.e4.ui.css.swt.CSSSWTConstants


- In the dispose method, get rid of all listeners that were created in the above intialize method


- In org.eclipse.e4.ui.css.swt.dom.SWTElement#isPseudoInstanceOf add the new selector with the use of an "if" statement

     if ("xxxx".equals(s)) {}


- Within the if statement, return the appropriate boolean value based on the setData() you used in your listener, and add any other conditional statements that may be necessary

     if ("xxxx".equals(s)) {
         Widget widget = getNativeWidget();
         if (widget != null) {
            return widget.getData("Some Qualified String") == null;
         }
     }


- Now, we must go in org.eclipse.e4.ui.css.swt.engine.AbstractCSSSWTEngineImpl , and register the name of the selector and the instance of the above class just created:

     super.registerDynamicPseudoClassHandler("xxxx", DynamicPseudoClassesSWTxxxxHandler.INSTANCE);