001: /*
002: #IFNDEF ALT_LICENSE
003: ThinWire(R) RIA Ajax Framework
004: Copyright (C) 2003-2007 Custom Credit Systems
005:
006: This library is free software; you can redistribute it and/or modify it under
007: the terms of the GNU Lesser General Public License as published by the Free
008: Software Foundation; either version 2.1 of the License, or (at your option) any
009: later version.
010:
011: This library is distributed in the hope that it will be useful, but WITHOUT ANY
012: WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
013: PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
014:
015: You should have received a copy of the GNU Lesser General Public License along
016: with this library; if not, write to the Free Software Foundation, Inc., 59
017: Temple Place, Suite 330, Boston, MA 02111-1307 USA
018:
019: Users who would rather have a commercial license, warranty or support should
020: contact the following company who invented, built and supports the technology:
021:
022: Custom Credit Systems, Richardson, TX 75081, USA.
023: email: info@thinwire.com ph: +1 (888) 644-6405
024: http://www.thinwire.com
025: #ENDIF
026: [ v1.2_RC2 ]
027: */
028: package thinwire.ui.style;
029:
030: /**
031: * @author Joshua J. Gertzen
032: */
033: public class Style {
034: public static final String PROPERTY_OPACITY = "opacity";
035:
036: private static final Style DEFAULT_STYLE;
037: static {
038: Style s = new Style();
039: s.setOpacity(100);
040:
041: Font f = s.getFont();
042: f.setFamily(Font.Family.SANS_SERIF);
043: f.setColor(Color.BLACK);
044: f.setItalic(false);
045: f.setBold(false);
046: f.setUnderline(false);
047: f.setStrike(false);
048: f.setSize(8);
049:
050: Background bg = s.getBackground();
051: bg.setColor(Color.WHITE);
052: bg.setImage("");
053: bg.setRepeat(Background.Repeat.NONE);
054: bg.setPosition(Background.Position.LEFT_TOP);
055:
056: Border b = s.getBorder();
057: b.setColor(Color.WHITE);
058: b.setSize(0);
059: b.setType(Border.Type.NONE);
060: b.setImage("");
061:
062: FX fx = s.getFX();
063: fx.setPositionChange(Effect.Motion.NONE);
064: fx.setSizeChange(Effect.Motion.NONE);
065: fx.setVisibleChange(Effect.Motion.NONE);
066: fx.setOpacityChange(Effect.Motion.NONE);
067: fx.setColorChange(Effect.Motion.NONE);
068:
069: DEFAULT_STYLE = s;
070: }
071:
072: private Object parent;
073: private Color color;
074: private int opacity = -1;
075: private Font font;
076: private Background background;
077: private Border border;
078: private FX fx;
079: String stringValue;
080: Style defaultStyle;
081:
082: public Style() {
083: this (null, null);
084: }
085:
086: public Style(Style defaultStyle) {
087: this (defaultStyle, null);
088: }
089:
090: protected Style(Style defaultStyle, Object parent) {
091: this .parent = parent;
092:
093: if (defaultStyle == null) {
094: this .defaultStyle = DEFAULT_STYLE;
095: } else {
096: this .defaultStyle = new Style();
097: this .defaultStyle.copy(defaultStyle);
098: }
099:
100: this .font = new Font(this );
101: this .background = new Background(this );
102: this .border = new Border(this );
103: this .fx = new FX(this );
104: if (this .defaultStyle != null)
105: this .copy(this .defaultStyle);
106: }
107:
108: //NOTE: This is overridden by Component so it can receive these property change notifications
109: protected void firePropertyChange(Object source,
110: String propertyName, Object oldValue, Object newValue) {
111:
112: }
113:
114: public String toString() {
115: if (stringValue == null)
116: stringValue = "Style{background:" + getBackground()
117: + ",border:" + getBorder() + ",font:" + getFont()
118: + ",fX:" + getFX() + "}";
119: return stringValue;
120: }
121:
122: public int hashCode() {
123: return toString().hashCode();
124: }
125:
126: public boolean equals(Object o) {
127: if (!(o instanceof Style))
128: return false;
129: if (this == o)
130: return true;
131: return this .toString().equals(o.toString());
132: }
133:
134: public void copy(Style style) {
135: copy(style, false);
136: }
137:
138: public void copy(Style style, boolean onlyIfDefault) {
139: if (style == null)
140: throw new IllegalArgumentException("style == null");
141:
142: if (onlyIfDefault) {
143: if (getOpacity() == defaultStyle.getOpacity())
144: setOpacity(style.getOpacity());
145: } else {
146: setOpacity(style.getOpacity());
147: }
148:
149: getFont().copy(style.getFont(), onlyIfDefault);
150: getBackground().copy(style.getBackground(), onlyIfDefault);
151: getBorder().copy(style.getBorder(), onlyIfDefault);
152: getFX().copy(style.getFX());
153: }
154:
155: public void setProperty(String name, Object value) {
156: if (name.equals(PROPERTY_OPACITY)) {
157: setOpacity((Integer) value);
158: } else if (name.startsWith("background")) {
159: getBackground().setProperty(name, value);
160: } else if (name.startsWith("border")) {
161: getBorder().setProperty(name, value);
162: } else if (name.startsWith("font")) {
163: getFont().setProperty(name, value);
164: } else if (name.startsWith("fX")) {
165: getFX().setProperty(name, value);
166: } else {
167: throw new IllegalArgumentException(
168: "unknown style property '" + name + "'");
169: }
170: }
171:
172: public Object getProperty(String name) {
173: Object ret;
174:
175: if (name.equals(PROPERTY_OPACITY)) {
176: ret = getOpacity();
177: } else if (name.startsWith("background")) {
178: ret = getBackground().getProperty(name);
179: } else if (name.startsWith("border")) {
180: ret = getBorder().getProperty(name);
181: } else if (name.startsWith("font")) {
182: ret = getFont().getProperty(name);
183: } else if (name.startsWith("fX")) {
184: ret = getFX().getProperty(name);
185: } else {
186: throw new IllegalArgumentException(
187: "unknown style property '" + name + "'");
188: }
189:
190: return ret;
191: }
192:
193: public Object getParent() {
194: return parent;
195: }
196:
197: public int getOpacity() {
198: if (opacity == -1)
199: throw new IllegalStateException("opacity not initialized");
200: return opacity;
201: }
202:
203: public void setOpacity(int opacity) {
204: if (opacity < 0 && defaultStyle != null)
205: opacity = defaultStyle.getOpacity();
206: if (opacity < 0 && opacity > 100)
207: throw new IllegalArgumentException(
208: "opacity < 0 || opacity > 100");
209: int oldOpacity = this .opacity;
210: this .opacity = opacity;
211: if (parent != null)
212: firePropertyChange(this , PROPERTY_OPACITY, oldOpacity,
213: opacity);
214: }
215:
216: public Font getFont() {
217: return font;
218: }
219:
220: public Background getBackground() {
221: return background;
222: }
223:
224: public Border getBorder() {
225: return border;
226: }
227:
228: public FX getFX() {
229: return fx;
230: }
231: }
|