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.netbeans.modules.visualweb.spi.designer.cssengine.CssUserAgentInfo;
044: import org.apache.batik.css.engine.CSSContext;
045: import org.apache.batik.css.engine.CSSStylableElement;
046: import org.apache.batik.css.engine.SystemColorSupport;
047: import org.apache.batik.css.engine.value.Value;
048: import org.apache.batik.util.CSSConstants;
049: import org.apache.batik.util.ParsedURL;
050: import org.w3c.dom.Document;
051: import org.w3c.dom.Element;
052:
053: /**
054: * Context used by the CSS parser code in Batik to get user agent specific stuff
055: *
056: * @author Tor Norbye
057: */
058: public class DesignerContext implements CSSContext {
059: private Document document;
060: private UserAgent userAgent;
061: private XhtmlCssEngine engine;
062: private final CssUserAgentInfo userAgentInfo;
063:
064: public DesignerContext(Document document, UserAgent userAgent,
065: CssUserAgentInfo userAgentInfo) {
066: this .document = document;
067: this .userAgent = userAgent;
068: this .userAgentInfo = userAgentInfo;
069: }
070:
071: public void setDocument(Document document) {
072: this .document = document;
073: }
074:
075: public void setEngine(XhtmlCssEngine engine) {
076: this .engine = engine;
077: }
078:
079: /**
080: * Returns the Value corresponding to the given system color.
081: */
082: public Value getSystemColor(String ident) {
083: if (ident.equals(CSSConstants.CSS_LINKCOLOR_VALUE)) {
084: return engine.getLinkColor();
085: }
086:
087: return SystemColorSupport.getSystemColor(ident);
088: }
089:
090: /**
091: * Returns the value corresponding to the default font.
092: */
093: public Value getDefaultFontFamily() {
094: // No cache needed since the default font family is asked only
095: // one time on the root element (only if it does not have its
096: // own font-family).
097: // XXX shouldn't this be cached?
098: // RaveDocument doc = (RaveDocument)document;
099: Document doc = document;
100: //CSSStylableElement root = (CSSStylableElement)doc.getDocumentElement();
101: CSSStylableElement root = null;
102: if (doc != null) {
103: root = (CSSStylableElement) doc.getDocumentElement();
104: }
105: String str = userAgent.getDefaultFontFamily();
106: return engine.parsePropertyValue(root,
107: CssConstants.CSS_FONT_FAMILY_PROPERTY, str);
108: }
109:
110: /**
111: * Returns a lighter font-weight.
112: */
113: public float getLighterFontWeight(float f) {
114: return userAgent.getLighterFontWeight(f);
115: }
116:
117: /**
118: * Returns a bolder font-weight.
119: */
120: public float getBolderFontWeight(float f) {
121: return userAgent.getBolderFontWeight(f);
122: }
123:
124: /**
125: * Returns the size of a px CSS unit in millimeters.
126: */
127: public float getPixelUnitToMillimeter() {
128: return userAgent.getPixelUnitToMillimeter();
129: }
130:
131: /**
132: * Returns the size of a px CSS unit in millimeters.
133: * This will be removed after next release.
134: * @see #getPixelUnitToMillimeter()
135: */
136: public float getPixelToMillimeter() {
137: return getPixelUnitToMillimeter();
138:
139: }
140:
141: /**
142: * Returns the medium font size.
143: */
144: public float getMediumFontSize() {
145: return userAgent.getMediumFontSize();
146: }
147:
148: /**
149: * Returns the width of the block which directly contains the
150: * given element.
151: */
152: public float getBlockWidth(Element elt) {
153: // return DesignerServiceHack.getDefault().getBlockWidth(elt);
154: return userAgentInfo.getBlockWidth(document, elt);
155: }
156:
157: /**
158: * Returns the height of the block which directly contains the
159: * given element.
160: */
161: public float getBlockHeight(Element elt) {
162: // return DesignerServiceHack.getDefault().getBlockHeight(elt);
163: return userAgentInfo.getBlockHeight(document, elt);
164: }
165:
166: /**
167: * This method throws a SecurityException if the resource
168: * found at url and referenced from docURL
169: * should not be loaded.
170: *
171: * This is a convenience method to call checkLoadExternalResource
172: * on the ExternalResourceSecurity strategy returned by
173: * getExternalResourceSecurity.
174: *
175: * @param resourceURL url for the script, as defined in
176: * the script's xlink:href attribute. If that
177: * attribute was empty, then this parameter should
178: * be null
179: * @param docURL url for the document into which the
180: * script was found.
181: */
182: public void checkLoadExternalResource(ParsedURL resourceURL,
183: ParsedURL docURL) throws SecurityException {
184: //userAgent.checkLoadExternalResource(resourceURL,
185: // docURL);
186: }
187:
188: /** Mark the document as dynamic - this means event listeners etc.
189: * will be installed.
190: * @see org.apache.batik.css.engine.CSSContext#isDynamic()
191: */
192: public boolean isDynamic() {
193: return true;
194: }
195:
196: public boolean isInteractive() {
197: return true;
198: }
199:
200: }
|