0

基本上我想实现类似于GWT 文档中定义的单元格着色的东西

但是,我不想直接在 DIV 元素上指定样式,而是想从我CSSResource为 CellTable 定义的自定义中分配一个混淆样式名。

这是一些代码:

Resources我为我的 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();
    }
}

我将它传递给我的 CellTable 的构造函数:

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

这就是我的自定义单元格的样子。

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);
    }
  }

如何访问我CellTableRessources在自定义单元格中传递给 CellTable 的内容?这是重要的部分:

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

我想出的唯一解决方案是将 传递CellTableRessources给我的AbstractCell. 没有更优雅的方式吗(我已经将它传递给了 CellTable)。

我认为主要问题是:
“如何从单元格或列访问 CellTable 变量?”

4

1 回答 1

0

“更优雅的方式”的问题在于它暗示 CellTable 自己的样式将在其他地方有用,而它们可能不会。即使他们为样式提供了一个 getter,它也会返回一个 Style 类型的实例,然后您必须将其转换为您自己的样式。

最好将此视为您的风格,它提供了一些选项:

  • 保留参考,以便您可以从您的单元格中访问它
  • GWT.create a new copy of the client bundle in your cell and call ensureInjected() - 它实际上只会注入一次,所以这真的不是问题,只是一个好习惯,特别是如果有人决定使用你的 cell桌子本身的风格。
  • 最后,将单元格所需的样式分解为它们自己的 clientbundle/cssresource,并使它们成为单元格本身的一部分。这使您可以完全分解单元格对甚至放入单元格表的依赖性(与单元格列表或单元格浏览器等相反)。

唯一棘手的部分是单元格上的样式是否确实依赖于表格中的样式,在这种情况下,您正在处理的这种烦人的依赖关系是一件好事——它要求您了解样式本身的依赖关系。如果你选择第三个(我认为这是最干净的)选项但仍然有这种依赖关系,你可以更进一步 - 在你的单元格中声明一个样式/客户端包,但像你对 CellTable 的 ClientBundle 所做的那样扩展它 - 和由于这些是接口,您可以制作一个扩展这两者的捆绑包,并提供给每个表格和单元格。

于 2012-01-13T23:35:05.523 回答