1/* 2 * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26package apple.laf; 27 28import apple.laf.JRSUIConstants.*; 29 30@SuppressWarnings("unchecked") 31public class JRSUIState { 32// static HashSet<JRSUIState> states = new HashSet<JRSUIState>(); 33 34 final long encodedState; 35 long derivedEncodedState; 36 37 static JRSUIState prototype = new JRSUIState(0); 38 public static JRSUIState getInstance() { 39 return prototype.derive(); 40 } 41 42 JRSUIState(final Widget widget) { 43 this(widget.apply(0)); 44 } 45 46 JRSUIState(final long encodedState) { 47 this.encodedState = derivedEncodedState = encodedState; 48 } 49 50 boolean isDerivationSame() { 51 return encodedState == derivedEncodedState; 52 } 53 54 public <T extends JRSUIState> T derive() { 55 if (isDerivationSame()) return (T)this; 56 final T derivation = (T)createDerivation(); 57 58// if (!states.add(derivation)) { 59// System.out.println("dupe: " + states.size()); 60// } 61 62 return derivation; 63 } 64 65 public <T extends JRSUIState> T createDerivation() { 66 return (T)new JRSUIState(derivedEncodedState); 67 } 68 69 public void reset() { 70 derivedEncodedState = encodedState; 71 } 72 73 public void set(final Property property) { 74 derivedEncodedState = property.apply(derivedEncodedState); 75 } 76 77 public void apply(final JRSUIControl control) { 78 control.setEncodedState(encodedState); 79 } 80 81 @Override 82 public boolean equals(final Object obj) { 83 if (!(obj instanceof JRSUIState)) return false; 84 return encodedState == ((JRSUIState)obj).encodedState && getClass().equals(obj.getClass()); 85 } 86 87 public boolean is(Property property) { 88 return (byte)((derivedEncodedState & property.encoding.mask) >> property.encoding.shift) == property.ordinal; 89 } 90 91 @Override 92 public int hashCode() { 93 return (int)(encodedState ^ (encodedState >>> 32)) ^ getClass().hashCode(); 94 } 95 96 public static class AnimationFrameState extends JRSUIState { 97 final int animationFrame; 98 int derivedAnimationFrame; 99 100 AnimationFrameState(final long encodedState, final int animationFrame) { 101 super(encodedState); 102 this.animationFrame = derivedAnimationFrame = animationFrame; 103 } 104 105 @Override 106 boolean isDerivationSame() { 107 return super.isDerivationSame() && (animationFrame == derivedAnimationFrame); 108 } 109 110 @Override 111 public <T extends JRSUIState> T createDerivation() { 112 return (T)new AnimationFrameState(derivedEncodedState, derivedAnimationFrame); 113 } 114 115 @Override 116 public void reset() { 117 super.reset(); 118 derivedAnimationFrame = animationFrame; 119 } 120 121 public void setAnimationFrame(final int frame) { 122 this.derivedAnimationFrame = frame; 123 } 124 125 @Override 126 public void apply(final JRSUIControl control) { 127 super.apply(control); 128 control.set(Key.ANIMATION_FRAME, animationFrame); 129 } 130 131 @Override 132 public boolean equals(final Object obj) { 133 if (!(obj instanceof AnimationFrameState)) return false; 134 return animationFrame == ((AnimationFrameState)obj).animationFrame && super.equals(obj); 135 } 136 137 @Override 138 public int hashCode() { 139 return super.hashCode() ^ animationFrame; 140 } 141 } 142 143 public static class ValueState extends JRSUIState { 144 final double value; 145 double derivedValue; 146 147 ValueState(final long encodedState, final double value) { 148 super(encodedState); 149 this.value = derivedValue = value; 150 } 151 152 @Override 153 boolean isDerivationSame() { 154 return super.isDerivationSame() && (value == derivedValue); 155 } 156 157 @Override 158 public <T extends JRSUIState> T createDerivation() { 159 return (T)new ValueState(derivedEncodedState, derivedValue); 160 } 161 162 @Override 163 public void reset() { 164 super.reset(); 165 derivedValue = value; 166 } 167 168 public void setValue(final double value) { 169 derivedValue = value; 170 } 171 172 @Override 173 public void apply(final JRSUIControl control) { 174 super.apply(control); 175 control.set(Key.VALUE, value); 176 } 177 178 @Override 179 public boolean equals(final Object obj) { 180 if (!(obj instanceof ValueState)) return false; 181 return value == ((ValueState)obj).value && super.equals(obj); 182 } 183 184 @Override 185 public int hashCode() { 186 final long bits = Double.doubleToRawLongBits(value); 187 return super.hashCode() ^ (int)bits ^ (int)(bits >>> 32); 188 } 189 } 190 191 public static class TitleBarHeightState extends ValueState { 192 TitleBarHeightState(final long encodedState, final double value) { 193 super(encodedState, value); 194 } 195 196 @Override 197 public <T extends JRSUIState> T createDerivation() { 198 return (T)new TitleBarHeightState(derivedEncodedState, derivedValue); 199 } 200 201 @Override 202 public void apply(final JRSUIControl control) { 203 super.apply(control); 204 control.set(Key.WINDOW_TITLE_BAR_HEIGHT, value); 205 } 206 } 207 208 public static class ScrollBarState extends ValueState { 209 final double thumbProportion; 210 double derivedThumbProportion; 211 final double thumbStart; 212 double derivedThumbStart; 213 214 ScrollBarState(final long encodedState, final double value, final double thumbProportion, final double thumbStart) { 215 super(encodedState, value); 216 this.thumbProportion = derivedThumbProportion = thumbProportion; 217 this.thumbStart = derivedThumbStart = thumbStart; 218 } 219 220 @Override 221 boolean isDerivationSame() { 222 return super.isDerivationSame() && (thumbProportion == derivedThumbProportion) && (thumbStart == derivedThumbStart); 223 } 224 225 @Override 226 public <T extends JRSUIState> T createDerivation() { 227 return (T)new ScrollBarState(derivedEncodedState, derivedValue, derivedThumbProportion, derivedThumbStart); 228 } 229 230 @Override 231 public void reset() { 232 super.reset(); 233 derivedThumbProportion = thumbProportion; 234 derivedThumbStart = thumbStart; 235 } 236 237 public void setThumbPercent(final double thumbPercent) { 238 derivedThumbProportion = thumbPercent; 239 } 240 241 public void setThumbStart(final double thumbStart) { 242 derivedThumbStart = thumbStart; 243 } 244 245 @Override 246 public void apply(final JRSUIControl control) { 247 super.apply(control); 248 control.set(Key.THUMB_PROPORTION, thumbProportion); 249 control.set(Key.THUMB_START, thumbStart); 250 } 251 252 @Override 253 public boolean equals(final Object obj) { 254 if (!(obj instanceof ScrollBarState)) return false; 255 return (thumbProportion == ((ScrollBarState)obj).thumbProportion) && (thumbStart == ((ScrollBarState)obj).thumbStart) && super.equals(obj); 256 } 257 258 @Override 259 public int hashCode() { 260 final long bits = Double.doubleToRawLongBits(thumbProportion) ^ Double.doubleToRawLongBits(thumbStart); 261 return super.hashCode() ^ (int)bits ^ (int)(bits >>> 32); 262 } 263 } 264} 265