📄 jxmonthview.java
字号:
} setFont(font); String imageLocation = UIManager.getString("JXMonthView.monthDownFileName"); if (imageLocation == null) { imageLocation = "resources/month-down.png"; } _monthDownImage = new ImageIcon( JXMonthView.class.getResource(imageLocation)); imageLocation = UIManager.getString("JXMonthView.monthUpFileName"); if (imageLocation == null) { imageLocation = "resources/month-up.png"; } _monthUpImage = new ImageIcon( JXMonthView.class.getResource(imageLocation)); } /** * Returns the first displayed date. * * @return long The first displayed date. */ public long getFirstDisplayedDate() { return _firstDisplayedDate; } /** * Set the first displayed date. We only use the month and year of * this date. The <code>Calendar.DAY_OF_MONTH</code> field is reset to * 1 and all other fields, with exception of the year and month , * are reset to 0. * * @param date The first displayed date. */ public void setFirstDisplayedDate(long date) { long old = _firstDisplayedDate; _cal.setTimeInMillis(date); _cal.set(Calendar.DAY_OF_MONTH, 1); _cal.set(Calendar.HOUR_OF_DAY, 0); _cal.set(Calendar.MINUTE, 0); _cal.set(Calendar.SECOND, 0); _cal.set(Calendar.MILLISECOND, 0); _firstDisplayedDate = _cal.getTimeInMillis(); _firstDisplayedMonth = _cal.get(Calendar.MONTH); _firstDisplayedYear = _cal.get(Calendar.YEAR); calculateLastDisplayedDate(); firePropertyChange("firstDisplayedDate", old, _firstDisplayedDate); repaint(); } /** * Returns the last date able to be displayed. For example, if the last * visible month was April the time returned would be April 30, 23:59:59. * * @return long The last displayed date. */ public long getLastDisplayedDate() { return _lastDisplayedDate; } private void calculateLastDisplayedDate() { long old = _lastDisplayedDate; _cal.setTimeInMillis(_firstDisplayedDate); // Figure out the last displayed date. _cal.add(Calendar.MONTH, ((_numCalCols * _numCalRows) - 1)); _cal.set(Calendar.DAY_OF_MONTH, _cal.getActualMaximum(Calendar.DAY_OF_MONTH)); _cal.set(Calendar.HOUR_OF_DAY, 23); _cal.set(Calendar.MINUTE, 59); _cal.set(Calendar.SECOND, 59); _lastDisplayedDate = _cal.getTimeInMillis(); firePropertyChange("lastDisplayedDate", old, _lastDisplayedDate); } /** * Moves the <code>date</code> into the visible region of the calendar. * If the date is greater than the last visible date it will become the * last visible date. While if it is less than the first visible date * it will become the first visible date. * * @param date Date to make visible. */ public void ensureDateVisible(long date) { if (date < _firstDisplayedDate) { setFirstDisplayedDate(date); } else if (date > _lastDisplayedDate) { _cal.setTimeInMillis(date); int month = _cal.get(Calendar.MONTH); int year = _cal.get(Calendar.YEAR); _cal.setTimeInMillis(_lastDisplayedDate); int lastMonth = _cal.get(Calendar.MONTH); int lastYear = _cal.get(Calendar.YEAR); int diffMonths = month - lastMonth + ((year - lastYear) * MONTHS_IN_YEAR); _cal.setTimeInMillis(_firstDisplayedDate); _cal.add(Calendar.MONTH, diffMonths); setFirstDisplayedDate(_cal.getTimeInMillis()); } if (_startSelectedDate != -1 || _endSelectedDate != -1) { calculateDirtyRectForSelection(); } } /** * Returns a date span of the selected dates. The result will be null if * no dates are selected. */ public DateSpan getSelectedDateSpan() { DateSpan result = null; if (_startSelectedDate != -1) { result = new DateSpan(new Date(_startSelectedDate), new Date(_endSelectedDate)); } return result; } /** * Selects the dates in the DateSpan. This method will not change the * initial date displayed so the caller must update this if necessary. * If we are in SINGLE_SELECTION mode only the start time from the DateSpan * will be used. If we are in WEEK_SELECTION mode the span will be * modified to be valid if necessary. * * @param dateSpan DateSpan defining the selected dates. Passing * <code>null</code> will clear the selection. */ public void setSelectedDateSpan(DateSpan dateSpan) { DateSpan oldSpan = null; if (_startSelectedDate != -1 && _endSelectedDate != -1) { oldSpan = new DateSpan(_startSelectedDate, _endSelectedDate); } if (dateSpan == null) { _startSelectedDate = -1; _endSelectedDate = -1; } else { _cal.setTimeInMillis(dateSpan.getStart()); _cal.set(Calendar.HOUR_OF_DAY, 0); _cal.set(Calendar.MINUTE, 0); _cal.set(Calendar.SECOND, 0); _cal.set(Calendar.MILLISECOND, 0); _startSelectedDate = _cal.getTimeInMillis(); if (_selectionMode == SINGLE_SELECTION) { _endSelectedDate = _startSelectedDate; } else { _cal.setTimeInMillis(dateSpan.getEnd()); _cal.set(Calendar.HOUR_OF_DAY, 0); _cal.set(Calendar.MINUTE, 0); _cal.set(Calendar.SECOND, 0); _cal.set(Calendar.MILLISECOND, 0); _endSelectedDate = _cal.getTimeInMillis(); if (_selectionMode == WEEK_SELECTION) { // Make sure if we are over 7 days we span full weeks. _cal.setTimeInMillis(_startSelectedDate); int count = 1; while (_cal.getTimeInMillis() < _endSelectedDate) { _cal.add(Calendar.DAY_OF_MONTH, 1); count++; } if (count > DAYS_IN_WEEK) { // Make sure start date is on the beginning of the // week. _cal.setTimeInMillis(_startSelectedDate); int dayOfWeek = _cal.get(Calendar.DAY_OF_WEEK); if (dayOfWeek != _firstDayOfWeek) { // Move the start date back to the first day of the // week. int daysFromStart = dayOfWeek - _firstDayOfWeek; if (daysFromStart < 0) { daysFromStart += DAYS_IN_WEEK; } _cal.add(Calendar.DAY_OF_MONTH, -daysFromStart); count += daysFromStart; _startSelectedDate = _cal.getTimeInMillis(); } // Make sure we have full weeks. Otherwise modify the // end date. int remainder = count % DAYS_IN_WEEK; if (remainder != 0) { _cal.setTimeInMillis(_endSelectedDate); _cal.add(Calendar.DAY_OF_MONTH, (DAYS_IN_WEEK - remainder)); _endSelectedDate = _cal.getTimeInMillis(); } } } } // Restore original time value. _cal.setTimeInMillis(_firstDisplayedDate); } repaint(_dirtyRect); calculateDirtyRectForSelection(); repaint(_dirtyRect); // Fire property change. firePropertyChange("selectedDates", oldSpan, dateSpan); } /** * Returns the current selection mode for this JXMonthView. * * @return int Selection mode. */ public int getSelectionMode() { return _selectionMode; } /** * Set the selection mode for this JXMonthView. * * @throws IllegalArgumentException */ public void setSelectionMode(int mode) throws IllegalArgumentException { if (mode != SINGLE_SELECTION && mode != MULTIPLE_SELECTION && mode != WEEK_SELECTION && mode != NO_SELECTION) { throw new IllegalArgumentException(mode + " is not a valid selection mode"); } _selectionMode = mode; } /** * An array of longs defining days that should be flagged. This array is * assumed to be in sorted order from least to greatest. */ public void setFlaggedDates(long[] flaggedDates) { _flaggedDates = flaggedDates; if (_flaggedDates == null) { repaint(); return; } // Loop through the flaggedDates and set the hour, minute, seconds and // milliseconds to 0 so we can compare times later. for (int i = 0; i < _flaggedDates.length; i++) { _cal.setTimeInMillis(_flaggedDates[i]); // We only want to compare the day, month and year // so reset all other values to 0. _cal.set(Calendar.HOUR_OF_DAY, 0); _cal.set(Calendar.MINUTE, 0); _cal.set(Calendar.SECOND, 0); _cal.set(Calendar.MILLISECOND, 0); _flaggedDates[i] = _cal.getTimeInMillis(); } // Restore the time. _cal.setTimeInMillis(_firstDisplayedDate); repaint(); } /** * Returns the padding used between days in the calendar. */ public int getBoxPaddingX() { return _boxPaddingX; } /** * Sets the number of pixels used to pad the left and right side of a day. * The padding is applied to both sides of the days. Therefore, if you * used the padding value of 3, the number of pixels between any two days * would be 6. */ public void setBoxPaddingX(int _boxPaddingX) { this._boxPaddingX = _boxPaddingX; _dirty = true; } /** * Returns the padding used above and below days in the calendar. */ public int getBoxPaddingY() { return _boxPaddingY; } /** * Sets the number of pixels used to pad the top and bottom of a day. * The padding is applied to both the top and bottom of a day. Therefore, * if you used the padding value of 3, the number of pixels between any * two days would be 6. */ public void setBoxPaddingY(int _boxPaddingY) { this._boxPaddingY = _boxPaddingY; _dirty = true; } /** * Returns whether or not the month view supports traversing months. * * @return <code>true</code> if month traversing is enabled. */ public boolean getTraversable() { return _traversable; } /** * Set whether or not the month view will display buttons to allow the * user to traverse to previous or next months. * * @param traversable set to true to enable month traversing, * false otherwise. */ public void setTraversable(boolean traversable) { _traversable = traversable; _dirty = true; repaint(); } /** * Sets the single character representation for each day of the * week. For this method the first days of the week days[0] is assumed to * be <code>Calendar.SUNDAY</code>. * * @throws IllegalArgumentException if <code>days.length</code> != DAYS_IN_WEEK * @throws NullPointerException if <code>days</code> == null */ public void setDaysOfTheWeek(String[] days) throws IllegalArgumentException, NullPointerException { if (days == null) { throw new NullPointerException("Array of days is null."); } else if (days.length != DAYS_IN_WEEK) { throw new IllegalArgumentException( "Array of days is not of length " + DAYS_IN_WEEK + " as expected."); } // TODO: This could throw off internal size information we should // call update and then recalculate displayed calendars and start // positions. _daysOfTheWeek = days; _dirty = true; repaint(); } /** * Returns the single character representation for each day of the * week.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -