Example usage for org.joda.time MutableDateTime addDays

List of usage examples for org.joda.time MutableDateTime addDays

Introduction

In this page you can find the example usage for org.joda.time MutableDateTime addDays.

Prototype

public void addDays(final int days) 

Source Link

Document

Add a number of days to the date.

Usage

From source file:com.tmathmeyer.sentinel.ui.views.day.DayCalendar.java

License:Open Source License

/**
 * Get visible events for the current day view
 * /* w w  w .  j  a va 2s .  c o m*/
 * @return returns the list of events to display
 */
private List<Displayable> getVisibleDisplayables() {
    // Set up from and to datetime for search
    MutableDateTime f = new MutableDateTime(time);
    f.setMillisOfDay(0);
    DateTime from = f.toDateTime();
    f.addDays(1);
    DateTime to = f.toDateTime();

    // Return list of events to be displayed
    List<Displayable> visibleDisplayables = new ArrayList<Displayable>();
    visibleDisplayables.addAll(EventClient.getInstance().getWithinRange(from, to));
    visibleDisplayables.addAll(CommitmentClient.getInstance().getCommitments(from, to));

    Collections.sort(visibleDisplayables, new Comparator<Displayable>() {
        public int compare(Displayable d1, Displayable d2) {
            return d1.getStart().getMinuteOfDay() < d2.getStart().getMinuteOfDay() ? -1
                    : d1.getStart().getMinuteOfDay() > d2.getStart().getMinuteOfDay() ? 1 : 0;
        }
    });

    return visibleDisplayables;
}

From source file:com.tmathmeyer.sentinel.ui.views.month.MonthCalendar.java

License:Open Source License

/**
 * /* w  ww .j av a  2 s  .c  o  m*/
 * @param fom the mutable date time
 */
public void generateHeaders(MutableDateTime fom) {
    // Set up label for month title
    monthLabel.setHorizontalAlignment(JLabel.CENTER);
    monthLabel.setFont(new java.awt.Font("DejaVu Sans", Font.BOLD, 25));

    // Set up the container title panel (only holds monthLabel for now)
    calendarTitlePanel.setLayout(new BorderLayout());
    calendarTitlePanel.add(monthLabel, BorderLayout.CENTER);

    // layout code
    mainCalendarView.setBackground(Colors.TABLE_BACKGROUND);
    mainCalendarView.setLayout(new BorderLayout());
    top.setLayout(new GridLayout(1, 7));

    mainCalendarView.add(top, BorderLayout.NORTH);
    mainCalendarView.add(inside, BorderLayout.CENTER);

    this.add(mainCalendarView, BorderLayout.CENTER);
    this.add(calendarTitlePanel, BorderLayout.NORTH);
    // end layout code

    fom.setDayOfMonth(1);
    fom.setMillisOfDay(0);
    int first = (fom.getDayOfWeek() % 7);
    fom.addDays(-first);

    // generate days on top
    for (int i = 0; i < 7; i++) {
        JLabel jl = new JLabel(fom.dayOfWeek().getAsText());
        jl.setHorizontalAlignment(SwingConstants.CENTER);
        fom.addDays(1);
        top.add(jl);
    }
}

From source file:com.tmathmeyer.sentinel.ui.views.month.MonthCalendar.java

License:Open Source License

/**
 * Adds or deletes a displayable from the days its occupies
 * /*ww  w  .  ja  v  a  2  s . c  om*/
 * @param e
 * @param add
 */
protected void traverseDisplayable(Displayable e, Lambda<MonthDay> func) {
    // get and normalize the range of the disp
    Interval ival = e.getInterval();
    MutableDateTime startDay = new MutableDateTime(ival.getStart());
    MutableDateTime endDay = new MutableDateTime(ival.getEnd());
    endDay.setMillisOfDay(0);
    startDay.setMillisOfDay(0);

    // bound it inside this visible month
    if (startDay.isBefore(firstOnMonth))
        startDay = new MutableDateTime(firstOnMonth);
    if (endDay.isAfter(lastOnMonth))
        endDay = new MutableDateTime(lastOnMonth);

    // go from start to end and add it to all the days in the way
    while (!endDay.isBefore(startDay)) {
        MonthDay md = this.days.get(startDay.getDayOfYear());
        if (md != null)
            func.call(md);
        startDay.addDays(1);
    }
}

