0

I need to create dynamic columnas in one TableViewer, in an eclipse RCP Application.

The idea is that I need to create a cross table.

Column 0 | Column 1| Column 2| Column 3| Column n
=================================================
Data 0,0 | Data 0,1| Data 0,2| Data 0,3| Data 0,n
Data 1,0 | Data 1,1| Data 1,2| Data 1,3| Data 1,n
Data 2,0 | Data 2,1| Data 2,2| Data 2,3| Data 2,n
Data 3,0 | Data 3,1| Data 3,2| Data 3,3| Data 3,n

I'm using this to create dynamic columns.

    //Recuperar Crossing Block del Grupo
    for (int i=0; i < cbsCombinado.size(); i++) {
        tableViewerColumn = new TableViewerColumn(this.tableCombinadoViewer, SWT.NONE);
        tblclmnParcela = tableViewerColumn.getColumn();
        tblclmnParcela.setWidth(100);
        tblclmnParcela.setText( cbsCombinado.get(i).getNroParcela().toString());
        tblclmnParcela.setData(cbsCombinado.get(i)); //Asigno el Participante.
    };

So i'm reading records from database an create a columns with each record.

The problem is that I need to retrieve the data that I set in te column. I need to get the Id of column n and row i, to query a Database.

Any idea? Best Regards

Hi, my approach is this right now. create a classwith an ArrayList of columns:

class Grilla {
    private BloqueCruzamiento bc_x;
    private ArrayList<BloqueCruzamiento> bc_list_y;
    private Boolean selected;

}

and the ContentProvider:

class BloqueCruzaCombContentProvider implements IStructuredContentProvider {

    public void dispose() {
        // TODO Auto-generated method stub

    }

    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
        // TODO Auto-generated method stub

    }

    @SuppressWarnings("unchecked")
    public Object[] getElements(Object obj) {
        ArrayList<Grilla> grid = (ArrayList<Grilla>) obj;
        return grid.toArray();
    }

}   

And this is how I populate the ArrayList:

    List<Grilla> cbsArrayL = new  ArrayList<Grilla>() ;
    Grilla grid = new Grilla();

    for (BloqueCruzamiento cb : cbs) {
        grid.bc_x       = cb;
        grid.bc_list_y  = new ArrayList<BloqueCruzamiento>();
        grid.selected   = false;

        for (BloqueCruzamiento ccb : cbsCombinado)
        {
            grid.bc_list_y.add(ccb);
        }
        cbsArrayL.add(grid);
        grid = new Grilla();
    }

    tableCombinadoViewer.setContentProvider(new BloqueCruzaCombContentProvider());
    tableCombinadoViewer.setLabelProvider(new BloqueCruzaCombLabelProvider());
    if (cbs!=null)  tableCombinadoViewer.setInput(cbsArrayL);

My goal now is to make each CELL editable, but under some circumstances the should NOT be editable. Regards Again

Nicolas400
  • 85
  • 15

1 Answers1

0

I assume that you have one java class for each of the table structures that you will be displaying. You can hence use java reflection API to give headings to table viewer columns and display data

Edit: I see that ur code has almost 70% of what needs to be done. I am just repeating the steps with some additional details for the sake of understanding

  1. Define a java class describing the fields in your table. Include getters and setters for the fields. If there are 'n' different tables create 'n' such classes with getters and setters. Say one of the classes is named Employee
  2. An array list of one of the above classes would become the input for the Tableviewer that you use .use something like Tableviewer.setInput(empList)
  3. Now implement your own label provider. Now it is within the Labelprovider's getValue() method you should make use of reflection API to call the getXXXX() of the table field that you wish to show. A good reference would be here. Reflection is nothing but an introspection API provided by Java which gets the details about a class and it's properties.
  4. You can enable editing by creating a new class by sub-classing EditingSupport Class and adding the instance to the columnviewer's addEditingSupport() method

Remember that the tableviewer column headers can be the field names of your Employee Class which can also be got by reflection API .

Hope this clarifies

Community
  • 1
  • 1
ssdimmanuel
  • 448
  • 10
  • 29
  • Thanks, I added more info in the question. May be you can explain more about reflection API, and if it can be used in RCP Aplications. Regards. – Nicolas400 Aug 21 '14 at 12:31