Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 100
Registered: ‎01-13-2011
My Device: Not Specified

Can focus checkboxes, button... only on devise

Hello,

 

My english is not so good but I'll try to explain as better as I can.

I have had this difficulty for a year and have not had success yet.

 

This occur only on devise but not on simulator.

 

I am using a Custom Manager to display a table, each cell of the table has a checkbox a "link" and a button.

Whe I try app on simulator it works ok but if I use my devise elements on firts rows cant be focused, selector goes directly to the second or even to the last row.

 

my code is something like this:

 

CustomGridFieldManager grid = new CustomGridFieldManager(null, 1, -1);
VerticalFieldManager columna;
CheckboxField[] cbsSeleccionar = new CheckboxField[10];
RichTextField labelMonto;
ManagerDobleColumna filaMonto;

for (int i = 0; i < 10; i++) {
	labelMonto = new RichTextField("Monto: ");
        cbsSeleccionar[i] = new CheckboxField("Seleccionar", false, Field.FOCUSABLE);
	filaMonto = new ManagerDobleColumna(FIELD_LEFT );
	filaMonto.add(labelMonto);
	filaMonto.add(cbsSeleccionar[i]);
	CustomGridFieldManager filaBtnTop = new CustomGridFieldManager(null, 3, -1);
	labelRegistro = new RichTextField("Registro " );
      Bitmap bitmapTop = Bitmap.getBitmapResource("button-up-off.JPG");
	Bitmap bitmapTopOn = Bitmap.getBitmapResource("button-up-on.JPG");
	BitmapButtonField top = new BitmapButtonField(bitmapTop, bitmapTopOn);
	top.setFont(getFont().derive(Font.PLAIN, 8, Ui.UNITS_pt));
      filaBtnTop.add(new NullField(NON_FOCUSABLE));
      filaBtnTop.add(labelRegistro);
	filaBtnTop.add(top);      
      HorizontalFieldManager x = new HorizontalFieldManager(USE_ALL_WIDTH);
      x.add(filaBtnTop);
      columna = new VerticalFieldManager(Field.FIELD_LEFT);
      columna.add(filaMonto);
	columna.add(x);
      grid.add(columna);
}

 where CustomGridFieldManager is:

 

public class CustomGridFieldManager extends Manager {
	private int[] columnWidths;
	private int columnNumber;
	private int allRowHeight = -1;


	public CustomGridFieldManager(int[] columnWidths, int columNumber, int allRowHeight ) {
		super(Field.FIELD_HCENTER | Manager.VERTICAL_SCROLL);
		this.columnWidths = columnWidths;
		this.allRowHeight = allRowHeight;
		this.columnNumber = columNumber;
	}
	
	public void setColumnWidths(int[] columnWidths) {
		this.columnWidths = columnWidths;
	}


	protected boolean navigationMovement(int dx, int dy, int status, int time) {

		int focusIndex = getFieldWithFocusIndex();
		while(dy > 0) {
			focusIndex += columnNumber;
			if (focusIndex >= getFieldCount()) {
				return false; // Focus moves out of this manager
			}
			else {
				Field f = getField(focusIndex);
				if (f.isFocusable()) { // Only move the focus onto focusable fields
					f.setFocus();
					dy--;
				}
			}
		}
		while(dy < 0) {
			focusIndex -= columnNumber;
			if (focusIndex < 0) {
				return false;
			}
			else {
				Field f = getField(focusIndex);
				if (f.isFocusable()) {
					f.setFocus();
					dy++;
				}
			}
		}

		while(dx > 0) {
			focusIndex ++;
			if (focusIndex >= getFieldCount()) {
				return false;
			}
			else {
				Field f = getField(focusIndex);
				if (f.isFocusable()) {
					f.setFocus();
					dx--;
				}
			}
		}
		while(dx < 0) {
			focusIndex --;
			if (focusIndex < 0) {
				return false;
			}
			else {
				Field f = getField(focusIndex);
				if (f.isFocusable()) {
					f.setFocus();
					dx++;
				}
			}
		}
		return true;
	}
	
	protected void sublayout(int width, int height) {
		int y = 0;
		if (columnWidths == null) {
			columnWidths = new int[columnNumber];
			for(int i = 0; i < columnNumber; i++) {
				columnWidths[i] = width/columnNumber;
			}
		}
		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();
			}
			currentColumn++;
			if (currentColumn == columnWidths.length || i == getFieldCount()-1) {
				int x = 0;
				if (this.allRowHeight >= 0) {
					rowHeight = this.allRowHeight;
				}
				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();
					}
					else if (fieldHalign == Field.FIELD_HCENTER) {
						fieldXOffset = (columnWidths[c]-fields[c].getWidth())/2;
					}
					int fieldYOffset = 0;
					long fieldValign = fieldStyle & Field.FIELD_VALIGN_MASK;
					if (fieldValign == Field.FIELD_BOTTOM) {
						fieldYOffset = rowHeight - fields[c].getHeight();
					}
					else if (fieldValign == Field.FIELD_VCENTER) {
						fieldYOffset = (rowHeight-fields[c].getHeight())/2;
					}

