AutoResizeColumnsCommand

Developers
Jose
2012-05-03
2013-05-08
  • Jose
    Jose
    2012-05-03

    Hi,

    What I am trying to achieve here is that when I create a natTable, by default it'll resize the columns to the optimal size so all the data is shown.

    I try to do that by using the following code:

         NatTable natTable = new NatTable(parent, gridLayer, false);
        InitializeAutoResizeColumnsCommand command = new InitializeAutoResizeColumnsCommand(natTable, 7,    configRegistry, new GCFactory(natTable));
    //      natTable.doCommand(command);
            natTable.doCommand(new AutoResizeColumnsCommand(command));
    

    But it is doing nothing. It does nothing if I execute the commented line or if I do not execute the last line. Any hint what I can be missing here?

    Thanks,
    Jose

     
  • Dirk Fauth
    Dirk Fauth
    2012-05-04

    Hi,

    it doesn't work because the initialization of the NatTable and it's columns and sizes isn't finished yet.

    There is some workaround. Set the default column width of your body data layer to 1 and use a TextPainer with the calculate flag set to true. This way the TextPainter will resize the columns to a width where the text can be showed completely. The only implication is that you are not able to resize the column to be smaller than the text showed.
    For longer cell values this may be not the best way, but for smaller cell values it may be a suitable solution.

    Greez,
    Dirk

     
  • Jose
    Jose
    2012-05-04

    Hi Dirk,

    Thanks for your answer. Unfortunately the workaround may not work for us since we'd like to have the possibility of resize columns to be smaller than the text. If I do right click->auto resize, it does work fine, and as far as I could debug, is using the same code that I'm trying to use here. Is there any listener than I can use and would work so once nattable has been packed and ready, do the resizing as if it was the right click->auto resize action?

    Thanks,
    Jose

     
  • Dirk Fauth
    Dirk Fauth
    2012-05-04

    Hi,

    digging a bit deeper into this I've found out that it doesn't work because the client area is not initialized yet so the ViewportLayer can not calculate the correct column position (getColumnIndexes() returns 0 because of this which is why the processing of the command will stop here).

    Maybe you can solve this by adding an IPartListener to your view where the NatTable is added to. I don't know if there is a post render lifecycle event anywhere.

    Greez,
    Dirk

     
  • Jose
    Jose
    2012-05-04

    Hi Dirk,

    I have added a Listener as you suggested (not the IPartListerner though) and now the autoresizing is working fine :)

    One question here: Do you know if the auto-resize also works with different DPI than the default 100% (i.e. 125% 150%)?

    Thanks,
    Jose

     
  • Dirk Fauth
    Dirk Fauth
    2012-05-07

    Hi Jose,

    sounds great. Would you mind sharing your solution with us? I guess there are more people out there that are trying to achieve the same you did. As we are currently building the website under eclipse nebula it could me some good point for the FAQ.

    The auto-resize calculates the width using the GC. I don't know for sure if it takes the DPI into account, but I guess it does.

    Greez,
    Dirk

     
  • Jose
    Jose
    2012-05-07

    Sure :-)

    In the class that creates the nattable I added the following code:
    (note: the boolean was added so if the user resize the column by hand, the table won't get auto resized again)

    table.addListener(SWT.Paint, new Listener(){
                boolean resized = false; 
                @Override
                public void handleEvent(Event arg0) {
                    if (resized) {
                        return;
                    }
                    resized = true;             
                    for (int i=0; i<table.getColumnCount(); i++) {
                        InitializeAutoResizeColumnsCommand columnCommand = new                       InitializeAutoResizeColumnsCommand(
                                table, i, table.getConfigRegistry(), new GCFactory(table));
                        table.doCommand(columnCommand);
                    }
                    for (int i=0; i<table.getRowCount(); i++) {
                        InitializeAutoResizeRowsCommand rowCommand = new InitializeAutoResizeRowsCommand(
                                table, i, table.getConfigRegistry(), new GCFactory(table));
                        table.doCommand(rowCommand);
    
                    }
                }
            });
    

    Thanks,
    Jose