⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calendartag.java

📁 Tree taglib,生成树的标签库
💻 JAVA
字号:
/*
    Copyright 2004 Jenkov Development

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/



package com.jenkov.prizetags.calendar.impl;

import com.jenkov.prizetags.calendar.itf.IEvent;
import com.jenkov.prizetags.util.RequestUtil;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import java.util.*;


public class CalendarTag extends TagSupport{

    protected static final long ONE_DAY_IN_MILLIS = 1000 * 60 * 60 * 24;
    protected static final String NEXT_EVENT = "#nextEvent";
    public static final String CURRENT_DATE = "#currentDate";

    protected String events = null;
    protected String event  = null;
    protected String scope  = null;

    protected List eventList = null;

    protected int  currentDay        = 0;
    protected int  currentEventIndex = 0;
    protected long fromDateMillis    = 0;
    protected long toDateMillis      = 0;

    protected List[] days = null;

    public String getScope() {
        return scope;
    }

    public void setScope(String scope) {
        this.scope = scope;
    }

    public String getEvents() {
        return events;
    }

    public void setEvents(String events) {
        this.events = events;
    }

    public String getEvent() {
        return event;
    }

    public long getFromDateMillis() {
        return fromDateMillis;
    }

    public void setFromDateMillis(long fromDateMillis) {
        this.fromDateMillis = fromDateMillis;
    }

    public long getToDateMillis() {
        return toDateMillis;
    }

    public void setToDateMillis(long toDateMillis) {
        this.toDateMillis = toDateMillis;
    }

    public void setEvent(String event) {
        this.event = event;
    }

    public int doStartTag() throws JspException{
        //System.out.println("\n*** Start ***");
        this.currentDay        = 0;
        this.currentEventIndex = 0;

        Object o = RequestUtil.findObject(pageContext, getEvents(), getScope());
        if(o == null) throw new JspException("No listItems or array found at any scope for the name " + getEvents());
        if(o.getClass().isArray()){
            o = Arrays.asList((Object[]) o);
        }

        this.eventList = (List) o;
        Collections.sort(this.eventList, new EventDateComparator());
        this.days = prepareDayArray();


        setCurrentDate();


        if(this.days[0] != null && this.days[0].size() != 0){
            pageContext.getRequest().setAttribute(getEvent(), this.days[0].get(0));
            this.currentEventIndex = 1;
        }

        return EVAL_BODY_INCLUDE;
    }

    public int doAfterBody() throws JspException {

        if(moreEventsForCurrentDay()){
            moveToNextEvent();
        } else {
            moveToNextDay();
            if(this.currentDay >= this.days.length){
                return SKIP_BODY;
            }
            if(moreEventsForCurrentDay()){
                moveToNextEvent();
            }
        }
        setCurrentDate();

        return EVAL_BODY_AGAIN;
    }


    public void release() {
        this.currentDay = 0;
        this.currentEventIndex = 0;
        this.fromDateMillis = 0;
        this.toDateMillis = 0;

        if(this.days == null) return ;

        for(int i=0; i<this.days.length; i++){
            if(this.days[i] != null){
                this.days[i].clear();
                this.days[i] = null;
            }
        }
        this.days = null;
    }


    public void publishNextEvent(){
        IEvent event =   (IEvent) RequestUtil.findObject(pageContext, getEvent() + NEXT_EVENT, null);
        RequestUtil.storeObject(pageContext, getEvent(), null, event);
    }

    protected List[] prepareDayArray(){
        List[] days = new List[calculateNumberOfDays()];

        Iterator iterator = this.eventList.iterator();
        while(iterator.hasNext()){
            IEvent event = (IEvent) iterator.next();
            int dayArrayOffset = calculateDayDiff(this.fromDateMillis, event.getStartDate().getTime());
            if(dayArrayOffset >= days.length){
                continue;
            }
            if(days[dayArrayOffset] == null){
                days[dayArrayOffset] = new ArrayList();
            }
            days[dayArrayOffset].add(event);
        }

        return days;
    }

    protected int calculateNumberOfDays(){
        long intervalMillis = this.toDateMillis - this.fromDateMillis;
        long days = intervalMillis / ONE_DAY_IN_MILLIS;
        return (int)days + 1;
    }

    protected int calculateDayDiff(long date1Millis, long date2Millis){
        long interval = date2Millis - date1Millis;
        long dayOffset = interval / ONE_DAY_IN_MILLIS;
        return (int) dayOffset;
    }

    protected boolean moreEventsForCurrentDay(){
        if(this.days[this.currentDay] == null) return false;
        //System.out.println(this.days[this.currentDay].size());
        return this.currentEventIndex < this.days[this.currentDay].size();
    }


    protected void printDayArray(){
        for(int i=0; i< this.days.length; i++){
            System.out.println(this.days[i]);
        }
    }

    protected void setCurrentDate(){
        pageContext.getRequest().setAttribute(getEvent() + CURRENT_DATE,
                new Long(this.fromDateMillis + (currentDay * ONE_DAY_IN_MILLIS) ));
    }

    protected void moveToNextEvent(){
        pageContext.getRequest().setAttribute(getEvent(),
            this.days[this.currentDay].get(this.currentEventIndex));
        this.currentEventIndex++;
    }

    protected void moveToNextDay(){
        pageContext.getRequest().removeAttribute(getEvent());
        this.currentDay++;
        this.currentEventIndex = 0;
    }


    public long getCurrentDate(){
        return this.fromDateMillis + ONE_DAY_IN_MILLIS * this.currentDay;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -