This is my first mobile application which application mainly help for the doctors.I enjoyed so much for developing this application and i met some delicious point so i like to expose those.
Calender pop up screen
Here is CalendarPopupScreen.java
/**
*
* @author aruna
*/
import cutermize.field.BorderedLabelField;
import cutermize.field.ButtonBorderedLabelField;
import cutermize.field.HeaderBorderedLabelField;
import cutermize.field.TableLayoutManager;
import cutermize.field.UnfocusableBorderedLabelField;
import cutermize.field.VerticalSpacerField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.UiApplication;
import java.util.Calendar;
import java.util.Date;
import net.rim.device.api.util.DateTimeUtilities;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.i18n.SimpleDateFormat;
import net.rim.device.api.system.Characters;
import net.rim.device.api.system.Display;
public class CalendarPopupScreen extends PopupScreen {
//statics ------------------------------------------------------------------
private static SimpleDateFormat sdfWeekDay = new SimpleDateFormat("E");
private static SimpleDateFormat sdfMonth = new SimpleDateFormat("MMMM yyyy");
private static String SINGLE_BLANK = " ";
private static String FIELD_SIZE_STRING = " 30 ";
//variables ------------------------------------------------------------------
private int _selectedDay = -1; // None selected
private int _currentFocusDay;
private int _currentMonth; // This is the month as usually defined.
// In Calendar, January is month 0.
private String _currentMonthString;
private int _currentYear;
private Calendar _cl = Calendar.getInstance();
private LabelField _prevMonthButton;
private LabelField _currentMonthField;
private LabelField _nextMonthButton;
private static int [] tableStyles = new int [] {
TableLayoutManager.FIXED_WIDTH,
TableLayoutManager.FIXED_WIDTH,
TableLayoutManager.FIXED_WIDTH,
TableLayoutManager.FIXED_WIDTH,
TableLayoutManager.FIXED_WIDTH,
TableLayoutManager.FIXED_WIDTH,
TableLayoutManager.FIXED_WIDTH
};
private int [] tableSizes = new int [7] ;
private TableLayoutManager _monthManager; // This is where we display the Dates
private Field _initialFocusField = null;
private Font _normalFont;
private Font _boldFont;
public CalendarPopupScreen() {
this(new Date());
}
public CalendarPopupScreen(long dateMilliSecs) {
this(new Date(dateMilliSecs));
}
public CalendarPopupScreen(Date selectedDate) {
super(new VerticalFieldManager());
_cl.setTime(selectedDate);
createScreen(_cl.get(Calendar.DAY_OF_MONTH), _cl.get(Calendar.MONTH) + 1, _cl.get(Calendar.YEAR));
}
public CalendarPopupScreen(int focusDay, int startMonth, int startYear) {
super(new VerticalFieldManager(VerticalFieldManager.VERTICAL_SCROLL | VerticalFieldManager.VERTICAL_SCROLLBAR));
createScreen(focusDay, startMonth, startYear);
}
protected void onDisplay() {
if ( _initialFocusField != null ) {
_initialFocusField.setFocus();
_initialFocusField = null;
}
super.onDisplay();
}
private void createScreen(int focusDay, int startMonth, int startYear) {
// Initial values for Screen
_currentFocusDay = focusDay;
_currentMonth = startMonth;
_currentYear = startYear;
// Try to set Font based on Display Size so thjat Popup fits on Screen
_normalFont = this.getFont().derive(Font.PLAIN);
if ( _normalFont.getAdvance(FIELD_SIZE_STRING) > Display.getWidth()/10 ) {
// Font too big, make it a bit smaller
do {
_normalFont = _normalFont.derive(Font.PLAIN, _normalFont.getHeight()-1);
} while ( _normalFont.getAdvance(FIELD_SIZE_STRING) > Display.getWidth()/10 );
this.setFont(_normalFont);
}
_boldFont = _normalFont.derive(Font.BOLD);
// Create Month and button Fields once
HorizontalFieldManager hfm = new HorizontalFieldManager(HorizontalFieldManager.FIELD_HCENTER);
_prevMonthButton = new ButtonBorderedLabelField(SINGLE_BLANK + Characters.BLACK_LEFT_POINTING_TRIANGLE + SINGLE_BLANK,LabelField.FOCUSABLE);
hfm.add(_prevMonthButton);
_currentMonthField = new LabelField(SINGLE_BLANK + _currentMonthString + SINGLE_BLANK);
hfm.add(_currentMonthField);
_nextMonthButton = new ButtonBorderedLabelField(SINGLE_BLANK + Characters.BLACK_RIGHT_POINTING_TRIANGLE + SINGLE_BLANK,LabelField.FOCUSABLE);
hfm.add(_nextMonthButton);
this.add(hfm);
this.add(new VerticalSpacerField(3));
int columnSize = this.getFont().getAdvance(FIELD_SIZE_STRING);
for ( int i = 0; i < tableSizes.length; i++ ) {
tableSizes[i] = columnSize;
}
_monthManager = new TableLayoutManager(tableStyles, tableSizes, 0, TableLayoutManager.FIELD_HCENTER);
this.add(_monthManager);
displayMonth();
}
private void displayMonth() {
_monthManager.deleteAll(); // Delete the stuff currently there
// Determine start of the Month
_cl.set(Calendar.DAY_OF_MONTH, 1);
_cl.set(Calendar.MONTH, _currentMonth - 1);
_cl.set(Calendar.YEAR, _currentYear);
_cl.set(Calendar.HOUR_OF_DAY, 12);
_cl.set(Calendar.MINUTE, 0);
_cl.set(Calendar.SECOND, 0);
_cl.set(Calendar.MILLISECOND, 1);
long startOfMonth = _cl.getTime().getTime();
// set Month in 'header'
_currentMonthString = sdfMonth.formatLocal(_cl.getTime().getTime());
_currentMonthField.setText(SINGLE_BLANK + _currentMonthString + SINGLE_BLANK);
// Figure out where the month display needs to start
int workDay = _cl.get(Calendar.DAY_OF_WEEK);
int startAt = 0;
switch (workDay) {
case(Calendar.MONDAY): {
startAt = -6;
break;
}
case(Calendar.TUESDAY): {
startAt = -1;
break;
}
case(Calendar.WEDNESDAY): {
startAt = -2;
break;
}
case(Calendar.THURSDAY): {
startAt = -3;
break;
}
case(Calendar.FRIDAY): {
startAt = -4;
break;
}
case(Calendar.SATURDAY): {
startAt = -5;
break;
}
case(Calendar.SUNDAY): {
startAt = -6;
break;
}
}
Date workDate = _cl.getTime();
long workDateTime = workDate.getTime() + ((long)startAt) * ((long)DateTimeUtilities.ONEDAY);
long dayTime = workDateTime;
for ( int i = 0; i < 7; i++ ) {
String weekDay = sdfWeekDay.formatLocal(dayTime);
HeaderBorderedLabelField hblf = new HeaderBorderedLabelField(weekDay.substring(0,1), LabelField.USE_ALL_WIDTH | DrawStyle.HCENTER);
hblf.setFont(_boldFont);
_monthManager.add(hblf);
dayTime = dayTime + DateTimeUtilities.ONEDAY;
}
for ( int i = 0; i < 42; i++ ) { // Need at most 6 rows
workDate.setTime(workDateTime);
_cl.setTime(workDate);
workDateTime = workDateTime + DateTimeUtilities.ONEDAY;
BorderedLabelField blf = null;
int actualDate = _cl.get(Calendar.DAY_OF_MONTH);
String tempDateString = Integer.toString(actualDate);
if ( _cl.get(Calendar.MONTH) == _currentMonth - 1 ) {
blf = new BorderedLabelField(tempDateString, LabelField.USE_ALL_WIDTH | DrawStyle.HCENTER | LabelField.FOCUSABLE);
if ( _currentFocusDay == actualDate ) {
_initialFocusField = blf;
}
} else
if ( (i % 7 == 0) && (startOfMonth < _cl.getTime().getTime()) ) {
// We have finished the month
break;
} else {
blf = new UnfocusableBorderedLabelField(tempDateString, LabelField.USE_ALL_WIDTH | DrawStyle.HCENTER);
}
_monthManager.add(blf);
}
if ( this.isDisplayed() && _initialFocusField != null ) {
_initialFocusField.setFocus();
_initialFocusField = null;
}
}
public boolean keyChar(char key, int status, int time) {
boolean retval = false;
switch (key) {
case Characters.ENTER: {
// We have selected something
processFocus();
retval = true;
break;
}
case Characters.ESCAPE: {
close();
retval = true;
break;
}
default:
retval = super.keyChar(key, status, time);
break;
}
return retval;
}
private boolean processFocus() {
// Should deal with whatever the focsu is currently on
Field focusField = this.getDelegate().getLeafFieldWithFocus();
if ( focusField instanceof ButtonBorderedLabelField ) {
_currentFocusDay = -1; // Leave focus on 'button'
_initialFocusField = focusField;
int monthIncrement = 33;
if ( focusField == _prevMonthButton ) {
monthIncrement = -3;
}
_cl.set(Calendar.DAY_OF_MONTH, 1);
_cl.set(Calendar.MONTH, _currentMonth-1);
_cl.set(Calendar.YEAR, _currentYear);
Date workDate = _cl.getTime();
workDate.setTime(workDate.getTime() + (((long)monthIncrement) * ((long)DateTimeUtilities.ONEDAY)));
_cl.setTime(workDate);
_currentMonth = _cl.get(Calendar.MONTH) + 1;
_currentYear = _cl.get(Calendar.YEAR);
displayMonth();
return true;
} else
if ( focusField instanceof BorderedLabelField ) {
LabelField lab = (LabelField) focusField;
_selectedDay = Integer.parseInt(lab.getText());
close();
return true;
}
return false;
}
protected boolean trackwheelClick( int status, int time ) {
// Use navigationClick if only supporting devices post 4.2
if ( processFocus() ) {
return true;
}
return super.trackwheelClick(status, time);
}
public void close() {
UiApplication.getUiApplication().popScreen(this);
}
public Date getSelectedDate() {
if ( _selectedDay == -1 ) {
return null;
}
Calendar cl = Calendar.getInstance();
cl.set(Calendar.YEAR, _currentYear);
cl.set(Calendar.MONTH, _currentMonth - 1);
cl.set(Calendar.DAY_OF_MONTH, _selectedDay);
cl.set(Calendar.HOUR_OF_DAY, 0);
cl.set(Calendar.MINUTE, 0);
cl.set(Calendar.SECOND, 0);
cl.set(Calendar.MILLISECOND, 1);
return cl.getTime();
}
}
Here is CallenderDisplayScreen.java
import java.util.Date;
import net.rim.device.api.i18n.SimpleDateFormat;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.DateField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.BackgroundFactory;
import net.rim.device.api.ui.decor.BorderFactory;
/**
*
* @author se-3
*/
public class CallenderDisplayScreen extends MainScreen{
private static SimpleDateFormat sdFormatTimeTableDate = new SimpleDateFormat("yyyy-MM-dd");
public CallenderDisplayScreen(){
this.getMainManager().setBackground(
BackgroundFactory.createLinearGradientBackground(0x0099CCFF,
0x0099CCFF,0x00336699,0x00336699)
);
Bitmap borderBitmap = Bitmap.getBitmapResource("border.png");
VerticalFieldManager m = new VerticalFieldManager();
m.setBorder(BorderFactory.createBitmapBorder(new XYEdges(20,20,20,20), borderBitmap));
HorizontalFieldManager hfm = new HorizontalFieldManager();
m.add(hfm);
hfm.add(_fromTableTime);
add(m);
}
public DateField _fromTableTime = new DateField("Date:", System.currentTimeMillis(), DateField.READONLY, sdFormatTimeTableDate) {
public boolean trackwheelClick(int status, int time) {
CalendarPopupScreen cs = new CalendarPopupScreen(this.getDate());
UiApplication.getUiApplication().pushModalScreen(cs);
Date selectedDate = cs.getSelectedDate();
if ( selectedDate != null ) {
this.setDate(selectedDate.getTime());
}
return true;
}
};
}
Here is StartApp.java
import net.rim.device.api.ui.UiApplication;
public class StartApp extends UiApplication{
public static void main(String[] args){
StartApp app=new StartApp();
app.enterEventDispatcher();
}
public StartApp(){
pushScreen(new CallenderDisplayScreen());
}
}