5

I am trying to create a CompositeCell which consists of a TextCell and a ButtonCell. I want to add the CompositeCell to Column ordinarily and then the Column to a CellTable. However I am not able to figure out how the instance of the column should be. In particular I cannot find its type parameters in the following code:

 Column<FilterInfo, ?> compositeColumn = new Column<FilterInfo, ?>(createCompositeCell()) {

  @Override
  public Object getValue(Object object) {
    // TODO Auto-generated method stub
    return null;
  }}; 

The method which creates the CompositeCell of the custom class FilterInfo (is it necessary?) is:

private CompositeCell<FilterInfo> createCompositeCell(){

HasCell<FilterInfo, String> filterName = new HasCell<FilterInfo, String>() {

  public Cell<String> getCell() {
    return new TextCell();
  }

  public FieldUpdater<FilterInfo, String> getFieldUpdater() {
    // TODO Auto-generated method stub
    return null;
  }

  public String getValue(FilterInfo object) {
    return object.getFilterName();
  }};

  HasCell<FilterInfo, String> filterButton = new HasCell<FilterInfo,String>(){

    public Cell<String> getCell() {
      return new ButtonCell();
    }

    public FieldUpdater<FilterInfo, String> getFieldUpdater() {
      // TODO Auto-generated method stub
      return null;
    }

    public String getValue(FilterInfo object) {
      // TODO Auto-generated method stub
      return "...";
    }
  };

  List<HasCell<FilterInfo, ?>> cells = new ArrayList<HasCell<FilterInfo, ?>>();
  cells.add(filterName);
  cells.add(filterButton);

  CompositeCell<FilterInfo> compositeCell = new CompositeCell<FilterInfo>(cells);

  return compositeCell;

}

I would be grateful by any indication to adapt the code or another suggestion to create the desired CompositeCell and to add it to the CellTable properly.

arjacsoh
  • 8,932
  • 28
  • 106
  • 166

4 Answers4

3

Do you have to use a composite cell? To me this seems like a lot of work, and it may be a lot easier to just create your own custom cell.

Have a read of the documentation on Creating Custom Cells

logan
  • 3,416
  • 1
  • 33
  • 45
2

Do not try to focus too much on the type parameter here. Your IDE is too smart and will give you type error. It will complain "Raw Type. XXX< C> should be parametrized" however you should be able to compile and run code that way.

Here is a code to have a Column with a CompositeCell of N buttons:

private Column<DTO, DTO> getButtonColumn() {
    return new Column<DTO, DTO>(getButtonsCell()) {
        @Override
        public DTO getValue(DTO object) {
            return object;
        }
    };
} 

private CompositeCell getButtonsCell() {
    HasCell<DTO,DTO> button1 = new AbstractActionButton<DTO>() {
                @Override
                public void execute(final DTO object) {
                    //Action on button click
                }

                @Override
                public void render(Context context, DTO data, SafeHtmlBuilder sb) {
                    //
                }
            };
    HasCell<DTO,DTO> button2 = new AbstractActionButton<DTO>(){ 
        //Complete me ...
    }

    List<HasCell<DTO, ?>> cells = new LinkedList<>();
    cells.add(button1);
    cells.add(button2);
    CompositeCell<DTO> compositeCell = new CompositeCell<>(cells);

    return compositeCell;
}


public abstract class AbstractActionButton<DTO> implements HasCell<DTO, DTO> {

@Override
public Cell<DTO> getCell() {
    return new ActionCell<DTO>("Button title", new ActionCell.Delegate<DTO>() {
        @Override
        public void execute(DTO object) {
            AbstractActionButton.this.execute(object);
        }
    }) {
        @Override
        public void render(Context context, DTO data, SafeHtmlBuilder sb) {
            AbstractActionButton.this.render(context, data, sb);
        }
    };
}

//Replaced by delegate but still need to be overriden
@Override
public FieldUpdater<DTO, DTO> getFieldUpdater() {
    return null; 
}

@Override
public DTO getValue(DTO object) {
    return object;
}

/**
 * You can override this method to render your button differently. Not mandatory
 * @param context
 * @param data
 * @param sb
 */
public abstract void render(Context context, DTO data, SafeHtmlBuilder sb);

/**
 * Called when the button is clicked
 * @param object
 */
public abstract void execute(DTO object);
}
Kbii
  • 185
  • 1
  • 3
  • 11
1

If your CellTable takes a list of FilterInfo and your Cell takes a FilterInfo, then use an IdentityColumn.

Thomas Broyer
  • 64,353
  • 7
  • 91
  • 164
0

I created a compositecell (Checkboxcell + TextCell) by using GXT XTemplate to render the text of the text cell. Use Template if you want to stick to pure GWT and also replace other GXT widgets with GWT.

https://gist.github.com/Aadi1/4949994

aadidasu
  • 1,037
  • 4
  • 15
  • 26