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.views.month.MonthDay.java

License:Open Source License

public MonthDay(DateTime initDay, DayStyle style, final MonthCalendar parent) {
    this.day = initDay;
    this.style = style;
    Color grayit = Colors.TABLE_GRAY_HEADER, textit = Colors.TABLE_TEXT, bg = Colors.TABLE_BACKGROUND;
    switch (style) {
    case Normal:// w w  w.  ja v  a2s .  c o  m
        grayit = Colors.TABLE_GRAY_HEADER;
        textit = Colors.TABLE_GRAY_TEXT;
        break;
    case OutOfMonth:
        grayit = bg;
        break;
    case Today:
        grayit = Colors.TABLE_GRAY_HEADER;
        textit = Colors.TABLE_GRAY_TEXT;
        break;
    default:
        throw new IllegalStateException("DayStyle is not a valid DayStyle!");
    }
    setBackground(bg);
    setForeground(textit);
    borderTop = grayit.equals(bg);
    setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.Y_AXIS));

    header.setBackground(grayit);
    header.setForeground(textit);
    header.setFont(new java.awt.Font("DejaVu Sans", style == DayStyle.Today ? Font.BOLD : Font.PLAIN, 12));
    header.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
    header.setText(Integer.toString(initDay.getDayOfMonth()));
    header.setAutoscrolls(true);
    header.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
    header.setMaximumSize(new java.awt.Dimension(10000, 17));
    header.setOpaque(true);

    if (style == DayStyle.Today) {
        Font font = header.getFont();
        Map<TextAttribute, ?> attributes = font.getAttributes();
        Map<TextAttribute, Object> newtributes = new HashMap<>();
        newtributes.putAll(attributes);
        newtributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
        header.setFont(font.deriveFont(newtributes));
    }

    add(header);

    addMouseListener(new MouseListener() {
        @Override
        public void mousePressed(MouseEvent e) {
            parent.dispatchEvent(e);
            MainPanel.getInstance().setSelectedDay(day);
            MainPanel.getInstance().clearSelected();
            parent.setEscaped(false);
        }

        @Override
        public void mouseClicked(MouseEvent e) {

        }

        @Override
        public void mouseReleased(MouseEvent e) {
            if (parent.isEscaped()) {
                MonthDay releasedDay = parent.getMonthDayAtCursor();
                Displayable selected = MainPanel.getInstance().getSelectedEvent();
                if (selected != null && releasedDay != null) {
                    MutableDateTime newTime = new MutableDateTime(selected.getStart());

                    newTime.setYear(releasedDay.day.getYear());
                    newTime.setDayOfYear(releasedDay.day.getDayOfYear());

                    selected.setTime(newTime.toDateTime());

                    selected.update();
                }
            }
            parent.dispatchEvent(e);
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            parent.dispatchEvent(e);
        }

        @Override
        public void mouseExited(MouseEvent e) {
            setBackground(Colors.TABLE_BACKGROUND);
            parent.setEscaped(true);

        }
    });

    header.addMouseListener(new MouseListener() {

        @Override
        public void mousePressed(MouseEvent e) {
            MainPanel.getInstance().setSelectedDay(day);
            MainPanel.getInstance().clearSelected();
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            MainPanel.getInstance().miniMove(day);
            MainPanel.getInstance().viewDay();
        }

        @Override
        public void mouseReleased(MouseEvent e) {
        }

        @Override
        public void mouseEntered(MouseEvent e) {
        }

        @Override
        public void mouseExited(MouseEvent e) {
            setBackground(Colors.TABLE_BACKGROUND);
        }
    });

    addMouseMotionListener(new MouseMotionListener() {

        @Override
        public void mouseDragged(MouseEvent e) {
            parent.repaint();
            parent.dispatchEvent(e);
        }

        @Override
        public void mouseMoved(MouseEvent e) {
            parent.dispatchEvent(e);
        }

    });
}

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

License:Open Source License

/**
 * Generates the day displays in the week panel
 *///from w ww. ja v a 2 s .  c o 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
 * /*from   w  w  w  . j  a v  a2s.co m*/
 * @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.year.YearCalendar.java

License:Open Source License

/**
 * //from  www . j a v  a  2s  .c  o  m
 * @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 www . j a  v a2  s .co m*/
 * 
 * @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;
}

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

License:Open Source License

@Override
public void previous() {
    MutableDateTime mdt = new MutableDateTime(this.calendarStart);
    mdt.addYears(-2);/*from   ww  w  . j a  v  a2 s  .  c om*/
    this.display(mdt.toDateTime());
}

From source file:com.tmathmeyer.sentinel.utils.Months.java

License:Open Source License

public static DateTime nextMonth(DateTime time) {
    MutableDateTime mdt = new MutableDateTime(time);
    mdt.addMonths(1);/*from   w ww  . j  a  va  2 s  .com*/
    return mdt.toDateTime();
}

From source file:com.tmathmeyer.sentinel.utils.Months.java

License:Open Source License

public static DateTime prevMonth(DateTime time) {
    MutableDateTime mdt = new MutableDateTime(time);
    mdt.addMonths(-1);//  w  w w  .ja  va2  s  .  c o m
    return mdt.toDateTime();
}

From source file:com.tmathmeyer.sentinel.utils.Months.java

License:Open Source License

public static DateTime nextDay(DateTime time) {
    MutableDateTime mdt = new MutableDateTime(time);
    mdt.addDays(1);/*from   w  ww  .j  a  v a 2s. co  m*/
    return mdt.toDateTime();
}

From source file:com.tmathmeyer.sentinel.utils.Months.java

License:Open Source License

public static DateTime prevDay(DateTime time) {
    MutableDateTime mdt = new MutableDateTime(time);
    mdt.addDays(-1);/*from   w w w .j  av  a 2 s .c  om*/
    return mdt.toDateTime();
}