0001: /*
0002:
0003: Licensed to the Apache Software Foundation (ASF) under one or more
0004: contributor license agreements. See the NOTICE file distributed with
0005: this work for additional information regarding copyright ownership.
0006: The ASF licenses this file to You under the Apache License, Version 2.0
0007: (the "License"); you may not use this file except in compliance with
0008: the License. You may obtain a copy of the License at
0009:
0010: http://www.apache.org/licenses/LICENSE-2.0
0011:
0012: Unless required by applicable law or agreed to in writing, software
0013: distributed under the License is distributed on an "AS IS" BASIS,
0014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0015: See the License for the specific language governing permissions and
0016: limitations under the License.
0017:
0018: */
0019: package org.apache.batik.css.dom;
0020:
0021: import java.util.ArrayList;
0022:
0023: import org.apache.batik.css.engine.value.FloatValue;
0024: import org.apache.batik.css.engine.value.Value;
0025: import org.apache.batik.css.engine.value.svg.ICCColor;
0026: import org.apache.batik.util.CSSConstants;
0027:
0028: import org.w3c.dom.DOMException;
0029: import org.w3c.dom.css.CSSPrimitiveValue;
0030: import org.w3c.dom.css.CSSValue;
0031: import org.w3c.dom.css.Counter;
0032: import org.w3c.dom.css.RGBColor;
0033: import org.w3c.dom.css.Rect;
0034: import org.w3c.dom.svg.SVGColor;
0035: import org.w3c.dom.svg.SVGICCColor;
0036: import org.w3c.dom.svg.SVGNumber;
0037: import org.w3c.dom.svg.SVGNumberList;
0038:
0039: /**
0040: * This class implements the {@link SVGColor} interface.
0041: *
0042: * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
0043: * @version $Id: CSSOMSVGColor.java 489226 2006-12-21 00:05:36Z cam $
0044: */
0045: public class CSSOMSVGColor implements SVGColor, RGBColor, SVGICCColor,
0046: SVGNumberList {
0047:
0048: /**
0049: * The associated value.
0050: */
0051: protected ValueProvider valueProvider;
0052:
0053: /**
0054: * The modifications handler.
0055: */
0056: protected ModificationHandler handler;
0057:
0058: /**
0059: * The red component, if this value is a RGBColor.
0060: */
0061: protected RedComponent redComponent;
0062:
0063: /**
0064: * The green component, if this value is a RGBColor.
0065: */
0066: protected GreenComponent greenComponent;
0067:
0068: /**
0069: * The blue component, if this value is a RGBColor.
0070: */
0071: protected BlueComponent blueComponent;
0072:
0073: /**
0074: * To store the ICC color list.
0075: */
0076: protected ArrayList iccColors;
0077:
0078: /**
0079: * Creates a new CSSOMSVGColor.
0080: */
0081: public CSSOMSVGColor(ValueProvider vp) {
0082: valueProvider = vp;
0083: }
0084:
0085: /**
0086: * Sets the modification handler of this value.
0087: */
0088: public void setModificationHandler(ModificationHandler h) {
0089: handler = h;
0090: }
0091:
0092: /**
0093: * <b>DOM</b>: Implements {@link org.w3c.dom.css.CSSValue#getCssText()}.
0094: */
0095: public String getCssText() {
0096: return valueProvider.getValue().getCssText();
0097: }
0098:
0099: /**
0100: * <b>DOM</b>: Implements {@link
0101: * org.w3c.dom.css.CSSValue#setCssText(String)}.
0102: */
0103: public void setCssText(String cssText) throws DOMException {
0104: if (handler == null) {
0105: throw new DOMException(
0106: DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
0107: } else {
0108: iccColors = null;
0109: handler.textChanged(cssText);
0110: }
0111: }
0112:
0113: /**
0114: * <b>DOM</b>: Implements {@link
0115: * org.w3c.dom.css.CSSValue#getCssValueType()}.
0116: */
0117: public short getCssValueType() {
0118: return CSS_CUSTOM;
0119: }
0120:
0121: /**
0122: * <b>DOM</b>: Implements {@link
0123: * org.w3c.dom.svg.SVGColor#getColorType()}.
0124: */
0125: public short getColorType() {
0126: Value value = valueProvider.getValue();
0127: int cssValueType = value.getCssValueType();
0128: switch (cssValueType) {
0129: case CSSValue.CSS_PRIMITIVE_VALUE:
0130: int primitiveType = value.getPrimitiveType();
0131: switch (primitiveType) {
0132: case CSSPrimitiveValue.CSS_IDENT: {
0133: if (value.getStringValue().equalsIgnoreCase(
0134: CSSConstants.CSS_CURRENTCOLOR_VALUE))
0135: return SVG_COLORTYPE_CURRENTCOLOR;
0136: return SVG_COLORTYPE_RGBCOLOR;
0137: }
0138: case CSSPrimitiveValue.CSS_RGBCOLOR:
0139: return SVG_COLORTYPE_RGBCOLOR;
0140: }
0141: // there was no case for this primitiveType, prevent throwing the other exception
0142: throw new IllegalStateException(
0143: "Found unexpected PrimitiveType:" + primitiveType);
0144:
0145: case CSSValue.CSS_VALUE_LIST:
0146: return SVG_COLORTYPE_RGBCOLOR_ICCCOLOR;
0147: }
0148: // should not happen
0149: throw new IllegalStateException(
0150: "Found unexpected CssValueType:" + cssValueType);
0151: }
0152:
0153: /**
0154: * <b>DOM</b>: Implements {@link
0155: * org.w3c.dom.svg.SVGColor#getRGBColor()}.
0156: */
0157: public RGBColor getRGBColor() {
0158: return this ;
0159: }
0160:
0161: /**
0162: * Returns the RGBColor value for this SVGColor.
0163: * For the SVG 1.1 ECMAScript binding.
0164: */
0165: public RGBColor getRgbColor() {
0166: return this ;
0167: }
0168:
0169: /**
0170: * <b>DOM</b>: Implements {@link
0171: * org.w3c.dom.svg.SVGColor#setRGBColor(String)}.
0172: */
0173: public void setRGBColor(String color) {
0174: if (handler == null) {
0175: throw new DOMException(
0176: DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
0177: } else {
0178: handler.rgbColorChanged(color);
0179: }
0180: }
0181:
0182: /**
0183: * <b>DOM</b>: Implements {@link
0184: * org.w3c.dom.svg.SVGColor#getICCColor()}.
0185: */
0186: public SVGICCColor getICCColor() {
0187: return this ;
0188: }
0189:
0190: /**
0191: * Returns the SVGICCColor value of this SVGColor.
0192: * For the SVG 1.1 ECMAScript binding.
0193: */
0194: public SVGICCColor getIccColor() {
0195: return this ;
0196: }
0197:
0198: /**
0199: * <b>DOM</b>: Implements {@link
0200: * org.w3c.dom.svg.SVGColor#setRGBColorICCColor(String,String)}.
0201: */
0202: public void setRGBColorICCColor(String rgb, String icc) {
0203: if (handler == null) {
0204: throw new DOMException(
0205: DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
0206: } else {
0207: iccColors = null;
0208: handler.rgbColorICCColorChanged(rgb, icc);
0209: }
0210: }
0211:
0212: /**
0213: * <b>DOM</b>: Implements {@link
0214: * org.w3c.dom.svg.SVGColor#setColor(short,String,String)}.
0215: */
0216: public void setColor(short type, String rgb, String icc) {
0217: if (handler == null) {
0218: throw new DOMException(
0219: DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
0220: } else {
0221: iccColors = null;
0222: handler.colorChanged(type, rgb, icc);
0223: }
0224: }
0225:
0226: // RGBColor ///////////////////////////////////////////////////
0227:
0228: /**
0229: * <b>DOM</b>: Implements {@link org.w3c.dom.css.RGBColor#getRed()}.
0230: */
0231: public CSSPrimitiveValue getRed() {
0232: valueProvider.getValue().getRed();
0233: if (redComponent == null) {
0234: redComponent = new RedComponent();
0235: }
0236: return redComponent;
0237: }
0238:
0239: /**
0240: * <b>DOM</b>: Implements {@link org.w3c.dom.css.RGBColor#getGreen()}.
0241: */
0242: public CSSPrimitiveValue getGreen() {
0243: valueProvider.getValue().getGreen();
0244: if (greenComponent == null) {
0245: greenComponent = new GreenComponent();
0246: }
0247: return greenComponent;
0248: }
0249:
0250: /**
0251: * <b>DOM</b>: Implements {@link org.w3c.dom.css.RGBColor#getBlue()}.
0252: */
0253: public CSSPrimitiveValue getBlue() {
0254: valueProvider.getValue().getBlue();
0255: if (blueComponent == null) {
0256: blueComponent = new BlueComponent();
0257: }
0258: return blueComponent;
0259: }
0260:
0261: // SVGICCColor //////////////////////////////////////////////////
0262:
0263: /**
0264: * <b>DOM</b>: Implements {@link org.w3c.dom.svg.SVGICCColor#getColorProfile()}.
0265: */
0266: public String getColorProfile() {
0267: if (getColorType() != SVG_COLORTYPE_RGBCOLOR_ICCCOLOR) {
0268: throw new DOMException(DOMException.SYNTAX_ERR, "");
0269: }
0270: Value value = valueProvider.getValue();
0271: return ((ICCColor) value.item(1)).getColorProfile();
0272: }
0273:
0274: /**
0275: * <b>DOM</b>: Implements {@link SVGICCColor#setColorProfile(String)}.
0276: */
0277: public void setColorProfile(String colorProfile)
0278: throws DOMException {
0279: if (handler == null) {
0280: throw new DOMException(
0281: DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
0282: } else {
0283: handler.colorProfileChanged(colorProfile);
0284: }
0285: }
0286:
0287: /**
0288: * <b>DOM</b>: Implements {@link SVGICCColor#getColors()}.
0289: */
0290: public SVGNumberList getColors() {
0291: return this ;
0292: }
0293:
0294: // SVGNumberList ///////////////////////////////////////////////
0295:
0296: /**
0297: * <b>DOM</b>: Implements {@link SVGNumberList#getNumberOfItems()}.
0298: */
0299: public int getNumberOfItems() {
0300: if (getColorType() != SVG_COLORTYPE_RGBCOLOR_ICCCOLOR) {
0301: throw new DOMException(DOMException.SYNTAX_ERR, "");
0302: }
0303: Value value = valueProvider.getValue();
0304: return ((ICCColor) value.item(1)).getNumberOfColors();
0305: }
0306:
0307: /**
0308: * <b>DOM</b>: Implements {@link SVGNumberList#clear()}.
0309: */
0310: public void clear() throws DOMException {
0311: if (handler == null) {
0312: throw new DOMException(
0313: DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
0314: } else {
0315: iccColors = null;
0316: handler.colorsCleared();
0317: }
0318: }
0319:
0320: /**
0321: * <b>DOM</b>: Implements {@link SVGNumberList#initialize(SVGNumber)}.
0322: */
0323: public SVGNumber initialize(SVGNumber newItem) throws DOMException {
0324: if (handler == null) {
0325: throw new DOMException(
0326: DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
0327: } else {
0328: float f = newItem.getValue();
0329: iccColors = new ArrayList();
0330: SVGNumber result = new ColorNumber(f);
0331: iccColors.add(result);
0332: handler.colorsInitialized(f);
0333: return result;
0334: }
0335: }
0336:
0337: /**
0338: * <b>DOM</b>: Implements {@link SVGNumberList#getItem(int)}.
0339: */
0340: public SVGNumber getItem(int index) throws DOMException {
0341: if (getColorType() != SVG_COLORTYPE_RGBCOLOR_ICCCOLOR) {
0342: throw new DOMException(DOMException.INDEX_SIZE_ERR, "");
0343: }
0344: int n = getNumberOfItems();
0345: if (index < 0 || index >= n) {
0346: throw new DOMException(DOMException.INDEX_SIZE_ERR, "");
0347: }
0348: if (iccColors == null) {
0349: iccColors = new ArrayList(n);
0350: for (int i = iccColors.size(); i < n; i++) {
0351: iccColors.add(null);
0352: }
0353: }
0354: Value value = valueProvider.getValue().item(1);
0355: float f = ((ICCColor) value).getColor(index);
0356: SVGNumber result = new ColorNumber(f);
0357: iccColors.set(index, result);
0358: return result;
0359: }
0360:
0361: /**
0362: * <b>DOM</b>: Implements {@link
0363: * SVGNumberList#insertItemBefore(SVGNumber,int)}.
0364: */
0365: public SVGNumber insertItemBefore(SVGNumber newItem, int index)
0366: throws DOMException {
0367: if (handler == null) {
0368: throw new DOMException(
0369: DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
0370: } else {
0371: int n = getNumberOfItems();
0372: if (index < 0 || index > n) {
0373: throw new DOMException(DOMException.INDEX_SIZE_ERR, "");
0374: }
0375: if (iccColors == null) {
0376: iccColors = new ArrayList(n);
0377: for (int i = iccColors.size(); i < n; i++) {
0378: iccColors.add(null);
0379: }
0380: }
0381: float f = newItem.getValue();
0382: SVGNumber result = new ColorNumber(f);
0383: iccColors.add(index, result);
0384: handler.colorInsertedBefore(f, index);
0385: return result;
0386: }
0387: }
0388:
0389: /**
0390: * <b>DOM</b>: Implements {@link
0391: * SVGNumberList#replaceItem(SVGNumber,int)}.
0392: */
0393: public SVGNumber replaceItem(SVGNumber newItem, int index)
0394: throws DOMException {
0395: if (handler == null) {
0396: throw new DOMException(
0397: DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
0398: } else {
0399: int n = getNumberOfItems();
0400: if (index < 0 || index >= n) {
0401: throw new DOMException(DOMException.INDEX_SIZE_ERR, "");
0402: }
0403: if (iccColors == null) {
0404: iccColors = new ArrayList(n);
0405: for (int i = iccColors.size(); i < n; i++) {
0406: iccColors.add(null);
0407: }
0408: }
0409: float f = newItem.getValue();
0410: SVGNumber result = new ColorNumber(f);
0411: iccColors.set(index, result);
0412: handler.colorReplaced(f, index);
0413: return result;
0414: }
0415: }
0416:
0417: /**
0418: * <b>DOM</b>: Implements {@link SVGNumberList#removeItem(int)}.
0419: */
0420: public SVGNumber removeItem(int index) throws DOMException {
0421: if (handler == null) {
0422: throw new DOMException(
0423: DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
0424: } else {
0425: int n = getNumberOfItems();
0426: if (index < 0 || index >= n) {
0427: throw new DOMException(DOMException.INDEX_SIZE_ERR, "");
0428: }
0429: SVGNumber result = null;
0430: if (iccColors != null) {
0431: result = (ColorNumber) iccColors.get(index);
0432: }
0433: if (result == null) {
0434: Value value = valueProvider.getValue().item(1);
0435: result = new ColorNumber(((ICCColor) value)
0436: .getColor(index));
0437: }
0438: handler.colorRemoved(index);
0439: return result;
0440: }
0441: }
0442:
0443: /**
0444: * <b>DOM</b>: Implements {@link SVGNumberList#appendItem(SVGNumber)}.
0445: */
0446: public SVGNumber appendItem(SVGNumber newItem) throws DOMException {
0447: if (handler == null) {
0448: throw new DOMException(
0449: DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
0450: } else {
0451: if (iccColors == null) {
0452: int n = getNumberOfItems();
0453: iccColors = new ArrayList(n);
0454: for (int i = 0; i < n; i++) {
0455: iccColors.add(null);
0456: }
0457: }
0458: float f = newItem.getValue();
0459: SVGNumber result = new ColorNumber(f);
0460: iccColors.add(result);
0461: handler.colorAppend(f);
0462: return result;
0463: }
0464: }
0465:
0466: /**
0467: * To represent a SVGNumber which is part of a color list.
0468: */
0469: protected class ColorNumber implements SVGNumber {
0470:
0471: /**
0472: * The value of this number, when detached.
0473: */
0474: protected float value;
0475:
0476: /**
0477: * Creates a new ColorNumber.
0478: */
0479: public ColorNumber(float f) {
0480: value = f;
0481: }
0482:
0483: /**
0484: * Implements {@link SVGNumber#getValue()}.
0485: */
0486: public float getValue() {
0487: if (iccColors == null) {
0488: return value;
0489: }
0490: int idx = iccColors.indexOf(this );
0491: if (idx == -1) {
0492: return value;
0493: }
0494: Value value = valueProvider.getValue().item(1);
0495: return ((ICCColor) value).getColor(idx);
0496: }
0497:
0498: /**
0499: * Implements {@link SVGNumber#setValue(float)}.
0500: */
0501: public void setValue(float f) {
0502: value = f;
0503: if (iccColors == null) {
0504: return;
0505: }
0506: int idx = iccColors.indexOf(this );
0507: if (idx == -1) {
0508: return;
0509: }
0510: if (handler == null) {
0511: throw new DOMException(
0512: DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
0513: } else {
0514: handler.colorReplaced(f, idx);
0515: }
0516: }
0517: }
0518:
0519: /**
0520: * To provide the actual value.
0521: */
0522: public interface ValueProvider {
0523:
0524: /**
0525: * Returns the current value associated with this object.
0526: */
0527: Value getValue();
0528: }
0529:
0530: /**
0531: * To manage the modifications on a CSS value.
0532: */
0533: public interface ModificationHandler {
0534:
0535: /**
0536: * Called when the value text has changed.
0537: */
0538: void textChanged(String text) throws DOMException;
0539:
0540: /**
0541: * Called when the red value text has changed.
0542: */
0543: void redTextChanged(String text) throws DOMException;
0544:
0545: /**
0546: * Called when the red float value has changed.
0547: */
0548: void redFloatValueChanged(short unit, float value)
0549: throws DOMException;
0550:
0551: /**
0552: * Called when the green value text has changed.
0553: */
0554: void greenTextChanged(String text) throws DOMException;
0555:
0556: /**
0557: * Called when the green float value has changed.
0558: */
0559: void greenFloatValueChanged(short unit, float value)
0560: throws DOMException;
0561:
0562: /**
0563: * Called when the blue value text has changed.
0564: */
0565: void blueTextChanged(String text) throws DOMException;
0566:
0567: /**
0568: * Called when the blue float value has changed.
0569: */
0570: void blueFloatValueChanged(short unit, float value)
0571: throws DOMException;
0572:
0573: /**
0574: * Called when the RGBColor text has changed.
0575: */
0576: void rgbColorChanged(String text) throws DOMException;
0577:
0578: /**
0579: * Called when the RGBColor and the ICCColor text has changed.
0580: */
0581: void rgbColorICCColorChanged(String rgb, String icc)
0582: throws DOMException;
0583:
0584: /**
0585: * Called when the SVGColor has changed.
0586: */
0587: void colorChanged(short type, String rgb, String icc)
0588: throws DOMException;
0589:
0590: /**
0591: * Called when the ICC color profile has changed.
0592: */
0593: void colorProfileChanged(String cp) throws DOMException;
0594:
0595: /**
0596: * Called when the ICC colors has changed.
0597: */
0598: void colorsCleared() throws DOMException;
0599:
0600: /**
0601: * Called when the ICC colors has been initialized.
0602: */
0603: void colorsInitialized(float f) throws DOMException;
0604:
0605: /**
0606: * Called when the ICC color has been inserted.
0607: */
0608: void colorInsertedBefore(float f, int idx) throws DOMException;
0609:
0610: /**
0611: * Called when the ICC color has been replaced.
0612: */
0613: void colorReplaced(float f, int idx) throws DOMException;
0614:
0615: /**
0616: * Called when the ICC color has been removed.
0617: */
0618: void colorRemoved(int idx) throws DOMException;
0619:
0620: /**
0621: * Called when the ICC color has been append.
0622: */
0623: void colorAppend(float f) throws DOMException;
0624: }
0625:
0626: /**
0627: * Provides an abstract implementation of a ModificationHandler.
0628: */
0629: public abstract class AbstractModificationHandler implements
0630: ModificationHandler {
0631:
0632: /**
0633: * Returns the associated value.
0634: */
0635: protected abstract Value getValue();
0636:
0637: /**
0638: * Called when the red value text has changed.
0639: */
0640: public void redTextChanged(String text) throws DOMException {
0641: StringBuffer sb = new StringBuffer(40);
0642: Value value = getValue();
0643: switch (getColorType()) {
0644: case SVG_COLORTYPE_RGBCOLOR:
0645: sb.append("rgb(");
0646: sb.append(text);
0647: sb.append(',');
0648: sb.append(value.getGreen().getCssText());
0649: sb.append(',');
0650: sb.append(value.getBlue().getCssText());
0651: sb.append(')');
0652: break;
0653:
0654: case SVG_COLORTYPE_RGBCOLOR_ICCCOLOR:
0655: sb.append("rgb(");
0656: sb.append(text);
0657: sb.append(',');
0658: sb.append(value.item(0).getGreen().getCssText());
0659: sb.append(',');
0660: sb.append(value.item(0).getBlue().getCssText());
0661: sb.append(')');
0662: sb.append(value.item(1).getCssText());
0663: break;
0664:
0665: default:
0666: throw new DOMException(
0667: DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
0668: }
0669: textChanged(sb.toString());
0670: }
0671:
0672: /**
0673: * Called when the red float value has changed.
0674: */
0675: public void redFloatValueChanged(short unit, float fValue)
0676: throws DOMException {
0677: StringBuffer sb = new StringBuffer(40);
0678: Value value = getValue();
0679: switch (getColorType()) {
0680: case SVG_COLORTYPE_RGBCOLOR:
0681: sb.append("rgb(");
0682: sb.append(FloatValue.getCssText(unit, fValue));
0683: sb.append(',');
0684: sb.append(value.getGreen().getCssText());
0685: sb.append(',');
0686: sb.append(value.getBlue().getCssText());
0687: sb.append(')');
0688: break;
0689:
0690: case SVG_COLORTYPE_RGBCOLOR_ICCCOLOR:
0691: sb.append("rgb(");
0692: sb.append(FloatValue.getCssText(unit, fValue));
0693: sb.append(',');
0694: sb.append(value.item(0).getGreen().getCssText());
0695: sb.append(',');
0696: sb.append(value.item(0).getBlue().getCssText());
0697: sb.append(')');
0698: sb.append(value.item(1).getCssText());
0699: break;
0700:
0701: default:
0702: throw new DOMException(
0703: DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
0704: }
0705: textChanged(sb.toString());
0706: }
0707:
0708: /**
0709: * Called when the green value text has changed.
0710: */
0711: public void greenTextChanged(String text) throws DOMException {
0712: StringBuffer sb = new StringBuffer(40);
0713: Value value = getValue();
0714: switch (getColorType()) {
0715: case SVG_COLORTYPE_RGBCOLOR:
0716: sb.append("rgb(");
0717: sb.append(value.getRed().getCssText());
0718: sb.append(',');
0719: sb.append(text);
0720: sb.append(',');
0721: sb.append(value.getBlue().getCssText());
0722: sb.append(')');
0723: break;
0724:
0725: case SVG_COLORTYPE_RGBCOLOR_ICCCOLOR:
0726: sb.append("rgb(");
0727: sb.append(value.item(0).getRed().getCssText());
0728: sb.append(',');
0729: sb.append(text);
0730: sb.append(',');
0731: sb.append(value.item(0).getBlue().getCssText());
0732: sb.append(')');
0733: sb.append(value.item(1).getCssText());
0734: break;
0735:
0736: default:
0737: throw new DOMException(
0738: DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
0739: }
0740: textChanged(sb.toString());
0741: }
0742:
0743: /**
0744: * Called when the green float value has changed.
0745: */
0746: public void greenFloatValueChanged(short unit, float fValue)
0747: throws DOMException {
0748: StringBuffer sb = new StringBuffer(40);
0749: Value value = getValue();
0750: switch (getColorType()) {
0751: case SVG_COLORTYPE_RGBCOLOR:
0752: sb.append("rgb(");
0753: sb.append(value.getRed().getCssText());
0754: sb.append(',');
0755: sb.append(FloatValue.getCssText(unit, fValue));
0756: sb.append(',');
0757: sb.append(value.getBlue().getCssText());
0758: sb.append(')');
0759: break;
0760:
0761: case SVG_COLORTYPE_RGBCOLOR_ICCCOLOR:
0762: sb.append("rgb(");
0763: sb.append(value.item(0).getRed().getCssText());
0764: sb.append(',');
0765: sb.append(FloatValue.getCssText(unit, fValue));
0766: sb.append(',');
0767: sb.append(value.item(0).getBlue().getCssText());
0768: sb.append(')');
0769: sb.append(value.item(1).getCssText());
0770: break;
0771:
0772: default:
0773: throw new DOMException(
0774: DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
0775: }
0776: textChanged(sb.toString());
0777: }
0778:
0779: /**
0780: * Called when the blue value text has changed.
0781: */
0782: public void blueTextChanged(String text) throws DOMException {
0783: StringBuffer sb = new StringBuffer(40);
0784: Value value = getValue();
0785: switch (getColorType()) {
0786: case SVG_COLORTYPE_RGBCOLOR:
0787: sb.append("rgb(");
0788: sb.append(value.getRed().getCssText());
0789: sb.append(',');
0790: sb.append(value.getGreen().getCssText());
0791: sb.append(',');
0792: sb.append(text);
0793: sb.append(')');
0794: break;
0795:
0796: case SVG_COLORTYPE_RGBCOLOR_ICCCOLOR:
0797: sb.append("rgb(");
0798: sb.append(value.item(0).getRed().getCssText());
0799: sb.append(',');
0800: sb.append(value.item(0).getGreen().getCssText());
0801: sb.append(',');
0802: sb.append(text);
0803: sb.append(')');
0804: sb.append(value.item(1).getCssText());
0805: break;
0806:
0807: default:
0808: throw new DOMException(
0809: DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
0810: }
0811: textChanged(sb.toString());
0812: }
0813:
0814: /**
0815: * Called when the blue float value has changed.
0816: */
0817: public void blueFloatValueChanged(short unit, float fValue)
0818: throws DOMException {
0819: StringBuffer sb = new StringBuffer(40);
0820: Value value = getValue();
0821: switch (getColorType()) {
0822: case SVG_COLORTYPE_RGBCOLOR:
0823: sb.append("rgb(");
0824: sb.append(value.getRed().getCssText());
0825: sb.append(',');
0826: sb.append(value.getGreen().getCssText());
0827: sb.append(',');
0828: sb.append(FloatValue.getCssText(unit, fValue));
0829: sb.append(')');
0830: break;
0831:
0832: case SVG_COLORTYPE_RGBCOLOR_ICCCOLOR:
0833: sb.append("rgb(");
0834: sb.append(value.item(0).getRed().getCssText());
0835: sb.append(',');
0836: sb.append(value.item(0).getGreen().getCssText());
0837: sb.append(',');
0838: sb.append(FloatValue.getCssText(unit, fValue));
0839: sb.append(')');
0840: sb.append(value.item(1).getCssText());
0841: break;
0842:
0843: default:
0844: throw new DOMException(
0845: DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
0846: }
0847: textChanged(sb.toString());
0848: }
0849:
0850: /**
0851: * Called when the RGBColor text has changed.
0852: */
0853: public void rgbColorChanged(String text) throws DOMException {
0854: switch (getColorType()) {
0855: case SVG_COLORTYPE_RGBCOLOR:
0856: break;
0857:
0858: case SVG_COLORTYPE_RGBCOLOR_ICCCOLOR:
0859: text += getValue().item(1).getCssText();
0860: break;
0861:
0862: default:
0863: throw new DOMException(
0864: DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
0865: }
0866: textChanged(text);
0867: }
0868:
0869: /**
0870: * Called when the RGBColor and the ICCColor text has changed.
0871: */
0872: public void rgbColorICCColorChanged(String rgb, String icc)
0873: throws DOMException {
0874: switch (getColorType()) {
0875: case SVG_COLORTYPE_RGBCOLOR_ICCCOLOR:
0876: textChanged(rgb + ' ' + icc);
0877: break;
0878:
0879: default:
0880: throw new DOMException(
0881: DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
0882: }
0883: }
0884:
0885: /**
0886: * Called when the SVGColor has changed.
0887: */
0888: public void colorChanged(short type, String rgb, String icc)
0889: throws DOMException {
0890: switch (type) {
0891: case SVG_COLORTYPE_CURRENTCOLOR:
0892: textChanged(CSSConstants.CSS_CURRENTCOLOR_VALUE);
0893: break;
0894:
0895: case SVG_COLORTYPE_RGBCOLOR:
0896: textChanged(rgb);
0897: break;
0898:
0899: case SVG_COLORTYPE_RGBCOLOR_ICCCOLOR:
0900: textChanged(rgb + ' ' + icc);
0901: break;
0902:
0903: default:
0904: throw new DOMException(DOMException.NOT_SUPPORTED_ERR,
0905: "");
0906: }
0907: }
0908:
0909: /**
0910: * Called when the ICC color profile has changed.
0911: */
0912: public void colorProfileChanged(String cp) throws DOMException {
0913: Value value = getValue();
0914: switch (getColorType()) {
0915: case SVG_COLORTYPE_RGBCOLOR_ICCCOLOR:
0916: StringBuffer sb = new StringBuffer(value.item(0)
0917: .getCssText());
0918: sb.append(" icc-color(");
0919: sb.append(cp);
0920: ICCColor iccc = (ICCColor) value.item(1);
0921: for (int i = 0; i < iccc.getLength(); i++) {
0922: sb.append(',');
0923: sb.append(iccc.getColor(i));
0924: }
0925: sb.append(')');
0926: textChanged(sb.toString());
0927: break;
0928:
0929: default:
0930: throw new DOMException(
0931: DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
0932: }
0933: }
0934:
0935: /**
0936: * Called when the ICC colors has changed.
0937: */
0938: public void colorsCleared() throws DOMException {
0939: Value value = getValue();
0940: switch (getColorType()) {
0941: case SVG_COLORTYPE_RGBCOLOR_ICCCOLOR:
0942: StringBuffer sb = new StringBuffer(value.item(0)
0943: .getCssText());
0944: sb.append(" icc-color(");
0945: ICCColor iccc = (ICCColor) value.item(1);
0946: sb.append(iccc.getColorProfile());
0947: sb.append(')');
0948: textChanged(sb.toString());
0949: break;
0950:
0951: default:
0952: throw new DOMException(
0953: DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
0954: }
0955: }
0956:
0957: /**
0958: * Called when the ICC colors has been initialized.
0959: */
0960: public void colorsInitialized(float f) throws DOMException {
0961: Value value = getValue();
0962: switch (getColorType()) {
0963: case SVG_COLORTYPE_RGBCOLOR_ICCCOLOR:
0964: StringBuffer sb = new StringBuffer(value.item(0)
0965: .getCssText());
0966: sb.append(" icc-color(");
0967: ICCColor iccc = (ICCColor) value.item(1);
0968: sb.append(iccc.getColorProfile());
0969: sb.append(',');
0970: sb.append(f);
0971: sb.append(')');
0972: textChanged(sb.toString());
0973: break;
0974:
0975: default:
0976: throw new DOMException(
0977: DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
0978: }
0979: }
0980:
0981: /**
0982: * Called when the ICC color has been inserted.
0983: */
0984: public void colorInsertedBefore(float f, int idx)
0985: throws DOMException {
0986: Value value = getValue();
0987: switch (getColorType()) {
0988: case SVG_COLORTYPE_RGBCOLOR_ICCCOLOR:
0989: StringBuffer sb = new StringBuffer(value.item(0)
0990: .getCssText());
0991: sb.append(" icc-color(");
0992: ICCColor iccc = (ICCColor) value.item(1);
0993: sb.append(iccc.getColorProfile());
0994: for (int i = 0; i < idx; i++) {
0995: sb.append(',');
0996: sb.append(iccc.getColor(i));
0997: }
0998: sb.append(',');
0999: sb.append(f);
1000: for (int i = idx; i < iccc.getLength(); i++) {
1001: sb.append(',');
1002: sb.append(iccc.getColor(i));
1003: }
1004: sb.append(')');
1005: textChanged(sb.toString());
1006: break;
1007:
1008: default:
1009: throw new DOMException(
1010: DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
1011: }
1012: }
1013:
1014: /**
1015: * Called when the ICC color has been replaced.
1016: */
1017: public void colorReplaced(float f, int idx) throws DOMException {
1018: Value value = getValue();
1019: switch (getColorType()) {
1020: case SVG_COLORTYPE_RGBCOLOR_ICCCOLOR:
1021: StringBuffer sb = new StringBuffer(value.item(0)
1022: .getCssText());
1023: sb.append(" icc-color(");
1024: ICCColor iccc = (ICCColor) value.item(1);
1025: sb.append(iccc.getColorProfile());
1026: for (int i = 0; i < idx; i++) {
1027: sb.append(',');
1028: sb.append(iccc.getColor(i));
1029: }
1030: sb.append(',');
1031: sb.append(f);
1032: for (int i = idx + 1; i < iccc.getLength(); i++) {
1033: sb.append(',');
1034: sb.append(iccc.getColor(i));
1035: }
1036: sb.append(')');
1037: textChanged(sb.toString());
1038: break;
1039:
1040: default:
1041: throw new DOMException(
1042: DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
1043: }
1044: }
1045:
1046: /**
1047: * Called when the ICC color has been removed.
1048: */
1049: public void colorRemoved(int idx) throws DOMException {
1050: Value value = getValue();
1051: switch (getColorType()) {
1052: case SVG_COLORTYPE_RGBCOLOR_ICCCOLOR:
1053: StringBuffer sb = new StringBuffer(value.item(0)
1054: .getCssText());
1055: sb.append(" icc-color(");
1056: ICCColor iccc = (ICCColor) value.item(1);
1057: sb.append(iccc.getColorProfile());
1058: for (int i = 0; i < idx; i++) {
1059: sb.append(',');
1060: sb.append(iccc.getColor(i));
1061: }
1062: for (int i = idx + 1; i < iccc.getLength(); i++) {
1063: sb.append(',');
1064: sb.append(iccc.getColor(i));
1065: }
1066: sb.append(')');
1067: textChanged(sb.toString());
1068: break;
1069:
1070: default:
1071: throw new DOMException(
1072: DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
1073: }
1074: }
1075:
1076: /**
1077: * Called when the ICC color has been append.
1078: */
1079: public void colorAppend(float f) throws DOMException {
1080: Value value = getValue();
1081: switch (getColorType()) {
1082: case SVG_COLORTYPE_RGBCOLOR_ICCCOLOR:
1083: StringBuffer sb = new StringBuffer(value.item(0)
1084: .getCssText());
1085: sb.append(" icc-color(");
1086: ICCColor iccc = (ICCColor) value.item(1);
1087: sb.append(iccc.getColorProfile());
1088: for (int i = 0; i < iccc.getLength(); i++) {
1089: sb.append(',');
1090: sb.append(iccc.getColor(i));
1091: }
1092: sb.append(',');
1093: sb.append(f);
1094: sb.append(')');
1095: textChanged(sb.toString());
1096: break;
1097:
1098: default:
1099: throw new DOMException(
1100: DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
1101: }
1102: }
1103: }
1104:
1105: /**
1106: * To store a component.
1107: */
1108: protected abstract class AbstractComponent implements
1109: CSSPrimitiveValue {
1110:
1111: /**
1112: * The returns the actual value of this component.
1113: */
1114: protected abstract Value getValue();
1115:
1116: /**
1117: * <b>DOM</b>: Implements {@link
1118: * org.w3c.dom.css.CSSValue#getCssText()}.
1119: */
1120: public String getCssText() {
1121: return getValue().getCssText();
1122: }
1123:
1124: /**
1125: * <b>DOM</b>: Implements {@link
1126: * org.w3c.dom.css.CSSValue#getCssValueType()}.
1127: */
1128: public short getCssValueType() {
1129: return getValue().getCssValueType();
1130: }
1131:
1132: /**
1133: * <b>DOM</b>: Implements {@link
1134: * org.w3c.dom.css.CSSPrimitiveValue#getPrimitiveType()}.
1135: */
1136: public short getPrimitiveType() {
1137: return getValue().getPrimitiveType();
1138: }
1139:
1140: /**
1141: * <b>DOM</b>: Implements {@link
1142: * org.w3c.dom.css.CSSPrimitiveValue#getFloatValue(short)}.
1143: */
1144: public float getFloatValue(short unitType) throws DOMException {
1145: return CSSOMValue.convertFloatValue(unitType, getValue());
1146: }
1147:
1148: /**
1149: * <b>DOM</b>: Implements {@link
1150: * org.w3c.dom.css.CSSPrimitiveValue#getStringValue()}.
1151: */
1152: public String getStringValue() throws DOMException {
1153: return valueProvider.getValue().getStringValue();
1154: }
1155:
1156: /**
1157: * <b>DOM</b>: Implements {@link
1158: * org.w3c.dom.css.CSSPrimitiveValue#getCounterValue()}.
1159: */
1160: public Counter getCounterValue() throws DOMException {
1161: throw new DOMException(DOMException.INVALID_ACCESS_ERR, "");
1162: }
1163:
1164: /**
1165: * <b>DOM</b>: Implements {@link
1166: * org.w3c.dom.css.CSSPrimitiveValue#getRectValue()}.
1167: */
1168: public Rect getRectValue() throws DOMException {
1169: throw new DOMException(DOMException.INVALID_ACCESS_ERR, "");
1170: }
1171:
1172: /**
1173: * <b>DOM</b>: Implements {@link
1174: * org.w3c.dom.css.CSSPrimitiveValue#getRGBColorValue()}.
1175: */
1176: public RGBColor getRGBColorValue() throws DOMException {
1177: throw new DOMException(DOMException.INVALID_ACCESS_ERR, "");
1178: }
1179:
1180: // CSSValueList ///////////////////////////////////////////////////////
1181:
1182: /**
1183: * <b>DOM</b>: Implements {@link
1184: * org.w3c.dom.css.CSSValueList#getLength()}.
1185: */
1186: public int getLength() {
1187: throw new DOMException(DOMException.INVALID_ACCESS_ERR, "");
1188: }
1189:
1190: /**
1191: * <b>DOM</b>: Implements {@link
1192: * org.w3c.dom.css.CSSValueList#item(int)}.
1193: */
1194: public CSSValue item(int index) {
1195: throw new DOMException(DOMException.INVALID_ACCESS_ERR, "");
1196: }
1197: }
1198:
1199: /**
1200: * To store a Float component.
1201: */
1202: protected abstract class FloatComponent extends AbstractComponent {
1203:
1204: /**
1205: * <b>DOM</b>: Implements {@link
1206: * org.w3c.dom.css.CSSPrimitiveValue#setStringValue(short,String)}.
1207: */
1208: public void setStringValue(short stringType, String stringValue)
1209: throws DOMException {
1210: throw new DOMException(DOMException.INVALID_ACCESS_ERR, "");
1211: }
1212: }
1213:
1214: /**
1215: * To represents a red component.
1216: */
1217: protected class RedComponent extends FloatComponent {
1218:
1219: /**
1220: * The returns the actual value of this component.
1221: */
1222: protected Value getValue() {
1223: return valueProvider.getValue().getRed();
1224: }
1225:
1226: /**
1227: * <b>DOM</b>: Implements {@link
1228: * org.w3c.dom.css.CSSValue#setCssText(String)}.
1229: */
1230: public void setCssText(String cssText) throws DOMException {
1231: if (handler == null) {
1232: throw new DOMException(
1233: DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
1234: } else {
1235: getValue();
1236: handler.redTextChanged(cssText);
1237: }
1238: }
1239:
1240: /**
1241: * <b>DOM</b>: Implements {@link
1242: * org.w3c.dom.css.CSSPrimitiveValue#setFloatValue(short,float)}.
1243: */
1244: public void setFloatValue(short unitType, float floatValue)
1245: throws DOMException {
1246: if (handler == null) {
1247: throw new DOMException(
1248: DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
1249: } else {
1250: getValue();
1251: handler.redFloatValueChanged(unitType, floatValue);
1252: }
1253: }
1254:
1255: }
1256:
1257: /**
1258: * To represents a green component.
1259: */
1260: protected class GreenComponent extends FloatComponent {
1261:
1262: /**
1263: * The returns the actual value of this component.
1264: */
1265: protected Value getValue() {
1266: return valueProvider.getValue().getGreen();
1267: }
1268:
1269: /**
1270: * <b>DOM</b>: Implements {@link
1271: * org.w3c.dom.css.CSSValue#setCssText(String)}.
1272: */
1273: public void setCssText(String cssText) throws DOMException {
1274: if (handler == null) {
1275: throw new DOMException(
1276: DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
1277: } else {
1278: getValue();
1279: handler.greenTextChanged(cssText);
1280: }
1281: }
1282:
1283: /**
1284: * <b>DOM</b>: Implements {@link
1285: * org.w3c.dom.css.CSSPrimitiveValue#setFloatValue(short,float)}.
1286: */
1287: public void setFloatValue(short unitType, float floatValue)
1288: throws DOMException {
1289: if (handler == null) {
1290: throw new DOMException(
1291: DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
1292: } else {
1293: getValue();
1294: handler.greenFloatValueChanged(unitType, floatValue);
1295: }
1296: }
1297:
1298: }
1299:
1300: /**
1301: * To represents a blue component.
1302: */
1303: protected class BlueComponent extends FloatComponent {
1304:
1305: /**
1306: * The returns the actual value of this component.
1307: */
1308: protected Value getValue() {
1309: return valueProvider.getValue().getBlue();
1310: }
1311:
1312: /**
1313: * <b>DOM</b>: Implements {@link
1314: * org.w3c.dom.css.CSSValue#setCssText(String)}.
1315: */
1316: public void setCssText(String cssText) throws DOMException {
1317: if (handler == null) {
1318: throw new DOMException(
1319: DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
1320: } else {
1321: getValue();
1322: handler.blueTextChanged(cssText);
1323: }
1324: }
1325:
1326: /**
1327: * <b>DOM</b>: Implements {@link
1328: * org.w3c.dom.css.CSSPrimitiveValue#setFloatValue(short,float)}.
1329: */
1330: public void setFloatValue(short unitType, float floatValue)
1331: throws DOMException {
1332: if (handler == null) {
1333: throw new DOMException(
1334: DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
1335: } else {
1336: getValue();
1337: handler.blueFloatValueChanged(unitType, floatValue);
1338: }
1339: }
1340:
1341: }
1342:
1343: }
|