Example usage for org.joda.time MutableDateTime toDateTime

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

Introduction

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

Prototype

DateTime toDateTime();

Source Link

Document

Get this object as a DateTime.

Usage

From source file:com.tmathmeyer.sentinel.ui.navigation.MiniMonth.java

License:Open Source License

public MiniMonth(DateTime time, final MiniCalendarHostIface mc, boolean monthOnly) {
    this.setLayout(new GridLayout(7, 7));
    MutableDateTime prevMonth = new MutableDateTime(time);
    prevMonth.setDayOfMonth(1);//from w w w .j  a  v  a 2  s . c  o  m
    prevMonth.addMonths(-1); // What is prevMonth for?
    String[] dayLabel = { "S", "M", "T", "W", "R", "F", "S" };

    MouseListener monthChanger = new MouseListener() {
        @Override
        public void mouseClicked(MouseEvent me) {
        }

        @Override
        public void mouseEntered(MouseEvent me) {
        }

        @Override
        public void mouseExited(MouseEvent me) {
        }

        @Override
        public void mousePressed(MouseEvent me) {
        }

        @Override
        public void mouseReleased(MouseEvent me) {
            DayLabel d = (DayLabel) (me.getSource());
            if (!(d instanceof DescriptiveDayLabel)) {
                mc.display(d.getMonth());
            }
        }
    };

    MutableDateTime referenceDay = new MutableDateTime(time);
    // reset to the first of the month at midnight, then find Sunday
    referenceDay.setDayOfMonth(1);
    referenceDay.setMillisOfDay(0);
    int first = referenceDay.getDayOfWeek();
    referenceDay.addDays(-first);
    boolean flipFlop = false;

    // add day labels
    for (int i = 0; i < 7; i++) {
        DayLabel day = new DescriptiveDayLabel(dayLabel[i], time);
        day.borderize((i % 7) == 0, i >= 5 * 7, (i % 7) == 6);
        add(day);
        day.addMouseListener(monthChanger);
    }

    // generate days, 6*7 covers all possible months, so we just loop
    // through and add each day
    for (int i = 0; i < (6 * 7); i++) {
        DayLabel day;
        if (monthOnly || MainPanel.getInstance().getView() == ViewSize.Month) {
            if (referenceDay.getDayOfMonth() == 1)
                flipFlop ^= true; // flops the flip flop flappity flip
        } else if (MainPanel.getInstance().getView() == ViewSize.Day)
            flipFlop = referenceDay.getDayOfYear() == time.getDayOfYear()
                    && referenceDay.getYear() == time.getYear();
        else if (MainPanel.getInstance().getView() == ViewSize.Week) {
            if (Months.getWeekStart(time).getMonthOfYear() == 12
                    && Months.getWeekStart(time).getDayOfMonth() >= 26) // Exception
                // case
                // for
                // weeks
                // between
                // years
                flipFlop = time.getMonthOfYear() == 12 ? i >= 35 : i <= 6;
            else
                flipFlop = referenceDay.getDayOfYear() >= Months.getWeekStart(time).getDayOfYear()
                        && referenceDay.getDayOfYear() <= Months.getWeekStart(time).getDayOfYear() + 6;
        }

        if (flipFlop)
            day = new ActiveDayLabel(referenceDay.toDateTime());
        else
            day = new InactiveDayLabel(referenceDay.toDateTime());

        day.borderize((i % 7) == 0, i >= 5 * 7, (i % 7) == 6);
        add(day);
        day.addMouseListener(monthChanger);
        referenceDay.addDays(1); // go to next day
    }
}

From source file:com.tmathmeyer.sentinel.ui.tabs.AddCommitmentDisplay.java

License:Open Source License

/**
 * Sets the default date and time text fields to the current date and time
 * //from www.  jav a2 s  .co  m
 * Should be only called if creating a new commitment, not when editing
 * since edit event already has a date and time to fill the text fields with
 */
public void setCurrentDateAndTime() {
    this.startTimeDatePicker.setDate(selectedTime);
    MutableDateTime mdt = DateTime.now().toMutableDateTime();
    int quarterHours = mdt.getMinuteOfHour() / 15;
    int minutes = quarterHours < 4 ? (quarterHours + 1) * 15 : (quarterHours) * 15;
    if (minutes == 60) {
        mdt.addHours(1);
        mdt.setMinuteOfHour(0);
    } else
        mdt.setMinuteOfHour(minutes);
    this.startTimeDatePicker.setTime(mdt.toDateTime());
}