From source file:com.tmathmeyer.sentinel.ui.views.month.MonthCalendar.java

License:Open Source License

/**
 * Fill calendar with month in referenceDay
 * //from  w  ww . jav a2s . co m
 * @param referenceDay what month should we display
 */
protected void generateDays(MutableDateTime referenceDay) {
    // reset to the first of the month at midnight, then find Sunday
    referenceDay.setDayOfMonth(1);
    referenceDay.setMillisOfDay(0);
    int first = (referenceDay.getDayOfWeek() % 7);
    int daysInView = first + referenceDay.dayOfMonth().getMaximumValue();
    int weeks = (int) Math.ceil(daysInView / 7.0);

    inside.setLayout(new java.awt.GridLayout(weeks, 7));
    referenceDay.addDays(-first);

    firstOnMonth = new DateTime(referenceDay);

    // remove all old days
    inside.removeAll();

    DateTime from = referenceDay.toDateTime();

    // generate days, weeks*7 covers all possible months, so we just loop
    // through and add each day
    for (int i = 0; i < (weeks * 7); i++) {
        MonthDay md = new MonthDay(referenceDay.toDateTime(), getMarker(referenceDay), this);
        inside.add(md);
        md.reBorder(i < 7, (i % 7) == 0, i >= (weeks - 1) * 7);
        this.days.put(referenceDay.getDayOfYear(), md);
        referenceDay.addDays(1); // go to next day
    }

    referenceDay.addDays(-1);// go back one to counteract last add one

    lastOnMonth = new DateTime(referenceDay);
    setDisplayables(getVisibleItems(from, referenceDay.toDateTime()));

    monthLabel.setText(this.getTime().toString(Months.monthLblFormat));

    // notify mini-calendar to change
    mainPanel.miniMove(time);

    // repaint when changed
    mainPanel.revalidate();
}

From source file:com.tmathmeyer.sentinel.ui.views.week.WeekCalendar.java

License:Open Source License

/**
 * Generates the day displays in the week panel
 *//*  ww  w .ja  v  a2s.  co  m*/
private void generateDay() {
    // clear out the specifics
    smithsonian.removeAll();
    headerBox.removeAll();

    // add the day grid back in
    hourLabels = new DayGridLabel();
    smithsonian.add(hourLabels, "cell 0 0,grow");

    MutableDateTime increment = new MutableDateTime(weekStartTime);
    increment.setMillisOfDay(0);
    DateTime now = DateTime.now().withMillisOfDay(0);

    displayableList = getVisibleDisplayables();

    for (int i = 0; i < 7; i++) {
        // add day views to the day grid
        this.daysOfWeekArray[i] = new DayPanel(true, this);
        this.daysOfWeekArray[i].setEvents(
                getDisplayablesInInterval(increment.toDateTime(), increment.toDateTime().plusDays(1)),
                increment.toDateTime());
        this.daysOfWeekArray[i]
                .setBorder(BorderFactory.createMatteBorder(1, 0, 1, i == 6 ? 0 : 1, Colors.BORDER));

        this.smithsonian.add(this.daysOfWeekArray[i], "cell " + (i + 1) + " 0,grow");

        // add day titles to the title grid
        dayHeaders[i].setText(increment.toDateTime().toString(dayTitleFmt));
        dayHeaders[i]
                .setFont(dayHeaders[i].getFont().deriveFont(increment.isEqual(now) ? Font.BOLD : Font.PLAIN));

        increment.addDays(1);
    }

    // populate and set up the multiDayEventGrid
    populateMultidayEventGrid();

    // setup week title
    increment.addDays(-1);

    // smart titles
    if (weekStartTime.getYear() != increment.getYear())
        weekTitle
                .setText(weekStartTime.toString(monthDayYearFmt) + " - " + increment.toString(monthDayYearFmt));
    else if (weekStartTime.getMonthOfYear() != increment.getMonthOfYear())
        weekTitle.setText(weekStartTime.toString(monthDayFmt) + " - " + increment.toString(monthDayYearFmt));
    else
        weekTitle.setText(weekStartTime.toString(monthDayFmt) + " - " + increment.toString(dayYearFmt));

    // notify mini-calendar to change
    mainPanel.miniMove(time);
}

