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
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: Text Wrapping for text in List Field items

Given that you have between 20 and 50, adding these as individual Fields to the Manager and having the Manager manage this is not going to be too arduous or Memory intensive, so it would, I think =, be practical to forget using ListField and just add your RichTextFields.  However, focus on these is a bit silly for this application, as I think you said at one stage, you would like it all to be focused at once.  So I thought, why not extend RichTextField to just have a single focus!

 

" hours later, I finally found the bug, I was swapping height ad width at one critical point.  So following is some code that will format a Field like a RichTextField, but will focus on the full extent of the Field. 

 

What use is this to you?  None as currently coded.  but if you like the idea, you can override navigationClick and process this like you would a ListField.  Perhaps we can even persuade the Field to work like a button.  I will have a look at doing that if you like the general concept.

 

This code is testing only and needs a lot of work.  I will tidy all this up too if you think there is value in this.  You will also note that I keep an 'extent' and use it - I did this because I found that getHeight() and getWidth() when called in drawFocus, seem in some OS Levels to return the height and width of the first character in the RichTextField (which is presumably what would ordinarily be focused. 

 

I also found some differences in this way these methods were processed in 4.0, 4.2, 4.6 and 4.7, but this code seems to work in all.

 

// Note this is not my actual test code, // I removed some pretty awful testing bits. // So this might not compile, but you get the idea.... class MyRichTextField extends RichTextField { private XYRect _ourExtent; public MyRichTextField(String text) { super(text, Field.READONLY | Field.FOCUSABLE); } public boolean isSelectable() { return false; } protected void drawFocus(Graphics graphics, boolean on) { if ( _ourExtent == null ) { super.drawFocus(graphics, on); } else { super.drawHighlightRegion(graphics, Field.HIGHLIGHT_FOCUS, on, 0, 0, _ourExtent.width, _ourExtent.height); } } public void getFocusRect(XYRect rect) { if ( _ourExtent != null ) { rect.x = 0; rect.y = 0; rect.width = _ourExtent.width; rect.height = _ourExtent.height; } else { super.getFocusRect(rect); } } protected void onFocus(int direction) { if ( _ourExtent == null ) { _ourExtent = this.getExtent(); } super.onFocus(direction); } public int moveFocus(int amount, int status, int time) { return amount; } }

 

Please use plain text.
New Developer
shishirc
Posts: 17
Registered: ‎05-14-2009
My Device: Not Specified

Re: Text Wrapping for text in List Field items

This new concept is interesting. This does open another new direction of thought.

I implemented it in my code, and I finally got stuck with this problem: I have multiple fields for each row in my ListField impl, 3 to be exact. The top most field was something like a heading, which I wanted to have a seperate background color. the other two have some simple formatting and some text. So, when I selected one row all these three fields got selected as one entity. All else is perfect except the fact that my middle field could have text that fits in one line or two lines, and hence the requirement of variable heights.

When I try the same as you pointed, with RichTextField impl, I can't seem to draw a different background color for the first line and a different one for the next lines within one Field. If in turn I make each row with multiple Fields, I can't get them to be focusable at one go..

 

My design for one row to be focusable at one go:

 

| First line with blue background   |

| Second  with white background   |

| and text flowing to third line        |

| Third field with text.                    | 

Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: Text Wrapping for text in List Field items

OK, so instead of single Focus Field, you want a single focus Manager.  I will try to find time to have a look at that later.  Don't expect anything from me before this time torrow.

 

Of course someone else might have time to try this, or tell us this can't be done.  It does sound do-able, but it might depend on what type of Fields you have in the Manager.  What type of Fields do you use?  Sounds like tjhey are all RichTextFields.

 

However you need to confirm that if we create a single focus manager, even if this was resticted to all the contained Field being RichTextFields, that would do the job for you?  Would it?

Please use plain text.
New Developer
shishirc
Posts: 17
Registered: ‎05-14-2009
My Device: Not Specified

Re: Text Wrapping for text in List Field items

Yeah, you are right, all my fields are RichTextFields. I just wanted different background colors for them. The idea you are suggesting will work for me, the single focus manager should do the trick.

On another thought, I was looking at the api and I saw a constructor in the ActiveRichTextField which takes in a list of background/foreground colors too. I am yet to try that out. If that works we might avoid writing the manager for focus. Will let you know if it works.

Thansk! 

Please use plain text.
New Developer
shishirc
Posts: 17
Registered: ‎05-14-2009
My Device: Not Specified

Re: Text Wrapping for text in List Field items

I tried the ActiveRichTextField, it does provide for differnet backgrounds within its text, but the color applies to the background of the text and not to the entire extent. This is again not useful to me.

 

Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: Text Wrapping for text in List Field items

The following works OK on a trackball device but does some pretty horrible things on a Storm.  I've spent too long on it, I think I'm going to have to admit you have to be cleverer than me to make this Manager.    But perhaps someone else can get it giong....

 

class SingleFocusManager extends VerticalFieldManager {
    private XYRect _ourExtent;
    private boolean _inFocus = false;
    private boolean _drawFocus = false;
    public SingleFocusManager() {
        super();
    }
    protected void drawFocus(boolean on) {
        System.out.println("drawFocus: " + on);
        Graphics graphics = this.getScreen().getGraphics();
        graphics.pushContext(_ourExtent, _ourExtent.x, _ourExtent.y);
        _drawFocus = true;
        super.drawHighlightRegion(graphics, Field.HIGHLIGHT_FOCUS, on, 0, 0, _ourExtent.width, _ourExtent.height);
        _drawFocus = false;
        graphics.popContext();
    }
    public void paint(Graphics graphics) {
        System.out.println("paint" + _drawFocus + _inFocus);
        if ( !_inFocus ) {
            graphics.clear();
            super.paint(graphics);
        } else
        if ( _drawFocus ) {
            super.paint(graphics);
        } else {
            _drawFocus = true;
            _ourExtent = getFieldExtent(this);
            super.drawHighlightRegion(graphics, Field.HIGHLIGHT_FOCUS, true, 0, 0, _ourExtent.width, _ourExtent.height);
            _drawFocus = false;
        }
    }
    public static final XYRect getFieldExtent(Field fld) {
        XYRect fieldRegion = fld.getExtent();
        int cy = fieldRegion.y;
        int cx = fieldRegion.x;
        int width = fieldRegion.width;
        int height = fieldRegion.height;
        Manager m = fld.getManager();
        while (m != null) {
            fieldRegion = m.getExtent();
            cy += fieldRegion.y - m.getVerticalScroll();
            cx += fieldRegion.x - m.getHorizontalScroll();
            if (m instanceof Screen)
                break;
            m = m.getManager();
        }
        return new XYRect(cx, cy, width, height);
    }
    public boolean isFocusable() {
        System.out.println("isFocusable");
        return true;
    }
    public Field getFieldWithFocus() {
        System.out.println("getFieldWithFocus: " + _inFocus);
        if ( _inFocus ) {
            return this;
        }
        return null;
    }
    public Field getLeafFieldWithFocus() {
        System.out.println("getLeafFieldWithFocus: " + _inFocus);
        if ( _inFocus ) {
            return this.getField(0);
        }
        return null;
    }
    protected void onFocus(int direction) {
        System.out.println("onFocus: " + direction);
        _inFocus = true;
        _ourExtent = getFieldExtent(this);
        Manager m = this.getManager();
        XYRect managersExtent = getFieldExtent(m);
        int currentScrollPos = m.getVerticalScroll();
        if ( _ourExtent.y < managersExtent.y ) {
            // need to move scroll so that this Field is at the top
            m.setVerticalScroll(currentScrollPos - (managersExtent.y - _ourExtent.y));
            _ourExtent.y = managersExtent.y;
        } else
        if ( _ourExtent.y + _ourExtent.height > managersExtent.y + managersExtent.height ) {
            // need to move scroll so that this Field is at the bottom
            int scrolldif = managersExtent.y + managersExtent.height - (_ourExtent.y + _ourExtent.height);
            _ourExtent.y = managersExtent.y + managersExtent.height - (_ourExtent.height);
            m.setVerticalScroll(currentScrollPos - scrolldif);
        }
        drawFocus(true);
    }
    public void onFocusFromField(int direction) {
        onFocus(direction);
    }
    protected void onUnfocus() {
        System.out.println("onUnfocus");
        _inFocus = false;
        if ( _ourExtent != null ) {
            drawFocus(false);
        }
    }

// Following added to try to get the Storm to work, but no luck.... 

// I don't know how the Storm tells the manager that it is in focus. 

// On trackball devices it is easy, as focus moves sequentuialy through the Fields.
    public int moveFocus(int amount, int status, int time) {
        System.out.println("moveFocus: " + amount);
        return amount;
    }
    protected void moveFocus(int x, int y, int status, int time) {
        System.out.println("moveFocus: " + x + ":" + y);
        onFocus(0);
    }
    public boolean setFocus(int x, int y, int status) {
        System.out.println("setFocus: " + x + ":" + y);
        onFocus(0);
        return true;
    }
}

Please use plain text.
Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: Text Wrapping for text in List Field items

I posted this code a while ago that seems to implement variable height rows in a ListField. At the time, I asked if anyone knew if my code was working only "by accident" (as it were) or if this was a reliable method. Nobody answered. :smileysad:

 

 




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Please use plain text.
New Developer
shishirc
Posts: 17
Registered: ‎05-14-2009
My Device: Not Specified

Re: Text Wrapping for text in List Field items

Sorry Peter for suddenly disappearing, I went on vacation and just returned. We finally uploaded our application without using the final piece of advice for the time. So we have constant height rows and are using Richtextfields inside ListFields. It is good enough for the start.

Thanks for all the help.

 

But, since we ourselves dont have Blackberrys here to test, we upload the application and have someone else test the app. He tried to download and install the app, everything went smoothly, but he can not find the application. This is very surprising to us, and we never thought this issue could prop up! He is using a BlackBerry Pearl (not sure of model no) and running OD 4.3. Is there a mistake in our code that could cause such a problem? 

Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: Text Wrapping for text in List Field items

Can you raise this as a new problem?
Please use plain text.
New Developer
shishirc
Posts: 17
Registered: ‎05-14-2009
My Device: Not Specified

Re: Text Wrapping for text in List Field items

I thought so too. Here is the new thread I started for this:

http://supportforums.blackberry.com/rim/board/message?board.id=java_dev&thread.id=39759 

Please use plain text.