001 package org.apache.myfaces.tobago.model; 002 003 /* 004 * Licensed to the Apache Software Foundation (ASF) under one or more 005 * contributor license agreements. See the NOTICE file distributed with 006 * this work for additional information regarding copyright ownership. 007 * The ASF licenses this file to You under the Apache License, Version 2.0 008 * (the "License"); you may not use this file except in compliance with 009 * the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019 020 /* 021 * Created: Nov 20, 2002 10:05:10 PM 022 * $Id: CalendarModel.java 578592 2007-09-23 18:51:32Z bommel $ 023 */ 024 025 026 import java.text.DecimalFormat; 027 import java.text.SimpleDateFormat; 028 import java.util.Calendar; 029 030 public class CalendarModel { 031 032 private DateModel[][] calendarArray; 033 private int firstDayOffset; 034 //private int firstDayOfWeek; 035 036 public CalendarModel(Calendar calendar) { 037 // int weekCount = CalendarUtils.weekCount(calendar); 038 int weekCount = 6; // switching off dynamic weekCount! 039 calendarArray = new DateModel[weekCount][7]; 040 Calendar c = (Calendar) calendar.clone(); 041 c.clear(Calendar.DAY_OF_MONTH); 042 c.set(Calendar.DAY_OF_MONTH, 1); 043 // assert c.isLenient() : "'add -x days' may not work in a non-lenient calendar"; 044 firstDayOffset = firstDayOffset(c); 045 //firstDayOfWeek = c.getFirstDayOfWeek(); 046 c.add(Calendar.DAY_OF_WEEK, -firstDayOffset); 047 for (int week = 0; week < weekCount; ++week) { 048 for (int day = 0; day < 7; ++day) { 049 calendarArray[week][day] = new DateModel(c); 050 c.add(Calendar.DAY_OF_MONTH, 1); 051 } 052 } 053 } 054 055 public int getWeekCount() { 056 return calendarArray.length; 057 } 058 059 public int getMonth() { 060 return calendarArray[0][firstDayOffset].getMonth(); 061 } 062 063 public int getYear() { 064 return calendarArray[0][firstDayOffset].getYear(); 065 } 066 067 public DateModel getDate(int week, int day) { 068 return calendarArray[week][day]; 069 } 070 071 private int firstDayOffset(Calendar calendar) { 072 Calendar c = (Calendar) calendar.clone(); 073 c.clear(Calendar.DAY_OF_MONTH); 074 c.set(Calendar.DAY_OF_MONTH, 1); 075 int day = c.get(Calendar.DAY_OF_WEEK); 076 int firstDayOfWeek = c.getFirstDayOfWeek(); 077 // Fails: assertEquals((1+7-3)%7, (1-3)%7); 078 return (day + 7 - firstDayOfWeek) % 7; 079 } 080 081 public String toString() { 082 StringBuilder buffer = new StringBuilder(); 083 buffer.append("Month: ").append(getMonth()).append("\n"); 084 int weekCount = getWeekCount(); 085 DecimalFormat format = new DecimalFormat("00"); 086 SimpleDateFormat dateFormat = new SimpleDateFormat("E"); 087 for (int day = 0; day < 7; ++day) { 088 DateModel date = getDate(0, day); 089 String dayName = dateFormat.format(date.getCalendar().getTime()); 090 buffer.append(dayName.substring(0, 2)).append(" "); 091 } 092 buffer.append("\n"); 093 for (int week = 0; week < weekCount; ++week) { 094 for (int day = 0; day < 7; ++day) { 095 DateModel date = getDate(week, day); 096 buffer.append(format.format(date.getDay())).append(" "); 097 } 098 buffer.append("\n"); 099 } 100 return buffer.toString(); 101 } 102 103 }