Friday, May 10, 2013

Custom Email Field Blackberry

Custom Email Field ini tidak jauh berbeda dengan Custom Edit Field maupun Custom Number Field yang dah pernah saya tulis, untuk lebih jelasnya saya tuliskan code-nya.

public class EmailFieldCustom extends Manager {
 
    private final static int DEFAULT_LEFT_MARGIN = 1;
    private final static int DEFAULT_RIGHT_MARGIN = 1;
    private final static int DEFAULT_TOP_MARGIN = 3;
    private final static int DEFAULT_BOTTOM_MARGIN = 1;
       
    private final static int DEFAULT_LEFT_PADDING = 5;
    private final static int DEFAULT_RIGHT_PADDING = 5;
    private final static int DEFAULT_TOP_PADDING = 3;
    private final static int DEFAULT_BOTTOM_PADDING = 3;
       
    private int topMargin = DEFAULT_TOP_MARGIN;
    private int bottomMargin = DEFAULT_BOTTOM_MARGIN;
    private int leftMargin = DEFAULT_LEFT_MARGIN;
    private int rightMargin = DEFAULT_RIGHT_MARGIN;
       
    private int topPadding = DEFAULT_TOP_PADDING;
    private int bottomPadding = DEFAULT_BOTTOM_PADDING;
    private int leftPadding = DEFAULT_LEFT_PADDING;
    private int rightPadding = DEFAULT_RIGHT_PADDING;
       
    private int totalHorizontalEmptySpace = leftMargin + leftPadding + rightPadding + rightMargin;
    private int totalVerticalEmptySpace = topMargin + topPadding + bottomPadding + bottomMargin;
       
    private int minHeight = 21 + totalVerticalEmptySpace;
    private int width;
    private int height = minHeight;
    private int text_colour = 0xFF0000;
    private int border_color = 0x000066;
    private int background_color = 0xFFFF99;
    private String label = "";
    private Font headingFont = getFont().derive(Font.PLAIN, 20);
    private EmailAddressEditField editField;
      
    public EmailFieldCustom(){
        super(0);
        this.label = "";
        String initialValue = "";
        this.width = Display.getWidth()/2;
        editField = new EmailAddressEditField(label, initialValue);
        add(editField);
    }
      
    public EmailFieldCustom(int panjang){
        super(0);
        this.label = "";
        String initialValue = "";
        this.width = panjang;
        editField = new EmailAddressEditField(label, initialValue);
        add(editField);
    }

    public EmailFieldCustom(int panjang, long style){
        super(0);
        this.label = "";
        String initialValue = "";
        this.width = panjang;
        editField = new EmailAddressEditField(label, initialValue, 10000, style);
        add(editField);
    }
       
    protected void sublayout(int width, int height)
    {
        Field field = getField(0);
        layoutChild(field, this.width - totalHorizontalEmptySpace , this.height - totalVerticalEmptySpace);
        setPositionChild(field, leftMargin+leftPadding, topMargin+topPadding);
        setExtent(this.width, this.height);
    }   
       
    protected void paint(Graphics graphics){   
        graphics.setColor(border_color);
        graphics.drawRoundRect(leftMargin, topMargin, width - (leftMargin+rightMargin), height - (topMargin+bottomMargin), 7, 7);
        graphics.setColor(background_color);
        graphics.fillRoundRect(leftMargin+1, topMargin+1, (width - (leftMargin+rightMargin))-2, (height- (topMargin+bottomMargin))-2, 7, 7);
          
        graphics.setColor(Color.BLACK);
        if(headingFont!=null){
            setFont(headingFont);
            graphics.drawText(label, 2, 10);
        }else{
            graphics.drawText(label, 2, 7);
        }
          
        graphics.setColor(text_colour);
        EmailAddressEditField ef = (EmailAddressEditField)getField(0);
        String entireText = ef.getText();
          
        boolean longText = false;
        String textToDraw = "";
        Font font = headingFont;
        int availableWidth = width - totalHorizontalEmptySpace;
        if (font.getAdvance(entireText) <= availableWidth)
        {
            textToDraw = entireText;
        }
        else
        {
            int endIndex = entireText.length();
            for (int beginIndex = 1; beginIndex < endIndex; beginIndex++)
            {
                textToDraw = entireText.substring(beginIndex);
                if (font.getAdvance(textToDraw) <= availableWidth)
                {
                    longText = true;
                    break;
                }
            }
        }
          
        if (longText == true)
        {      
            ef.setText(textToDraw);        
            super.paint(graphics);
            ef.setText(entireText);
        }
        else
        {
            super.paint(graphics);
        }
           
    }
       
    public int getPreferredWidth(){
        return width;
    }
       
    public int getPreferredHeight(){
        return height;
    }
       
    protected boolean keyChar(char ch, int status, int time){      
       if (ch == Characters.ENTER)
       {
            return true;
       }   
       else
       {
            return super.keyChar(ch, status, time);
       }
    }
     
    protected void onUnfocus() {
       super.onUnfocus();
    }
     
    protected void onFocus(int direction) {
       super.onFocus(direction);
    }
    
    protected void drawFocus(Graphics graphics, boolean on) {
       super.drawFocus(graphics, on);
    }
  
    public String getText(){
       return ((EmailAddressEditField) getField(0)).getText();
    }
           
    public void setText(final String text){
       ((EmailAddressEditField) getField(0)).setText(text);
    }
}

No comments:

Post a Comment

Silahkan Isi komentar anda,thanks!