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.StringMap;
047: import org.apache.batik.css.engine.value.Value;
048: import org.w3c.css.sac.LexicalUnit;
049: import org.w3c.dom.DOMException;
050: import org.w3c.dom.css.CSSPrimitiveValue;
051: import org.apache.batik.css.engine.value.IdentifierProvider;
052:
053: /**
054: * This class provides a manager for border-top-width, border-left-width,
055: * border-right-width and border-bottom-width.
056: *
057: * @author Tor Norbye
058: */
059: public class BorderWidthManager extends NonautoableLengthManager
060: implements IdentifierProvider {
061: private String property;
062:
063: /**
064: * The identifier values.
065: */
066: protected final static StringMap values = new StringMap();
067: static {
068: values.put(CssConstants.CSS_THIN_VALUE,
069: CssValueConstants.THIN_VALUE);
070: values.put(CssConstants.CSS_MEDIUM_VALUE,
071: CssValueConstants.MEDIUM_VALUE);
072: values.put(CssConstants.CSS_THICK_VALUE,
073: CssValueConstants.THICK_VALUE);
074: }
075:
076: public BorderWidthManager(String property) {
077: this .property = property;
078: }
079:
080: public boolean isInheritedProperty() {
081: return false;
082: }
083:
084: public String getPropertyName() {
085: return property;
086: }
087:
088: public Value getDefaultValue() {
089: return CssValueConstants.MEDIUM_VALUE;
090: }
091:
092: public Value createValue(LexicalUnit lu, CSSEngine engine)
093: throws DOMException {
094: switch (lu.getLexicalUnitType()) {
095: case LexicalUnit.SAC_INHERIT:
096: return CssValueConstants.INHERIT_VALUE;
097:
098: case LexicalUnit.SAC_IDENT:
099: String s = lu.getStringValue().toLowerCase().intern();
100: Object v = values.get(s);
101: if (v == null) {
102: throw createInvalidIdentifierDOMException(s, engine);
103: }
104: return (Value) v;
105: }
106: return super .createValue(lu, engine);
107: }
108:
109: public Value createStringValue(short type, String value,
110: CSSEngine engine) throws DOMException {
111: if (type != CSSPrimitiveValue.CSS_IDENT) {
112: throw createInvalidStringTypeDOMException(type, engine);
113: }
114: Object v = values.get(value.toLowerCase().intern());
115: if (v == null) {
116: throw createInvalidIdentifierDOMException(value, engine);
117: }
118: return (Value) v;
119: }
120:
121: public Value computeValue(CSSStylableElement elt, String pseudo,
122: CSSEngine engine, int idx, StyleMap sm, Value value) {
123:
124: if (value.getPrimitiveType() == CSSPrimitiveValue.CSS_PERCENTAGE) {
125: return CssValueConstants.NUMBER_0;
126: }
127:
128: // Border thickness - these are the values Mozilla 1.5 empirically
129: // seems to use (on Solaris, hopefully not platform specific)
130: // In the working draft for the CSS3 Box Model, they mention UA's
131: // could make it depend on the font size, e.g. use the below sizes
132: // when the font size is less than 17pt, and bump them up for bigger
133: // fonts.
134: if (value == CssValueConstants.THIN_VALUE) {
135: return CssValueConstants.NUMBER_1;
136: } else if (value == CssValueConstants.MEDIUM_VALUE) {
137: return CssValueConstants.NUMBER_3;
138: } else if (value == CssValueConstants.THICK_VALUE) {
139: return CssValueConstants.NUMBER_5;
140: }
141:
142: /*
143: // absolute identifiers
144: CSSContext ctx = engine.getCSSContext();
145: float fs = ctx.getMediumFontSize();
146: String s = value.getStringValue();
147: switch (s.charAt(0)) {
148: case 'm':
149: break;
150:
151: case 's':
152: fs = (float)(fs / 1.2);
153: break;
154:
155: case 'l':
156: fs = (float)(fs * 1.2);
157: break;
158:
159: default: // 'x'
160: switch (s.charAt(1)) {
161: case 'x':
162: switch (s.charAt(3)) {
163: case 's':
164: fs = (float)(((fs / 1.2) / 1.2) / 1.2);
165: break;
166:
167: default: // 'l'
168: fs = (float)(fs * 1.2 * 1.2 * 1.2);
169: }
170: break;
171:
172: default: // '-'
173: switch (s.charAt(2)) {
174: case 's':
175: fs = (float)((fs / 1.2) / 1.2);
176: break;
177:
178: default: // 'l'
179: fs = (float)(fs * 1.2 * 1.2);
180: }
181: }
182: }
183: return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, fs);
184: */
185:
186: return super .computeValue(elt, pseudo, engine, idx, sm, value);
187: }
188:
189: protected int getOrientation() {
190: return HORIZONTAL_ORIENTATION; // doesn't matter / not used, we don't allow %
191: }
192:
193: public StringMap getIdentifierMap() {
194: return values;
195: }
196: }
|