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: 103
Registered: ‎12-12-2011
My Device: Developer
My Carrier: Robosoft

Enable/Disable custom field

Hello all,

               I am developing for a BB news app in which,  for story detail page there are previous and next options to navigate through different story pages. For previous and next the UI element used is a custom field provided by RIM and source is as below:

 

public class BitmapButtonField extends BaseButtonField
{
    private static final int NORMAL = 0;
    private static final int FOCUS = 1;
    
    private Bitmap[] _bitmaps;
    private boolean mClicked = false;
    private boolean isFocusable = true;
    
    public BitmapButtonField( Bitmap normalState )
    {        
        this( normalState, normalState, 0 );
    }
    
    public BitmapButtonField( Bitmap normalState, Bitmap focusState )
    {        
        this( normalState, focusState, 0 );
    }
    
    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();
    }
    
      
    public int getPreferredHeight() {
        return _bitmaps[NORMAL].getHeight();
    }
    
      
    protected void layout( int width, int height ) {
        setExtent( _bitmaps[NORMAL].getWidth(), _bitmaps[NORMAL].getHeight() );
    }
    
      
    protected void paint( Graphics g ) {
        int index = NORMAL; //g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS ) ? FOCUS : NORMAL;
        if(mClicked || g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS))
            index = FOCUS;
        else
            index = NORMAL;
        g.drawBitmap( 0, 0, _bitmaps[index].getWidth(), _bitmaps[index].getHeight(), _bitmaps[index], 0, 0 );
    }
    
    /**
     * 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 );
    }

    protected boolean navigationClick(int status, int time) {
        return super.navigationClick(status, time);
    }

    public void repaintFocus(boolean status){
        mClicked = status;
        invalidate();
    }
    
    public void setNoFocus(){
        onUnfocus();
    }
    
}

 

The above class extends BaseButtonField given below:

 

public abstract class BaseButtonField extends Field
{
    private XYRect _drawFocusTempRect = new XYRect();
    
    public BaseButtonField()
    {
        this( 0 );
    }
    
    public BaseButtonField( long style )
    {        
        super( Field.FOCUSABLE | style );
    }
        
      
    protected void layout( int width, int height )
    {
        setExtent( 10, 10 );
    }
    
    
    protected void drawFocus( Graphics g, boolean on )
    {
        getFocusRect( _drawFocusTempRect );

        boolean oldDrawStyleFocus = g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS );
        boolean notEmpty = g.pushContext( _drawFocusTempRect.x, _drawFocusTempRect.y, _drawFocusTempRect.width, _drawFocusTempRect.height, 0, 0 );

        try {
            if( notEmpty ) {
                g.setDrawingStyle( Graphics.DRAWSTYLE_FOCUS, on );
                paintBackground( g );
                paint( g );
            }
        } finally {
            g.popContext();
            g.setDrawingStyle( Graphics.DRAWSTYLE_FOCUS, oldDrawStyleFocus );
        }
    }
    
    
    protected boolean keyChar( char character, int status, int time )
    {
        if( character == Characters.ENTER ) {
            clickButton();
            return true;
        }
        return super.keyChar( character, status, time );
    }
    
      
    protected boolean navigationClick( int status, int time )
    {
        clickButton();
        return true;    
    }
    
      
    protected boolean trackwheelClick( int status, int time )
    {        
        clickButton();    
        return true;
    }
    
      
    protected boolean invokeAction( int action )
    {
        switch( action ) {
            case ACTION_INVOKE: {
                clickButton();
                return true;
            }
        }
        return super.invokeAction( action );
    }    

      
    public void setDirty( boolean dirty ) {
        // We never want to be dirty or muddy
    }
     
      
    public void setMuddy( boolean muddy ) {
        // We never want to be dirty or muddy
    }
         
    /**
     * A public way to click this button
     */
    public void clickButton()
    {
        fieldChangeNotify( 0 );
    }
    
}

 

Now I want to disable these fields when required, i.e. disable next field when last news is reached and disable previous field when first news is shown. Kindly help me to get the solution. Thanks in advance.

 

 

Developer
Posts: 206
Registered: ‎02-22-2011
My Device: BB TORCH & 9800
My Carrier: idea

Re: Enable/Disable custom field

[ Edited ]

use setImage () and setFocusImage() of your given code .

Get the field count by getFieldCount() , check if its 0 (for first news) and getFieldCount() matches the total fields i.e.,getFieldCount() == SIZE (for lastt news ) then pass the required images to those methods .

*****************************************************************************
* Give kudos when you like it
* Mark it as a solution if it fixes your issue
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Enable/Disable custom field

The problem as I see it with the approach suggested by tklanilkumar is that it does not actually stop the user focusing on the Field and so the users can still click it.

 

Instead you can create a 'setFocusable()' method, that sets the isFocusable boolean that already exists.  Them set it off for these Fields when they should not be pressed.  Then you override isFocusable(the standard Field method) and return the value of your isFocusable flag.  This will stop the Field being clicked.

 

You may want to set a slightly different image in this case, or you can do funny things like overriding paint further and setting the globalalpha when painting the bitmap to make the image look greyed out.  Or set a different image for starters.