					setPositionChild(fields[c], x+fieldXOffset, y + fieldYOffset);
					x += columnWidths[c];
				}
				currentColumn = 0;
				y += rowHeight;
			}
			if (y >= height) {
				break;
			}
		}
		int totalWidth = 0;
		for(int i = 0; i < columnWidths.length; i++) {
			totalWidth += columnWidths[i];
		}
		setExtent(totalWidth, Math.min(y, height));
	}
	
	
	public void paint(Graphics g) {
		
		int y = 0;
		if (columnWidths == null) {
			columnWidths = new int[columnNumber];
			for(int i = 0; i < columnNumber; i++) {
				columnWidths[i] = getWidth()/columnNumber;
			}
		}
		Field[] fields = new Field[columnWidths.length];
		int currentColumn = 0;
		int rowHeight = 0;
		int row = 0;
		LabelField etiqueta;
		String texto;
		int posicionX;
		int posicionY;
		for(int i = 0; i < getFieldCount(); i++) {
			
			fields[currentColumn] = getField(i);
			
			if (fields[currentColumn].getHeight() > rowHeight) {
				rowHeight = fields[currentColumn].getHeight();
			}
			currentColumn++;
			if (currentColumn == columnWidths.length){
				row++;
			}
			if (currentColumn == columnWidths.length || i == getFieldCount()-1) {
				int x = 0;
				if (this.allRowHeight >= 0) {
					rowHeight = this.allRowHeight;
				}
				for(int c = 0; c < currentColumn; c++) {
					if(row%2 == 0){
						//g.setColor(0xa3c0dc);
						g.setColor(0xe4e4e4);
					}
					else{
						//g.setColor(0xd9dadb);
						g.setColor(0xf9f9f9);
					}
					g.fillRect(x, y, columnWidths[c], rowHeight);
					g.setColor(0x000000);
					//g.drawRect(x, y, columnWidths[c], rowHeight);
					
					x += columnWidths[c];					
				}				
				currentColumn = 0;
				y += rowHeight;
			}
			
			if (y >= getHeight()) {
				break;
			}
			
		}	
		super.paint(g);
	}

}

 ManagerDobleColumna is:

 

public class ManagerDobleColumna extends Manager {
	int columnWidth = -1;
	public ManagerDobleColumna(long style){
		super(style);
	}
	
	public ManagerDobleColumna(long style, int columnWidth){
		super(style);
		this.columnWidth = columnWidth;
	}

	protected void sublayout(int width, int height) {
		int y = 0;
		int[] columnWidths = new int[2];
		if(columnWidth > 0){
			columnWidths[0] = columnWidth;
		}
		else{
			columnWidths[0] = width / 2;
		}
		columnWidths[1] = width - columnWidths[1];
		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();
			}
			currentColumn++;
			if (currentColumn == columnWidths.length || i == getFieldCount()-1) {
				int x = 0;
				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();
					}
					else if (fieldHalign == Field.FIELD_HCENTER) {
						fieldXOffset = (columnWidths[c]-fields[c].getWidth())/2;
					}

					int fieldYOffset = 0;
					long fieldValign = fieldStyle & Field.FIELD_VALIGN_MASK;
					if (fieldValign == Field.FIELD_BOTTOM) {
						fieldYOffset = rowHeight - fields[c].getHeight();
					}
					else if (fieldValign == Field.FIELD_VCENTER) {
						fieldYOffset = (rowHeight-fields[c].getHeight())/2;
					}

					setPositionChild(fields[c], x+fieldXOffset, y + fieldYOffset);
					x += columnWidths[c];
				}
				currentColumn = 0;
				y += rowHeight;
			}
			if (y >= height) {
				break;
			}
		}
		int totalWidth = 0;
		for(int i = 0; i < columnWidths.length; i++) {
			totalWidth += columnWidths[i];
		}
		setExtent(totalWidth, Math.min(y, height));			
	}

}

 and BitmapButtonField:

 

public class BitmapButtonField extends BaseButtonField
{
    private Bitmap[] _bitmaps;
    private static final int NORMAL = 0;
    private static final int FOCUS = 1;
    private String textTooltip;

    
    public BitmapButtonField( Bitmap normalState )
    {        
        this( normalState, normalState, 0 );
    }
    
