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:
042: package org.netbeans.modules.visualweb.designer.cssengine;
043:
044: import org.netbeans.modules.visualweb.api.designer.cssengine.CssListValue;
045: import org.netbeans.modules.visualweb.api.designer.cssengine.CssValue;
046: import org.netbeans.modules.visualweb.api.designer.cssengine.CssValueService;
047: import org.netbeans.modules.visualweb.api.designer.cssengine.XhtmlCss;
048: import java.awt.Color;
049: import java.awt.Font;
050: import java.awt.GraphicsEnvironment;
051: import java.awt.Toolkit;
052: import java.util.ArrayList;
053: import java.util.HashMap;
054: import java.util.List;
055: import java.util.Map;
056: import org.apache.batik.css.engine.value.ComputedValue;
057: import org.apache.batik.css.engine.value.ListValue;
058: import org.apache.batik.css.engine.value.Value;
059: import org.apache.batik.css.engine.value.ValueConstants;
060: import org.netbeans.modules.visualweb.designer.html.HtmlTag;
061: import org.w3c.dom.Element;
062: import org.w3c.dom.css.CSSPrimitiveValue;
063:
064: /**
065: * Impl of the <code>CssValueService</code>.
066: * XXX Impl code copied from various places before spread in the modules.
067: *
068: * @author Peter Zavadsky
069: */
070: public class CssValueServiceImpl implements CssValueService {
071:
072: private static final String CSS_FONT_FAMILY_NAME_MONOSPACE = "monospace"; // NOI18N
073: private static final String CSS_FONT_FAMILY_NAME_SANS_SERIF = "sans-serif"; // NOI18N
074:
075: private static final String JAVA_FONT_FAMILY_NAME_MONOSPACED = "monospaced"; // NOI18N
076: private static final String JAVA_FONT_FAMILY_NAME_SANS_SERIF = "SansSerif"; // NOI18N
077:
078: private static final Object LOCK_FONT_MAPPING = new Object();
079: private static Map<String, String> fontMapping;
080:
081: // XXX Moving to CssBoxUtilities.
082: // private static FontKey fontSearch = new FontKey(null, 0, 0);
083: // private static Hashtable fontTable = new Hashtable();
084:
085: private static final CssValueService instance = new CssValueServiceImpl();
086:
087: /** Creates a new instance of CssValueServiceImpl */
088: private CssValueServiceImpl() {
089: }
090:
091: public static CssValueService getDefault() {
092: return instance;
093: }
094:
095: private static Value getCssValue(Element e, int property) {
096: CssValueImpl cssValueImpl = CssEngineServiceImpl.get()
097: .getComputedValueImplForElement(e, property);
098: return cssValueImpl == null ? null : cssValueImpl.getValue();
099: }
100:
101: public Color getColorForElement(Element element, int styleIndex) {
102: Value v = getCssValue(element, styleIndex);
103:
104: if (v == CssValueConstants.TRANSPARENT_RGB_VALUE) {
105: return null;
106: }
107: if (v instanceof ComputedValue
108: && ((ComputedValue) v).getComputedValue() == CssValueConstants.TRANSPARENT_RGB_VALUE) {
109: return null;
110: }
111:
112: if (v != null) {
113: //if (v.getCssValueType() == CSSValue.CSS_PRIMITIVE_VALUE) {
114: return convertColor(v /*, 1*/);
115:
116: /*
117: } else {
118: return PaintServer.convertRGBICCColor
119: (e, v.item(0), (ICCColor)v.item(1), 1, ctx);
120: }
121: */
122: }
123:
124: return null;
125: }
126:
127: public boolean isColorTransparentForElement(Element element,
128: int styleIndex) {
129: Value v = getCssValue(element, styleIndex);
130: // XXX #94265 The initial is transparent, but that doesn't work (in browser it doesn't seem to be transparent),
131: // or there might be browser specific stylesheets changing the default behavior.
132: // Hack: For now we ignore when it is initial value.
133: // if (v == CssValueConstants.TRANSPARENT_RGB_VALUE) {
134: // return true;
135: // }
136: if (v instanceof ComputedValue) {
137: return ((ComputedValue) v).getComputedValue() == CssValueConstants.TRANSPARENT_RGB_VALUE;
138: }
139: return false;
140: }
141:
142: private static final String[] CSS_LENGTH_UNITS = { "%", "em", "ex",
143: "px", "cm", "mm", "in", "pt", "pc" }; // NOI18N
144:
145: public String[] getCssLengthUnits() {
146: return CSS_LENGTH_UNITS;
147: }
148:
149: /**
150: * Converts the given Value and opacity to a Color object.
151: * @param c The CSS color to convert.
152: * @param o The opacity value (0 <= o <= 1).
153: */
154: private static Color convertColor(Value c /*, float opacity*/) {
155: int r = resolveColorComponent(c.getRed());
156: int g = resolveColorComponent(c.getGreen());
157: int b = resolveColorComponent(c.getBlue());
158:
159: return new Color(r, g, b, 255 /*Math.round(opacity * 255f)*/);
160: }
161:
162: /**
163: * Returns the value of one color component (0 <= result <= 255).
164: * @param v the value that defines the color component
165: */
166: private static int resolveColorComponent(Value v) {
167: float f;
168:
169: switch (v.getPrimitiveType()) {
170: case CSSPrimitiveValue.CSS_PERCENTAGE:
171: f = v.getFloatValue();
172: f = (f > 100f) ? 100f : ((f < 0f) ? 0f : f);
173:
174: return Math.round((255f * f) / 100f);
175:
176: case CSSPrimitiveValue.CSS_NUMBER:
177: f = v.getFloatValue();
178: f = (f > 255f) ? 255f : ((f < 0f) ? 0f : f);
179:
180: return Math.round(f);
181:
182: default:
183: assert false;
184:
185: return 0;
186: }
187: }
188:
189: public float getFontSizeForElement(Element element, int defaultSize) {
190: Value val = getCssValue(element, XhtmlCss.FONT_SIZE_INDEX);
191:
192: if (val == null) {
193: return defaultSize; // XXX can this happen?
194: }
195:
196: return val.getFloatValue();
197: }
198:
199: public int getFontStyleForElement(Element element, int defaultStyle) {
200: int style = defaultStyle;
201: Value val = getCssValue(element, XhtmlCss.FONT_WEIGHT_INDEX);
202:
203: if (val != null && val.getFloatValue() > 400.0) {
204: style |= Font.BOLD;
205: }
206:
207: val = getCssValue(element, XhtmlCss.FONT_STYLE_INDEX);
208:
209: if ((val == ValueConstants.ITALIC_VALUE)
210: || (val == ValueConstants.OBLIQUE_VALUE)) {
211: // treat oblique as italic -- see for example
212: // http://www.scribeserver.com/medieval/abtfonts.htm
213: // Serif fonts typically have associated italic fonts and
214: // sans-serif fonts typically have associated oblique fonts,
215: // so oblique serves the purpose of an italic font.
216: style |= Font.ITALIC;
217: }
218: return style;
219: }
220:
221: public String[] getFontFamilyNamesForElement(Element element) {
222: // Font family
223: Value val = getCssValue(element, XhtmlCss.FONT_FAMILY_INDEX);
224: // String family = getFontFamily(val);
225: return getFontFamilyNames(val);
226: }
227:
228: // public Font[] getFontsForElement(Element element, int defaultSize, int type) {
229: // float size = getFontSizeForElement(element, defaultSize);
230: //
231: // // Font family
232: // Value val = getCssValue(element, XhtmlCss.FONT_FAMILY_INDEX);
233: //// String family = getFontFamily(val);
234: // String[] fontFamilyNames = getFontFamilyNames(val);
235: //
236: // int style = type;
237: // val = getCssValue(element, XhtmlCss.FONT_WEIGHT_INDEX);
238: //
239: // if (val != null && val.getFloatValue() > 400.0) {
240: // style |= Font.BOLD;
241: // }
242: //
243: // val = getCssValue(element, XhtmlCss.FONT_STYLE_INDEX);
244: //
245: // if ((val == ValueConstants.ITALIC_VALUE) || (val == ValueConstants.OBLIQUE_VALUE)) {
246: // // treat oblique as italic -- see for example
247: // // http://www.scribeserver.com/medieval/abtfonts.htm
248: // // Serif fonts typically have associated italic fonts and
249: // // sans-serif fonts typically have associated oblique fonts,
250: // // so oblique serves the purpose of an italic font.
251: // style |= Font.ITALIC;
252: // }
253: //
254: // List<Font> fonts = new ArrayList<Font>();
255: // for (String fontFamilyName : fontFamilyNames) {
256: //// Font f = getFont(family, style, (int)size);
257: // Font font = getFont(fontFamilyName, style, Math.round(size));
258: // if (fonts.contains(font)) {
259: // continue;
260: // }
261: // fonts.add(font);
262: // }
263: //
264: // return fonts.toArray(new Font[fonts.size()]);
265: // }
266:
267: // public FontMetrics[] getFontsMetricsForElement(Element element) {
268: // //Font font = doc.getFont(styleElement, CssEngineServiceImpl.getUserAgentInfo().getDefaultFontSize());
269: //// Font font = getFont(element, CssEngineServiceImpl.getUserAgentInfo().getDefaultFontSize());
270: // Font[] fonts = getFontsForElement(element, CssEngineServiceImpl.getUserAgentInfo().getDefaultFontSize(), Font.PLAIN);
271: //
272: // List<FontMetrics> fontsMetrics = new ArrayList<FontMetrics>();
273: // for (Font font : fonts) {
274: // FontMetrics fontMetrics = Toolkit.getDefaultToolkit().getFontMetrics(font);
275: // fontsMetrics.add(fontMetrics);
276: // }
277: //
278: // return fontsMetrics.toArray(new FontMetrics[fontsMetrics.size()]);
279: // }
280:
281: private String[] getFontFamilyNames(Value list) {
282: if ((list == null) || (list.getLength() == 0)) {
283: return new String[] { JAVA_FONT_FAMILY_NAME_SANS_SERIF };
284: }
285:
286: List<String> fontFamilyNames = new ArrayList<String>();
287: Map validNames = getValidFontNameMapping();
288: for (int i = 0, len = list.getLength(); i < len; i++) {
289: Value it = list.item(i);
290: String fontName = it.getStringValue();
291: String family = (String) validNames
292: .get(mapCssFontFamilyNameToJavaFontFamilyName(fontName));
293:
294: if (family == null) {
295: family = (String) validNames
296: .get(fontName.toLowerCase());
297: }
298:
299: if (family != null) {
300: // return family;
301: fontFamilyNames.add(family);
302: }
303: }
304:
305: if (fontFamilyNames.isEmpty()) {
306: // Fallback.
307: fontFamilyNames.add(JAVA_FONT_FAMILY_NAME_SANS_SERIF);
308: }
309:
310: return fontFamilyNames.toArray(new String[fontFamilyNames
311: .size()]);
312: }
313:
314: private Map<String, String> getValidFontNameMapping() {
315: // This can get called from multiple threads, lock before setting
316: synchronized (LOCK_FONT_MAPPING) {
317: if (fontMapping == null) {
318: String[] names = null;
319: GraphicsEnvironment ge = GraphicsEnvironment
320: .getLocalGraphicsEnvironment();
321:
322: if (ge != null) {
323: names = ge.getAvailableFontFamilyNames();
324: }
325:
326: if (names == null) {
327: names = Toolkit.getDefaultToolkit().getFontList();
328: }
329:
330: if (names != null) {
331: fontMapping = new HashMap<String, String>(
332: names.length * 2);
333:
334: for (int counter = names.length - 1; counter >= 0; counter--) {
335: // Put both lowercase and case value in table.
336: fontMapping.put(names[counter].toLowerCase(),
337: names[counter]);
338: fontMapping.put(names[counter], names[counter]);
339: }
340: } else {
341: fontMapping = new HashMap<String, String>();
342: }
343: }
344: }
345:
346: return fontMapping;
347: }
348:
349: /** Maps from CSS font family name to Java Font family name. */
350: private static String mapCssFontFamilyNameToJavaFontFamilyName(
351: String cssFontFamilyName) {
352: if (CSS_FONT_FAMILY_NAME_MONOSPACE.equals(cssFontFamilyName)) {
353: return JAVA_FONT_FAMILY_NAME_MONOSPACED;
354: } else if (CSS_FONT_FAMILY_NAME_SANS_SERIF
355: .equals(cssFontFamilyName)) {
356: return JAVA_FONT_FAMILY_NAME_SANS_SERIF;
357: }
358: // XXX TBD Perhaps more is necessaryhere.
359:
360: return cssFontFamilyName;
361: }
362:
363: // /**
364: // * Gets a new font. This returns a Font from a cache
365: // * if a cached font exists. If not, a Font is added to
366: // * the cache. This is basically a low-level cache for
367: // * 1.1 font features.
368: // *
369: // * @param family the font family (such as "Monospaced")
370: // * @param style the style of the font (such as Font.PLAIN)
371: // * @param size the point size >= 1
372: // * @return the new font
373: // */
374: // private static Font getFont(String family, int style, int size) {
375: // fontSearch.setValue(family, style, size);
376: //
377: // Font f = (Font)fontTable.get(fontSearch);
378: //
379: // if (f == null) {
380: // // haven't seen this one yet.
381: // f = new Font(family, style, size);
382: //
383: // FontKey key = new FontKey(family, style, size);
384: // fontTable.put(key, f);
385: // }
386: //
387: // return f;
388: // }
389:
390: public CssListValue getComputedCssListValue(CssValue cssValue) {
391: if (cssValue == null) {
392: return null;
393: }
394:
395: Value value = ((CssValueImpl) cssValue).getValue();
396:
397: if (value instanceof ListValue) {
398: return CssValueFactory
399: .createCssListValue((ListValue) value);
400: } else if (value instanceof ComputedValue) {
401: ComputedValue computedValue = (ComputedValue) value;
402: Value result = computedValue.getComputedValue();
403: if (result instanceof ListValue) {
404: return CssValueFactory
405: .createCssListValue((ListValue) result);
406: }
407: }
408:
409: return null;
410: }
411:
412: public boolean isAbsoluteValue(CssValue cssValue) {
413: return isCssValueConstant(cssValue,
414: CssValueConstants.ABSOLUTE_VALUE);
415: }
416:
417: public boolean isAutoValue(CssValue cssValue) {
418: return isCssValueConstant(cssValue,
419: CssValueConstants.AUTO_VALUE);
420: }
421:
422: public boolean isBaseLineValue(CssValue cssValue) {
423: return isCssValueConstant(cssValue,
424: CssValueConstants.BASELINE_VALUE);
425: }
426:
427: public boolean isBlockValue(CssValue cssValue) {
428: return isCssValueConstant(cssValue,
429: CssValueConstants.BLOCK_VALUE);
430: }
431:
432: public boolean isBothValue(CssValue cssValue) {
433: return isCssValueConstant(cssValue,
434: CssValueConstants.BOTH_VALUE);
435: }
436:
437: public boolean isBottomValue(CssValue cssValue) {
438: return isCssValueConstant(cssValue,
439: CssValueConstants.BOTTOM_VALUE);
440: }
441:
442: public boolean isCapitalizeValue(CssValue cssValue) {
443: return isCssValueConstant(cssValue,
444: CssValueConstants.CAPITALIZE_VALUE);
445: }
446:
447: public boolean isCenterValue(CssValue cssValue) {
448: return isCssValueConstant(cssValue,
449: CssValueConstants.CENTER_VALUE);
450: }
451:
452: public boolean isCircleValue(CssValue cssValue) {
453: return isCssValueConstant(cssValue,
454: CssValueConstants.CIRCLE_VALUE);
455: }
456:
457: public boolean isCollapseValue(CssValue cssValue) {
458: return isCssValueConstant(cssValue,
459: CssValueConstants.COLLAPSE_VALUE);
460: }
461:
462: public boolean isDashedValue(CssValue cssValue) {
463: return isCssValueConstant(cssValue,
464: CssValueConstants.DASHED_VALUE);
465: }
466:
467: public boolean isDecimalValue(CssValue cssValue) {
468: return isCssValueConstant(cssValue,
469: CssValueConstants.DECIMAL_VALUE);
470: }
471:
472: public boolean isDiscValue(CssValue cssValue) {
473: return isCssValueConstant(cssValue,
474: CssValueConstants.DISC_VALUE);
475: }
476:
477: public boolean isDottedValue(CssValue cssValue) {
478: return isCssValueConstant(cssValue,
479: CssValueConstants.DOTTED_VALUE);
480: }
481:
482: public boolean isDoubleValue(CssValue cssValue) {
483: return isCssValueConstant(cssValue,
484: CssValueConstants.DOUBLE_VALUE);
485: }
486:
487: public boolean isFixedValue(CssValue cssValue) {
488: return isCssValueConstant(cssValue,
489: CssValueConstants.FIXED_VALUE);
490: }
491:
492: public boolean isGridValue(CssValue cssValue) {
493: return isCssValueConstant(cssValue,
494: CssValueConstants.GRID_VALUE);
495: }
496:
497: public boolean isGrooveValue(CssValue cssValue) {
498: return isCssValueConstant(cssValue,
499: CssValueConstants.GROOVE_VALUE);
500: }
501:
502: public boolean isHiddenValue(CssValue cssValue) {
503: return isCssValueConstant(cssValue,
504: CssValueConstants.HIDDEN_VALUE);
505: }
506:
507: public boolean isLeftValue(CssValue cssValue) {
508: return isCssValueConstant(cssValue,
509: CssValueConstants.LEFT_VALUE);
510: }
511:
512: public boolean isListItemValue(CssValue cssValue) {
513: return isCssValueConstant(cssValue,
514: CssValueConstants.LIST_ITEM_VALUE);
515: }
516:
517: public boolean isLowerAlphaValue(CssValue cssValue) {
518: return isCssValueConstant(cssValue,
519: CssValueConstants.LOWER_ALPHA_VALUE);
520: }
521:
522: public boolean isLowerCaseValue(CssValue cssValue) {
523: return isCssValueConstant(cssValue,
524: CssValueConstants.LOWERCASE_VALUE);
525: }
526:
527: public boolean isLowerLatinValue(CssValue cssValue) {
528: return isCssValueConstant(cssValue,
529: CssValueConstants.LOWER_LATIN_VALUE);
530: }
531:
532: public boolean isLowerRomanValue(CssValue cssValue) {
533: return isCssValueConstant(cssValue,
534: CssValueConstants.LOWER_ROMAN_VALUE);
535: }
536:
537: public boolean isInlineValue(CssValue cssValue) {
538: return isCssValueConstant(cssValue,
539: CssValueConstants.INLINE_VALUE);
540: }
541:
542: public boolean isInlineBlockValue(CssValue cssValue) {
543: return isCssValueConstant(cssValue,
544: CssValueConstants.INLINE_BLOCK_VALUE);
545: }
546:
547: public boolean isInsetValue(CssValue cssValue) {
548: return isCssValueConstant(cssValue,
549: CssValueConstants.INSET_VALUE);
550: }
551:
552: public boolean isJustifyValue(CssValue cssValue) {
553: return isCssValueConstant(cssValue,
554: CssValueConstants.JUSTIFY_VALUE);
555: }
556:
557: public boolean isMiddleValue(CssValue cssValue) {
558: return isCssValueConstant(cssValue,
559: CssValueConstants.MIDDLE_VALUE);
560: }
561:
562: public boolean isNoneValue(CssValue cssValue) {
563: return isCssValueConstant(cssValue,
564: CssValueConstants.NONE_VALUE);
565: }
566:
567: public boolean isNormalValue(CssValue cssValue) {
568: return isCssValueConstant(cssValue,
569: CssValueConstants.NORMAL_VALUE);
570: }
571:
572: public boolean isNoRepeatValue(CssValue cssValue) {
573: return isCssValueConstant(cssValue,
574: CssValueConstants.NO_REPEAT_VALUE);
575: }
576:
577: public boolean isNoWrapValue(CssValue cssValue) {
578: return isCssValueConstant(cssValue,
579: CssValueConstants.NOWRAP_VALUE);
580: }
581:
582: public boolean isOutsetValue(CssValue cssValue) {
583: return isCssValueConstant(cssValue,
584: CssValueConstants.OUTSET_VALUE);
585: }
586:
587: public boolean isPreValue(CssValue cssValue) {
588: return isCssValueConstant(cssValue, CssValueConstants.PRE_VALUE);
589: }
590:
591: public boolean isPreWrapValue(CssValue cssValue) {
592: return isCssValueConstant(cssValue,
593: CssValueConstants.PRE_WRAP_VALUE);
594: }
595:
596: public boolean isRaveCenterValue(CssValue cssValue) {
597: return isCssValueConstant(cssValue,
598: CssValueConstants.RAVECENTER_VALUE);
599: }
600:
601: public boolean isRelativeValue(CssValue cssValue) {
602: return isCssValueConstant(cssValue,
603: CssValueConstants.RELATIVE_VALUE);
604: }
605:
606: public boolean isRepeatValue(CssValue cssValue) {
607: return isCssValueConstant(cssValue,
608: CssValueConstants.REPEAT_VALUE);
609: }
610:
611: public boolean isRepeatXValue(CssValue cssValue) {
612: return isCssValueConstant(cssValue,
613: CssValueConstants.REPEAT_X_VALUE);
614: }
615:
616: public boolean isRepeatYValue(CssValue cssValue) {
617: return isCssValueConstant(cssValue,
618: CssValueConstants.REPEAT_Y_VALUE);
619: }
620:
621: public boolean isRidgeValue(CssValue cssValue) {
622: return isCssValueConstant(cssValue,
623: CssValueConstants.RIDGE_VALUE);
624: }
625:
626: public boolean isRightValue(CssValue cssValue) {
627: return isCssValueConstant(cssValue,
628: CssValueConstants.RIGHT_VALUE);
629: }
630:
631: public boolean isSmallCapsValue(CssValue cssValue) {
632: return isCssValueConstant(cssValue,
633: CssValueConstants.SMALL_CAPS_VALUE);
634: }
635:
636: public boolean isSolidValue(CssValue cssValue) {
637: return isCssValueConstant(cssValue,
638: CssValueConstants.SOLID_VALUE);
639: }
640:
641: public boolean isSquareValue(CssValue cssValue) {
642: return isCssValueConstant(cssValue,
643: CssValueConstants.SQUARE_VALUE);
644: }
645:
646: public boolean isStaticValue(CssValue cssValue) {
647: return isCssValueConstant(cssValue,
648: CssValueConstants.STATIC_VALUE);
649: }
650:
651: public boolean isSubValue(CssValue cssValue) {
652: return isCssValueConstant(cssValue, CssValueConstants.SUB_VALUE);
653: }
654:
655: public boolean isSuperValue(CssValue cssValue) {
656: return isCssValueConstant(cssValue,
657: CssValueConstants.SUPER_VALUE);
658: }
659:
660: public boolean isTableValue(CssValue cssValue) {
661: return isCssValueConstant(cssValue,
662: CssValueConstants.TABLE_VALUE);
663: }
664:
665: public boolean isTableCaptionValue(CssValue cssValue) {
666: return isCssValueConstant(cssValue,
667: CssValueConstants.TABLE_CAPTION_VALUE);
668: }
669:
670: public boolean isTableCellValue(CssValue cssValue) {
671: return isCssValueConstant(cssValue,
672: CssValueConstants.TABLE_CELL_VALUE);
673: }
674:
675: public boolean isTableColumnValue(CssValue cssValue) {
676: return isCssValueConstant(cssValue,
677: CssValueConstants.TABLE_COLUMN_VALUE);
678: }
679:
680: public boolean isTableColumnGroupValue(CssValue cssValue) {
681: return isCssValueConstant(cssValue,
682: CssValueConstants.TABLE_COLUMN_GROUP_VALUE);
683: }
684:
685: public boolean isTableFooterGroupValue(CssValue cssValue) {
686: return isCssValueConstant(cssValue,
687: CssValueConstants.TABLE_FOOTER_GROUP_VALUE);
688: }
689:
690: public boolean isTableHeaderGroupValue(CssValue cssValue) {
691: return isCssValueConstant(cssValue,
692: CssValueConstants.TABLE_HEADER_GROUP_VALUE);
693: }
694:
695: public boolean isTableRowGroupValue(CssValue cssValue) {
696: return isCssValueConstant(cssValue,
697: CssValueConstants.TABLE_ROW_GROUP_VALUE);
698: }
699:
700: public boolean isTableRowValue(CssValue cssValue) {
701: return isCssValueConstant(cssValue,
702: CssValueConstants.TABLE_ROW_VALUE);
703: }
704:
705: public boolean isTextBottomValue(CssValue cssValue) {
706: return isCssValueConstant(cssValue,
707: CssValueConstants.TEXT_BOTTOM_VALUE);
708: }
709:
710: public boolean isTextTopValue(CssValue cssValue) {
711: return isCssValueConstant(cssValue,
712: CssValueConstants.TEXT_TOP_VALUE);
713: }
714:
715: public boolean isTopValue(CssValue cssValue) {
716: return isCssValueConstant(cssValue, CssValueConstants.TOP_VALUE);
717: }
718:
719: public boolean isUpperAlphaValue(CssValue cssValue) {
720: return isCssValueConstant(cssValue,
721: CssValueConstants.UPPER_ALPHA_VALUE);
722: }
723:
724: public boolean isUpperCaseValue(CssValue cssValue) {
725: return isCssValueConstant(cssValue,
726: CssValueConstants.UPPERCASE_VALUE);
727: }
728:
729: public boolean isUpperLatinValue(CssValue cssValue) {
730: return isCssValueConstant(cssValue,
731: CssValueConstants.UPPER_LATIN_VALUE);
732: }
733:
734: public boolean isUpperRomanValue(CssValue cssValue) {
735: return isCssValueConstant(cssValue,
736: CssValueConstants.UPPER_ROMAN_VALUE);
737: }
738:
739: public boolean isVisibleValue(CssValue cssValue) {
740: return isCssValueConstant(cssValue,
741: CssValueConstants.VISIBLE_VALUE);
742: }
743:
744: private static boolean isCssValueConstant(CssValue cssValue,
745: Value constant) {
746: if (cssValue == null) {
747: return false;
748: }
749:
750: return ((CssValueImpl) cssValue).getValue() == constant;
751: }
752:
753: public boolean isOfPrimitivePercentageType(CssValue cssValue) {
754: if (!(cssValue instanceof CssValueImpl)) {
755: return false;
756: }
757: return ((CssValueImpl) cssValue).getValue().getPrimitiveType() == CSSPrimitiveValue.CSS_PERCENTAGE;
758: }
759:
760: public boolean isOfPrimitiveEmsType(CssValue cssValue) {
761: if (!(cssValue instanceof CssValueImpl)) {
762: return false;
763: }
764: return ((CssValueImpl) cssValue).getValue().getPrimitiveType() == CSSPrimitiveValue.CSS_EMS;
765: }
766:
767: public CssValue getBothCssValueConstant() {
768: return CssValueFactory
769: .createCssValue(CssValueConstants.BOTH_VALUE);
770: }
771:
772: public CssValue getCollapseCssValueConstant() {
773: return CssValueFactory
774: .createCssValue(CssValueConstants.COLLAPSE_VALUE);
775: }
776:
777: public CssValue getDecimalCssValueConstant() {
778: return CssValueFactory
779: .createCssValue(CssValueConstants.DECIMAL_VALUE);
780: }
781:
782: public CssValue getDiscCssValueConstant() {
783: return CssValueFactory
784: .createCssValue(CssValueConstants.DISC_VALUE);
785: }
786:
787: public CssValue getTableFooterGroupValueConstant() {
788: return CssValueFactory
789: .createCssValue(CssValueConstants.TABLE_FOOTER_GROUP_VALUE);
790: }
791:
792: public CssValue getTableHeaderGroupValueConstant() {
793: return CssValueFactory
794: .createCssValue(CssValueConstants.TABLE_HEADER_GROUP_VALUE);
795: }
796:
797: public CssValue getTableRowGroupValueConstant() {
798: return CssValueFactory
799: .createCssValue(CssValueConstants.TABLE_ROW_GROUP_VALUE);
800: }
801:
802: public CssValue getTableRowValueConstant() {
803: return CssValueFactory
804: .createCssValue(CssValueConstants.TABLE_ROW_VALUE);
805: }
806:
807: public boolean isPositionProperty(String property) {
808: if (property == null) {
809: return false;
810: }
811: return property.equals(CssConstants.CSS_CLEAR_PROPERTY)
812: || property.equals(CssConstants.CSS_FLOAT_PROPERTY)
813: || property.equals(CssConstants.CSS_HEIGHT_PROPERTY)
814: || property.equals(CssConstants.CSS_LEFT_PROPERTY)
815: || property.equals(CssConstants.CSS_RIGHT_PROPERTY)
816: || property.equals(CssConstants.CSS_POSITION_PROPERTY)
817: || property.equals(CssConstants.CSS_TOP_PROPERTY)
818: || property.equals(CssConstants.CSS_Z_INDEX_PROPERTY)
819: || property.equals(CssConstants.CSS_WIDTH_PROPERTY);
820: }
821:
822: public boolean isTextProperty(String property) {
823: return property.startsWith("font-") // NOI18N
824: || property.startsWith("text-") // NOI18N
825: || property
826: .equals(CssConstants.CSS_VERTICAL_ALIGN_PROPERTY)
827: || property
828: .equals(CssConstants.CSS_WHITE_SPACE_PROPERTY)
829: || property.equals(CssConstants.CSS_COLOR_PROPERTY)
830: || property
831: .equals(CssConstants.CSS_LETTER_SPACING_PROPERTY)
832: || property
833: .equals(CssConstants.CSS_WORD_SPACING_PROPERTY);
834: }
835:
836: public boolean isAutoValue(String value) {
837: if (value == null) {
838: return false;
839: }
840:
841: return value.equals(CssConstants.CSS_AUTO_VALUE);
842: }
843:
844: public String getAbsoluteValue() {
845: return CssConstants.CSS_ABSOLUTE_VALUE;
846: }
847:
848: public String getGridValue() {
849: return CssConstants.CSS_GRID_VALUE;
850: }
851:
852: public String getHeightProperty() {
853: return CssConstants.CSS_HEIGHT_PROPERTY;
854: }
855:
856: public String getWidthProperty() {
857: return CssConstants.CSS_WIDTH_PROPERTY;
858: }
859:
860: public boolean hasNoUnits(String value) {
861: return XhtmlCssEngine.hasNoUnits(value);
862: }
863:
864: // /**
865: // * key for a font table
866: // */
867: // private static class FontKey {
868: // private String family;
869: // private int style;
870: // private int size;
871: //
872: // /**
873: // * Constructs a font key.
874: // */
875: // public FontKey(String family, int style, int size) {
876: // setValue(family, style, size);
877: // }
878: //
879: // public void setValue(String family, int style, int size) {
880: // this.family = (family != null) ? family.intern() : null;
881: // this.style = style;
882: // this.size = size;
883: // }
884: //
885: // /**
886: // * Returns a hashcode for this font.
887: // * @return a hashcode value for this font.
888: // */
889: // public int hashCode() {
890: // int fhash = (family != null) ? family.hashCode() : 0;
891: //
892: // return fhash ^ style ^ size;
893: // }
894: //
895: // /**
896: // * Compares this object to the specifed object.
897: // * The result is <code>true</code> if and only if the argument is not
898: // * <code>null</code> and is a <code>Font</code> object with the same
899: // * name, style, and point size as this font.
900: // * @param obj the object to compare this font with.
901: // * @return <code>true</code> if the objects are equal;
902: // * <code>false</code> otherwise.
903: // */
904: // public boolean equals(Object obj) {
905: // if (obj instanceof FontKey) {
906: // FontKey font = (FontKey)obj;
907: //
908: // return (size == font.size) && (style == font.style) && (family == font.family);
909: // }
910: //
911: // return false;
912: // }
913: // } // End of FontKey
914:
915: // XXX Moved from designer/../ContainerBox.
916: public boolean isInlineTag(CssValue cssDisplay, Element element,
917: HtmlTag tag) {
918: // if (display == CssValueConstants.NONE_VALUE) {
919: if (isNoneValue(cssDisplay)) {
920: return false;
921: }
922:
923: // if ((display == CssValueConstants.BLOCK_VALUE) ||
924: // (display == CssValueConstants.LIST_ITEM_VALUE) ||
925: // (display == CssValueConstants.TABLE_VALUE) ||
926: // (
927: // /* These are not always block
928: // display == CssValueConstants.COMPACT_VALUE ||
929: // display == CssValueConstants.RUN_IN_VALUE ||
930: // */
931: // display == CssValueConstants.INLINE_BLOCK_VALUE)) {
932: if (isBlockValue(cssDisplay) || isListItemValue(cssDisplay)
933: || isTableValue(cssDisplay)
934: || isInlineBlockValue(cssDisplay)) {
935: return false;
936: // } else if (display == CssValueConstants.INLINE_VALUE) {
937: } else if (isInlineValue(cssDisplay)) {
938: return true;
939:
940: // TODO: Handle rest of constants appropriately.
941: // The "inline" boolean flag is too simplistic; we should
942: // store the formatting type here and do the right type
943: // of layout
944:
945: /*
946: CssValueConstants.COMPACT_VALUE,
947: CssValueConstants.INLINE_TABLE_VALUE,
948: CssValueConstants.MARKER_VALUE,
949: CssValueConstants.RUN_IN_VALUE,
950: CssValueConstants.TABLE_VALUE,
951: CssValueConstants.TABLE_CAPTION_VALUE,
952: CssValueConstants.TABLE_CELL_VALUE,
953: CssValueConstants.TABLE_COLUMN_VALUE,
954: CssValueConstants.TABLE_COLUMN_GROUP_VALUE,
955: CssValueConstants.TABLE_FOOTER_GROUP_VALUE,
956: CssValueConstants.TABLE_HEADER_GROUP_VALUE,
957: CssValueConstants.TABLE_ROW_VALUE,
958: CssValueConstants.TABLE_ROW_GROUP_VALUE,
959: */
960: } else {
961: // Else - use tag default
962: if (tag == null) {
963: tag = HtmlTag.getTag(element.getTagName());
964: }
965:
966: if (tag != null) {
967: return tag.isInlineTag();
968: }
969: }
970:
971: return true;
972: }
973:
974: /** XXX */
975: public int getCssLength(Element element, int property) {
976: // Value val = getValue(element, property);
977: CssValue cssValue = CssEngineServiceImpl.get()
978: .getComputedValueForElement(element, property);
979:
980: // XXX #6460007 Possible NPE.
981: if (cssValue == null) {
982: // XXX What value to return?
983: return 0;
984: }
985:
986: // if (val == CssValueConstants.AUTO_VALUE) {
987: if (isAutoValue(cssValue)) {
988: return CssValue.AUTO;
989: }
990:
991: // return (int)val.getFloatValue();
992: return (int) cssValue.getFloatValue();
993: }
994:
995: }
|