001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.swing.plaf.synth;
019:
020: import javax.swing.JComponent;
021:
022: public class SynthContext {
023:
024: private JComponent component;
025:
026: private SynthStyle style;
027:
028: private int state;
029:
030: private Region region;
031:
032: /**
033: * Constructs SynthContext with given parameters
034: */
035: public SynthContext(JComponent component, Region region,
036: SynthStyle style, int state) {
037: this .component = component;
038: this .style = style;
039: this .state = state;
040: this .region = region;
041: }
042:
043: /**
044: * For the most of UIs context needed to represent style and state
045: */
046: SynthContext(SynthStyle style, int state) {
047: this .style = style;
048: this .state = state;
049: }
050:
051: /**
052: * Sometimes context just represents currentState
053: */
054: SynthContext(int state) {
055: this .state = state;
056: }
057:
058: /**
059: * @return The component uses this context
060: */
061: public JComponent getComponent() {
062: return component;
063: }
064:
065: /**
066: * @return The state for the component uses this context
067: */
068: public int getComponentState() {
069: return state;
070: }
071:
072: /** @return parent style */
073: public SynthStyle getStyle() {
074: return style;
075: }
076:
077: /** @return the region of the context */
078: public Region getRegion() {
079: return region;
080: }
081:
082: void setState(int state) {
083: this .state = state;
084: }
085:
086: /**
087: * Removes proposed state from current if it exists
088: */
089: void lossState(int proposed) {
090:
091: this .state &= ~proposed;
092: }
093:
094: /**
095: * Adds additional state to current if it doesn't exist
096: */
097: void gainState(int proposed) {
098: this .state |= proposed;
099: }
100:
101: static boolean isEnabled(int verifiedState) {
102: return (verifiedState & SynthConstants.ENABLED) != 0;
103: }
104:
105: static boolean isDisabled(int verifiedState) {
106: return (verifiedState & SynthConstants.DISABLED) != 0;
107: }
108:
109: static boolean isFocused(int verifiedState) {
110: return (verifiedState & SynthConstants.FOCUSED) != 0;
111: }
112:
113: static boolean isDefault(int verifiedState) {
114: return (verifiedState & SynthConstants.DEFAULT) != 0;
115: }
116:
117: static boolean isMouseOver(int verifiedState) {
118: return (verifiedState & SynthConstants.MOUSE_OVER) != 0;
119: }
120:
121: static boolean isPressed(int verifiedState) {
122: return (verifiedState & SynthConstants.PRESSED) != 0;
123: }
124:
125: static boolean isSelected(int verifiedState) {
126: return (verifiedState & SynthConstants.SELECTED) != 0;
127: }
128:
129: /**
130: * Used in UI's. Created to reduce code doubling
131: * */
132: static int getCommonComponentState(JComponent c) {
133: int result = c.isEnabled() ? SynthConstants.ENABLED
134: : SynthConstants.DISABLED;
135: if (c.isFocusOwner()) {
136: result |= SynthConstants.FOCUSED;
137: }
138: return result;
139: }
140: }
|