components/visual-panels/core/src/java/util/com/oracle/solaris/vp/util/swing/time/CalendarTableModel.java
changeset 827 0944d8c0158b
equal deleted inserted replaced
826:c6aad84d2493 827:0944d8c0158b
       
     1 
       
     2 /*
       
     3  * CDDL HEADER START
       
     4  *
       
     5  * The contents of this file are subject to the terms of the
       
     6  * Common Development and Distribution License (the "License").
       
     7  * You may not use this file except in compliance with the License.
       
     8  *
       
     9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
       
    10  * or http://www.opensolaris.org/os/licensing.
       
    11  * See the License for the specific language governing permissions
       
    12  * and limitations under the License.
       
    13  *
       
    14  * When distributing Covered Code, include this CDDL HEADER in each
       
    15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
       
    16  * If applicable, add the following below this CDDL HEADER, with the
       
    17  * fields enclosed by brackets "[]" replaced with your own identifying
       
    18  * information: Portions Copyright [yyyy] [name of copyright owner]
       
    19  *
       
    20  * CDDL HEADER END
       
    21  */
       
    22 
       
    23 /*
       
    24  * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
       
    25  */
       
    26 
       
    27 package com.oracle.solaris.vp.util.swing.time;
       
    28 
       
    29 import java.awt.Point;
       
    30 import java.text.DateFormat;
       
    31 import java.util.*;
       
    32 import javax.swing.table.AbstractTableModel;
       
    33 import com.oracle.solaris.vp.util.misc.ObjectUtil;
       
    34 
       
    35 @SuppressWarnings({"serial"})
       
    36 public abstract class CalendarTableModel extends AbstractTableModel {
       
    37     //
       
    38     // Instance data
       
    39     //
       
    40 
       
    41     protected Calendar cal;
       
    42     private Locale locale;
       
    43     private Calendar[][] data;
       
    44 
       
    45     //
       
    46     // Constructors
       
    47     //
       
    48 
       
    49     public CalendarTableModel(Calendar cal) {
       
    50 	this.cal = Calendar.getInstance();
       
    51 	this.cal.setLenient(false);
       
    52 
       
    53 	if (cal == null) {
       
    54 	    cal = this.cal;
       
    55 	} else {
       
    56 	    setCalendar(cal);
       
    57 	}
       
    58 
       
    59 	setLocale(Locale.getDefault());
       
    60     }
       
    61 
       
    62     public CalendarTableModel() {
       
    63 	this(null);
       
    64     }
       
    65 
       
    66     //
       
    67     // TableModel methods
       
    68     //
       
    69 
       
    70     @Override
       
    71     public int getColumnCount() {
       
    72 	return getData()[0].length;
       
    73     }
       
    74 
       
    75     /**
       
    76      * Default implementation that returns {@code null}.
       
    77      */
       
    78     @Override
       
    79     public String getColumnName(int colIndex) {
       
    80 	return null;
       
    81     }
       
    82 
       
    83     @Override
       
    84     public int getRowCount() {
       
    85 	return getData().length;
       
    86     }
       
    87 
       
    88     @Override
       
    89     public Object getValueAt(int row, int col) {
       
    90 	return getData()[row][col];
       
    91     }
       
    92 
       
    93     //
       
    94     // CalendarTableModel methods
       
    95     //
       
    96 
       
    97     /**
       
    98      * Compares the relevant fields of the given model elements.
       
    99      *
       
   100      * @param	    cal
       
   101      *		    an element of the model
       
   102      *
       
   103      * @param	    cal2
       
   104      *		    an element of the model
       
   105      *
       
   106      * @return	    a negative integer, zero, or a positive integer if {@code
       
   107      *		    cal} is less than, equal to, or greater than, respectively,
       
   108      *		    {@code cal2}
       
   109      */
       
   110     public abstract int compare(Calendar cal, Calendar cal2);
       
   111 
       
   112     /**
       
   113      * Creates the data shown in the table.
       
   114      */
       
   115     protected abstract Calendar[][] createData();
       
   116 
       
   117     /**
       
   118      * Gets a copy of the {@code Calendar} represented by this model.
       
   119      */
       
   120     public Calendar getCalendar() {
       
   121 	return (Calendar)cal.clone();
       
   122     }
       
   123 
       
   124     public Calendar[][] getData() {
       
   125 	if (data == null) {
       
   126 	    data = createData();
       
   127 	}
       
   128 	return data;
       
   129     }
       
   130 
       
   131     public Locale getLocale() {
       
   132 	return locale;
       
   133     }
       
   134 
       
   135     /**
       
   136      * Gets the number of milliseconds in the given number of intervals specific
       
   137      * to this {@code CalendarTableModel}.  For example, if this is a {@link
       
   138      * MonthTableModel} for 12/2008, {@code getMillis(2)} would return the
       
   139      * number of milliseconds between 12/1/2008 and 2/1/2009.  Note that {@code
       
   140      * getMillis(-n)} does not necessarily equal {@code -getMillis(n)}.
       
   141      */
       
   142     public abstract long getMillis(int intervals);
       
   143 
       
   144     /**
       
   145      * Gets the position of the given {@code Calendar} in this model.  Only
       
   146      * those fields in the {@code Calendar} that are relevant to this model are
       
   147      * {@link #compare compared}.
       
   148      *
       
   149      * @return	    a {@code Point}, or {@code null} if an equivalent {@code
       
   150      *		    Calendar} does not occur in the model
       
   151      */
       
   152     public Point getPosition(Calendar cal) {
       
   153 	int nRows = getRowCount();
       
   154 	int nCols = getColumnCount();
       
   155 
       
   156 	for (int mRow = 0; mRow < nRows; mRow++) {
       
   157 	    for (int mCol = 0; mCol < nCols; mCol++) {
       
   158 		Calendar cal2 = (Calendar)getValueAt(mRow, mCol);
       
   159 		if (compare(cal, cal2) == 0) {
       
   160 		    return new Point(mCol, mRow);
       
   161 		}
       
   162 	    }
       
   163 	}
       
   164 
       
   165 	return null;
       
   166     }
       
   167 
       
   168     /**
       
   169      * Gets a localized title for this {@code CalendarTableModel}.
       
   170      */
       
   171     public abstract String getTitle();
       
   172 
       
   173     /**
       
   174      * If not already invalid, marks this {@code CalendarTableModel}'s {@link
       
   175      * #getData data} as needing to be rebuilt and calls {@link
       
   176      * #fireTableDataChanged}.
       
   177      */
       
   178     protected void invalidate() {
       
   179 	if (data != null) {
       
   180 	    data = null;
       
   181 	    fireTableDataChanged();
       
   182 	}
       
   183     }
       
   184 
       
   185     /**
       
   186      * Determines whether the given {@code Calendar} is in the <i>main range</i>
       
   187      * of this model.  For example, if this model is a {@link MonthTableModel}
       
   188      * modelling all of the days in 12/2008, then the main range is 12/01/2008 -
       
   189      * 12/31/2008.  12/14/2008, then, would be in the main range of this model,
       
   190      * whereas 11/30/2008, even though it may be contained by this model, would
       
   191      * not.
       
   192      */
       
   193     public abstract boolean isInMainRange(Calendar cal);
       
   194 
       
   195     /**
       
   196      * Indicates whether the the model would need to be rebuilt if the given
       
   197      * {@code Calendar} were set in the model.	This implementation returns
       
   198      * {@code true} if {@link #isInMainRange} returns {@code false} or the time
       
   199      * zones in the two {@code Calendar}s differ; {@code false} otherwise.
       
   200      */
       
   201     protected boolean wouldInvalidate(Calendar cal) {
       
   202 	return cal == null || !isInMainRange(cal) ||
       
   203 	    !this.cal.getTimeZone().equals(cal.getTimeZone());
       
   204     }
       
   205 
       
   206     /**
       
   207      * Sets the {@code Calendar} in this {@code CalendarTableModel}. Calls
       
   208      * {@link #invalidate} if {@link #isInMainRange} returns {@code false}.
       
   209      */
       
   210     public void setCalendar(Calendar cal) {
       
   211 	boolean wouldInvalidate = wouldInvalidate(cal);
       
   212 	this.cal = cal;
       
   213 	if (wouldInvalidate) {
       
   214 	    invalidate();
       
   215 	}
       
   216     }
       
   217 
       
   218     /**
       
   219      * Sets the locale in this {@code CalendarTableModel} and calls
       
   220      * {@link #fireTableStructureChanged}.
       
   221      */
       
   222     public void setLocale(Locale locale) {
       
   223 	if (!ObjectUtil.equals(this.locale, locale)) {
       
   224 	    this.locale = locale;
       
   225 	    fireTableStructureChanged();
       
   226 	}
       
   227     }
       
   228 
       
   229     /**
       
   230      * Provides a default displayable value for the given {@code Calendar},
       
   231      * presumably contained in this model.
       
   232      */
       
   233     public abstract String toString(Calendar cal);
       
   234 
       
   235     //
       
   236     // Static methods
       
   237     //
       
   238 
       
   239     /**
       
   240      * Updates (if necessary) the given {@code DateFormat} with the time zone of
       
   241      * the given {@code Calendar}, then returns the formatted date string.
       
   242      */
       
   243     public static String format(DateFormat format, Calendar cal) {
       
   244 	TimeZone zone = cal.getTimeZone();
       
   245 	if (!format.getTimeZone().equals(zone)) {
       
   246 	    format.setTimeZone(zone);
       
   247 	}
       
   248 	return format.format(cal.getTime());
       
   249     }
       
   250 }