001: /*
002: ItsNat Java Web Application Framework
003: Copyright (C) 2007 Innowhere Software Services S.L., Spanish Company
004: Author: Jose Maria Arranz Santamaria
005:
006: This program is free software: you can redistribute it and/or modify
007: it under the terms of the GNU Affero General Public License as published by
008: the Free Software Foundation, either version 3 of the License, or
009: (at your option) any later version. See the GNU Affero General Public
010: License for more details. See the copy of the GNU Affero General Public License
011: included in this program. If not, see <http://www.gnu.org/licenses/>.
012: */
013:
014: package org.itsnat.impl.core.css;
015:
016: import org.w3c.dom.DOMException;
017: import org.w3c.dom.css.CSSPrimitiveValue;
018: import org.w3c.dom.css.RGBColor;
019: import org.itsnat.impl.core.css.lex.Comma;
020: import org.itsnat.impl.core.css.lex.HexNumber;
021: import org.itsnat.impl.core.css.lex.SourceCode;
022:
023: /**
024: *
025: * @author jmarranz
026: */
027: public class RGBColorImpl extends CSSPrimitiveValueLiteralImpl
028: implements RGBColor, ObjectValueParent {
029: protected CSSPrimitiveValueImpl parent;
030: protected CSSPrimitiveValueImpl red;
031: protected CSSPrimitiveValueImpl green;
032: protected CSSPrimitiveValueImpl blue;
033:
034: /** Creates a new instance of RGBColorImpl */
035: public RGBColorImpl(SourceCode cssTextCode,
036: CSSPrimitiveValueImpl parent) {
037: this .parent = parent;
038: SourceCode[] colorList = cssTextCode
039: .split(Comma.getSingleton());
040: if (colorList.length != 3)
041: throw new DOMException(DOMException.INVALID_ACCESS_ERR,
042: "RGBColor syntax error, property: "
043: + getPropertyName() + " value: "
044: + cssTextCode.toString());
045:
046: createColors(colorList);
047: }
048:
049: public RGBColorImpl(HexNumber token, CSSPrimitiveValueImpl parent) {
050: this .parent = parent;
051: String color = token.toString();
052: SourceCode[] colorList = new SourceCode[3];
053: if (color.length() == 4) // #rgb format
054: {
055: char r = color.charAt(1);
056: colorList[0] = SourceCode.newSourceCode(Integer
057: .toString(HexNumber.toIntFromHex(r)));
058:
059: char g = color.charAt(1);
060: colorList[1] = SourceCode.newSourceCode(Integer
061: .toString(HexNumber.toIntFromHex(g)));
062:
063: char b = color.charAt(2);
064: colorList[2] = SourceCode.newSourceCode(Integer
065: .toString(HexNumber.toIntFromHex(b)));
066: } else if (color.length() == 7) // #rrggbb format
067: {
068: String r = color.substring(1, 3);
069: colorList[0] = SourceCode.newSourceCode(Integer
070: .toString(HexNumber.toIntFromHex(r)));
071:
072: String g = color.substring(3, 5);
073: colorList[1] = SourceCode.newSourceCode(Integer
074: .toString(HexNumber.toIntFromHex(g)));
075:
076: String b = color.substring(5, 7);
077: colorList[2] = SourceCode.newSourceCode(Integer
078: .toString(HexNumber.toIntFromHex(b)));
079: } else
080: throw new DOMException(DOMException.INVALID_ACCESS_ERR,
081: "RGBColor syntax error, property: "
082: + getPropertyName() + " value: " + color);
083:
084: createColors(colorList);
085: }
086:
087: public int getCode() {
088: return RGB;
089: }
090:
091: private void createColors(SourceCode[] colorList) {
092: this .red = new CSSPrimitiveValueImpl(colorList[0], 0, this );
093: this .green = new CSSPrimitiveValueImpl(colorList[1], 1, this );
094: this .blue = new CSSPrimitiveValueImpl(colorList[2], 2, this );
095: }
096:
097: public CSSPrimitiveValue getRed() {
098: return red;
099: }
100:
101: public CSSPrimitiveValue getGreen() {
102: return green;
103: }
104:
105: public CSSPrimitiveValue getBlue() {
106: return blue;
107: }
108:
109: public String getPropertyName() {
110: return parent.getCSSValueParent().getPropertyName();
111: }
112:
113: public Object getUpdatedChildObjectValueFromElement(
114: Object requester, int requesterCode) {
115: RGBColor current = (RGBColor) parent
116: .getUpdatedChildObjectValueFromElement(this , getCode());
117: if (current != this ) {
118: if (requesterCode == 0)
119: return current.getRed();
120: else if (requesterCode == 1)
121: return current.getGreen();
122: else if (requesterCode == 2)
123: return current.getBlue();
124:
125: throw new DOMException(DOMException.INVALID_ACCESS_ERR,
126: "Internal Error");
127: } else
128: return requester;
129: }
130:
131: public void notifyToElementChangedCSSText(SourceCode cssTextCode,
132: Object requester) {
133: // Una de las partes se ha actualizado (da igual cual sea), hay que notificar el cambio
134: // de toda la estructura RGB.
135: String cssTextRGB = "rgb(" + red.getCssTextSourceCode(false)
136: + "," + green.getCssTextSourceCode(false) + ","
137: + blue.getCssTextSourceCode(false) + ")";
138: SourceCode cssTextRGBCode = new SourceCode(cssTextRGB);
139: parent.setCssTextSourceCode(cssTextRGBCode, true);
140: }
141:
142: }
|