Updated PillButtonField in RIM's Advanced UI examples


/ Published in: Java
Save to your folder(s)

I have updated the PillButtonField that RIM published in order to have the theme customizable in the same way that you can customize their EmbossedButtonField.


Copy this code and paste it in your HTML
  1. /*
  2.  * PillButtonField.java
  3.  *
  4.  * Research In Motion Limited proprietary and confidential
  5.  * Copyright Research In Motion Limited, 2009-2009
  6.  *
  7.  * Theming ability added by RyanNickel
  8.  * http://www.ryannickel.com
  9.  */
  10.  
  11. package com.blackberry.toolkit.ui.component;
  12.  
  13. import com.blackberry.toolkit.ui.container.PillButtonSet;
  14.  
  15. import net.rim.device.api.system.*;
  16. import net.rim.device.api.ui.*;
  17. import net.rim.device.api.util.LongIntHashtable;
  18.  
  19. /**
  20.  * A custom button field
  21.  */
  22. public class PillButtonField extends BaseButtonField
  23. {
  24. private static final int CORNER_RADIUS = 18;
  25.  
  26. public static int DRAWPOSITION_LEFT = 0;
  27. public static int DRAWPOSITION_RIGHT = 1;
  28. public static int DRAWPOSITION_MIDDLE = 2;
  29. public static int DRAWPOSITION_SINGLE = 3;
  30.  
  31. public static final long COLOUR_BORDER = 0x70afec541cc1667fL; //net.rim.device.api.ui.component.PillButtonField.COLOUR_BORDER;
  32. public static final long COLOUR_BORDER_FOCUS = 0x457f474312256dbdL; //net.rim.device.api.ui.component.PillButtonField.COLOUR_BORDER_FOCUS
  33. public static final long COLOUR_BORDER_SELECTED = 0x484cd982484a28e0L; //net.rim.device.api.ui.component.PillButtonField.COLOUR_BORDER_SELECTED
  34. public static final long COLOUR_TEXT = 0xa00b8dd7ce1b1db1L; //net.rim.device.api.ui.component.PillButtonField.COLOUR_TEXT
  35. public static final long COLOUR_TEXT_FOCUS = 0xee3293de59141b4fL; //net.rim.device.api.ui.component.PillButtonField.COLOUR_TEXT_FOCUS
  36. public static final long COLOUR_TEXT_SELECTED = 0x45b7eed3e6d47e14L; //net.rim.device.api.ui.component.PillButtonField.COLOUR_TEXT_SELECTED
  37. public static final long COLOUR_BACKGROUND = 0x7b66bde7e495f6b4L; //net.rim.device.api.ui.component.PillButtonField.COLOUR_BACKGROUND
  38. public static final long COLOUR_BACKGROUND_FOCUS = 0x2f15204ab0ca5d91L; //net.rim.device.api.ui.component.PillButtonField.COLOUR_BACKGROUND_FOCUS
  39. public static final long COLOUR_BACKGROUND_SELECTED = 0xef45148bf9a8f51bL; //net.rim.device.api.ui.component.PillButtonField.COLOUR_BACKGROUND_SELECTED
  40.  
  41. private static final int XPADDING = Display.getWidth() <= 320 ? 6 : 8;
  42. private static final int YPADDING = Display.getWidth() <= 320 ? 5 : 7;
  43.  
  44. private String _text;
  45. private Font _buttonFont;
  46. private boolean _pressed = false;
  47. private boolean _selected = false;
  48.  
  49. private int _width;
  50. private int _height;
  51.  
  52. private int _drawPosition = -1;
  53. private LongIntHashtable _colourTable;
  54.  
  55.  
  56. public PillButtonField( String text )
  57. {
  58. super( Field.FOCUSABLE );
  59. _text = text;
  60. _colourTable = new LongIntHashtable();
  61. _colourTable.put(PillButtonField.COLOUR_BORDER, 0x212121);
  62. _colourTable.put(PillButtonField.COLOUR_BORDER_FOCUS, 0x212121);
  63. _colourTable.put(PillButtonField.COLOUR_BORDER_SELECTED, 0x212121);
  64. _colourTable.put(PillButtonField.COLOUR_TEXT, 0xD6D6D6);
  65. _colourTable.put(PillButtonField.COLOUR_TEXT_FOCUS, 0xFFFFFF);
  66. _colourTable.put(PillButtonField.COLOUR_TEXT_SELECTED, 0xFFFFFF);
  67. _colourTable.put(PillButtonField.COLOUR_BACKGROUND, 0x727272);
  68. _colourTable.put(PillButtonField.COLOUR_BACKGROUND_FOCUS, 0x125DDE);
  69. _colourTable.put(PillButtonField.COLOUR_BACKGROUND_SELECTED, 0x32427E);
  70. }
  71.  
  72. /**
  73.   * DRAWPOSITION_LEFT | DRAWPOSITION_RIGHT | DRAWPOSITION_MIDDLE | DRAWPOSITION_SINGLE
  74.   * Determins how the field is drawn (border corners)
  75.   */
  76. public void setDrawPosition( int drawPosition )
  77. {
  78. _drawPosition = drawPosition;
  79. }
  80.  
  81. public void setColorTable( LongIntHashtable colorTable)
  82. {
  83. _colourTable = colorTable;
  84. }
  85.  
  86.  
  87. public void setSelected( boolean selected )
  88. {
  89. _selected = selected;
  90. invalidate();
  91. }
  92.  
  93. public int getPreferredWidth()
  94. {
  95. return 2 * XPADDING + _buttonFont.getAdvance( _text ); // not actually used
  96. }
  97.  
  98. public int getPreferredHeight()
  99. {
  100. return 2 * YPADDING + _buttonFont.getHeight();
  101. }
  102.  
  103. protected void layout( int width, int height )
  104. {
  105. _buttonFont = getFont();
  106. setExtent( width, getPreferredHeight() );
  107. _width = getWidth();
  108. _height = getHeight();
  109. }
  110.  
  111. protected void onUnfocus()
  112. {
  113. super.onUnfocus();
  114. if( _pressed ) {
  115. _pressed = false;
  116. invalidate();
  117. }
  118. }
  119.  
  120. /**
  121.   * A public way to click this button
  122.   */
  123. public void clickButton()
  124. {
  125. Manager manager = getManager();
  126. if( manager instanceof PillButtonSet ) {
  127. ( ( PillButtonSet ) manager ).setSelectedField( this );
  128. }
  129. super.clickButton();
  130. }
  131.  
  132. protected boolean navigationClick(int status, int time) {
  133. _pressed = true;
  134. invalidate();
  135. return super.navigationClick( status, time );
  136. }
  137.  
  138. protected boolean navigationUnclick(int status, int time) {
  139. _pressed = false;
  140. invalidate();
  141. return true;
  142. }
  143.  
  144. protected void paint( Graphics g )
  145. {
  146. int oldColour = g.getColor();
  147. try {
  148. int foregroundColor;
  149. if ( _pressed || _selected ) {
  150. foregroundColor = _colourTable.get(COLOUR_TEXT_SELECTED);
  151. } else if( g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS ) ) {
  152. foregroundColor = _colourTable.get(COLOUR_TEXT_FOCUS);
  153. } else {
  154. foregroundColor = _colourTable.get(COLOUR_TEXT);
  155. }
  156.  
  157. g.setColor( foregroundColor );
  158. g.drawText( _text, 0, YPADDING, DrawStyle.HCENTER, _width );
  159. } finally {
  160. g.setColor( oldColour );
  161. }
  162. }
  163.  
  164. protected void paintBackground( Graphics g )
  165. {
  166. int oldColour = g.getColor();
  167.  
  168. int backgroundColor;
  169. int borderColor;
  170.  
  171. if( _pressed ) {
  172. backgroundColor = _colourTable.get(COLOUR_BACKGROUND_SELECTED);
  173. borderColor = _colourTable.get(COLOUR_BORDER_SELECTED);
  174. } else if ( g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS ) ) {
  175. backgroundColor = _colourTable.get(COLOUR_BACKGROUND_FOCUS) ;
  176. borderColor = _colourTable.get(COLOUR_BORDER_FOCUS);
  177. }else if( _selected ) {
  178. backgroundColor = _colourTable.get(COLOUR_BACKGROUND_SELECTED);
  179. borderColor = _colourTable.get(COLOUR_BORDER_SELECTED);
  180. } else {
  181. backgroundColor = _colourTable.get(COLOUR_BACKGROUND);
  182. borderColor = _colourTable.get(COLOUR_BORDER);
  183. }
  184.  
  185. try {
  186. if( _drawPosition == 0 ) {
  187. // Left
  188. drawButtonBackground( g, 0, 0, getWidth() + CORNER_RADIUS, getHeight(), backgroundColor, borderColor );
  189. drawSeparator( g, 0, 0, getWidth(), getHeight(), false, true );
  190. } else if( _drawPosition == 1 ) {
  191. // Right
  192. drawButtonBackground( g, -CORNER_RADIUS, 0, getWidth() + CORNER_RADIUS, getHeight(), backgroundColor, borderColor );
  193. drawSeparator( g, 0, 0, getWidth(), getHeight(), true, false );
  194. } else if( _drawPosition == 2 ) {
  195. // Middle
  196. drawButtonBackground( g, -CORNER_RADIUS, 0, getWidth() + 2 * CORNER_RADIUS, getHeight(), backgroundColor, borderColor );
  197. drawSeparator( g, 0, 0, getWidth(), getHeight(), true, true );
  198. } else {
  199. // Single
  200. drawButtonBackground( g, 0, 0, getWidth(), getHeight(), backgroundColor, borderColor );
  201. }
  202.  
  203. } finally {
  204. g.setColor( oldColour );
  205. }
  206. }
  207.  
  208. private void drawButtonBackground( Graphics g, int x, int y, int width, int height, int backgroundColor, int borderColor )
  209. {
  210. int oldAlpha = g.getGlobalAlpha();
  211. try {
  212. g.setColor( backgroundColor );
  213. g.fillRoundRect( x, y, width, height, CORNER_RADIUS, CORNER_RADIUS );
  214.  
  215. g.setGlobalAlpha( 0x44 );
  216. g.setColor( 0xFFFFFF ); // White highlight
  217. g.drawRoundRect( x, _selected ? y : y + 1, width, _height - 1, CORNER_RADIUS, CORNER_RADIUS );
  218. g.setColor( 0x000000 ); // Black lowlight
  219. g.drawRoundRect( x, _selected ? y + 1: y, width, height - 1, CORNER_RADIUS, CORNER_RADIUS );
  220.  
  221. g.setGlobalAlpha( 0xFF );
  222. g.setColor( borderColor );
  223. g.drawRoundRect( x, y, width, height, CORNER_RADIUS, CORNER_RADIUS );
  224. } finally {
  225. g.setGlobalAlpha( oldAlpha );
  226. }
  227. }
  228.  
  229. private void drawSeparator( Graphics g, int x, int y, int width, int height, boolean left, boolean right )
  230. {
  231. int oldAlpha = g.getGlobalAlpha();
  232. try {
  233. width--;
  234. g.setGlobalAlpha( 0x33 );
  235. if( left ) {
  236. g.setColor( 0x000000 ); // Black lowlight
  237. g.drawLine( 0, y, 0, y + height );
  238. }
  239. if( right ){
  240. g.setColor( 0xFFFFFF ); // White highlight
  241. g.drawLine( width, y, width, y + height );
  242. }
  243. } finally {
  244. g.setGlobalAlpha( oldAlpha );
  245. }
  246. }
  247. }

URL: http://www.ryannickel.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.