    public BitmapButtonField( Bitmap normalState, Bitmap focusState )
    {        
        this( normalState, focusState, 0 );
    }
    
    public BitmapButtonField( Bitmap normalState, Bitmap focusState, String tooltip )
    {        
        this( normalState, focusState );
        this.textTooltip = tooltip;
        
    }
    
    public BitmapButtonField( Bitmap normalState, Bitmap focusState, long style )
    {        
        super( Field.FOCUSABLE | style );
        
        if( (normalState.getWidth() != focusState.getWidth())
            || (normalState.getHeight() != focusState.getHeight()) ){
            
            throw new IllegalArgumentException( "Image sizes don't match" );
        }
        
        _bitmaps = new Bitmap[] { normalState, focusState };
    }
    
    public void setImage( Bitmap normalState ){
        _bitmaps[NORMAL] = normalState;
        invalidate();
    }
    
    public void setFocusImage( Bitmap focusState ){
        _bitmaps[FOCUS] = focusState;
        invalidate();
    }
    
    public int getPreferredWidth() {
    	return _bitmaps[NORMAL].getWidth();
    	/*int tooltipWidth = Font.getDefault().derive(Font.PLAIN, 5, Ui.UNITS_pt).getAdvance(textTooltip);
    	int thisWidth = Math.max(tooltipWidth, _bitmaps[NORMAL].getWidth());
    	return thisWidth;*/
    }
    
    public int getPreferredHeight() {
        return _bitmaps[NORMAL].getHeight();
    	/*int fontHeight = Font.getDefault().derive(Font.PLAIN, 5, Ui.UNITS_pt).getHeight() + 2;
    	return _bitmaps[NORMAL].getHeight() + fontHeight;*/
    }
    
    protected void layout( int width, int height ) {
        setExtent( _bitmaps[NORMAL].getWidth(), _bitmaps[NORMAL].getHeight() );
    	/*int fontHeight = Font.getDefault().derive(Font.PLAIN, 6, Ui.UNITS_pt).getHeight() + 2;
    	int tooltipWidth = Font.getDefault().derive(Font.PLAIN, 5, Ui.UNITS_pt).getAdvance(textTooltip);
    	int thisWidth = Math.max(tooltipWidth, _bitmaps[NORMAL].getWidth());
        setExtent( thisWidth, _bitmaps[NORMAL].getHeight()+ fontHeight );*/
    }
    
    protected void paint( Graphics g ) {
    	int index = g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS ) ? FOCUS : NORMAL;
    	g.drawBitmap( 0, 0, _bitmaps[index].getWidth(), _bitmaps[index].getHeight(), _bitmaps[index], 0, 0 );
    	/*int tooltipWidth = Font.getDefault().derive(Font.PLAIN, 5, Ui.UNITS_pt).getAdvance(textTooltip);
    	int thisWidth = Math.max(tooltipWidth, _bitmaps[NORMAL].getWidth());
    	int tooltipHeight = Font.getDefault().derive(Font.PLAIN, 5, Ui.UNITS_pt).getHeight();
    	g.drawBitmap( (thisWidth - _bitmaps[index].getWidth())/2, 0, _bitmaps[index].getWidth(), _bitmaps[index].getHeight(), _bitmaps[index], 0, 0 );
    	
    	if (textTooltip != null ) {
    		if(index == 1){
    			g.setColor(0xFFFFFF);
    	    	g.fillRect(0, _bitmaps[index].getHeight(), tooltipWidth, tooltipHeight);
    	    	g.setFont(Font.getDefault().derive(Font.PLAIN, 5, Ui.UNITS_pt));
    	    	g.setColor(0x000000);
    	    	g.drawText(textTooltip, 1, _bitmaps[index].getHeight());
    		}
    		else{
    			g.setColor(0x00152E);
    	    	g.fillRect(0, _bitmaps[index].getHeight(), tooltipWidth, tooltipHeight);
    		}
    	}*/
    }
    
    /**
     * With this commented out the default focus will show through
     * If an app doesn't want focus colours then it should override this and do nothing
     **/
    /*
    protected void paintBackground( Graphics g ) {
        // Nothing to do here
    }
    */
    
    protected void drawFocus( Graphics g, boolean on ) {
        // Paint() handles it all
        g.setDrawingStyle( Graphics.DRAWSTYLE_FOCUS, true );
        paintBackground( g );
        paint( g );
    }

	public String getTextTooltip() {
		return textTooltip;
	}

	public void setTextTooltip(String textTooltip) {
		this.textTooltip = textTooltip;
	}
    
}