我们可以在GWT的同一列上同时使用ClickableTextCell和ButtonCell吗?

时间:2014-07-21 15:07:25

标签: gwt gwtp

好的,我有IndexedColumn

public class IndexedColumn extends Column<List<String>, String>{
    public IndexedColumn(int index){
       super(new ClickableTextCell()); // by default it is ClickableTextCell
    }
}

创建列

IndexedColumn objectColumn=new IndexedColumn(6){
    @Override
    public String getCellStyleNames(Context context, List<String> object) {
        if (object.get(12).equals("button")){
            return getView().getRes().css().gwtCellButton();
        }
        else if(object.get(12).equals("1")){
            return getView().getRes().css().redColor();
        }
        return getView().getRes().css().myClickableTextCellStyle();
    }
}

// CSS

.myClickableTextCellStyleBold{
    font-weight:bold;
}
.myClickableTextCellStyleNormal{
    font-weight:normal !important;
}

.myClickableTextCellStyle, .myClickableTextCellStyleBold, .myClickableTextCellStyleNormal
{
    cursor: pointer;
    cursor: hand;
}
.gwt-Button, .gwtCellButton button, .gwtCellButtonSmall button {
  margin: 0;
  padding: 5px 7px;
  text-decoration: none;
  cursor: pointer;
  cursor: hand;
  font-size:small;
  background: black;
  border:1px solid #bbb;
  border-bottom: 1px solid #a0a0a0;
  border-radius: 3px;
 -moz-border-radius: 3px;
  color: white;

}

运行后,可以选择getView().getRes().css().redColor(),但没有选择getView().getRes().css().gwtCellButton()

是否可以在同一列上同时使用ClickableTextCell和ButtonCell,如果是,那么该怎么做?

1 个答案:

答案 0 :(得分:0)

您必须为两个类(即TextCell和ButtonCell)实现HasCell接口,然后使用CompositeCell将两个(或更多)单元组合在一起。

使用以下类:

public abstract class CustomTextCell<T> extends AbstractCell<String> {

private T object;
private Set<String> set ;

@Override
public Set<String> getConsumedEvents() {
    set = new HashSet<String>();
    set.add("click");
    set.add("keydown");
    return set;
}

public void setObject(T object) {
    this.object = object;

}

@Override
protected void onEnterKeyDown(Context context, Element parent, String value,
        NativeEvent event, ValueUpdater<String> valueUpdater) {
    doAction(value, valueUpdater,object);
}

@Override
public void onBrowserEvent(Context context, Element parent,
        String value, NativeEvent event,
        ValueUpdater<String> valueUpdater) {
    super.onBrowserEvent(context, parent, value, event, valueUpdater);

    if ("click".equals(event.getType())) {
        EventTarget eventTarget = (EventTarget) event.getEventTarget();
        if (parent.getFirstChildElement().isOrHasChild(Element.as(eventTarget))) {
            doAction(value, valueUpdater,object);
        }
    }
}

protected abstract void doAction(String value, ValueUpdater<String> valueUpdater,T object);

以下类实现HasCell接口。

public abstract class ClickableTextCellHasCellImpl<T> implements HasCell<T, String> {

private FieldUpdater<T, String> fieldUpdater;
private T object;
private CustomTextCell<T> cell = new CustomTextCell<T>() {

    @Override
    protected void doAction(String value,
            ValueUpdater<String> valueUpdater, T object) {
        onTextClick(object);
    }

    @Override
    public void render(com.google.gwt.cell.client.Cell.Context context,
            String value, SafeHtmlBuilder sb) {
        sb.appendHtmlConstant("<h4>"+ getValue(object)+"</h4>");            
    }

};

protected abstract void onTextClick(T object);

@Override
public Cell<String> getCell() {
    return cell;
}

@Override
public FieldUpdater<T, String> getFieldUpdater() {
    return fieldUpdater;
}


public void setFieldUpdater(FieldUpdater<T, String> fieldUpdater){
    this.fieldUpdater=fieldUpdater;
}

@Override
public abstract String getValue(T object);

public void setObjectValue(T object){
    this.object = object;
    cell.setObject(object);
}

}

您也可以为Button创建一个类似的类。对于ButtonHasCellImpl,只需将render方法更改为:

@Override
public void render(com.google.gwt.cell.client.Cell.Context context,
        String value, SafeHtmlBuilder sb) {
        sb.appendHtmlConstant("<button type=\"button\">"+ getValue(object)+"</button>");            
}

最后将它们添加到复合单元格中:

CompositeCell compositeCell = new CompositeCell(hasCellImplLst);

    Column<MyObject, String> compColum = new Column<MyObject, String>(compositeCell) {

        @Override
        public String getValue(MyObject object) {
            textCell.setObjectValue(object);
            buttonCell.setObjectValue(object);
            return null;
        }
    };

总结一下,你将不得不使用复合单元,而复合单元又使用HasCell实现列表。