001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.visualweb.designer.cssengine;
042:
043: import java.util.ArrayList;
044: import java.util.Iterator;
045: import java.util.List;
046:
047: import org.apache.batik.css.engine.CSSEngine;
048: import org.apache.batik.css.engine.value.AbstractValueFactory;
049: import org.apache.batik.css.engine.value.ShorthandManager;
050: import org.apache.batik.css.engine.value.svg.ColorManager;
051: import org.apache.batik.css.parser.CSSLexicalUnit;
052: import org.w3c.css.sac.LexicalUnit;
053: import org.w3c.dom.DOMException;
054:
055: /**
056: * Represents the "background" shorthand property for setting
057: * background-color, background-image, background-position,
058: * background-repeat, and background-attachment.
059: *
060: * @todo I need to RESET all the values for the various properties...
061: *
062: * @author Tor Norbye
063: */
064: public class BackgroundShorthandManager extends AbstractValueFactory
065: implements ShorthandManager {
066: public String getPropertyName() {
067: return CssConstants.CSS_BACKGROUND_PROPERTY;
068: }
069:
070: public void setValues(CSSEngine eng,
071: ShorthandManager.PropertyHandler ph, LexicalUnit lu,
072: boolean imp) throws DOMException {
073: // I cannot handle background-position lexical units immediately because there
074: // may be multiple, and these should be sent in as a group.
075: // ArrayList positions = new ArrayList(2);
076: List<CSSLexicalUnit> positions = new ArrayList<CSSLexicalUnit>(
077: 2);
078:
079: for (; lu != null; lu = lu.getNextLexicalUnit()) {
080: switch (lu.getLexicalUnitType()) {
081: /* Inherit isn't allowed is it?
082: case LexicalUnit.SAC_INHERIT:
083: return ValueConstants.INHERIT_VALUE;
084: */
085: case LexicalUnit.SAC_URI:
086: ph.property(CssConstants.CSS_BACKGROUND_IMAGE_PROPERTY,
087: lu, imp);
088:
089: break;
090:
091: case LexicalUnit.SAC_RGBCOLOR:
092: ph.property(CssConstants.CSS_BACKGROUND_COLOR_PROPERTY,
093: lu, imp);
094:
095: break;
096:
097: case LexicalUnit.SAC_INTEGER: {
098: // I've gotta duplicate the unit (without having a list) such
099: // that the BackgroundPositionManager doesn't look at the
100: // next lexical unit to use it as a second number
101: CSSLexicalUnit lu2 = CSSLexicalUnit.createInteger(lu
102: .getIntegerValue(), null);
103: positions.add(lu2);
104:
105: break;
106: }
107:
108: case LexicalUnit.SAC_EM:
109: case LexicalUnit.SAC_EX:
110: case LexicalUnit.SAC_PIXEL:
111: case LexicalUnit.SAC_CENTIMETER:
112: case LexicalUnit.SAC_MILLIMETER:
113: case LexicalUnit.SAC_INCH:
114: case LexicalUnit.SAC_POINT:
115: case LexicalUnit.SAC_PICA:
116: case LexicalUnit.SAC_REAL:
117: case LexicalUnit.SAC_PERCENTAGE: {
118: // I've gotta duplicate the unit (without having a list) such
119: // that the BackgroundPositionManager doesn't look at the
120: // next lexical unit to use it as a second number
121: CSSLexicalUnit lu2 = CSSLexicalUnit
122: .createFloat(lu.getLexicalUnitType(), lu
123: .getFloatValue(), null);
124: positions.add(lu2);
125:
126: break;
127: }
128:
129: case LexicalUnit.SAC_IDENT:
130: case LexicalUnit.SAC_STRING_VALUE:
131:
132: String s = lu.getStringValue().toLowerCase().intern();
133:
134: if (BackgroundPositionManager.values.get(s) != null) {
135: // I've gotta duplicate the unit (without having a list) such
136: // that the BackgroundPositionManager doesn't look at the
137: // next lexical unit to use it as a second number
138: CSSLexicalUnit lu2 = CSSLexicalUnit.createString(lu
139: .getLexicalUnitType(), lu.getStringValue(),
140: null);
141: positions.add(lu2);
142: } else if (ColorManager.values.get(s) != null) {
143: ph.property(
144: CssConstants.CSS_BACKGROUND_COLOR_PROPERTY,
145: lu, imp);
146: } else if (BackgroundRepeatManager.values.get(s) != null) {
147: ph
148: .property(
149: CssConstants.CSS_BACKGROUND_REPEAT_PROPERTY,
150: lu, imp);
151:
152: /*
153: } else if (BackgroundAttachmentManager.values.get(s) != null) {
154: ph.property(CssConstants.CSS_BACKGROUND_ATTACHMENT_PROPERTY, lu, imp);
155: */
156: }
157:
158: break;
159: }
160: }
161:
162: // Process positions.
163: if (positions.size() == 1) {
164: ph.property(CssConstants.CSS_BACKGROUND_POSITION_PROPERTY,
165: positions.get(0), imp);
166: } else if (positions.size() > 1) {
167: CSSLexicalUnit prev = null;
168: // Iterator it = positions.iterator();
169: Iterator<CSSLexicalUnit> it = positions.iterator();
170:
171: while (it.hasNext()) {
172: CSSLexicalUnit lu2 = it.next();
173:
174: if (prev != null) {
175: prev.setNextLexicalUnit(lu2);
176: }
177:
178: lu2.setPreviousLexicalUnit(prev);
179: prev = lu2;
180: }
181:
182: ph.property(CssConstants.CSS_BACKGROUND_POSITION_PROPERTY,
183: positions.get(0), imp);
184: }
185: }
186: }
|