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 org.apache.batik.css.engine.CSSEngine;
044: import org.apache.batik.css.engine.CSSStylableElement;
045: import org.apache.batik.css.engine.StyleMap;
046: import org.apache.batik.css.engine.value.FloatValue;
047: import org.apache.batik.css.engine.value.StringMap;
048: import org.apache.batik.css.engine.value.Value;
049: import org.w3c.css.sac.LexicalUnit;
050: import org.w3c.dom.DOMException;
051: import org.w3c.dom.css.CSSPrimitiveValue;
052: import org.apache.batik.css.engine.value.IdentifierProvider;
053:
054: /**
055: * This class provides a manager for the "line-height" CSS property
056: *
057: * @author Tor Norbye
058: */
059: public class LineHeightManager extends NonautoableLengthManager
060: implements IdentifierProvider {
061:
062: /**
063: * The identifier values.
064: */
065: protected final static StringMap values = new StringMap();
066: static {
067: values.put(CssConstants.CSS_NORMAL_VALUE,
068: CssValueConstants.NORMAL_VALUE);
069: }
070:
071: public boolean isInheritedProperty() {
072: return true;
073: }
074:
075: public String getPropertyName() {
076: return CssConstants.CSS_LINE_HEIGHT_PROPERTY;
077: }
078:
079: public Value getDefaultValue() {
080: return CssValueConstants.NORMAL_VALUE;
081: }
082:
083: public Value createValue(LexicalUnit lu, CSSEngine engine)
084: throws DOMException {
085: switch (lu.getLexicalUnitType()) {
086: case LexicalUnit.SAC_INHERIT:
087: return CssValueConstants.INHERIT_VALUE;
088:
089: case LexicalUnit.SAC_IDENT:
090: /* See faster impl below as long as we have only a single ident
091: Object v = values.get(lu.getStringValue().toLowerCase().intern());
092: if (v == null) {
093: throw createInvalidIdentifierDOMException(lu.getStringValue());
094: }
095: return (Value)v;
096: */
097: String s = lu.getStringValue();
098: if (CssConstants.CSS_NORMAL_VALUE.equalsIgnoreCase(s)) {
099: return CssValueConstants.NORMAL_VALUE;
100: }
101: throw createInvalidIdentifierDOMException(s, engine);
102: }
103: return super .createValue(lu, engine);
104: }
105:
106: public Value createStringValue(short type, String value,
107: CSSEngine engine) throws DOMException {
108: if (type != CSSPrimitiveValue.CSS_IDENT) {
109: throw createInvalidIdentifierDOMException(value, engine);
110: }
111: Object v = values.get(value.toLowerCase().intern());
112: if (v == null) {
113: throw createInvalidIdentifierDOMException(value, engine);
114: }
115: return (Value) v;
116: }
117:
118: public Value computeValue(CSSStylableElement elt, String pseudo,
119: CSSEngine engine, int idx, StyleMap sm, Value value) {
120: switch (value.getPrimitiveType()) {
121: case CSSPrimitiveValue.CSS_PERCENTAGE: {
122: sm.putFontSizeRelative(idx, true);
123: float v = value.getFloatValue();
124: int fsidx = engine.getFontSizeIndex();
125: float fs;
126: fs = engine.getComputedStyle(elt, pseudo, fsidx)
127: .getFloatValue();
128: return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, v * fs
129: / 100);
130: }
131: // XXX What about INTEGER AND REAL?
132: case CSSPrimitiveValue.CSS_NUMBER: {
133: // for line-height the number refers to multiples of the font size
134: sm.putFontSizeRelative(idx, true);
135: float v = value.getFloatValue();
136: int fsidx = engine.getFontSizeIndex();
137: float fs;
138: fs = engine.getComputedStyle(elt, pseudo, fsidx)
139: .getFloatValue();
140: return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, v * fs);
141: }
142: }
143: return super .computeValue(elt, pseudo, engine, idx, sm, value);
144: }
145:
146: protected int getOrientation() {
147: return BOTH_ORIENTATION; // Not used
148: }
149:
150: public StringMap getIdentifierMap() {
151: return values;
152: }
153: }
|