From source file:com.tmathmeyer.sentinel.ui.views.week.WeekCalendar.java

License:Open Source License

/**
 * updates the week start and end variables
 * /*  ww  w. j  a  v a  2s .  c om*/
 * @param time a time in the week
 */
private void updateWeekStartAndEnd(DateTime time) {
    MutableDateTime mdt = new MutableDateTime(time);
    mdt.addDays(-(time.getDayOfWeek() % 7));
    mdt.setMillisOfDay(0);
    this.weekStartTime = mdt.toDateTime();
    mdt.addDays(7);
    this.weekEndTime = mdt.toDateTime();
}

From source file:com.tmathmeyer.sentinel.ui.views.week.WeekCalendar.java

License:Open Source License

/**
 * Selects an event's corresponding Displayable
 * //from  w  w w.  j  a v a2s .co m
 * @param on Event being selected
 * @param setTo Displayable of Event being selected
 */
private void selectEvents(Event on, Displayable setTo) {
    // TODO: refactor this pattern
    DayPanel mLouvreTour;
    MutableDateTime startDay = new MutableDateTime(on.getStart());
    MutableDateTime endDay = new MutableDateTime(on.getEnd());

    endDay.setMillisOfDay(0);
    endDay.addDays(1);
    endDay.addMillis(-1);
    startDay.setMillisOfDay(0);

    Interval eventLength = new Interval(startDay, endDay);
    if (setTo == null || eventLength.toDuration().getStandardHours() > 24) {
        for (WeekMultidayEventItem multidayItem : multidayItemList) {
            if (setTo != null && multidayItem.getEvent().getUuid().equals(((Event) on).getUuid()))
                multidayItem.setSelected(true);
            else
                multidayItem.setSelected(false);
        }
        return;
    }

    // TODO: can be simplified now that multiday events are handled
    // elsewhere
    int index = 0;
    for (int i = 0; i < 7; i++) {
        if (startDay.getDayOfYear() == daysOfWeekArray[i].getDisplayDate().getDayOfYear()) {
            index = i;
            break;
        }
    }

    while (index < 7 && !endDay.isBefore(daysOfWeekArray[index].getDisplayDate())) {
        mLouvreTour = daysOfWeekArray[index];
        try {
            mLouvreTour.select(setTo);
        } catch (NullPointerException ex) {
            // silently ignore as this is apparently not in the view
        }
        index++;
    }
}

From source file:com.tmathmeyer.sentinel.ui.views.year.YearCalendar.java

License:Open Source License

/**
 * /*from www  . ja  v  a  2 s  .  c  om*/
 * @param dt any date time
 * @return the date time that corresponds to january first of the year of
 *         the date time provided
 */
private MutableDateTime getStartOfYearCalendar(DateTime dt) {
    MutableDateTime jan1 = new MutableDateTime(new DateTime(dt.getYear(), 1, 1, 1, 1));
    jan1.addDays(0 - (jan1.getDayOfWeek()));
    return jan1;
}

From source file:com.tmathmeyer.sentinel.ui.views.year.YearCalendar.java

License:Open Source License

/**
 * //  w w w  .ja v  a2 s  .  c om
 * @param mdt a mutable date time on which to start the calendar. this can
 *            be used to make the dynamic month scrolling work if needed.
 */
