If you are using Internet Explorer, please remove blackberry.com from your compatibility view settings.

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
New Contributor
AchintaPaul
Posts: 5
Registered: ‎03-26-2010
My Device: 9000
My Carrier: AT&T
Accepted Solution

Active Rich Text Field

Hi All,

 

I am working in a BlackBerry application where in one of the screen i have a HyperLink text, which can be focused and can generate events on user input. The hyper link text is in between other texts, something like this.

 

"This is my requirment for a screen which i need to implement.”

 

In the above text the "screen" should be underline, can be focused and can generate events on user input. I can achive this with the help of the ActiveRichTextField and PatternRepository. But now I have to add the following things also.

 

  1. When the ActiveRichTextField is focused at that time the highlighter should be on the link text by default and the highlighter cannot be moved to other text.
  2. How to change the background color of the link text when it is focused or not focused, for e.g. when focused the background color could be in Orange and when not focused the background color could be in Blue color.

Is the above functionality can be achived using ActiveRichTextField, or is there any othere alternative available?

 

Please use plain text.
Developer
jcnielsen
Posts: 90
Registered: ‎06-03-2009
My Device: Dev Alpha B
My Carrier: None atm.

Re: Active Rich Text Field

You can implement drawFocus for the specific ActiveRichTextField object kinda like this.

 

myCoolTextField = new ActiveRichTextField(........,...., RichTextField.FOCUSABLE | RichTextField.HIGHLIGHT_FOCUS | RichTextField.HIGHLIGHT_SELECT){

 

           protected void drawFocus(Graphics g, boolean on) {

                g.setBackgroundColor(0xFFFFFF);
                g.clear();
                g.setColor(0xAABB00);
                g.setFont(myFont);
                g.drawText(getText(), 0, this.getTop());
                this.invalidate();
            }

 

};

 

Br
J. C. Nielsen

Attended DEVCON 2010

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

Re: Active Rich Text Field

[ Edited ]

Sorry I have not looked at your requirement, but I would be extremely cautious about using the code supplied.

 

Calling setFont in the middle of the a drawFocus will, as I understand it, cause the Field to be relayed out, which might end up causing some recursive invocation of drawFocus, since focus will have to redrawn once it has been laid out. 

 

In addition, this code will attempt to paint the entire text of the Field, which will not work if the text is over one line.

 

As noted, not looked at your requirement, just really concerned that this drawFocus code is dangerous and/or won't work. 

Please use plain text.
Developer
jcnielsen
Posts: 90
Registered: ‎06-03-2009
My Device: Dev Alpha B
My Carrier: None atm.

Re: Active Rich Text Field

I understand your concern and maybe I have just been lucky with the situations I have done this in, but it does seem to work for me. There is probably a more elegant way to do something similar.

Br
J. C. Nielsen

Attended DEVCON 2010

Free Tibet
Please use plain text.
New Contributor
AchintaPaul
Posts: 5
Registered: ‎03-26-2010
My Device: 9000
My Carrier: AT&T

Re: Active Rich Text Field

 

But as per my requirement,

 

  1. the cursor should be present in the ActiveRichTextField, and when the focus is on the richTextField the link text should be highlight, in this case the string "screen" from the previous post, not any other text from the message. And on scrolling the focus should be moved to other fields.
  2.  And the background color of the link text only should be changed not the entire ActiveRechText Field.

Hope this makes it more clear. Thx for the response, hope to hear more from you.

Please use plain text.
Developer
jcnielsen
Posts: 90
Registered: ‎06-03-2009
My Device: Dev Alpha B
My Carrier: None atm.

Re: Active Rich Text Field

1) I don't knoiw anything about placing a cursor on to of a link.

2) Split up the text so only 'screen' is an ActiveRichTextField.

Br
J. C. Nielsen

Attended DEVCON 2010

Free Tibet
Please use plain text.
Developer
mgontar
Posts: 58
Registered: ‎09-23-2009
My Device: Not Specified

Re: Active Rich Text Field

Maybe some flag can resolve this issue? like

 

boolean isFontSet = false;

myCoolTextField = new ActiveRichTextField(........,...., RichTextField.FOCUSABLE | RichTextField.HIGHLIGHT_FOCUS | RichTextField.HIGHLIGHT_SELECT){

 

           protected void drawFocus(Graphics g, boolean on) {

                g.setBackgroundColor(0xFFFFFF);
                g.clear();
                g.setColor(0xAABB00);

                if(!isFontSet )

                {
                    g.setFont(myFont);

                    isFontSet = true;

                }
                g.drawText(getText(), 0, this.getTop());
                this.invalidate();
            }

 

};

