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.itsnat.impl.core.css.lex.Colon;
018: import org.itsnat.impl.core.css.lex.Identifier;
019: import org.itsnat.impl.core.css.lex.SourceCode;
020:
021: /**
022: *
023: * @author jmarranz
024: */
025: public class CSSPropertyImpl implements ObjectValueParent {
026: protected String propertyName;
027: protected CSSValueImpl value;
028: protected CSSStyleDeclarationImpl parent;
029: protected SourceCode cssTextValue; // El valor
030:
031: /** Creates a new instance of CSSPropertyImpl */
032: public CSSPropertyImpl(SourceCode cssTextProp,
033: CSSStyleDeclarationImpl parent) {
034: SourceCode[] pairNameValue = cssTextProp.split(Colon
035: .getSingleton());
036: if (pairNameValue.length < 2)
037: throw new DOMException(DOMException.INVALID_ACCESS_ERR,
038: "Missing : or missing name-value, code: "
039: + cssTextProp.toString());
040: else if (pairNameValue.length > 2)
041: throw new DOMException(DOMException.INVALID_ACCESS_ERR,
042: "CSS: unexpected \":\" , code: "
043: + cssTextProp.toString());
044:
045: SourceCode srcPropName = pairNameValue[0];
046: srcPropName = srcPropName.trim();
047: if (srcPropName.tokenCount() > 1)
048: throw new DOMException(DOMException.INVALID_ACCESS_ERR,
049: "CSS: syntax error , code: "
050: + srcPropName.toString());
051: if (!(srcPropName.getToken(0) instanceof Identifier))
052: throw new DOMException(DOMException.INVALID_ACCESS_ERR,
053: "CSS: expected an identifier: "
054: + srcPropName.toString());
055:
056: String propertyName = srcPropName.toString();
057: propertyName = propertyName.toLowerCase();
058:
059: this .propertyName = propertyName;
060: this .cssTextValue = pairNameValue[1];
061: this .parent = parent;
062: }
063:
064: public CSSPropertyImpl(String propertyName, String cssText,
065: CSSStyleDeclarationImpl parent) {
066: propertyName = propertyName.trim();
067: propertyName = propertyName.toLowerCase();
068:
069: this .propertyName = propertyName;
070: this .cssTextValue = new SourceCode(cssText);
071: this .parent = parent;
072: }
073:
074: public String getCssText(boolean updateIfNeeded) {
075: SourceCode sourceCode = getCssTextSourceCode(updateIfNeeded);
076: return sourceCode.toString();
077: }
078:
079: public void setCssText(String value, boolean updateParent) {
080: setCssTextSourceCode(new SourceCode(value), updateParent);
081: }
082:
083: public SourceCode getCssTextSourceCode() {
084: return getCssTextSourceCode(true);
085: }
086:
087: public SourceCode getCssTextSourceCode(boolean updateIfNeeded) {
088: if (updateIfNeeded)
089: rebuild();
090: return this .cssTextValue;
091: }
092:
093: public void setCssTextSourceCode(SourceCode cssText,
094: boolean updateParent) {
095: rebuild(cssText);
096:
097: if (updateParent)
098: parent.notifyToElementChangedProperty(this , cssText);
099: }
100:
101: public CSSValueImpl getCSSValue() {
102: if (value == null) // Sólo lo creamos si se solicita pues hace parseos que llevan tiempo
103: this .value = CSSValueImpl.newCSSValue(cssTextValue, -1,
104: this );
105: return value;
106: }
107:
108: public String getPropertyName() {
109: return propertyName;
110: }
111:
112: public Object getUpdatedChildObjectValueFromElement(
113: Object requester, int requesterCode) {
114: return parent.getPropertyCSSValue(propertyName);
115: }
116:
117: public void rebuild() {
118: CSSPropertyImpl property = parent
119: .getPropertyObject(propertyName);
120: if (property != this ) // Ha cambiado
121: {
122: SourceCode cssText = property.getCssTextSourceCode(false);
123: rebuild(cssText);
124: }
125: }
126:
127: public void rebuild(SourceCode cssText) {
128: if (this .cssTextValue.equals(cssText))
129: return; // Nada que hacer
130:
131: this .cssTextValue = cssText;
132: this .value = null; // Pues puede haber cambiado el tipo de valor
133: }
134:
135: public void notifyToElementChangedCSSText(SourceCode cssText,
136: Object requester) {
137: setCssTextSourceCode(cssText, true);
138: }
139: }
|