private void drawCalendar(MutableDateTime mdt) {
    this.removeAll();
    mdt = this.calendarStart = getStartOfYearCalendar(mdt.toDateTime());

    MutableDateTime upperBound = mdt.copy();
    upperBound.addDays(378);
    getVisibleEvents(mdt.toDateTime(), upperBound.toDateTime());

    MutableDateTime yearDate = mdt.copy();
    yearDate.addDays(20);
    int year = yearDate.getYear();

    JLabel title = new JLabel(year + "");
    title.setHorizontalAlignment(SwingConstants.CENTER);
    title.setFont(new Font("DejaVu Sans", Font.BOLD, 25));
    JPanel content = new JPanel();

    this.add(title, BorderLayout.NORTH);
    this.add(content, BorderLayout.CENTER);

    MutableDateTime monthCounter = mdt.copy();

    content.setLayout(new BoxLayout(content, 0));
    for (int i = 0; i < 3; i++) {
        content.add(new Box.Filler(new Dimension(0, 0), new Dimension(0, 0), new Dimension(65566, 0)));
        content.add(getFourMonthLabel(monthCounter.copy()));
        content.add(getFourMonthGrid(mdt.copy()));
        content.add(new Box.Filler(new Dimension(0, 0), new Dimension(0, 0), new Dimension(65566, 0)));
        mdt.addDays(18 * 7);
        monthCounter.addMonths(4);
    }
}

From source file:com.tmathmeyer.sentinel.ui.views.year.YearCalendar.java

License:Open Source License

/**
 * gets the drawn year/*from  w  ww  .ja  v  a  2  s  . c  om*/
 * 
 * @param start the date time that starts the series.
 * @return the jpanel that contains the colored days and the S/M/T/W/R/F/S
 *         labels
 */
private JPanel getFourMonthGrid(MutableDateTime start) {
    int gridHeight = 19;
    int gridWidth = 7;

    JPanel p = new JPanel();
    p.setLayout(new GridLayout(gridHeight, gridWidth));

    String[] days = { "S", "M", "T", "W", "R", "F", "S" };
    for (int i = 0; i < 7; i++) {
        JLabel header = new JLabel(days[i]);
        header.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Colors.BORDER));
        header.setHorizontalAlignment(SwingConstants.CENTER);
        header.setFont(new Font("DejaVu Sans", Font.ITALIC, 12));
        p.add(header);
    }

    for (int j = 0; j < 18 * 7; j++) {
        Color dayBackground = start.getMonthOfYear() % 2 == 0 ? Colors.TABLE_BACKGROUND
                : Colors.TABLE_GRAY_HEADER;

        Integer eventCount = events.get(start.getDayOfYear());
        eventCount = eventCount == null ? 0 : eventCount;

        YearlyDayHolder day = new YearlyDayHolder(start.toDateTime(), dayBackground);
        MutableDateTime today = new MutableDateTime(DateTime.now());
        today.setMillisOfDay(0);
        MutableDateTime checking = new MutableDateTime(start);
        start.setMillisOfDay(0);
        if (checking.toDateTime().isEqual(today)) {
            day.setBackground(Colors.SELECTED_BACKGROUND);
            day.setForeground(Colors.SELECTED_TEXT);
        }
        JLabel dayLabel = new JLabel(start.getDayOfMonth() + "");
        dayLabel.setHorizontalAlignment(SwingConstants.CENTER);

        day.setLayout(new GridLayout(1, 1));
        day.add(dayLabel);
        day.setBorder(
                BorderFactory.createMatteBorder(0, start.getDayOfWeek() % 7 == 0 ? 1 : 0, 1, 1, Colors.BORDER));

        day.addMouseListener(new MouseListener() {

            @Override
            public void mouseClicked(MouseEvent me) {
                YearlyDayHolder event = (YearlyDayHolder) (me.getSource());
                MainPanel.getInstance().miniMove(event.getDateTime());
                MainPanel.getInstance().viewDay();
            }

            @Override
            public void mouseEntered(MouseEvent me) {
                // this is just a demo of what it can do
            }

            @Override
            public void mouseExited(MouseEvent me) {
            }

            @Override
            public void mousePressed(MouseEvent me) {
                // TODO: something? maybe nothing? have to decide with
                // team/steakholders
            }

            @Override
            public void mouseReleased(MouseEvent me) {
                YearlyDayHolder event = (YearlyDayHolder) (me.getSource());
                MainPanel.getInstance().miniMove(event.getDateTime());
            }

        });

        p.add(day);
        start.addDays(1);
    }

    int width = 280;
    int height = 570;

    p.setMinimumSize(new Dimension(0, height - 150));
    p.setPreferredSize(new Dimension(width, height));
    p.setMaximumSize(new Dimension(width + 350, height + 150));

    return p;
}