Dear all expert

I want to generate hijri (jalali) date picker, there is no problem in
the UI design, but the callendar converter doesn't work accurately
with GWT. It should be mentioned that the converter itself is working
correctly and the problem starts when I try to use it in GWT.
Can anyone help me?????????????????

import com.google.gwt.user.client.*;
import java.util.Date;

import com.google.gwt.user.client.ui.*;

class CalendarWidget extends Composite
implements ClickListener, SourcesChangeEvents {

private class NavBar extends Composite implements ClickListener{

public final DockPanel bar = new DockPanel();
public final Button prevMonth = new Button("<", this);
public final Button prevYear = new Button("<<", this);
public final Button nextYear = new Button(">>", this);
public final Button nextMonth = new Button(">", this);
public final HTML title = new HTML();

private final CalendarWidget calendar;

public NavBar(CalendarWidget calendar) {
  this.calendar = calendar;

  setWidget(bar);
  bar.setStyleName("navbar");
  title.setStyleName("header");

  HorizontalPanel prevButtons = new HorizontalPanel();
  prevButtons.add(prevMonth);
  prevButtons.add(prevYear);

  HorizontalPanel nextButtons = new HorizontalPanel();
  nextButtons.add(nextYear);
  nextButtons.add(nextMonth);

  bar.add(prevButtons, DockPanel.WEST);
  bar.setCellHorizontalAlignment(prevButtons, DockPanel.ALIGN_LEFT);
  bar.add(nextButtons, DockPanel.EAST);
  bar.setCellHorizontalAlignment(nextButtons, DockPanel.ALIGN_RIGHT);
  bar.add(title, DockPanel.CENTER);
  bar.setVerticalAlignment(DockPanel.ALIGN_MIDDLE);
  bar.setCellHorizontalAlignment(title, HasAlignment.ALIGN_CENTER);
  bar.setCellVerticalAlignment(title, HasAlignment.ALIGN_MIDDLE);
  bar.setCellWidth(title, "100%");
}

public void onClick(Widget sender) {
  if (sender == prevMonth) {
    calendar.prevMonth();
  } else if (sender == prevYear) {
    calendar.prevYear();
  } else if (sender == nextYear) {
    calendar.nextYear();
  } else if (sender == nextMonth) {
    calendar.nextMonth();
  }
}
}

private static class CellHTML extends HTML {
private int day;

public CellHTML(String text, int day) {
  super(text);
  this.day = day;
}

public int getDay() {
  return day;
}
}

private final NavBar navbar = new NavBar(this);
private final DockPanel outer = new DockPanel();
private final Label label = new Label();

private final Grid grid = new Grid(6, 7) {
public boolean clearCell(int row, int column) {
  boolean retValue = super.clearCell(row, column);

  Element td = getCellFormatter().getElement(row, column);
  DOM.setInnerHTML(td, "");
  return retValue;
}
};

private Date date = gregorianToJalali(new Date(), 'Y');

private ChangeListenerCollection changeListeners;

private String[] days = new String[] { "شنبه", "یکشنبه", "دوشنبه",
  "سه شنبه", "چهارشنبه", "پنج شنبه", "جمعه" };

private String[] months = new String[] { "فروردین", "اردیبهشت",
"خرداد",
  "تیر", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی",
  "بهمن", "اسفند" };

public CalendarWidget() {
setWidget(outer);
grid.setStyleName("table");
grid.setCellSpacing(0);
outer.add(navbar, DockPanel.NORTH);
outer.add(grid, DockPanel.CENTER);
drawCalendar();
setStyleName("CalendarWidget");
}

private void drawCalendar() {
int year = getYear();
int month = getMonth();
int day = getDay();
setHeaderText(year, month);
grid.getRowFormatter().setStyleName(0, "weekheader");
for (int i = 0; i < days.length; i++) {
  grid.getCellFormatter().setStyleName(0, i, "days");
  grid.setText(0, i, days[i]);
}

Date now = gregorianToJalali(new Date(), 'Y');
int sameDay = now.getDate();
int today = (now.getMonth() == month && now.getYear()+1900 == year) ?
sameDay : 0;

int firstDay = gregorianToJalali(new Date(year - 1900, month, 1),
'Y').getDay();
int numOfDays = getDaysInMonth(year, month);

int j = 0;
for (int i = 1; i < 6; i++) {
  for (int k = 0; k < 7; k++, j++) {
    int displayNum = (j - firstDay + 1);
    if (j < firstDay || displayNum > numOfDays) {
      grid.getCellFormatter().setStyleName(i, k, "empty");
      grid.setHTML(i, k, "&nbsp;");
    } else {
      HTML html = new CellHTML(
        "<span>" + String.valueOf(displayNum) + "</span>",
        displayNum);
      html.addClickListener(this);
      grid.getCellFormatter().setStyleName(i, k, "cell");
      if (displayNum == today) {
        grid.getCellFormatter().addStyleName(i, k, "today");
      } else if (displayNum == sameDay) {
        grid.getCellFormatter().addStyleName(i, k, "day");
      }
      grid.setWidget(i, k, html);
    }
  }
}
}

protected void setHeaderText(int year, int month) {
navbar.title.setText(months[month] + ", " + year);
}

private int getDaysInMonth(int year, int month) {
switch (month) {
  case 1:
    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
      return 29; // leap year
    else
      return 28;
  case 3:
    return 30;
  case 5:
    return 30;
  case 8:
    return 30;
  case 10:
    return 30;
  default:
    return 31;
}
}

public void prevMonth() {
int month = getMonth() - 1;
if (month < 0) {
  setDate(getYear() - 1, 11, getDay());
} else {
  setMonth(month);
}
drawCalendar();
}

public void nextMonth() {
        int month = getMonth() + 1;
        if (month > 11) {
                setDate(getYear() + 1, 0, getDay());
        } else {
                setMonth(month);
        }
        drawCalendar();
}

public void prevYear() {
        setYear(getYear() - 1);
        drawCalendar();
}

public void nextYear() {
        setYear(getYear() + 1);
        drawCalendar();
}

private void setDate(int year, int month, int day) {
        date = gregorianToJalali(new Date(year - 1900, month, day), 'M');
}

private void setYear(int year) {
        date.setYear(year - 1900);
}

private void setMonth(int month) {
        date.setMonth(month);
}

public int getYear() {
        return 1900 + date.getYear();
}

public int getMonth() {
return date.getMonth();
}

public int getDay() {
return date.getDate();
}

public Date getDate() {
return gregorianToJalali(new Date(), 'M');
}

public void onClick(Widget sender) {
CellHTML cell = (CellHTML)sender;
setDate(getYear(), getMonth(), cell.getDay());
drawCalendar();
if (changeListeners != null) {
  changeListeners.fireChange(this);
}
}

public void addChangeListener(ChangeListener listener) {
if (changeListeners == null)
  changeListeners = new ChangeListenerCollection();
changeListeners.add(listener);
}

public void removeChangeListener(ChangeListener listener) {
if (changeListeners != null)
  changeListeners.remove(listener);
}

public static Date gregorianToJalali(Date nowDate, char choice) {
    String jalaliDate = "";

    double year = new Double(String.valueOf(nowDate.getYear()));
    double month = new Double(String.valueOf(nowDate.getMonth()));
    double day = new Double(String.valueOf(nowDate.getDay()));
    int hour = nowDate.getHours();
    int minute = nowDate.getMinutes();
    int second = nowDate.getSeconds();


    double g_days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30,
31, 30, 31};
    double j_days_in_month[] = {31, 31, 31, 31, 31, 31, 30, 30, 30,
30, 30, 29};

    double gy, gm, gd;
    double jy, jm, jd;
    double g_day_no, j_day_no;
    double j_np;

    int i;
    gy = year - 1600;
    gm = month - 1;
    gd = day - 1;

    g_day_no = 365 * gy + Math.floor((gy + 3) / 4) - Math.floor((gy +
99) / 100) + Math.floor((gy + 399) / 400);
    for (i = 0; i < gm; ++i) {
        g_day_no += g_days_in_month[i];
    }
    if (gm > 1 && ((gy % 4 == 0 && gy % 100 != 0) || (gy % 400 ==
0))) /* leap and after Feb */ {
        ++g_day_no;
    }
    g_day_no += gd;

    j_day_no = g_day_no - 79;

    j_np = Math.floor(j_day_no / 12053);
    j_day_no %= 12053;

    jy = 979 + 33 * j_np + 4 * Math.floor((j_day_no / 1461));
    j_day_no %= 1461;

    if (j_day_no >= 366) {
        jy += Math.floor((j_day_no - 1) / 365);
        j_day_no = (j_day_no - 1) % 365;
    }

    for (i = 0; i < 11 && j_day_no >=
j_days_in_month[Integer.parseInt(String.valueOf(i))]; ++i) {
        j_day_no -=
j_days_in_month[Integer.parseInt(String.valueOf(i))];
    }
    jm = i + 1;
    jd = j_day_no + 1;

    String strjm = new String(String.valueOf((int) jm));
    String strjd = new String(String.valueOf((int) jd));

    if (jm < 10) {
        strjm = "0" + (int) jm;
    }
    if (jd < 10) {
        strjd = "0" + (int) jd;
    }

    if (choice == 'y' || choice == 'Y') {
        jalaliDate = String.valueOf(jy);
    } else if (choice == 'm' || choice == 'M') {
        jalaliDate = strjm;
    } else if (choice == 'd' || choice == 'D') {
        jalaliDate = strjd;
    } else {
        jalaliDate = String.valueOf((int) jy) + '-' + strjm + '-' +
strjd + " " + hour + ":" + minute + ":" + second;
    }

    java.util.Date date = new java.util.Date();
    date.setYear((int) jy);
    date.setMonth((int) jm);
    date.setDate((int) jd);
    date.setHours((int) hour);
    date.setMinutes((int) minute);
    date.setSeconds((int) second);

    return date;
}
}

and in entry point i use:

FlowPanel fp = new FlowPanel();

        final CalendarWidget calendar = new CalendarWidget();
        calendar.addChangeListener(new ChangeListener() {

        public void onChange(Widget sender) {
          Window.alert("Date selected: " + calendar.getDate());
        }
      });
       fp.add(calendar);
       RootPanel.get("bmMenu").add(fp);


Regards

-- 
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-web-toolkit?hl=en.

Reply via email to