alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (SpinnerDateModel.java)

This example Java source code file (SpinnerDateModel.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

abstractspinnermodel, calendar, comparable, date, illegalargumentexception, object, serializable, spinnerdatemodel, util

The SpinnerDateModel.java Java example source code

/*
 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package javax.swing;

import java.util.*;
import java.io.Serializable;


/**
 * A <code>SpinnerModel for sequences of Dates.
 * The upper and lower bounds of the sequence are defined by properties called
 * <code>start and end and the size
 * of the increase or decrease computed by the <code>nextValue
 * and <code>previousValue methods is defined by a property
 * called <code>calendarField.  The start
 * and <code>end properties can be null to
 * indicate that the sequence has no lower or upper limit.
 * <p>
 * The value of the <code>calendarField property must be one of the
 * <code>java.util.Calendar constants that specify a field
 * within a <code>Calendar.  The getNextValue
 * and <code>getPreviousValue
 * methods change the date forward or backwards by this amount.
 * For example, if <code>calendarField is Calendar.DAY_OF_WEEK,
 * then <code>nextValue produces a Date that's 24
 * hours after the current <code>value, and previousValue
 * produces a <code>Date that's 24 hours earlier.
 * <p>
 * The legal values for <code>calendarField are:
 * <ul>
 *   <li>Calendar.ERA
 *   <li>Calendar.YEAR
 *   <li>Calendar.MONTH
 *   <li>Calendar.WEEK_OF_YEAR
 *   <li>Calendar.WEEK_OF_MONTH
 *   <li>Calendar.DAY_OF_MONTH
 *   <li>Calendar.DAY_OF_YEAR
 *   <li>Calendar.DAY_OF_WEEK
 *   <li>Calendar.DAY_OF_WEEK_IN_MONTH
 *   <li>Calendar.AM_PM
 *   <li>Calendar.HOUR
 *   <li>Calendar.HOUR_OF_DAY
 *   <li>Calendar.MINUTE
 *   <li>Calendar.SECOND
 *   <li>Calendar.MILLISECOND
 * </ul>
 * However some UIs may set the calendarField before committing the edit
 * to spin the field under the cursor. If you only want one field to
 * spin you can subclass and ignore the setCalendarField calls.
 * <p>
 * This model inherits a <code>ChangeListener.  The
 * <code>ChangeListeners are notified whenever the models
 * <code>value, calendarField,
 * <code>start, or end properties changes.
 *
 * @see JSpinner
 * @see SpinnerModel
 * @see AbstractSpinnerModel
 * @see SpinnerListModel
 * @see SpinnerNumberModel
 * @see Calendar#add
 *
 * @author Hans Muller
 * @since 1.4
 */
public class SpinnerDateModel extends AbstractSpinnerModel implements Serializable
{
    private Comparable start, end;
    private Calendar value;
    private int calendarField;


    private boolean calendarFieldOK(int calendarField) {
        switch(calendarField) {
        case Calendar.ERA:
        case Calendar.YEAR:
        case Calendar.MONTH:
        case Calendar.WEEK_OF_YEAR:
        case Calendar.WEEK_OF_MONTH:
        case Calendar.DAY_OF_MONTH:
        case Calendar.DAY_OF_YEAR:
        case Calendar.DAY_OF_WEEK:
        case Calendar.DAY_OF_WEEK_IN_MONTH:
        case Calendar.AM_PM:
        case Calendar.HOUR:
        case Calendar.HOUR_OF_DAY:
        case Calendar.MINUTE:
        case Calendar.SECOND:
        case Calendar.MILLISECOND:
            return true;
        default:
            return false;
        }
    }


    /**
     * Creates a <code>SpinnerDateModel that represents a sequence of dates
     * between <code>start and end.  The
     * <code>nextValue and previousValue methods
     * compute elements of the sequence by advancing or reversing
     * the current date <code>value by the
     * <code>calendarField time unit.  For a precise description
     * of what it means to increment or decrement a <code>Calendar
     * <code>field, see the add method in
     * <code>java.util.Calendar.
     * <p>
     * The <code>start and end parameters can be
     * <code>null to indicate that the range doesn't have an
     * upper or lower bound.  If <code>value or
     * <code>calendarField is null, or if both
     * <code>start and end are specified and
     * <code>minimum > maximum then an
     * <code>IllegalArgumentException is thrown.
     * Similarly if <code>(minimum <= value <= maximum) is false,
     * an IllegalArgumentException is thrown.
     *
     * @param value the current (non <code>null) value of the model
     * @param start the first date in the sequence or <code>null
     * @param end the last date in the sequence or <code>null
     * @param calendarField one of
     *   <ul>
     *    <li>Calendar.ERA
     *    <li>Calendar.YEAR
     *    <li>Calendar.MONTH
     *    <li>Calendar.WEEK_OF_YEAR
     *    <li>Calendar.WEEK_OF_MONTH
     *    <li>Calendar.DAY_OF_MONTH
     *    <li>Calendar.DAY_OF_YEAR
     *    <li>Calendar.DAY_OF_WEEK
     *    <li>Calendar.DAY_OF_WEEK_IN_MONTH
     *    <li>Calendar.AM_PM
     *    <li>Calendar.HOUR
     *    <li>Calendar.HOUR_OF_DAY
     *    <li>Calendar.MINUTE
     *    <li>Calendar.SECOND
     *    <li>Calendar.MILLISECOND
     *   </ul>
     *
     * @throws IllegalArgumentException if <code>value or
     *    <code>calendarField are null,
     *    if <code>calendarField isn't valid,
     *    or if the following expression is
     *    false: <code>(start <= value <= end).
     *
     * @see Calendar#add
     * @see #setValue
     * @see #setStart
     * @see #setEnd
     * @see #setCalendarField
     */
    public SpinnerDateModel(Date value, Comparable start, Comparable end, int calendarField) {
        if (value == null) {
            throw new IllegalArgumentException("value is null");
        }
        if (!calendarFieldOK(calendarField)) {
            throw new IllegalArgumentException("invalid calendarField");
        }
        if (!(((start == null) || (start.compareTo(value) <= 0)) &&
              ((end == null) || (end.compareTo(value) >= 0)))) {
            throw new IllegalArgumentException("(start <= value <= end) is false");
        }
        this.value = Calendar.getInstance();
        this.start = start;
        this.end = end;
        this.calendarField = calendarField;

        this.value.setTime(value);
    }


    /**
     * Constructs a <code>SpinnerDateModel whose initial
     * <code>value is the current date, calendarField
     * is equal to <code>Calendar.DAY_OF_MONTH, and for which
     * there are no <code>start/end limits.
     */
    public SpinnerDateModel() {
        this(new Date(), null, null, Calendar.DAY_OF_MONTH);
    }


    /**
     * Changes the lower limit for Dates in this sequence.
     * If <code>start is null,
     * then there is no lower limit.  No bounds checking is done here:
     * the new start value may invalidate the
     * <code>(start <= value <= end)
     * invariant enforced by the constructors.  This is to simplify updating
     * the model.  Naturally one should ensure that the invariant is true
     * before calling the <code>nextValue, previousValue,
     * or <code>setValue methods.
     * <p>
     * Typically this property is a <code>Date however it's possible to use
     * a <code>Comparable with a compareTo method for Dates.
     * For example <code>start might be an instance of a class like this:
     * <pre>
     * MyStartDate implements Comparable {
     *     long t = 12345;
     *     public int compareTo(Date d) {
     *            return (t < d.getTime() ? -1 : (t == d.getTime() ? 0 : 1));
     *     }
     *     public int compareTo(Object o) {
     *            return compareTo((Date)o);
     *     }
     * }
     * </pre>
     * Note that the above example will throw a <code>ClassCastException
     * if the <code>Object passed to compareTo(Object)
     * is not a <code>Date.
     * <p>
     * This method fires a <code>ChangeEvent if the
     * <code>start has changed.
     *
     * @param start defines the first date in the sequence
     * @see #getStart
     * @see #setEnd
     * @see #addChangeListener
     */
    public void setStart(Comparable start) {
        if ((start == null) ? (this.start != null) : !start.equals(this.start)) {
            this.start = start;
            fireStateChanged();
        }
    }


    /**
     * Returns the first <code>Date in the sequence.
     *
     * @return the value of the <code>start property
     * @see #setStart
     */
    public Comparable getStart() {
        return start;
    }


    /**
     * Changes the upper limit for <code>Dates in this sequence.
     * If <code>start is null, then there is no upper
     * limit.  No bounds checking is done here: the new
     * start value may invalidate the <code>(start <= value <= end)
     * invariant enforced by the constructors.  This is to simplify updating
     * the model.  Naturally, one should ensure that the invariant is true
     * before calling the <code>nextValue, previousValue,
     * or <code>setValue methods.
     * <p>
     * Typically this property is a <code>Date however it's possible to use
     * <code>Comparable with a compareTo method for
     * <code>Dates.  See setStart for an example.
     * <p>
     * This method fires a <code>ChangeEvent if the end
     * has changed.
     *
     * @param end defines the last date in the sequence
     * @see #getEnd
     * @see #setStart
     * @see #addChangeListener
     */
    public void setEnd(Comparable end) {
        if ((end == null) ? (this.end != null) : !end.equals(this.end)) {
            this.end = end;
            fireStateChanged();
        }
    }


    /**
     * Returns the last <code>Date in the sequence.
     *
     * @return the value of the <code>end property
     * @see #setEnd
     */
    public Comparable getEnd() {
        return end;
    }


    /**
     * Changes the size of the date value change computed
     * by the <code>nextValue and previousValue methods.
     * The <code>calendarField parameter must be one of the
     * <code>Calendar field constants like Calendar.MONTH
     * or <code>Calendar.MINUTE.
     * The <code>nextValue and previousValue methods
     * simply move the specified <code>Calendar field forward or backward
     * by one unit with the <code>Calendar.add method.
     * You should use this method with care as some UIs may set the
     * calendarField before committing the edit to spin the field under
     * the cursor. If you only want one field to spin you can subclass
     * and ignore the setCalendarField calls.
     *
     * @param calendarField one of
     *  <ul>
     *    <li>Calendar.ERA
     *    <li>Calendar.YEAR
     *    <li>Calendar.MONTH
     *    <li>Calendar.WEEK_OF_YEAR
     *    <li>Calendar.WEEK_OF_MONTH
     *    <li>Calendar.DAY_OF_MONTH
     *    <li>Calendar.DAY_OF_YEAR
     *    <li>Calendar.DAY_OF_WEEK
     *    <li>Calendar.DAY_OF_WEEK_IN_MONTH
     *    <li>Calendar.AM_PM
     *    <li>Calendar.HOUR
     *    <li>Calendar.HOUR_OF_DAY
     *    <li>Calendar.MINUTE
     *    <li>Calendar.SECOND
     *    <li>Calendar.MILLISECOND
     *  </ul>
     * <p>
     * This method fires a <code>ChangeEvent if the
     * <code>calendarField has changed.
     *
     * @see #getCalendarField
     * @see #getNextValue
     * @see #getPreviousValue
     * @see Calendar#add
     * @see #addChangeListener
     */
    public void setCalendarField(int calendarField) {
        if (!calendarFieldOK(calendarField)) {
            throw new IllegalArgumentException("invalid calendarField");
        }
        if (calendarField != this.calendarField) {
            this.calendarField = calendarField;
            fireStateChanged();
        }
    }


    /**
     * Returns the <code>Calendar field that is added to or subtracted from
     * by the <code>nextValue and previousValue methods.
     *
     * @return the value of the <code>calendarField property
     * @see #setCalendarField
     */
    public int getCalendarField() {
        return calendarField;
    }


    /**
     * Returns the next <code>Date in the sequence, or null if
     * the next date is after <code>end.
     *
     * @return the next <code>Date in the sequence, or null if
     *     the next date is after <code>end.
     *
     * @see SpinnerModel#getNextValue
     * @see #getPreviousValue
     * @see #setCalendarField
     */
    public Object getNextValue() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(value.getTime());
        cal.add(calendarField, 1);
        Date next = cal.getTime();
        return ((end == null) || (end.compareTo(next) >= 0)) ? next : null;
    }


    /**
     * Returns the previous <code>Date in the sequence, or null
     * if the previous date is before <code>start.
     *
     * @return the previous <code>Date in the sequence, or
     *     <code>null if the previous date
     *     is before <code>start
     *
     * @see SpinnerModel#getPreviousValue
     * @see #getNextValue
     * @see #setCalendarField
     */
    public Object getPreviousValue() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(value.getTime());
        cal.add(calendarField, -1);
        Date prev = cal.getTime();
        return ((start == null) || (start.compareTo(prev) <= 0)) ? prev : null;
    }


    /**
     * Returns the current element in this sequence of <code>Dates.
     * This method is equivalent to <code>(Date)getValue.
     *
     * @return the <code>value property
     * @see #setValue
     */
    public Date getDate() {
        return value.getTime();
    }


    /**
     * Returns the current element in this sequence of <code>Dates.
     *
     * @return the <code>value property
     * @see #setValue
     * @see #getDate
     */
    public Object getValue() {
        return value.getTime();
    }


    /**
     * Sets the current <code>Date for this sequence.
     * If <code>value is null,
     * an <code>IllegalArgumentException is thrown.  No bounds
     * checking is done here:
     * the new value may invalidate the <code>(start <= value < end)
     * invariant enforced by the constructors.  Naturally, one should ensure
     * that the <code>(start <= value <= maximum) invariant is true
     * before calling the <code>nextValue, previousValue,
     * or <code>setValue methods.
     * <p>
     * This method fires a <code>ChangeEvent if the
     * <code>value has changed.
     *
     * @param value the current (non <code>null)
     *    <code>Date for this sequence
     * @throws IllegalArgumentException if value is <code>null
     *    or not a <code>Date
     * @see #getDate
     * @see #getValue
     * @see #addChangeListener
     */
    public void setValue(Object value) {
        if ((value == null) || !(value instanceof Date)) {
            throw new IllegalArgumentException("illegal value");
        }
        if (!value.equals(this.value.getTime())) {
            this.value.setTime((Date)value);
            fireStateChanged();
        }
    }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java SpinnerDateModel.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.