BrightSide Workbench Full Report + Source Code
HTMLCalendar.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.html.calendar;
19 
20 import java.io.IOException;
21 import java.util.Calendar;
22 import java.util.Date;
23 import java.util.Locale;
24 import org.turro.string.ObjectString;
25 import org.turro.elephant.context.IElement;
26 import org.turro.elephant.impl.util.Localizer;
27 import org.turro.html.HTMLComponent;
28 import org.turro.i18n.I_;
29 
34 public class HTMLCalendar extends HTMLComponent {
35  protected IDayRenderer dayRenderer = null;
36  protected Date date;
37  protected Locale locale;
38  protected String[] dayNames, monthNames;
39 
41  public HTMLCalendar(IElement iel) throws IOException {
42  super(iel);
44  locale = I_.api().used();
46  monthNames = CalendarUtil.getMonthNames(locale, false);
47 
48  // Calendar state
49  String sdate = constructor.getParameter("caldate");
50  if(sdate != null) {
51  date = (Date) ObjectString.parseString(sdate, ObjectString.COMPRESSED_DATE_PATTERN, Date.class, true);
53  }
54  else {
55  date = (Date)constructor.getSessionAttribute("caldate");
56  }
57  if(date == null) date = new Date();
58  }
59 
60  public Date getCurrentDate() {
61  // hour and minute to current
62  Calendar today = Calendar.getInstance(locale);
63  Calendar cal = Calendar.getInstance(locale);
64  cal.setTime(date);
65  cal.set(Calendar.HOUR_OF_DAY, today.get(Calendar.HOUR_OF_DAY));
66  cal.set(Calendar.MINUTE, today.get(Calendar.MINUTE));
67  cal.set(Calendar.SECOND, 0);
68  return date;
69  }
70 
72  this.dayRenderer = dayRenderer;
73  }
74 
75  public void renderMonth() {
76  Calendar cal = Calendar.getInstance(locale);
77  cal.setTime(date);
78  int day = cal.get(Calendar.DAY_OF_MONTH),
79  month = cal.get(Calendar.MONTH),
80  year = cal.get(Calendar.YEAR);
81  renderMonthTitle();
82  startTag("table", new String[] {
83  "cellspacing=1",
84  "cellpadding=0",
85  "class='wcalendar'"
86  });
87  startTag("thead");
88  startTag("tr");
89  startTag("td", "class='week'");
90  endTag();
91  for(int i = 0; i < 7; i++) {
92  startTag("td");
93  write(dayNames[i]);
94  endTag();
95  }
96  endTag();
97  endTag();
98  startTag("tbody");
100  while(true) {
101  startTag("tr");
102  startTag("td", "class='week'");
103  startAnchor(
104  "?calweek=" + cal.get(Calendar.WEEK_OF_YEAR),
105  "",
106  "daymonth"
107  );
108  write("" + cal.get(Calendar.WEEK_OF_YEAR));
109  endTag();
110  endTag();
111  for(int i = 0; i < 7; i++) {
112  if(cal.get(Calendar.MONTH) == month) {
113  if(cal.get(Calendar.DAY_OF_MONTH) == day) {
114  startTag("td", "class='selected'");
115  }
116  else {
117  startTag("td", "class='inmonth'");
118  }
119  }
120  else {
121  startTag("td", "class='outmonth'");
122  }
123  startAnchor(
124  "?caldate=" + ObjectString.formatObject(cal.getTime(), ObjectString.COMPRESSED_DATE_PATTERN, false),
125  "",
126  "daymonth"
127  );
128  write("" + cal.get(Calendar.DAY_OF_MONTH));
129  endTag();
130  if(dayRenderer != null) dayRenderer.renderDay(cal.getTime(), this);
131  cal.add(Calendar.DAY_OF_MONTH, 1);
132  }
133  endTag();
134  if(cal.get(Calendar.MONTH) > month || cal.get(Calendar.YEAR) > year) break;
135  }
136  endAllTags();
137  startTag("div", "class='wcalfooter'");
138  renderToday();
139  renderPreviousMonth();
140  renderNextMonth();
141  endTag();
142  }
143 
144  private void renderToday() {
145  startAnchor(
146  "?caldate=" + ObjectString.formatObject(new Date(), ObjectString.COMPRESSED_DATE_PATTERN, false),
147  ""
148  );
149  write(I_.get("Today"));
150  endTag();
151  }
152 
153  private void renderPreviousMonth() {
154  Calendar cal = Calendar.getInstance(locale);
155  cal.setTime(date);
156  cal.add(Calendar.MONTH, -1);
157  startAnchor(
158  "?caldate=" + ObjectString.formatObject(cal.getTime(), ObjectString.COMPRESSED_DATE_PATTERN, false),
159  ""
160  );
161  startTag("span", "class='previous'");
162  write(monthNames[cal.get(Calendar.MONTH)]);
163  endTag();
164  endTag();
165  }
166 
167  private void renderNextMonth() {
168  Calendar cal = Calendar.getInstance(locale);
169  cal.setTime(date);
170  cal.add(Calendar.MONTH, 1);
171  startAnchor(
172  "?caldate=" + ObjectString.formatObject(cal.getTime(), ObjectString.COMPRESSED_DATE_PATTERN, false),
173  ""
174  );
175  startTag("span", "class='next'");
176  write(monthNames[cal.get(Calendar.MONTH)]);
177  endTag();
178  endTag();
179  }
180 
181  private void renderMonthTitle() {
182  Localizer localizer = constructor.getLocalizer();
183  startTag("div", "class='wcaltitle'");
184  write(localizer.parseValue("date", "month_year", date));
185  endTag();
186  }
187 }
HTMLGenerator startTag(String tag)
HTMLGenerator startAnchor(String url, String hint)
HTMLGenerator write(String value)
Definition: HTMLHelper.java:69
static String[] getDayNames(Locale locale)
static String[] getMonthNames(Locale locale)
static void positionFirstCell(Calendar date)
void setDayRenderer(IDayRenderer dayRenderer)
static String get(String msg)
Definition: I_.java:41
static I18nApiWrapper api()
Definition: I_.java:65
void setSessionAttribute(String key, Object value)
void renderDay(Date date, HTMLCalendar calendar)