Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » scout » Table with dynamic number of columns
Table with dynamic number of columns [message #889307] Tue, 19 June 2012 05:34 Go to next message
Patrick Bänziger is currently offline Patrick BänzigerFriend
Messages: 23
Registered: September 2011
Junior Member
Hello everyone

In my application, I need to create a form describing a set of „changes". Usually those are describe in a „Name-Value" like style.
Until now, I used a table with a single column (String column with HTML) to describe the changes, using multiline-cells. Something like this:



Event name: Birthday party
Location: Grand Central Park
Duration: 180 minutes


Event name: Funeral
Location: Grand Central Cemetery
Duration: 60 minutes




Now the question came up whether we could transform this into a horizontal form (i.e. a table with more than one column).

While one set of changes would have the same number of columns, another set may have a larger (or smaller) number of columns.
Until now, I just had a fixed number of columns or made as many columns as one might need (and just hide the ones that are not necessary).

But in general (or for an unknown number of columns, is it possible in Scout to tell a table at runtime to create N columns which we can then fill?

Thanks!
Re: Table with dynamic number of columns [message #889338 is a reply to message #889307] Tue, 19 June 2012 06:19 Go to previous messageGo to next message
Adrian MoserFriend
Messages: 67
Registered: March 2011
Member
Have a look at AbstractTableCustomizer.
If you override injectCustomColumns(), you can add columns to the list at runtime.

You need to add your TableCustomizer to your table by overriding
public ITableCustomizer getTableCustomizer()


Hope that helps
Adrian

[Updated on: Tue, 19 June 2012 06:23]

Report message to a moderator

Re: Table with dynamic number of columns [message #889381 is a reply to message #889307] Tue, 19 June 2012 07:19 Go to previous messageGo to next message
Ivan Motsch is currently offline Ivan MotschFriend
Messages: 100
Registered: March 2010
Senior Member
This is an example:

import java.util.ArrayList;
import java.util.List;

import org.eclipse.scout.commons.annotations.Order;
import org.eclipse.scout.commons.exception.ProcessingException;
import org.eclipse.scout.rt.client.ui.basic.cell.Cell;
import org.eclipse.scout.rt.client.ui.basic.table.AbstractTable;
import org.eclipse.scout.rt.client.ui.basic.table.HeaderCell;
import org.eclipse.scout.rt.client.ui.basic.table.ITableRow;
import org.eclipse.scout.rt.client.ui.basic.table.columns.AbstractColumn;
import org.eclipse.scout.rt.client.ui.basic.table.columns.AbstractDoubleColumn;
import org.eclipse.scout.rt.client.ui.basic.table.columns.AbstractLongColumn;
import org.eclipse.scout.rt.client.ui.basic.table.columns.IColumn;
import org.eclipse.scout.rt.client.ui.desktop.outline.pages.AbstractPageWithTable;
import org.eclipse.scout.rt.client.ui.desktop.outline.pages.ISearchForm;
import org.eclipse.scout.rt.shared.TEXTS;
import org.eclipse.scout.rt.shared.data.basic.FontSpec;
import org.eclipse.scout.rt.shared.services.common.jdbc.SearchFilter;
import org.eclipse.scout.service.SERVICES;

public class DynamicColumnsTablePage extends AbstractPageWithTable<DynamicColumnsTablePage.Table> {
  private List<IColumn<?>> m_injectedColumns;

  @Override
  protected String getConfiguredIconId() {
    return Icons.TreeNode;
  }

  @Override
  protected boolean getConfiguredLeaf() {
    return true;
  }

  @Override
  protected Class<? extends ISearchForm> getConfiguredSearchForm() {
    return DynamicDataSearchForm.class;
  }

  @Override
  protected boolean getConfiguredSearchRequired() {
    return true;
  }

  @Override
  protected String getConfiguredTitle() {
    return TEXTS.get("DynamicColumns");
  }

  @Override
  protected Object[][] execLoadTableData(SearchFilter filter) throws ProcessingException {
    DynamicDataSearchFormData formData = (DynamicDataSearchFormData) filter.getFormData();
    DynamicDataMatrixData matrixData = SERVICES.getService(IDynamicDataPageService.class).getStatisticsMatrixData(formData);
    if (matrixData == null) {
      return new Object[0][];
    }
    //create dynamic columns based on matrix data assuming matrixData has a property "getColumnSpecs" with  bean ColumnSpec
    
    updateDynamicColumns(matrixData.getColumnSpecs());
    
    //fill rows
    return matrixData.getRows();
  }
  
  private void updateDynamicColumns(ColumnSpec[] columnSpecs) throws ProcessingException {
    if (columnSpecs.length == 0) {
      return;
    }
    Table table = getTable();
    m_injectedColumns = new ArrayList<IColumn<?>>();
    for (ColumnSpec spec: columnSpecs) {
      //XXX switch on spec.getType() or something like that...
      m_injectedColumns.add(createDynamicDoubleColumn(spec.getId(), spec.getText()));
      //...
    }
    table.resetColumnConfiguration();
  }

  private IColumn<?> createDynamicDoubleColumn(final String columnId, final String label) {
    return new AbstractDoubleColumn() {
      @Override
      protected String getConfiguredHeaderText() {
        return label;
      }

      @Override
      public String getColumnId() {
        return columnId;
      }

      @Override
      protected int getConfiguredWidth() {
        return 120;
      }

      @Override
      protected Double execParseValue(ITableRow row, Object rawValue) throws ProcessingException {
        rawValue = preprocessDynamicColumnsCell(this, row, rawValue);
        return super.execParseValue(row, rawValue);
      }
    };
  }

  /**
   * @return {@link DecoratedValue#getValue()} after decorating the table cell
   */
  private Object preprocessDynamicColumnsCell(IColumn<?> col, ITableRow row, Object rawValue) throws ProcessingException {
    DecoratedValue rawCell = (DecoratedValue) rawValue;
    if (rawCell != null) {
      row.getCellForUpdate(col).setBackgroundColor(rawCell.getColor());
      return rawCell.getValue();
    }
    return rawValue;
  }

  @Order(10.0f)
  public class Table extends AbstractTable {

    @Override
    protected boolean getConfiguredSortEnabled() {
      return false;
    }

    @Override
    protected void injectColumnsInternal(List<IColumn<?>> columnList) {
      if (m_injectedColumns != null) {
        columnList.addAll(m_injectedColumns);
      }
    }
  }

}
icon14.gif  Re: Table with dynamic number of columns [message #889480 is a reply to message #889307] Tue, 19 June 2012 09:19 Go to previous message
Patrick Bänziger is currently offline Patrick BänzigerFriend
Messages: 23
Registered: September 2011
Junior Member
Great, thank you both for your speedy replies!
Previous Topic:migrate from 3.7 to 3.8
Next Topic:Provide Service Config Properties from Plug-in
Goto Forum:
  


Current Time: Thu Dec 18 12:40:34 GMT 2014

Powered by FUDForum. Page generated in 0.01863 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software