|
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 MillenniumTableModel extends CalendarTableModel { |
|
34 // |
|
35 // Static data |
|
36 // |
|
37 |
|
38 private static DateFormat[] mFormat = { |
|
39 new SimpleDateFormat(Finder.getString( |
|
40 "date.format.millennium.title.0")), |
|
41 new SimpleDateFormat(Finder.getString( |
|
42 "date.format.millennium.title.1")), |
|
43 }; |
|
44 |
|
45 private static DateFormat[] eFormat = { |
|
46 new SimpleDateFormat(Finder.getString( |
|
47 "date.format.millennium.element.0")), |
|
48 new SimpleDateFormat(Finder.getString( |
|
49 "date.format.millennium.element.1")), |
|
50 }; |
|
51 |
|
52 // |
|
53 // Constructors |
|
54 // |
|
55 |
|
56 public MillenniumTableModel(Calendar cal) { |
|
57 super(cal); |
|
58 } |
|
59 |
|
60 public MillenniumTableModel() { |
|
61 this(null); |
|
62 } |
|
63 |
|
64 // |
|
65 // CalendarTableModel methods |
|
66 // |
|
67 |
|
68 @Override |
|
69 public int compare(Calendar cal, Calendar cal2) { |
|
70 if (cal.get(Calendar.YEAR) / 100 == cal2.get(Calendar.YEAR) / 100) { |
|
71 return 0; |
|
72 } |
|
73 return cal.compareTo(cal2); |
|
74 } |
|
75 |
|
76 @Override |
|
77 protected Calendar[][] createData() { |
|
78 Calendar[][] centuries = new Calendar[3][4]; |
|
79 int offset = cal.get(Calendar.YEAR) % 1000 + 100; |
|
80 |
|
81 for (int r = 0; r < centuries.length; r++) { |
|
82 Calendar[] row = centuries[r]; |
|
83 for (int c = 0; c < row.length; c++) { |
|
84 row[c] = (Calendar)cal.clone(); |
|
85 row[c].add(Calendar.YEAR, 100 * (r * row.length + c) - offset); |
|
86 } |
|
87 } |
|
88 |
|
89 return centuries; |
|
90 } |
|
91 |
|
92 @Override |
|
93 public long getMillis(int intervals) { |
|
94 Calendar cal = getCalendar(); |
|
95 long before = cal.getTimeInMillis(); |
|
96 |
|
97 cal = (Calendar)cal.clone(); |
|
98 cal.add(Calendar.YEAR, 1000 * intervals); |
|
99 long after = cal.getTimeInMillis(); |
|
100 |
|
101 return after - before; |
|
102 } |
|
103 |
|
104 @Override |
|
105 public String getTitle() { |
|
106 Calendar cal = (Calendar)this.cal.clone(); |
|
107 String[] params = new String[2]; |
|
108 |
|
109 int offset = cal.get(Calendar.YEAR) % 1000; |
|
110 cal.add(Calendar.YEAR, -offset); |
|
111 params[0] = format(mFormat[0], cal); |
|
112 |
|
113 cal.add(Calendar.YEAR, 999); |
|
114 params[1] = format(mFormat[1], cal); |
|
115 |
|
116 return Finder.getString("date.format.millennium.title", |
|
117 (Object[])params); |
|
118 } |
|
119 |
|
120 @Override |
|
121 public boolean isInMainRange(Calendar cal) { |
|
122 return this.cal.get(Calendar.YEAR) / 1000 == |
|
123 cal.get(Calendar.YEAR) / 1000; |
|
124 } |
|
125 |
|
126 @Override |
|
127 public String toString(Calendar cal) { |
|
128 cal = (Calendar)cal.clone(); |
|
129 String[] params = new String[2]; |
|
130 |
|
131 int offset = cal.get(Calendar.YEAR) % 100; |
|
132 cal.add(Calendar.YEAR, -offset); |
|
133 params[0] = format(eFormat[0], cal); |
|
134 |
|
135 cal.add(Calendar.YEAR, 99); |
|
136 params[1] = format(eFormat[1], cal); |
|
137 |
|
138 return Finder.getString("date.format.millennium.element", |
|
139 (Object[])params); |
|
140 } |
|
141 } |