package com.thinkingblackberry; import net.rim.device.api.ui.Field; import net.rim.device.api.ui.Font; import net.rim.device.api.ui.Graphics; import net.rim.device.api.ui.Keypad; public class CustomButtonField extends Field { private String label; private int textColorUnfocused; private int textColorFocused; private int backgroundColorUnfocused; private int backgroundColorFocused; private int horizontalPadding = 5; private int verticalPadding = 5; public CustomButtonField(String label, int textColorUnfocused, int textColorFocused, int backgroundColorUnfocused, int backgroundColorFocused, long style) { super(style); this.label = label; this.textColorUnfocused = textColorUnfocused; this.textColorFocused = textColorFocused; this.backgroundColorUnfocused = backgroundColorUnfocused; this.backgroundColorFocused = backgroundColorFocused; } protected void layout(int maxWidth, int maxHeight) { Font font = getFont(); int width = font.getAdvance(label) + (horizontalPadding * 2); int height = font.getHeight() + (verticalPadding * 2); // Respect the maximum width and height available from our manager setExtent(Math.min(width, maxWidth), Math.min(height, maxHeight)); } protected void paint(Graphics graphics) { // Draw background graphics.setColor(isFocus() ? backgroundColorFocused : backgroundColorUnfocused); graphics.fillRoundRect(0, 0, getWidth(), getHeight(), 10, 10); // Draw text graphics.setColor(isFocus() ? textColorFocused : textColorUnfocused); graphics.drawRoundRect(0, 0, getWidth(), getHeight(), 10, 10); int fontWidth = getFont().getAdvance(label); graphics.drawText(label, (getWidth()-fontWidth)/2, verticalPadding); } protected void drawFocus(Graphics graphics, boolean on) { // Don't draw the default focus } protected void onFocus(int direction) { super.onFocus(direction); invalidate(); } protected void onUnfocus() { super.onUnfocus(); invalidate(); } public boolean isFocusable() { return true; } protected boolean navigationClick(int status, int time) { fieldChangeNotify(0); return true; } protected boolean keyChar(char character, int status, int time) { if (character == Keypad.KEY_ENTER) { fieldChangeNotify(0); return true; } return super.keyChar(character, status, time); } public String getLabel() { return label; } }