に応募しました。カレンダー出力するだけのプログラムが対象なのでお気軽にできるかなーというのと、久しぶりにGWTで書いてみようかということでチャレンジしましたが、意外と手こずり、3時間もかかってしまいました。GWTではCalendarクラスが使えないので、代替するクラスを調べたり自作したり、、、という余計な手間がかかったのが原因です。見栄えのところは全く手付かずで、とりあえずLabelにHTML出力しただけです。ソースコードは下記の通り。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.ochilab.pronama.calendar.client; | |
import java.util.Date; | |
import java.util.HashMap; | |
import java.util.Map; | |
import com.google.gwt.core.client.EntryPoint; | |
import com.google.gwt.i18n.client.DateTimeFormat; | |
import com.google.gwt.i18n.client.NumberFormat; | |
import com.google.gwt.safehtml.shared.SafeHtmlBuilder; | |
import com.google.gwt.user.client.ui.HTML; | |
import com.google.gwt.user.client.ui.RootPanel; | |
import com.google.gwt.user.datepicker.client.CalendarUtil; | |
/** | |
* Entry point classes define <code>onModuleLoad()</code>. | |
*/ | |
public class ProNamaCalendar implements EntryPoint { | |
/** | |
* This is the entry point method. | |
*/ | |
public void onModuleLoad() { | |
/* GWTではCalendarクラスが使えないので変なことしてます*/ | |
//現在の日時の入手 | |
Date startDate = new Date(); | |
//月初め | |
CalendarUtil.setToFirstDayOfMonth(startDate); | |
//月初めの曜日番号を求める | |
int dayNum = getDayNumber(startDate); | |
//次の月初めを求める | |
Date endDate = CalendarUtil.copyDate(startDate); | |
CalendarUtil.addMonthsToDate(endDate, 1); | |
//1日ずらして今月末にする | |
CalendarUtil.addDaysToDate(endDate, -1); | |
//今月の日数を求める | |
int daysBetween = CalendarUtil.getDaysBetween(startDate, endDate); | |
daysBetween+=2; | |
SafeHtmlBuilder shb = new SafeHtmlBuilder(); | |
int index; | |
for(index=0;index<dayNum;index++){ | |
shb.appendHtmlConstant("-- "); | |
} | |
for(int i=1; i<daysBetween;i++){ | |
if(index%7==0){ | |
System.out.println(""); | |
shb.appendHtmlConstant("<br>"); | |
} | |
String s = NumberFormat.getFormat("00").format(i); | |
shb.appendEscaped(s+" "); | |
index++; | |
} | |
//とりあえずLabelに出力 | |
HTML label = new HTML(shb.toSafeHtml()); | |
RootPanel.get("nameFieldContainer").add(label); | |
} | |
/** | |
* | |
* GWT では Calendarクラスが使えないので。。。。 | |
* @param day | |
* @return | |
*/ | |
private int getDayNumber(Date day){ | |
DateTimeFormat fmt = DateTimeFormat.getFormat("EEE"); | |
Map<String, String> map = new HashMap<String,String>(); | |
map.put("Sun", "0"); | |
map.put("Mon", "1"); | |
map.put("Tue", "2"); | |
map.put("Wed", "3"); | |
map.put("Thr", "4"); | |
map.put("Fri", "5"); | |
map.put("Sat", "6"); | |
String temp = fmt.format(day); | |
return Integer.parseInt(map.get(temp)); | |
} | |
} |