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.beans.*; |
|
29 import java.util.*; |
|
30 import com.oracle.solaris.vp.util.misc.event.PropertyChangeListeners; |
|
31 |
|
32 @SuppressWarnings({"serial"}) |
|
33 public class SimpleTimeModel implements TimeModel { |
|
34 // |
|
35 // Instance data |
|
36 // |
|
37 |
|
38 private Calendar cal = Calendar.getInstance(); |
|
39 private long timeOffset; |
|
40 |
|
41 private PropertyChangeListeners listeners = |
|
42 new PropertyChangeListeners(); |
|
43 |
|
44 // |
|
45 // Constructors |
|
46 // |
|
47 |
|
48 /** |
|
49 * Constructs a {@code SimpleTimeModel} with the specified {@link |
|
50 * #setTimeOffset offset}. |
|
51 */ |
|
52 public SimpleTimeModel(long timeOffset) { |
|
53 setTimeOffset(timeOffset); |
|
54 } |
|
55 |
|
56 /** |
|
57 * Constructs a {@code SimpleTimeModel} with a zero {@link #setTimeOffset |
|
58 * offset} from the system clock. |
|
59 */ |
|
60 public SimpleTimeModel() { |
|
61 this(0); |
|
62 } |
|
63 |
|
64 // |
|
65 // TimeModel methods |
|
66 // |
|
67 |
|
68 @Override |
|
69 public void addPropertyChangeListener(PropertyChangeListener listener) { |
|
70 listeners.add(listener); |
|
71 } |
|
72 |
|
73 @Override |
|
74 public void addPropertyChangeListener(String propertyName, |
|
75 PropertyChangeListener listener) { |
|
76 |
|
77 listeners.add(propertyName, listener); |
|
78 } |
|
79 |
|
80 @Override |
|
81 public Calendar getCalendar() { |
|
82 return (Calendar)cal.clone(); |
|
83 } |
|
84 |
|
85 @Override |
|
86 public long getTimeOffset() { |
|
87 return timeOffset; |
|
88 } |
|
89 |
|
90 @Override |
|
91 public TimeZone getTimeZone() { |
|
92 return cal.getTimeZone(); |
|
93 } |
|
94 |
|
95 @Override |
|
96 public void removePropertyChangeListener(PropertyChangeListener listener) { |
|
97 listeners.remove(listener); |
|
98 } |
|
99 |
|
100 @Override |
|
101 public void setTimeOffset(long timeOffset) { |
|
102 if (this.timeOffset != timeOffset) { |
|
103 long oldValue = this.timeOffset; |
|
104 this.timeOffset = timeOffset; |
|
105 updateTime(); |
|
106 firePropertyChange(PROPERTY_TIME_OFFSET, (Long)oldValue, |
|
107 (Long)timeOffset); |
|
108 } |
|
109 } |
|
110 |
|
111 @Override |
|
112 public void setTimeZone(TimeZone timeZone) { |
|
113 TimeZone oldValue = cal.getTimeZone(); |
|
114 if (!oldValue.equals(timeZone)) { |
|
115 cal.setTimeZone(timeZone); |
|
116 updateTime(); |
|
117 firePropertyChange(PROPERTY_TIME_ZONE, oldValue, timeZone); |
|
118 } |
|
119 } |
|
120 |
|
121 @Override |
|
122 public void updateTime() { |
|
123 long oldValue = cal.getTimeInMillis(); |
|
124 long newValue = System.currentTimeMillis() + timeOffset; |
|
125 cal.setTimeInMillis(newValue); |
|
126 firePropertyChange(PROPERTY_TIME, (Long)oldValue, (Long)newValue); |
|
127 } |
|
128 |
|
129 // |
|
130 // SimpleTimeModel methods |
|
131 // |
|
132 |
|
133 public void firePropertyChange(String propertyName, Object oldValue, |
|
134 Object newValue) { |
|
135 |
|
136 PropertyChangeEvent event = new PropertyChangeEvent( |
|
137 this, propertyName, oldValue, newValue); |
|
138 listeners.propertyChange(event); |
|
139 } |
|
140 |
|
141 public Calendar getModelCalendar() { |
|
142 return cal; |
|
143 } |
|
144 } |
|