KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > tags > CalendarTag


1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */

16 package dlog4j.tags;
17
18 import java.text.SimpleDateFormat JavaDoc;
19 import java.util.Calendar JavaDoc;
20 import java.util.Locale JavaDoc;
21
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.jsp.JspException JavaDoc;
24
25 import net.sf.hibernate.Session;
26
27 import dlog4j.LogManager;
28 import dlog4j.SiteManager;
29 import dlog4j.formbean.UserForm;
30
31 /**
32  * @author Liudong
33  * 日历标签库
34  */

35 public class CalendarTag extends DlogBaseTag {
36
37     protected String JavaDoc paramYear = "log_year";
38     protected String JavaDoc paramMonth = "log_month";
39     
40     protected String JavaDoc yearId = "year";
41     protected String JavaDoc monthId = "month";
42     protected String JavaDoc titleId = "title";
43     protected String JavaDoc datasId = "datas";
44     protected String JavaDoc logcId = "logcs";
45     
46     private final static SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc("MMMMM yyyy",Locale.ENGLISH);
47     /* (non-Javadoc)
48      * @see javax.servlet.jsp.tagext.Tag#doStartTag()
49      */

50     public int doStartTag() throws JspException JavaDoc {
51         Calendar JavaDoc cal = Calendar.getInstance();
52         try{
53             int year = Integer.parseInt(pageContext.getRequest().getParameter(paramYear));
54             if(year>0)
55                 cal.set(Calendar.YEAR,year);
56         }catch(Exception JavaDoc e){}
57         try{
58             int month = Integer.parseInt(pageContext.getRequest().getParameter(paramMonth));
59             if(month>0&&month<13)
60                 cal.set(Calendar.MONTH,month-1);
61             else if(month==0){
62                 cal.set(Calendar.MONTH,11);
63                 cal.add(Calendar.YEAR,-1);
64             }
65             else if(month==13){
66                 cal.set(Calendar.MONTH,0);
67                 cal.add(Calendar.YEAR,1);
68             }
69         }catch(Exception JavaDoc e){}
70         
71         int[][] datas = buildCalendar(cal);
72         pageContext.setAttribute(datasId,datas);
73         
74         pageContext.setAttribute(yearId,new Integer JavaDoc(cal.get(Calendar.YEAR)));
75         pageContext.setAttribute(monthId,new Integer JavaDoc(cal.get(Calendar.MONTH)+1));
76         
77         pageContext.setAttribute(titleId,sdf.format(cal.getTime()));
78         
79         //统计每天的日记数
80
Session ssn = null;
81         try{
82             ssn = getSession();
83             UserForm u = UserForm.getLoginUser((HttpServletRequest JavaDoc)pageContext.getRequest());
84             int[] lcs = LogManager.statLogs(ssn,SiteManager.getCurrentSite(pageContext.getRequest()),u,cal);
85             pageContext.setAttribute(logcId, lcs);
86         }catch(Exception JavaDoc e){
87             throw new JspException JavaDoc(e);
88         }finally{
89             if(ssn!=null)
90                 try{
91                     closeSession(ssn);
92                 }catch(Exception JavaDoc e){}
93         }
94         
95         return SKIP_BODY;
96     }
97     /**
98      * 构造日历
99      * @param year
100      * @param month
101      * @return
102      */

103     protected static int[][] buildCalendar(Calendar JavaDoc cal){
104         cal.set(Calendar.DAY_OF_MONTH,1);
105         int firstDateInWeek = cal.get(Calendar.DAY_OF_WEEK)-1;
106         int dateOfMonth = getMonthDateCount(cal);
107         int base = dateOfMonth + firstDateInWeek;
108         int row = base / 7;
109         row += ((base%7)>0)?1:0;
110         int[][] cals = new int[row][7];
111         int iCol=firstDateInWeek,iRow=0;
112         for(int i=1;i<=dateOfMonth;i++){
113             cals[iRow][iCol] = i;
114             if(iCol==6){
115                 iCol = 0;
116                 iRow++;
117             }
118             else
119                 iCol++;
120         }
121         return cals;
122     }
123     /**
124      * 得到指定月份的天数
125      * @param cal
126      * @return
127      */

128     protected static int getMonthDateCount(Calendar JavaDoc cal){
129         Calendar JavaDoc cal2 = (Calendar JavaDoc)cal.clone();
130         cal2.add(Calendar.MONTH,1);
131         cal2.set(Calendar.DAY_OF_MONTH,1);
132         cal2.add(Calendar.DAY_OF_MONTH,-1);
133         return cal2.get(Calendar.DAY_OF_MONTH);
134     }
135     
136     public static void main(String JavaDoc[] args) throws Exception JavaDoc{
137         int[][] days = buildCalendar(Calendar.getInstance());
138         for(int i=0;i<days.length;i++){
139             for(int j=0;j<days[i].length;j++)
140                 System.out.print(days[i][j]+" ");
141             System.out.println();
142         }
143     }
144
145     /**
146      * @return
147      */

148     public String JavaDoc getDatasId() {
149         return datasId;
150     }
151
152     /**
153      * @return
154      */

155     public String JavaDoc getMonthId() {
156         return monthId;
157     }
158
159     /**
160      * @return
161      */

162     public String JavaDoc getTitleId() {
163         return titleId;
164     }
165
166     /**
167      * @return
168      */

169     public String JavaDoc getYearId() {
170         return yearId;
171     }
172
173     /**
174      * @param string
175      */

176     public void setDatasId(String JavaDoc string) {
177         datasId = string;
178     }
179
180     /**
181      * @param string
182      */

183     public void setMonthId(String JavaDoc string) {
184         monthId = string;
185     }
186
187     /**
188      * @param string
189      */

190     public void setTitleId(String JavaDoc string) {
191         titleId = string;
192     }
193
194     /**
195      * @param string
196      */

197     public void setYearId(String JavaDoc string) {
198         yearId = string;
199     }
200
201     /**
202      * @return
203      */

204     public String JavaDoc getParamMonth() {
205         return paramMonth;
206     }
207
208     /**
209      * @return
210      */

211     public String JavaDoc getParamYear() {
212         return paramYear;
213     }
214
215     /**
216      * @param string
217      */

218     public void setParamMonth(String JavaDoc string) {
219         paramMonth = string;
220     }
221
222     /**
223      * @param string
224      */

225     public void setParamYear(String JavaDoc string) {
226         paramYear = string;
227     }
228
229     /**
230      * @return
231      */

232     public String JavaDoc getLogcId() {
233         return logcId;
234     }
235
236     /**
237      * @param string
238      */

239     public void setLogcId(String JavaDoc string) {
240         logcId = string;
241     }
242
243 }
244
Popular Tags