Advertisement
alishaik786

CustomDatePikcer

Oct 1st, 2011
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.87 KB | None | 0 0
  1. import java.util.Calendar;
  2. import java.util.Date;
  3.  
  4. import net.rim.device.api.ui.Color;
  5. import net.rim.device.api.ui.Field;
  6. import net.rim.device.api.ui.FieldChangeListener;
  7. import net.rim.device.api.ui.Font;
  8. import net.rim.device.api.ui.Graphics;
  9. import net.rim.device.api.ui.TouchEvent;
  10. import net.rim.device.api.ui.UiApplication;
  11. import net.rim.device.api.ui.component.ButtonField;
  12. import net.rim.device.api.ui.component.Dialog;
  13. import net.rim.device.api.ui.component.LabelField;
  14. import net.rim.device.api.ui.container.HorizontalFieldManager;
  15. import net.rim.device.api.ui.container.PopupScreen;
  16. import net.rim.device.api.ui.container.VerticalFieldManager;
  17. import net.rim.device.api.ui.decor.BackgroundFactory;
  18.  
  19. public class CustomDatePicker extends PopupScreen implements FieldChangeListener
  20. {
  21.  
  22. DatePickerField mDatePicker;
  23. ButtonField mOKButton;
  24. ButtonField mCancelButton;
  25.  
  26. public CustomDatePicker()
  27. {
  28. this(Calendar.getInstance().getTime());
  29. }
  30.  
  31. public CustomDatePicker(Date date)
  32. {
  33. super(new VerticalFieldManager(), PopupScreen.DEFAULT_CLOSE);
  34. setBackground(BackgroundFactory.createSolidBackground(Color.GREEN));
  35. add(mDatePicker = new DatePickerField(date));
  36. addButtons();
  37. }
  38.  
  39. private void addButtons()
  40. {
  41. HorizontalFieldManager hfm = new HorizontalFieldManager(FIELD_HCENTER);
  42. add(hfm);
  43.  
  44. mOKButton = new ButtonField("OK", ButtonField.CONSUME_CLICK);
  45. mOKButton.setChangeListener(this);
  46. hfm.add(mOKButton);
  47. mCancelButton = new ButtonField("Cancel", ButtonField.CONSUME_CLICK);
  48. mCancelButton.setChangeListener(this);
  49. hfm.add(mCancelButton);
  50. }
  51.  
  52. public void fieldChanged(Field field, int context)
  53. {
  54. if (mOKButton == field)
  55. {
  56. UiApplication.getUiApplication().invokeLater(new Runnable()
  57. {
  58. public void run()
  59. {
  60. Dialog.alert("You Selected: "+mDatePicker.getDate());
  61. }
  62. });
  63. }
  64. else if (mCancelButton == field)
  65. {
  66. close();
  67. }
  68. }
  69.  
  70. public void setDate(Date dateValue)
  71. {
  72. mDatePicker.setDate(dateValue);
  73. }
  74.  
  75. public String getDate()
  76. {
  77. return mDatePicker.getDate();
  78. }
  79.  
  80. class DatePickerField extends HorizontalFieldManager implements FieldChangeListener
  81. {
  82. private String[] daysOfWeek = new String[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
  83. // private String[] months = new String[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  84. private String[] months = new String[] { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12" };
  85. private int mDayOfMonth = 10;
  86. private int mMonth = 1;
  87. private int mYear = 2009;
  88.  
  89. private StrTimeField mDayOfWeekField;
  90. private StrTimeField mMonthField;
  91. private NumTimeField mDayOfMonthField;
  92. private NumTimeField mYearField;
  93. Calendar calendar = Calendar.getInstance();
  94.  
  95. public DatePickerField(Date date)
  96. {
  97. HorizontalFieldManager hr=new HorizontalFieldManager();
  98. calendar.setTime(date);
  99. mYear = calendar.get(Calendar.YEAR);
  100. // Calendar.JANUARY == 0, so +1 value
  101. mMonth = calendar.get(Calendar.MONTH);
  102. mDayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
  103. int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
  104. // think it's better to disable Day Of Week edit
  105. mDayOfWeekField = new StrTimeField(daysOfWeek, dayOfWeek - 1, NON_FOCUSABLE);
  106. mDayOfWeekField.setChangeListener(this);
  107. hr.add(mDayOfWeekField);
  108.  
  109. mDayOfMonthField = new NumTimeField(mDayOfMonth, 1, 31);
  110. mDayOfMonthField.setChangeListener(this);
  111. hr.add(mDayOfMonthField);
  112.  
  113. mMonthField = new StrTimeField(months, mMonth);
  114. mMonthField.setChangeListener(this);
  115. hr.add(mMonthField);
  116.  
  117. mYearField = new NumTimeField(mYear, 1900, 2012);
  118. mYearField.setChangeListener(this);
  119. hr.add(mYearField);
  120. add(hr);
  121. }
  122.  
  123. public void fieldChanged(Field field, int context)
  124. {
  125. mDayOfMonth = mDayOfMonthField.getValue();
  126. calendar.set(calendar.DAY_OF_MONTH, mDayOfMonth);
  127. mMonth = mMonthField.getValue();
  128. calendar.set(calendar.MONTH, mMonth);
  129. mYear = mYearField.getValue();
  130. calendar.set(calendar.YEAR, mYear);
  131. int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
  132. mDayOfWeekField.setIndex(dayOfWeek);
  133. }
  134.  
  135. public String getDate()
  136. {
  137. return mDayOfMonth+"-"+mMonth+"-"+mYear;
  138. }
  139.  
  140. public void setDate(Date date)
  141. {
  142. calendar.setTime(date);
  143. mYear = calendar.get(Calendar.YEAR);
  144. mMonth = calendar.get(Calendar.MONTH);
  145. mDayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
  146. int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
  147. mDayOfWeekField.setIndex(dayOfWeek - 1);
  148. mMonthField.setIndex(mMonth);
  149. mDayOfMonthField.setValue(mDayOfMonth);
  150. mYearField.setValue(mYear);
  151. }
  152.  
  153. abstract class TimeField extends LabelField
  154. {
  155. int mWidth = 0;
  156.  
  157. public TimeField()
  158. {
  159. super("", FOCUSABLE);
  160. }
  161.  
  162. public TimeField(long style)
  163. {
  164. super("", style);
  165. }
  166.  
  167. protected abstract void switchValue(int step);
  168.  
  169. protected boolean navigationMovement(int dx, int dy, int status, int time)
  170. {
  171. if (Math.abs(dy) > Math.abs(dx))
  172. {
  173. switchValue(-dy);
  174. return true;
  175. }
  176. else
  177. return super.navigationMovement(dx, dy, status, time);
  178. }
  179.  
  180. boolean prepared = false;
  181.  
  182. protected void onFocus(int direction)
  183. {
  184. prepared = false;
  185. super.onFocus(direction);
  186. }
  187.  
  188. protected void onUnfocus()
  189. {
  190. invalidate();
  191. super.onUnfocus();
  192. }
  193.  
  194. // comment on RIM OS < 4.7
  195. protected boolean touchEvent(TouchEvent msg) {
  196. if (isFocus() && msg.getEvent() == TouchEvent.CLICK) {
  197. if (!prepared) {
  198. prepared = true;
  199. } else {
  200. int y = msg.getY(1);
  201. int cy = getTop() + (getHeight() >> 1);
  202. switchValue((y > cy) ? -1 : 1);
  203. }
  204. }
  205. return false;
  206. }
  207.  
  208. public int getPreferredWidth() {
  209. return mWidth;
  210. }
  211.  
  212. public int getPreferredHeight() {
  213. return super.getPreferredHeight() + 24;
  214. }
  215.  
  216. protected void layout(int width, int height) {
  217. super.layout(width, height);
  218. setExtent(getPreferredWidth(), getPreferredHeight());
  219. }
  220.  
  221. protected void paint(Graphics graphics) {
  222. String text = getText();
  223. Font font = getFont();
  224. int x = (getPreferredWidth()
  225. - font.getAdvance(text)) >> 1;
  226. int y = (getPreferredHeight() - font.getHeight()) >> 1;
  227. graphics.drawText(text, x, y);
  228. if (isFocus()) {
  229. graphics.setColor(Color.WHITE);
  230. int xc = (getPreferredWidth() >> 1);
  231. int y1 = 10, y2 = 0, x2 = xc - 9, x1 = xc + 9;
  232.  
  233. int[] xPts = new int[] { x1, x2, xc };
  234. int[] yPts = new int[] { y1, y1, y2 };
  235. graphics.drawFilledPath(xPts, yPts,
  236. null, null);
  237.  
  238. y2 = getPreferredHeight();
  239. y1 = y2 - 10;
  240.  
  241. yPts = new int[] { y1, y1, y2 };
  242. graphics.drawFilledPath(xPts, yPts,
  243. null, null);
  244. }
  245. }
  246.  
  247. public abstract int getValue();
  248. }
  249.  
  250. class StrTimeField extends TimeField
  251. {
  252. String[] mValues;
  253. int mIndex;
  254.  
  255. public StrTimeField(String[] values)
  256. {
  257. this(values, 0);
  258. }
  259.  
  260. public StrTimeField(String[] values, int value)
  261. {
  262. this(values, value, FOCUSABLE);
  263. }
  264.  
  265. public StrTimeField(String[] values, int value, long style)
  266. {
  267. super(style);
  268. mValues = values;
  269. setIndex(value);
  270. Font font = getFont();
  271. for (int i = 0; i < mValues.length; i++) {
  272. int width = font.getAdvance(mValues[i]);
  273. mWidth = Math.max(mWidth, width);
  274. }
  275. mWidth += 10;
  276. }
  277.  
  278. protected void switchValue(int step)
  279. {
  280. int index = mIndex + step;
  281. if (index < 0 || index >= mValues.length)
  282. index += ((index > 0) ? -1 : 1)
  283. * mValues.length;
  284. setIndex(index);
  285. }
  286.  
  287. private void setIndex(int index) {
  288. if (index >= 0 && index < mValues.length) {
  289. mIndex = index;
  290. setText(mValues[mIndex]);
  291. }
  292. }
  293.  
  294. public int getValue() {
  295. return mIndex;
  296. }
  297.  
  298. }
  299.  
  300. class NumTimeField extends TimeField
  301. {
  302. int mValue;
  303. int mMinValue;
  304. int mMaxValue;
  305.  
  306. public NumTimeField(int val, int minVal, int maxVal)
  307. {
  308. this(val, minVal, maxVal, FOCUSABLE);
  309. }
  310.  
  311. public NumTimeField(int val, int minVal, int maxVal, long style)
  312. {
  313. super(style);
  314. mValue = val;
  315. mMinValue = minVal;
  316. mMaxValue = maxVal;
  317.  
  318. setText(String.valueOf(mValue));
  319. int maxDig = String.valueOf(mMaxValue).length();
  320. String test = "";
  321. for (int i = 0; i < maxDig; i++)
  322. test += "0";
  323. mWidth = getFont().getAdvance(test);
  324. mWidth += 10;
  325. }
  326.  
  327. protected void switchValue(int step)
  328. {
  329. int value = mValue + step;
  330. if (value > mMaxValue)
  331. value = value - (mMaxValue - mMinValue + 1);
  332. if (value < mMinValue)
  333. value = value + (mMaxValue - mMinValue + 1);
  334. setValue(value);
  335. }
  336.  
  337. private void setValue(int value)
  338. {
  339. mValue = value;
  340. setText(String.valueOf(mValue));
  341. }
  342.  
  343. public int getValue()
  344. {
  345. return mValue;
  346. }
  347. }
  348. }
  349. }
  350.  
  351.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement