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