From source file:com.tmathmeyer.sentinel.ui.tabs.AddEventDisplay.java

License:Open Source License

/**
 * Sets the default date and time text fields to the current date and time
 * /* www  .  ja  va 2 s .  co m*/
 * Should be only called if creating a new event, not when editing since
 * edit event already has a date and time to fill the text fields with
 */
public void setCurrentDateAndTime() {
    this.startTimeDatePicker.setDate(selectedTime);
    this.endTimeDatePicker.setDate(selectedTime);

    MutableDateTime mdt = DateTime.now().toMutableDateTime();
    int quarterHours = mdt.getMinuteOfHour() / 15;
    int minutes = quarterHours < 4 ? (quarterHours + 1) * 15 : (quarterHours) * 15;
    if (minutes == 60) {
        mdt.addHours(1);
        mdt.setMinuteOfHour(0);
    } else
        mdt.setMinuteOfHour(minutes);

    this.startTimeDatePicker.setTime(mdt.toDateTime());
    mdt.addHours(1);
    this.endTimeDatePicker.setTime(mdt.toDateTime());
}

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

License:Open Source License

public void addMinutesToEnd(int minutes) {
    MutableDateTime d = displayable.getEnd().toMutableDateTime();
    d.addMinutes(minutes);//from   w w w  . j  a v a2s .  c o  m
    DateTime newEnd = d.toDateTime();
    try {
        Interval potential = new Interval(displayable.getStart(), newEnd);
        if (potential.toDurationMillis() < 1000) {
            throw new IllegalArgumentException("cant make the event that short!!");
        }
        height = Math.max(45, height + minutes);

        displayable.setEnd(newEnd);
        height = Math.max(45, height + minutes);
        length = potential;
        reval();
    } catch (IllegalArgumentException e) {
        // if the date time interval is malformed (the user dragged to a bad spot)
    }
}

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

License:Open Source License

public void addMinutesToStart(int minutes) {
    MutableDateTime d = displayable.getStart().toMutableDateTime();
    d.addMinutes(minutes);/*  w w  w .  j  a v a 2s  .  c o m*/
    DateTime newStart = d.toDateTime();

    try {
        Interval potential = new Interval(newStart, displayable.getEnd());
        if (potential.toDurationMillis() < 1000) {
            throw new IllegalArgumentException("cant make the event that short!!");
        }
        height = Math.max(45, height + minutes);

        displayable.setStart(newStart);
        height += minutes;
        length = potential;
        reval();
    } catch (IllegalArgumentException e) {
        // if the date time interval is malformed (the user dragged to a bad spot)
    }
}

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

License:Open Source License

private DateTime getTimeAtCursor() {
    Point m = MouseInfo.getPointerInfo().getLocation();
    Point p = getLocationOnScreen();
    MutableDateTime d = date.toMutableDateTime();
    m.y -= offset;/*from w  ww .java  2  s. co  m*/
    if (m.y < p.y)
        m.y = p.y;
    if (m.y > 1440)
        m.y = 1440;

    d.setHourOfDay((m.y - p.y) / 60);
    int min = (m.y - p.y) % 60;

    if (min >= 7 && min < 23)
        min = 15;
    if (min < 7)
        min = 0;
    if (min >= 23 && min < 38)
        min = 30;
    if (min >= 38 && min < 55)
        min = 45;
    if (min >= 55) {
        min = 0;
        d.addHours(1);
    }
    d.setMinuteOfHour(min);
    return d.toDateTime();
}

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

License:Open Source License

/**
 * Get visible events for the current day view
 * //from  w  w  w.  j  av  a  2  s  .c  om
 * @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

public void next() {
    MutableDateTime fom = new MutableDateTime(time);
    fom.addMonths(1);
    time = fom.toDateTime();
    generateDays(fom);
}

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

License:Open Source License

public void previous() {
    MutableDateTime fom = new MutableDateTime(time);
    fom.addMonths(-1);/*  w w  w  . j  a  v a2 s.  co  m*/
    time = fom.toDateTime();
    generateDays(fom);
}

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

License:Open Source License

/**
 * Fill calendar with month in referenceDay
 * //from  w  w  w . j  av  a 2  s  .c  o 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();
}