[JOKE] - The Wedding Test

I was a very happy man. My wonderful girlfriend and I had been dating for over a year, and so we decided to get married. There was only one little thing bothering me.... It was her beautiful younger sister.

My prospective sister-in-law was twenty-two, wore very tight miniskirts, had great legs, and generally was bra-less. She would regularly bend down when she was near me, and I always got more than a nice view.. It had to be deliberate. Because she never did it when she was near anyone else.

One day the '
little' sister called and asked me to come over to check the wedding invitations . She was alone when I arrived, and she whispered to me that she had feelings and desires for me that she couldn't overcome. She told me that she wanted to make love to me just once before I got married and committed my life to her sister.

Well, I was in total shock, and couldn't say a word..

She said, '
I'm going upstairs to my bedroom, and if you want one last wild fling that you will never forget, just come up and get me.'

I was stunned and frozen in shock as I watched her go up the stairs. I stood there for a moment, then turned and made a beeline straight to the front door. I opened the door, and headed straight towards my car.

Lord... And behold, my entire future family was standing outside, all clapping!

With tears in his eyes, my future father-in-law hugged me and said, '
We are very happy that you have passed our little test. We couldn't ask for a better man for our daughter. Welcome to the family.'

And the moral of this story is:
Always keep your condoms in your car!

GWT-Ext automatically resize GridPanel columns

The problem here is that GWT-Ext framework has no API or support for automatically resizing of the GridPanel columns based on the content of the grid cells.

FORTUNATELY THERE IS A SOLUTION ! see it below what we need to do:

1) The resizing function.
2) Preparing the grid.

1. The resize static function
We have to define a static resize function that will work on any GridPanel or EditorGridPanel. Here's the code:
/**
* Automatically adjust Grid column widths. It gets the total of the
* adjustable columns and exclude fixed width columns from the target width.
*/
public static void resizeColumns(final Store gridStore,
final ColumnModel gridColsModel) {
if (gridStore == null || gridColsModel == null) {
return;
}
final BaseColumnConfig[] colCfgs = gridColsModel.getColumnConfigs();
if (colCfgs == null) {
return;
}
final int nbRows = gridStore.getCount();
if (nbRows > 0) {
final Record[] records = gridStore.getRecords();
/* Resize each column according the cell values */
for (int i = 0; i < colCfgs.length; i++) {
if (colCfgs[i] instanceof ColumnConfig) {
final ColumnConfig cfg = (ColumnConfig) colCfgs[i];
int colW = cfg.getHeader().length();
for (int r = 0; r < nbRows; r++) {
final String val = records[r].getAsString(cfg
.getDataIndex());
if (val != null && val.length() > colW) {
colW = val.length();
}
}
/* here's the weak of logic (6px for each char) */
gridColsModel.setColumnWidth(i, colW * 6 + 20);
}
}
}
}

2. Prepare the grid
For having this function called automatically we need to register 2 listeners:
  • one for the grid's store
  • second for the grid's wrapper (parent) panel
NOTES:
  1. Don't set the "auto fill" for the grid's view and don't set the auto-width column.
  2. Another big condtion is to keep the reference to the grid's ColumnModel object. This is used in the resizing function, and we cannot use the "grid.getColumnModel()" function because the rezult of this is a brand new instance, which doesn't have the list of ColumnConfig objects.

2.1 Grid store
When we create the GridPanel and set up its store we can register a StoreListener for this store.

Store gridStore = new Store (...);
....
/* need to preserve this object: gridColumnsModel */
ColumnModel gridColumnsModel = new Columnmodel (...);
....
gridStore .addStoreListener(new StoreListenerAdapter(){
public void onLoad(Store store, Record[] records) {
resizeColumns(store, gridColumnsModel);
}
});


2.2. Grid parent panel
When we add the grid to its parent panel (container) we have to register a "resize listener" for the parent.

grid.getView().setAutoFill(false);
grid.setStore(gridStore);
...
gridWrapperPane.add (grid);


gridWrapperPane.addListener(new ContainerListenerAdapter(){
@Override
public void onResize(BoxComponent component, int adjWidth, int adjHeight, int rawWidth, int rawHeight) {
if (grid != null) {
resizeColumns(grid.getStore(), gridColumnsModel);
}
}
});

DONE! now you don't need to worry about the grid columns size.
Enjoy it!