Style individual cells in CellTable using CSSResources

911 views Asked by At

Basically I want to implement something similar to the the cell-coloring which is defined in the GWT documentation

However I don't want to specify the style directly on the DIV element but want to assign an obfuscated stylename from my custom CSSResource which I defined for my CellTable.

Here is some code:

I defined a custom Resources interface for my CellTable:

public interface CellTableResources extends Resources {

    @Source({CellTable.Style.DEFAULT_CSS,CellTableStyle.STYLE})
    CellTableStyle cellTableStyle();

    public interface CellTableStyle extends Style {
        String STYLE = "CellTable.css";

        public Sring coloredCell();
    }
}

I pass it to the constructor of my CellTable:

CellTable<XY> table = new CellTable<XY>(15,cellTableResources);

This is how my custom cell looks like.

public class ColorCell extends AbstractCell<String> {

    interface Templates extends SafeHtmlTemplates {

      @SafeHtmlTemplates.Template("<div class=\"{0}\">{1}</div>")
      SafeHtml cell(String classname, SafeHtml value);
    }
    private static Templates templates = GWT.create(Templates.class);

    @Override
    public void render(Context context, String value, SafeHtmlBuilder sb) {
      if (value == null) {
        return;
      }
      // how can I access the CSSResources which I pass to the CellTable
      CellTableResources ressources = ?
      String className = ressources.cellTableStyle().coloredCell();

      SafeHtml safeValue = SafeHtmlUtils.fromString(value);
      SafeHtml rendered = templates.cell(className, safeValue);
      sb.append(rendered);
    }
  }

How can I access my CellTableRessources that I passed to my CellTable in my custom cell? Here is the important part:

// how can I access the CSSResources which I pass to the CellTable
CellTableResources ressources = ?
String className = ressources.cellTableStyle().coloredCell();

The only solution I come up with is to pass the CellTableRessources to the constructor of my AbstractCell. Isn't there a more elegant way (I already have passed it to the CellTable).

I think the main question is:
"How can I access CellTable variables from a Cell or Column?"

1

There are 1 answers

2
Colin Alworth On BEST ANSWER

The problem with a 'more elegant way' is that it implies that CellTable's own styles will be useful elsewhere, which they probably won't. Even if they provided a getter for style, that would return an instance of type Style, which you would then have to cast to your own style.

It is best to consider this to be your style, which presents a few options:

  • Keep a reference around so you can access it from within your cell
  • GWT.create a new copy of the client bundle within your cell and call ensureInjected() - it will only actually inject it once, so this really isn't a problem, just a good practice, esp if someone decides to use your cell without the style on the table itself.
  • And last, break out the styles needed for the cell into their own clientbundle/cssresource, and make them part of the cell itself. This lets you completely break apart the dependency of the cell on even being put in a celltable (as opposed to a celllist or cellbrowser, etc).

The only tricky part is if the styles on the cell do depend on the styles in the table, in which case this annoying dependency you are dealing with is a good thing - it is requiring you to be aware of that dependency in the styles themselves. If you go for the third (I see this as the cleanest) option but still have this dependency, you can go a step further - declare a style/clientbundle in your cell, but extend it like you are doing to the CellTable's ClientBundle - and since these are interfaces, you can make one bundle that extends both of these, and is supplied to each table and cell.