Custom GridField Manager in BlackBerry

Here is my first post. And its about blackberry. Adding the multiple fields in a single manager is a big challenge in blackberry as UI development is little wierd. Here i am posting a sample code to customize your gridfiled manager. Here Fields will added from left to right, one by one. The height of each row will be equal to the height of the tallest Field in that row:

</pre>
public class GridFieldManager extends Manager {

protected int[] columnWidths;
 private int columns;
 private int allRowHeight = -1;

public GridFieldManager(int columns, long style) {
 super(style);
 this.columns = columns;
 } // end GridFieldManager()

public GridFieldManager(int[] columnWidths, long style) {
 super(style);
 this.columnWidths = columnWidths;
 this.columns = columnWidths.length;
 } // end GridFieldManager()

public GridFieldManager(int[] columnWidths, int rowHeight, long style) {
 this(columnWidths, style);
 this.allRowHeight = rowHeight;
 } // end GridFieldManager()

// ~ Methods
 // ....................................................................................

protected boolean navigationMovement(int dx, int dy, int status, int time) {
 int focusIndex = getFieldWithFocusIndex();

while (dy > 0) {
 focusIndex += columns;

if (focusIndex >= getFieldCount()) {
 return false; // Focus moves out of this manager
 } // end if
 else {
 Field f = getField(focusIndex);

if (f.isFocusable()) { // Only move the focus onto focusable
 // fields
 f.setFocus();
 dy--;
 } // end if
 } // end else
 } // end while

while (dy < 0) {
 focusIndex -= columns;

if (focusIndex < 0) {
 return false;
 } // end if
 else {
 Field f = getField(focusIndex);

if (f.isFocusable()) {
 f.setFocus();
 dy++;
 } // end if
 } // end else
 } // end while

while (dx > 0) {
 focusIndex++;

if (focusIndex >= getFieldCount()) {
 return false;
 } // end if
 else {
 Field f = getField(focusIndex);

if (f.isFocusable()) {
 f.setFocus();
 dx--;
 } // end if
 } // end else
 } // end while

while (dx < 0) {
 focusIndex--;

if (focusIndex < 0) {
 return false;
 } // end if
 else {
 Field f = getField(focusIndex);

if (f.isFocusable()) {
 f.setFocus();
 dx++;
 } // end if
 } // end else
 } // end while

return true;
 } // end navigationMovement()

protected void sublayout(int width, int height) {
 {
 Logger.out("GridFieldManager", "Width = " + width + " Height = " + height);
 }
 int y = 0;

if (columnWidths == null) {
 columnWidths = new int[columns];

for (int i = 0; i < columns; i++) {
 columnWidths[i] = width / columns;
 } // end for
 } // end if

Field[] fields = new Field[columnWidths.length];
 int currentColumn = 0;
 int rowHeight = 0;

for (int i = 0; i < getFieldCount(); i++) {
 fields[currentColumn] = getField(i);
 layoutChild(fields[currentColumn], columnWidths[currentColumn], height - y);

if (fields[currentColumn].getHeight() > rowHeight) {
 rowHeight = fields[currentColumn].getHeight() + 10;
 } // end if

currentColumn++;

if ((currentColumn == columnWidths.length) || (i == (getFieldCount() - 1))) {
 int x = 0;

if (this.allRowHeight >= 0) {
 rowHeight = this.allRowHeight;
 } // end if

for (int c = 0; c < currentColumn; c++) {
 long fieldStyle = fields[c].getStyle();
 int fieldXOffset = 0;
 long fieldHalign = fieldStyle & Field.FIELD_HALIGN_MASK;

if (fieldHalign == Field.FIELD_RIGHT) {
 fieldXOffset = columnWidths[c] - fields[c].getWidth();
 } // end if
 else if (fieldHalign == Field.FIELD_HCENTER) {
 fieldXOffset = (columnWidths[c] - fields[c].getWidth()) / 2;
 } // end else if

int fieldYOffset = 0;
 long fieldValign = fieldStyle & Field.FIELD_VALIGN_MASK;

if (fieldValign == Field.FIELD_BOTTOM) {
 fieldYOffset = rowHeight - fields[c].getHeight();
 } // end if
 else if (fieldValign == Field.FIELD_VCENTER) {
 fieldYOffset = (rowHeight - fields[c].getHeight()) / 2;
 } // end else if

setPositionChild(fields[c], x + fieldXOffset, y + fieldYOffset);
 x += columnWidths[c];
 } // end for

currentColumn = 0;
 y += rowHeight;
 } // end if

if (y >= height) {
 break;
 } // end if
 } // end for

int totalWidth = 0;

for (int i = 0; i < columnWidths.length; i++) {
 totalWidth += columnWidths[i];
 } // end for

setExtent(totalWidth, Math.min(y, height));

} // end sublayout()
 } // end GridFieldManager
<pre>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s