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.css2;
042:
043: import org.netbeans.modules.visualweb.api.designer.cssengine.CssProvider;
044: import org.netbeans.modules.visualweb.api.designer.cssengine.CssValue;
045: import org.openide.ErrorManager;
046: import org.w3c.dom.Element;
047:
048: import org.netbeans.modules.visualweb.api.designer.cssengine.XhtmlCss;
049:
050: /**
051: * Class which represents a type of box - absolutel positioned,
052: * normal/static position, floats, etc.
053: * It's really an enumerated type.
054: * @author Tor Norbye
055: */
056: public class BoxType {
057: public static final BoxType NONE = new BoxType("none"); // NOI18N
058: public static final BoxType STATIC = new BoxType("static"); // NOI18N
059: public static final BoxType ABSOLUTE = new BoxType("absolute"); // NOI18N
060: public static final BoxType FIXED = new BoxType("fixed"); // NOI18N
061: public static final BoxType FLOAT = new BoxType("float"); // NOI18N
062: public static final BoxType RELATIVE = new BoxType("relative"); // NOI18N
063: public static final BoxType LINEBOX = new BoxType("linebox"); // NOI18N
064:
065: // Other types of boxes that are not laid out according to
066: // CSS2 rules like the above, but behave as separate box types
067: // needing treatment.
068: public static final BoxType SPACE = new BoxType("space"); // NOI18N
069: public static final BoxType LINEBREAK = new BoxType("linebreak"); // NOI18N
070: public static final BoxType TEXT = new BoxType("text"); // NOI18N
071: private final String description;
072:
073: /** Use factory method instead */
074: private BoxType(String description) {
075: this .description = description;
076: }
077:
078: /**
079: * Return the box type to use for the given element.
080: * See section CSS2 spec section 9.7.
081: * Should not be called on elements whose "display" CSS property
082: * returns "none".
083: */
084: public static BoxType getBoxType(Element element) {
085: // XXX This hack should not be necessary - check latest Jsf
086: // drop and see if I can remove it now
087: //element = getSourceElement(element);
088: // assert CssLookup.getValue(element, XhtmlCss.DISPLAY_INDEX) != CssValueConstants.NONE_VALUE;
089: if (CssProvider.getValueService().isNoneValue(
090: CssProvider.getEngineService()
091: .getComputedValueForElement(element,
092: XhtmlCss.DISPLAY_INDEX))) {
093: // XXX Why is it illegal?
094: ErrorManager.getDefault().notify(
095: ErrorManager.INFORMATIONAL,
096: new IllegalStateException(
097: "Element has none display style, element="
098: + element));
099: }
100:
101: // Value val = CssLookup.getValue(element, XhtmlCss.FLOAT_INDEX);
102: CssValue cssValue = CssProvider.getEngineService()
103: .getComputedValueForElement(element,
104: XhtmlCss.FLOAT_INDEX);
105:
106: // if (val != CssValueConstants.NONE_VALUE) {
107: if (!CssProvider.getValueService().isNoneValue(cssValue)) {
108: return FLOAT;
109: }
110:
111: // val = CssLookup.getValue(element, XhtmlCss.POSITION_INDEX);
112: cssValue = CssProvider.getEngineService()
113: .getComputedValueForElement(element,
114: XhtmlCss.POSITION_INDEX);
115:
116: // if (val == CssValueConstants.STATIC_VALUE) {
117: if (CssProvider.getValueService().isStaticValue(cssValue)) {
118: return STATIC;
119: // } else if (val == CssValueConstants.ABSOLUTE_VALUE) {
120: } else if (CssProvider.getValueService().isAbsoluteValue(
121: cssValue)) {
122: return ABSOLUTE;
123: // } else if (val == CssValueConstants.RELATIVE_VALUE) {
124: } else if (CssProvider.getValueService().isRelativeValue(
125: cssValue)) {
126: return RELATIVE;
127: // } else if (val == CssValueConstants.FIXED_VALUE) {
128: } else if (CssProvider.getValueService().isFixedValue(cssValue)) {
129: return FIXED;
130: }
131:
132: // TODO Consult "display" and return something appropriate:
133: // inline | block | list-item | run-in | compact | marker |
134: // table | inline-table | table-row-group | table-header-group |
135: // table-footer-group | table-row | table-column-group |
136: // table-column | table-cell | table-caption | none | inherit
137: return STATIC;
138: }
139:
140: /** Does this box type participate in normal flow? */
141: public boolean isNormalFlow() {
142: // XXX "RELATIVE" - are these positioned or not?
143: return (this == STATIC) || (this == RELATIVE)
144: || (this == LINEBOX);
145: }
146:
147: /** Are boxes of this type absolutely positioned? */
148: public boolean isAbsolutelyPositioned() {
149: return (this == ABSOLUTE) || (this == FIXED);
150: }
151:
152: /** Are boxes of this type positioned? Positioned means
153: * a box that allows left, right, top and/or bottom to be set.
154: * In other words, absolute boxes, fixed boxes, and relative
155: * boxes. Note that relative boxes are both positioned, AND
156: * participate in normal flow. */
157: public boolean isPositioned() {
158: // XXX "RELATIVE" - are these positioned or not?
159: return (this == ABSOLUTE) || (this == FIXED)
160: || (this == RELATIVE);
161: }
162:
163: /**
164: * Return whether this box represents a box for formatted inline content,
165: * like text, spaces or linebreaks.
166: */
167: public boolean isInlineTextBox() {
168: return (this == TEXT) || (this == LINEBREAK) || (this == SPACE);
169: }
170:
171: public String getDescription() {
172: return description;
173: }
174:
175: public String toString() {
176: return super .toString() + "[description=" + description + "]"; // NOI18N
177: }
178: }
|