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 java.awt.FontMetrics;
044: import org.netbeans.modules.visualweb.designer.CssUtilities;
045: import org.w3c.dom.Element;
046:
047: import org.netbeans.modules.visualweb.designer.WebForm;
048:
049: /**
050: * StringBox represents a box of text whose text is specified by
051: * an attribute rather than the text-node children of the box' element
052: * (which is the default).
053:
054: * @author Tor Norbye
055: */
056: public class StringBox extends ContainerBox {
057: private String string;
058:
059: /**
060: * Create a StringBox for the given string
061: *
062: * @param webform The <code>WebForm</code>
063: * @param element The element this string box is associated with
064: * @param boxType Type of box.
065: * @param string The string to manage
066: * @param width The width to use for the box
067: * @param height The height to use for the box
068: */
069: public StringBox(WebForm webform, Element element, BoxType boxType,
070: String string, CssBorder border, int width, int height) {
071: super (webform, element, boxType, true, true);
072: this .string = string;
073: this .width = width;
074: this .height = height;
075: this .border = border;
076: }
077:
078: protected void initializeBorder() {
079: if (border == null) {
080: super .initializeBorder();
081: }
082:
083: // if (border == null) {
084: // XXX #115938 Don't show design border when there is zero width for the component.
085: if (border == null && (string != null && string.length() > 0)) {
086: border = CssBorder.getDesignerBorder();
087: }
088:
089: if (border == null) {
090: leftBorderWidth = 0;
091: topBorderWidth = 0;
092: bottomBorderWidth = 0;
093: rightBorderWidth = 0;
094: } else {
095: leftBorderWidth = border.getLeftBorderWidth();
096: topBorderWidth = border.getTopBorderWidth();
097: bottomBorderWidth = border.getBottomBorderWidth();
098: rightBorderWidth = border.getRightBorderWidth();
099: }
100: }
101:
102: public int getIntrinsicWidth() {
103: if (width == AUTO) {
104: if (getBoxCount() > 0) {
105: assert getBox(0) instanceof LineBoxGroup;
106:
107: return getBox(0).getPrefWidth();
108: } else {
109: // width = 30;
110: // XXX #115938 No text (i.e. no child boxes), then no width.
111: width = 0;
112: }
113: }
114:
115: return width;
116: }
117:
118: public int getIntrinsicHeight() {
119: if (height == AUTO) {
120: if (getBoxCount() > 0) {
121: assert getBox(0) instanceof LineBoxGroup;
122:
123: return ((LineBoxGroup) getBox(0)).getMetrics()
124: .getHeight();
125: } else {
126: // height = 14;
127: // XXX #115938 Height should be based on the font.
128: FontMetrics fontMetrics = CssUtilities
129: .getDesignerFontMetricsForElement(getElement(),
130: string, webform.getDefaultFontSize());
131: height = fontMetrics.getHeight();
132: }
133: }
134:
135: return height;
136: }
137:
138: // public String toString() {
139: // return "StringBox[" + paramString() + "]";
140: // }
141:
142: protected String paramString() {
143: return super .paramString() + ", " + string;
144: }
145:
146: /** When building the box hierarchy, instead of adding content
147: * for children, add the string attribute content
148: */
149: protected void createChildren(CreateContext context) {
150: LineBoxGroup old = context.lineBox;
151: context.lineBox = null;
152: addText(context, null, getElement(), string);
153: finishLineBox(context);
154: context.lineBox = old;
155: }
156:
157: public int getPrefWidth() {
158: return getIntrinsicWidth();
159: }
160:
161: public int getPrefMinWidth() {
162: return getIntrinsicWidth();
163: }
164: }
|