components/visual-panels/core/src/java/util/com/oracle/solaris/vp/util/swing/time/CenturyTableModel.java
changeset 827 0944d8c0158b
equal deleted inserted replaced
826:c6aad84d2493 827:0944d8c0158b
       
     1 /*
       
     2  * CDDL HEADER START
       
     3  *
       
     4  * The contents of this file are subject to the terms of the
       
     5  * Common Development and Distribution License (the "License").
       
     6  * You may not use this file except in compliance with the License.
       
     7  *
       
     8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
       
     9  * or http://www.opensolaris.org/os/licensing.
       
    10  * See the License for the specific language governing permissions
       
    11  * and limitations under the License.
       
    12  *
       
    13  * When distributing Covered Code, include this CDDL HEADER in each
       
    14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
       
    15  * If applicable, add the following below this CDDL HEADER, with the
       
    16  * fields enclosed by brackets "[]" replaced with your own identifying
       
    17  * information: Portions Copyright [yyyy] [name of copyright owner]
       
    18  *
       
    19  * CDDL HEADER END
       
    20  */
       
    21 
       
    22 /*
       
    23  * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
       
    24  */
       
    25 
       
    26 package com.oracle.solaris.vp.util.swing.time;
       
    27 
       
    28 import java.text.*;
       
    29 import java.util.Calendar;
       
    30 import com.oracle.solaris.vp.util.misc.finder.Finder;
       
    31 
       
    32 @SuppressWarnings({"serial"})
       
    33 public class CenturyTableModel extends CalendarTableModel {
       
    34     //
       
    35     // Static data
       
    36     //
       
    37 
       
    38     private static DateFormat[] mFormat = {
       
    39 	new SimpleDateFormat(Finder.getString("date.format.century.title.0")),
       
    40 	new SimpleDateFormat(Finder.getString("date.format.century.title.1")),
       
    41     };
       
    42 
       
    43     private static DateFormat[] eFormat = {
       
    44 	new SimpleDateFormat(Finder.getString("date.format.century.element.0")),
       
    45 	new SimpleDateFormat(Finder.getString("date.format.century.element.1")),
       
    46     };
       
    47 
       
    48     //
       
    49     // Constructors
       
    50     //
       
    51 
       
    52     public CenturyTableModel(Calendar cal) {
       
    53 	super(cal);
       
    54     }
       
    55 
       
    56     public CenturyTableModel() {
       
    57 	this(null);
       
    58     }
       
    59 
       
    60     //
       
    61     // CalendarTableModel methods
       
    62     //
       
    63 
       
    64     @Override
       
    65     public int compare(Calendar cal, Calendar cal2) {
       
    66 	if (cal.get(Calendar.YEAR) / 10 == cal2.get(Calendar.YEAR) / 10) {
       
    67 	    return 0;
       
    68 	}
       
    69 	return cal.compareTo(cal2);
       
    70     }
       
    71 
       
    72     @Override
       
    73     protected Calendar[][] createData() {
       
    74 	Calendar[][] decades = new Calendar[3][4];
       
    75 	int offset = cal.get(Calendar.YEAR) % 100 + 10;
       
    76 
       
    77 	for (int r = 0; r < decades.length; r++) {
       
    78 	    Calendar[] row = decades[r];
       
    79 	    for (int c = 0; c < row.length; c++) {
       
    80 		row[c] = (Calendar)cal.clone();
       
    81 		row[c].add(Calendar.YEAR, 10 * (r * row.length + c) - offset);
       
    82 	    }
       
    83 	}
       
    84 
       
    85 	return decades;
       
    86     }
       
    87 
       
    88     @Override
       
    89     public long getMillis(int intervals) {
       
    90 	Calendar cal = getCalendar();
       
    91 	long before = cal.getTimeInMillis();
       
    92 
       
    93 	cal = (Calendar)cal.clone();
       
    94 	cal.add(Calendar.YEAR, 100 * intervals);
       
    95 	long after = cal.getTimeInMillis();
       
    96 
       
    97 	return after - before;
       
    98     }
       
    99 
       
   100     @Override
       
   101     public String getTitle() {
       
   102 	Calendar cal = (Calendar)this.cal.clone();
       
   103 	String[] params = new String[2];
       
   104 
       
   105 	int offset = cal.get(Calendar.YEAR) % 100;
       
   106 	cal.add(Calendar.YEAR, -offset);
       
   107 	params[0] = format(mFormat[0], cal);
       
   108 
       
   109 	cal.add(Calendar.YEAR, 99);
       
   110 	params[1] = format(mFormat[1], cal);
       
   111 
       
   112 	return Finder.getString("date.format.century.title", (Object[])params);
       
   113     }
       
   114 
       
   115     @Override
       
   116     public boolean isInMainRange(Calendar cal) {
       
   117 	return this.cal.get(Calendar.YEAR) / 100 ==
       
   118 	    cal.get(Calendar.YEAR) / 100;
       
   119     }
       
   120 
       
   121     @Override
       
   122     public String toString(Calendar cal) {
       
   123 	cal = (Calendar)cal.clone();
       
   124 	String[] params = new String[2];
       
   125 
       
   126 	int offset = cal.get(Calendar.YEAR) % 10;
       
   127 	cal.add(Calendar.YEAR, -offset);
       
   128 	params[0] = format(eFormat[0], cal);
       
   129 
       
   130 	cal.add(Calendar.YEAR, 9);
       
   131 	params[1] = format(eFormat[1], cal);
       
   132 
       
   133 	return Finder.getString("date.format.century.element",
       
   134 	    (Object[])params);
       
   135     }
       
   136 }