Please use plain text.
New Contributor
AchintaPaul
Posts: 5
Registered: ‎03-26-2010
My Device: 9000
My Carrier: AT&T

Re: Active Rich Text Field

Dear All,

 

I could achive the requirment to some extent, it works fine if there is a single link text in the message, but has problems when there is more then one links. The following is the code which i have used.

 

 

import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.LabelField;

public class CustomLinkMesgText {

public CustomLinkMesgText() {
// TODO Auto-generated constructor stub
}

/**
* Creates a LabelField for the Text Message with Link text.
* @param textMesg
* @param textLink
* @param fieldWidth
* @param fieldHeight
* @param focusColor
* @param fontFocusColor
* @param fontUnFocusColor
* @return LabelField
*/
public LabelField createLabelField(final String textMesg, final String textLink, final int fieldWidth, final int fieldHeight, final int focusColor, final int fontFocusColor, final int fontUnFocusColor){

return new LabelField(){

protected void layout(int _width, int _height) {
super.layout(fieldWidth, fieldHeight);
setExtent(fieldWidth, fieldHeight);
}

protected void drawFocus(Graphics graphics, boolean on) {

}

public boolean isFocusable() {
return true;
}

protected void paint(Graphics gr) {

int mesgLength = textMesg.length();
StringBuffer bufferWord = new StringBuffer();
int posX = 0;
int posY = 0;

for (int i = 0; i < mesgLength; i++) {
char text = textMesg.charAt(i);

if(text != ' ' && i != mesgLength-1)
bufferWord.append(text);
else{
Font font = null;

if(textLink.equals(bufferWord.toString()))
font = Font.getDefault().derive(Font.UNDERLINED, 14);
else
font = Font.getDefault().derive(Font.PLAIN, 14);

gr.setFont(font);

if(posX + font.getAdvance(bufferWord.toString()) >= fieldWidth){
posY += font.getHeight() + 4;
posX = 0;
}

String textToDraw = "";
for (int j = 0; j < bufferWord.length(); j++) {
/*
* The _ text should be replaced with the space " " as in the link text such as Learn More
* a space is required in between the text.
*
* For the link which doesn't contain any space in between it will be same
*/
if(bufferWord.charAt(j) == '_')
textToDraw += " ";
else
textToDraw += bufferWord.charAt(j);
}

if(isFocus() && textLink.equals(bufferWord.toString())){
gr.setColor(focusColor);
gr.fillRect(posX, posY, font.getAdvance(textToDraw), font.getHeight());
gr.setColor(fontFocusColor);
}
else if(!isFocus() && textLink.equals(bufferWord.toString())){
gr.setColor(fontUnFocusColor);
}
else{
gr.setColor(IAppConstant.BLACK_COLOR);
}

gr.drawText(textToDraw, posX, posY);

posX = posX + font.getAdvance(textToDraw)+font.getAdvance(" ");

bufferWord.delete(0, bufferWord.length());
}
}
}
};
}
}

 

 

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

Re: Active Rich Text Field

I have had a quick look at your requirement and you are asking a lot. 

 

I think the background color is do-able, by overriding drawFocus.

 

But I can't see how you can stop the focus movement without a lot of work and understanding how this Field actually identifies the 'hot spots'.  The least amount of work would be to split the Field up into the linkable and non linkable sections, but that assumes you know where they are.  It also has some serious formatting problems as to do this you would have to manage the word wrap yourself.  .  If someone asked me for the requirement you have, I would say it can't be done.  Which is not to say it can't be, just that I don't think the result is worth the days of effort that would be needed to develop it. 

 

Sorry,

 

But I'm no GUI expert.  Let us hope that someone who is, tells you different.  

Please use plain text.
New Contributor
AchintaPaul
Posts: 5
Registered: ‎03-26-2010
My Device: 9000
My Carrier: AT&T

Re: Active Rich Text Field

Hi Peter,

 

Thanks for your comments, I do agree with you. But to met my requirement i have made use of the LabelField insted of the ActiveRichText Field, as using LabelField i can get rid of the cursor. And when the focus is on the labelField i just do a fillRect of the color, so it displays as the link Text is focused. So it just works for my requirement as of now. :smileyhappy:

 

Please use plain text.