Sunday, August 30, 2009

Clickable LabelField act like Link (Href Field )


The example of a LabelField which act like html link.

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.FontFamily;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.system.Characters;


public class HrefField extends Field {

private String content;
private Font fieldFont;
private int fieldWidth;
private int fieldHeight;
private boolean active = false;
private int backgroundColour = 0xffffff;
//private int textColour = 0x333333;
private int textColour = 0x0000ff;
private int maskColour = 0xBBBBBB;

private int buttonId;
private String buttonName;

public HrefField(String content)
{
super(Field.FOCUSABLE);
this.content = content;
fieldFont = defaultFont();
fieldWidth = fieldFont.getAdvance(content)+2;
fieldHeight = fieldFont.getHeight() + 3;
}

public void setColours(int backgroundColour, int textColour, int maskColour)
{
this.backgroundColour = backgroundColour;
this.textColour = textColour;
this.maskColour = maskColour;
invalidate();
}

public void setBackgroundColour(int backgroundColour)
{
this.backgroundColour = backgroundColour;
invalidate();
}

public void setTextColour(int textColour)
{
this.textColour = textColour;
invalidate();
}

public void setMaskColour(int maskColour)
{
this.maskColour = maskColour;
invalidate();
}

public void setFont(Font fieldFont)
{
this.fieldFont = fieldFont;
}

public int getPreferredWidth()
{
return fieldWidth;
}

public int getPreferredHeight()
{
return fieldHeight;
}

protected void layout(int arg0, int arg1)
{
setExtent(getPreferredWidth(), getPreferredHeight());
}

protected void paint(Graphics graphics)
{

if (active)
{
graphics.setColor(maskColour);
graphics.fillRect(0, 0, fieldWidth, fieldHeight);
}
else
{
graphics.setColor(backgroundColour);
graphics.fillRect(0, 0, fieldWidth, fieldHeight);
}

graphics.setColor(textColour);
graphics.setFont(fieldFont);
graphics.drawText(content, 1, 1);
graphics.drawLine(1, fieldHeight-2, fieldWidth-2, fieldHeight-2);
}

protected boolean navigationClick(int status, int time)
{
fieldChangeNotify(1);
return true;
}

public void setButtonId(int buttonId)
{
this.buttonId = buttonId;
}

public void setButtonName(String buttonName)
{
this.buttonName = buttonName;
}

public int getButtonId()
{
return buttonId;
}
public String getButtonName()
{
return buttonName;
}
public boolean keyChar(char key, int status, int time)
{
if (key == Characters.ENTER)
{
fieldChangeNotify(0);
return true;
}

return false;
}


protected void onFocus(int direction)
{
active = true;
invalidate();
}

protected void onUnfocus()
{
active = false;
invalidate();
}

public static Font defaultFont()
{
return Font.getDefault();
}
}



No comments:

Post a Comment