Search code examples
eclipse-rcprcpe4

RCP4 Window loses its background color when closed and re-opened


I created this user management application and applied some styling to it. All my windows background colors appear fine when viewed for the first time, but if I close them using the cancel button or otherwise and then re-open them the background color is just gone. I thought that it might be due to the way I close the windows setting the to be rendered to false, but that can't be because the buttons and groups still retain their styling. Can anyone tell me why this is happening and how to fix it? If I should post any more code please request so. Thanks!

enter image description hereenter image description here

I assign the styling here:

@PostConstruct
public void postConstruct(Composite parent) 
{
    parent.setData("org.eclipse.e4.ui.css.CssClassName", "ColorGrad");

    RoleController roleController = new RoleController();
    parent.getShell().setBounds(0, 0, 380, 435);

    parent.setLayout(null);
    ......

The cancel button function if indeed it does somehow have something to do with it:

btnCancel = new Button(parent, SWT.NONE);
btnCancel.setBounds(185, 354, 160, 36);
btnCancel.setText("Cancel");
btnCancel.setData("org.eclipse.e4.ui.css.CssClassName", "orange");
btnCancel.addSelectionListener(new SelectionListener( ) {
    public void widgetDefaultSelected(SelectionEvent e) {
    }

    public void widgetSelected(SelectionEvent e) {
        modelService.find("ats_usermanagement_rcp.part.UserAddDialog", application).setToBeRendered(false);
        modelService.find("ats_usermanagement_rcp.dialog.UserAdmin", application).setToBeRendered(false);
    }
});

CSS:

.ColorGrad
{
    swt-background-mode: default;
    background-color: #697d87 #ff4612 #697d87 #697d87 #ff4612 60% 90% 95% 100%  
}

Solution

  • This works for me:

    In the Application.e4xmi I have a Window containing a single Part. Only the Window has 'to be rendered' turned off.

    The handler to show the Window does:

    @Execute
    public void execute(final EModelService modelService, final MApplication app)
    {
      final var window = modelService.find("window-id", app);
    
      window.setToBeRendered(true);
    }
    

    The part class has:

    @PostConstruct
    public void postConstruct(final Composite parent, final EModelService modelService, final MApplication app)
    {
      final var comp = new Composite(parent, SWT.None);
      comp.setLayout(new GridLayout());
    
      comp.setData("org.eclipse.e4.ui.css.CssClassName", "css-class-name");
    
      final var close = new Button(comp, SWT.PUSH);
      close.setText("Close");
      close.addListener(SWT.Selection, event ->
        {
          final var window = modelService.find(("window-id", app);
    
          window.setToBeRendered(false);
        });
    }
    

    This is setting the CSS style on a Composite which is the only child of parent.

    This code is for Java 10 or later (it uses var) it will need small changes for earlier versions of Java.