PDA

View Full Version : Portal - add Portlets without column and auto size columns



f.sauter
3 Sep 2010, 12:30 AM
Hi all,

here is a small class which extends Portal - it can arrange portlets without specifying the column.

add(Portlet portlet) -> forces to add the portlet in the column with the the fewest items.

Also it auto sizes the columns at the beginning. So you can save following code:
setColumnWidth(0, 0.5)
setColumnWidth(1, 0.5)

May it helps.



/**
* This class can arrange portlets without specifying the column.
*
* @author Florian Sauter <f.sauter@synergetic.ag>
*/
public class Portal extends com.extjs.gxt.ui.client.widget.custom.Portal {

private Integer maxColumns;

public Portal(int numColumns) {
super(numColumns);
this.maxColumns = numColumns;
autoSizeColumns();
}

public void autoSizeColumns() {
double columnWidth = 100.0 / (double) maxColumns / 100.0;
for (int i = 0; i < maxColumns; i++) {
setColumnWidth(i, columnWidth);
}
}

public void add(Portlet portlet) {
int firstFreeColumnIndex = getFirstFreeColumnlndex();
super.add(portlet, firstFreeColumnIndex);
}

public void remove(Portlet portlet) {
super.remove(portlet, getPortletIndex(portlet));
}

private int getFirstFreeColumnIndex() {
int firstFreeCoumnlId = 0;
for (int x = 1; x < maxColumns; x++) {
if ( getItemCount(x) < getItemCount(firstFreeCoumnlId) ) {
firstFreeCoumnlId = x;
}
}

return firstFreeCoumnlId;
}

/**
* Returns the column container item count.
*
* @param column
* the column index
* @return the column container item count. If no column is found 0.
*/
public int getItemCount(int columnIndex) {
int itemCount = 0;
LayoutContainer columnItem = getItem(columnIndex);
if (columnItem != null) {
itemCount = columnItem.getItemCount();
}
return itemCount;
}
}


cheers