001: /**********************************************************************************
002: * $URL:https://source.sakaiproject.org/svn/osp/trunk/common/tool-lib/src/java/org/theospi/portfolio/shared/control/tag/DateSelectTag.java $
003: * $Id:DateSelectTag.java 9134 2006-05-08 20:28:42Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.theospi.portfolio.shared.control.tag;
021:
022: import java.io.IOException;
023: import java.util.Calendar;
024: import java.util.Date;
025: import java.util.GregorianCalendar;
026:
027: import javax.servlet.jsp.JspException;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031: import org.sakaiproject.component.cover.ComponentManager;
032: import org.sakaiproject.metaobj.shared.mgt.PortalParamManager;
033:
034: public class DateSelectTag extends DateSelectPopupTag {
035: protected final transient Log logger = LogFactory
036: .getLog(getClass());
037:
038: private String earliestYear = String.valueOf(Calendar.getInstance()
039: .get(Calendar.YEAR) - 5);
040: private String latestYear = String.valueOf(Calendar.getInstance()
041: .get(Calendar.YEAR) + 5);
042: private Date selectedDate;
043:
044: public int doStartTag() throws JspException {
045:
046: try {
047: StringBuffer buffer = new StringBuffer();
048: buffer.append("<select name=\"" + getMonthSelectId()
049: + "\" id=\"" + getMonthSelectId()
050: + "\" onchange=\"blur();\">\n");
051: buffer.append("<option value=\"\">\n");
052: for (int i = 1; i < 13; i++) {
053: buffer.append("<option value=\"" + i + "\"");
054: if (getMonthSelected() == i) {
055: buffer.append("selected=\"selected\"");
056: }
057: buffer.append(">" + getMonthName(i) + "</option>\n");
058: }
059: buffer.append("</select>");
060:
061: buffer.append("<select name=\"" + getDaySelectId()
062: + "\" id=\"" + getDaySelectId()
063: + "\" onchange=\"blur();\">\n");
064: buffer.append("<option value=\"\">\n");
065: for (int i = 1; i < 31; i++) {
066: buffer.append("<option value=\"" + i + "\"");
067: if (getDaySelected() == i) {
068: buffer.append("selected=\"selected\"");
069: }
070: buffer.append(">" + i + "</option>\n");
071: }
072: buffer.append("</select>");
073:
074: buffer.append("<select name=\"" + getYearSelectId()
075: + "\" id=\"" + getYearSelectId()
076: + "\" onchange=\"blur();\">\n");
077: buffer.append("<option value=\"\">\n");
078: for (int i = Integer.parseInt(earliestYear); i < Integer
079: .parseInt(latestYear) + 1; i++) {
080: buffer.append("<option value=\"" + i + "\"");
081: if (getYearSelected() == i) {
082: buffer.append("selected=\"selected\"");
083: }
084: buffer.append(">" + i + "</option>\n");
085: }
086: buffer.append("</select>");
087:
088: pageContext.getOut().write(buffer.toString());
089: super .doStartTag();
090: } catch (IOException e) {
091: logger.error("", e);
092: throw new JspException(e);
093: }
094:
095: return EVAL_BODY_INCLUDE;
096: }
097:
098: protected String getMonthName(int month) throws JspException {
099: switch (month) {
100: case 1:
101: return "JAN";
102: case 2:
103: return "FEB";
104: case 3:
105: return "MAR";
106: case 4:
107: return "APR";
108: case 5:
109: return "MAY";
110: case 6:
111: return "JUN";
112: case 7:
113: return "JUL";
114: case 8:
115: return "AUG";
116: case 9:
117: return "SEP";
118: case 10:
119: return "OCT";
120: case 11:
121: return "NOV";
122: case 12:
123: return "DEC";
124: }
125: throw new JspException(month + " is not a valid month");
126: }
127:
128: protected PortalParamManager getPortalParamManager() {
129: return (PortalParamManager) ComponentManager.getInstance().get(
130: PortalParamManager.class.getName());
131: }
132:
133: protected Calendar getCalendar() {
134: Calendar calendar = new GregorianCalendar();
135: calendar.setTime(selectedDate);
136: return calendar;
137: }
138:
139: protected int getMonthSelected() {
140: if (selectedDate == null)
141: return -1;
142: return getCalendar().get(Calendar.MONTH) + 1; // Calendar indexes months starting at 0
143: }
144:
145: protected int getYearSelected() {
146: if (selectedDate == null)
147: return -1;
148: return getCalendar().get(Calendar.YEAR);
149: }
150:
151: protected int getDaySelected() {
152: if (selectedDate == null)
153: return -1;
154: return getCalendar().get(Calendar.DAY_OF_MONTH);
155: }
156:
157: public String getEarliestYear() {
158: return earliestYear;
159: }
160:
161: public void setEarliestYear(String earliestYear) {
162: this .earliestYear = earliestYear;
163: }
164:
165: public String getLatestYear() {
166: return latestYear;
167: }
168:
169: public void setLatestYear(String lastestYear) {
170: this .latestYear = lastestYear;
171: }
172:
173: public Date getSelectedDate() {
174: return selectedDate;
175: }
176:
177: public void setSelectedDate(Date selectedDate) {
178: this.selectedDate = selectedDate;
179: }
180: }
|