View Javadoc
1 /*** 2 * 3 * $Id$ 4 * 5 * Copyright (c) 2002-2003 JLCP.org 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a 8 * copy of this software and associated documentation files (the 9 * "Software"), to deal in the Software without restriction, including 10 * without limitation the rights to use, copy, modify, merge, publish, 11 * distribute, sublicense, and/or sell copies of the Software, and to 12 * permit persons to whom the Software is furnished to do so, subject to 13 * the following conditions: 14 * 15 * The above copyright notice and this permission notice shall be included 16 * in all copies or substantial portions of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 21 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 * 26 */ 27 28 package com.gotjava.model.calendar; 29 30 import com.gotjava.model.calendar.CalendarEvent; 31 32 import java.util.Date; 33 import java.util.Calendar; 34 import java.util.GregorianCalendar; 35 import java.util.Vector; 36 import java.util.Iterator; 37 import java.text.SimpleDateFormat; 38 /*** 39 * A collection of CalendarEvents with the same span. 40 */ 41 public class CalendarSpan { 42 43 public static final int YEAR = 0; 44 public static final int MONTH = 1; 45 public static final int WEEK = 2; 46 public static final int DAY = 3; 47 48 protected Date m_startDate; 49 protected Date m_endDate; 50 51 protected int m_span = MONTH; 52 53 protected Vector m_events = new Vector(); 54 55 /*** 56 * Creates a CalendarSpan of type 'span' for the given date. 57 * The start date of the span is INCLUSIVE and then end date is EXCLUSIVE. 58 * Therefore the span for the year 2003 would be 59 * 01/01/2003 00:00:00 - 01/01/2004 00:00:00. 60 * Week spans start on Sunday. 61 * @param span a valid span: YEAR, MONTH, WEEK, DAY 62 * @param date a date within the span 63 * 64 */ 65 public CalendarSpan(int span, Date date) { 66 m_span = span; 67 determineDates(date); 68 } 69 /*** 70 * get the span start date 71 */ 72 public Date getStartDate() { 73 return m_startDate; 74 } 75 76 /*** 77 * get the span end date 78 */ 79 public Date getEndDate() { 80 return m_endDate; 81 } 82 83 /*** 84 * get all events within the span 85 */ 86 public CalendarEvent[] getEvents() { 87 CalendarEvent[] events = new CalendarEvent[m_events.size()]; 88 for (int i = 0; i < m_events.size(); i++) { 89 events[i] = (CalendarEvent) m_events.elementAt(i); 90 } 91 return events; 92 } 93 94 /*** 95 * Add an event to the span 96 */ 97 public void addEvent(CalendarEvent event) { 98 if (inSpan(event.getDtstart())) { 99 m_events.addElement(event); 100 } 101 } 102 103 /*** 104 * Determine if the given date is within the span 105 */ 106 public boolean inSpan(Date date) { 107 return date.after(m_startDate) && date.before(m_endDate); 108 } 109 110 public int getSpanType(){ 111 return m_span; 112 } 113 114 public Iterator getSubSpan(int span){ 115 Iterator it = null; 116 if( span > m_span ){ 117 Vector v = new Vector(); 118 int field = Calendar.DAY_OF_WEEK; 119 switch(span){ 120 case MONTH : 121 field = Calendar.MONTH; 122 break; 123 case WEEK : 124 if(m_span == YEAR ) 125 field = Calendar.WEEK_OF_YEAR; 126 else 127 field = Calendar.WEEK_OF_MONTH; 128 break; 129 case DAY : 130 if(m_span == YEAR ) 131 field = Calendar.DAY_OF_YEAR; 132 else if (m_span == MONTH ) 133 field = Calendar.DAY_OF_MONTH; 134 else 135 field = Calendar.DAY_OF_WEEK; 136 break; 137 } 138 139 GregorianCalendar cal = new GregorianCalendar(); 140 cal.setTime(m_startDate); 141 int min = cal.getActualMinimum(field); 142 if(field == Calendar.WEEK_OF_MONTH) 143 min++; 144 int max = cal.getActualMaximum(field); 145 cal.set(field,min); 146 CalendarSpan firstSpan = new CalendarSpan(span,cal.getTime()); 147 for(int j=0; j < m_events.size(); j++){ 148 firstSpan.addEvent((CalendarEvent)m_events.get(j)); 149 } 150 151 v.add(firstSpan); 152 153 for(int i=0; i < (max-min); i++){ 154 cal.add(field,1); 155 Date current = cal.getTime(); 156 157 CalendarSpan cspan = new CalendarSpan(span,current); 158 159 for(int j=0; j < m_events.size(); j++){ 160 cspan.addEvent((CalendarEvent)m_events.get(j)); 161 } 162 v.add(cspan); 163 } 164 it = v.iterator(); 165 } 166 return it; 167 } 168 169 170 private void determineDates(Date date) { 171 172 GregorianCalendar gcal = new GregorianCalendar(); 173 gcal.setTime(date); 174 175 GregorianCalendar scal = new GregorianCalendar(); 176 GregorianCalendar ecal = new GregorianCalendar(); 177 178 switch (m_span) { 179 case YEAR : 180 int yyear = gcal.get(Calendar.YEAR); 181 scal.set(Calendar.YEAR, yyear); 182 scal.set(Calendar.MONTH, Calendar.JANUARY); 183 scal.set( 184 Calendar.DAY_OF_YEAR, 185 gcal.getActualMinimum(Calendar.DAY_OF_YEAR)); 186 187 ecal.set(Calendar.YEAR, yyear+1); 188 ecal.set(Calendar.MONTH, Calendar.JANUARY); 189 ecal.set( 190 Calendar.DAY_OF_YEAR, 191 gcal.getActualMinimum(Calendar.DAY_OF_YEAR)); 192 break; 193 case MONTH : 194 int myear = gcal.get(Calendar.YEAR); 195 int mmonth = gcal.get(Calendar.MONTH); 196 197 scal.set(Calendar.YEAR, myear); 198 scal.set(Calendar.MONTH, mmonth); 199 scal.set(Calendar.DAY_OF_MONTH, gcal.getActualMinimum(Calendar.DAY_OF_MONTH)); 200 201 ecal.set(Calendar.YEAR, myear); 202 ecal.set(Calendar.MONTH, mmonth+1); 203 ecal.set(Calendar.DAY_OF_MONTH, 1); 204 break; 205 case WEEK : 206 int wyear = gcal.get(Calendar.YEAR); 207 int wweek = gcal.get(Calendar.WEEK_OF_YEAR); 208 209 scal.set(Calendar.YEAR, wyear); 210 scal.set(Calendar.WEEK_OF_YEAR, wweek); 211 scal.set(Calendar.DAY_OF_WEEK, gcal.getActualMinimum(Calendar.DAY_OF_WEEK)); 212 213 ecal.set(Calendar.YEAR, wyear); 214 ecal.set(Calendar.WEEK_OF_YEAR, wweek+1); 215 ecal.set(Calendar.DAY_OF_WEEK, gcal.getActualMinimum(Calendar.DAY_OF_WEEK)); 216 break; 217 case DAY : 218 int dyear = gcal.get(Calendar.YEAR); 219 int dday = gcal.get(Calendar.DAY_OF_YEAR); 220 221 scal.set(Calendar.YEAR, dyear); 222 scal.set(Calendar.DAY_OF_YEAR, dday); 223 224 ecal.set(Calendar.YEAR, dyear); 225 ecal.set(Calendar.DAY_OF_YEAR, dday+1); 226 break; 227 } 228 229 scal.set(Calendar.HOUR_OF_DAY, 0); 230 scal.set(Calendar.MINUTE, 0); 231 scal.set(Calendar.SECOND, 0); 232 233 ecal.set(Calendar.MINUTE, 0); 234 ecal.set(Calendar.SECOND, 0); 235 ecal.set(Calendar.HOUR_OF_DAY, 0); 236 m_startDate = scal.getTime(); 237 m_endDate = ecal.getTime(); 238 239 } 240 241 242 }

This page was automatically generated by Maven