Path: blob/master/src/java.desktop/macosx/classes/apple/laf/JRSUIState.java
41152 views
/*1* Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package apple.laf;2627import apple.laf.JRSUIConstants.*;2829@SuppressWarnings("unchecked")30public class JRSUIState {31// static HashSet<JRSUIState> states = new HashSet<JRSUIState>();3233final long encodedState;34long derivedEncodedState;3536static JRSUIState prototype = new JRSUIState(0);37public static JRSUIState getInstance() {38return prototype.derive();39}4041JRSUIState(final Widget widget) {42this(widget.apply(0));43}4445JRSUIState(final long encodedState) {46this.encodedState = derivedEncodedState = encodedState;47}4849boolean isDerivationSame() {50return encodedState == derivedEncodedState;51}5253public <T extends JRSUIState> T derive() {54if (isDerivationSame()) return (T)this;55final T derivation = (T)createDerivation();5657// if (!states.add(derivation)) {58// System.out.println("dupe: " + states.size());59// }6061return derivation;62}6364public <T extends JRSUIState> T createDerivation() {65return (T)new JRSUIState(derivedEncodedState);66}6768public void reset() {69derivedEncodedState = encodedState;70}7172public void set(final Property property) {73derivedEncodedState = property.apply(derivedEncodedState);74}7576public void apply(final JRSUIControl control) {77control.setEncodedState(encodedState);78}7980@Override81public boolean equals(final Object obj) {82if (!(obj instanceof JRSUIState)) return false;83return encodedState == ((JRSUIState)obj).encodedState && getClass().equals(obj.getClass());84}8586public boolean is(Property property) {87return (byte)((derivedEncodedState & property.encoding.mask) >> property.encoding.shift) == property.ordinal;88}8990@Override91public int hashCode() {92return (int)(encodedState ^ (encodedState >>> 32)) ^ getClass().hashCode();93}9495public static class AnimationFrameState extends JRSUIState {96final int animationFrame;97int derivedAnimationFrame;9899AnimationFrameState(final long encodedState, final int animationFrame) {100super(encodedState);101this.animationFrame = derivedAnimationFrame = animationFrame;102}103104@Override105boolean isDerivationSame() {106return super.isDerivationSame() && (animationFrame == derivedAnimationFrame);107}108109@Override110public <T extends JRSUIState> T createDerivation() {111return (T)new AnimationFrameState(derivedEncodedState, derivedAnimationFrame);112}113114@Override115public void reset() {116super.reset();117derivedAnimationFrame = animationFrame;118}119120public void setAnimationFrame(final int frame) {121this.derivedAnimationFrame = frame;122}123124@Override125public void apply(final JRSUIControl control) {126super.apply(control);127control.set(Key.ANIMATION_FRAME, animationFrame);128}129130@Override131public boolean equals(final Object obj) {132if (!(obj instanceof AnimationFrameState)) return false;133return animationFrame == ((AnimationFrameState)obj).animationFrame && super.equals(obj);134}135136@Override137public int hashCode() {138return super.hashCode() ^ animationFrame;139}140}141142public static class ValueState extends JRSUIState {143final double value;144double derivedValue;145146ValueState(final long encodedState, final double value) {147super(encodedState);148this.value = derivedValue = value;149}150151@Override152boolean isDerivationSame() {153return super.isDerivationSame() && (value == derivedValue);154}155156@Override157public <T extends JRSUIState> T createDerivation() {158return (T)new ValueState(derivedEncodedState, derivedValue);159}160161@Override162public void reset() {163super.reset();164derivedValue = value;165}166167public void setValue(final double value) {168derivedValue = value;169}170171@Override172public void apply(final JRSUIControl control) {173super.apply(control);174control.set(Key.VALUE, value);175}176177@Override178public boolean equals(final Object obj) {179if (!(obj instanceof ValueState)) return false;180return value == ((ValueState)obj).value && super.equals(obj);181}182183@Override184public int hashCode() {185final long bits = Double.doubleToRawLongBits(value);186return super.hashCode() ^ (int)bits ^ (int)(bits >>> 32);187}188}189190public static class TitleBarHeightState extends ValueState {191TitleBarHeightState(final long encodedState, final double value) {192super(encodedState, value);193}194195@Override196public <T extends JRSUIState> T createDerivation() {197return (T)new TitleBarHeightState(derivedEncodedState, derivedValue);198}199200@Override201public void apply(final JRSUIControl control) {202super.apply(control);203control.set(Key.WINDOW_TITLE_BAR_HEIGHT, value);204}205}206207public static class ScrollBarState extends ValueState {208final double thumbProportion;209double derivedThumbProportion;210final double thumbStart;211double derivedThumbStart;212213ScrollBarState(final long encodedState, final double value, final double thumbProportion, final double thumbStart) {214super(encodedState, value);215this.thumbProportion = derivedThumbProportion = thumbProportion;216this.thumbStart = derivedThumbStart = thumbStart;217}218219@Override220boolean isDerivationSame() {221return super.isDerivationSame() && (thumbProportion == derivedThumbProportion) && (thumbStart == derivedThumbStart);222}223224@Override225public <T extends JRSUIState> T createDerivation() {226return (T)new ScrollBarState(derivedEncodedState, derivedValue, derivedThumbProportion, derivedThumbStart);227}228229@Override230public void reset() {231super.reset();232derivedThumbProportion = thumbProportion;233derivedThumbStart = thumbStart;234}235236public void setThumbPercent(final double thumbPercent) {237derivedThumbProportion = thumbPercent;238}239240public void setThumbStart(final double thumbStart) {241derivedThumbStart = thumbStart;242}243244@Override245public void apply(final JRSUIControl control) {246super.apply(control);247control.set(Key.THUMB_PROPORTION, thumbProportion);248control.set(Key.THUMB_START, thumbStart);249}250251@Override252public boolean equals(final Object obj) {253if (!(obj instanceof ScrollBarState)) return false;254return (thumbProportion == ((ScrollBarState)obj).thumbProportion) && (thumbStart == ((ScrollBarState)obj).thumbStart) && super.equals(obj);255}256257@Override258public int hashCode() {259final long bits = Double.doubleToRawLongBits(thumbProportion) ^ Double.doubleToRawLongBits(thumbStart);260return super.hashCode() ^ (int)bits ^ (int)(bits >>> 32);261}262}263}264265266