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.w3c.dom.Element;
044:
045: import org.netbeans.modules.visualweb.designer.WebForm;
046: import org.netbeans.modules.visualweb.designer.html.HtmlTag;
047:
048: /**
049: * Creates a box for a given element.
050: *
051: * @author Tor Norbye
052: */
053: public class BoxFactory {
054: private BoxFactory() {
055: }
056:
057: /**
058: * Return a box representing the element, or null if no box should
059: * be displayed (as in the case of an input hidden for example)
060: * @throws NullPointerException when tag is <code>null</code>
061: * @throws IllegalArgumentException when tag.isReplaceTag doesn't match replace parameter
062: */
063: public static CssBox create(CreateContext context, HtmlTag tag,
064: WebForm webform, Element element, BoxType boxType,
065: boolean inline, boolean replaced) {
066: // assert tag != null;
067: // assert tag.isReplacedTag() == replaced;
068: // XXX Later there should be throwin exceptions, for now just following the pattern.
069: if (tag == null) {
070: throw new NullPointerException(
071: "Parameter tag may not be null!"); // NOI18N
072: }
073: if (tag.isReplacedTag() != replaced) {
074: throw new IllegalArgumentException(
075: "Parameters are incorrect, tag.isReplaceTag="
076: + tag.isReplacedTag() // NOI18N
077: + ", and replaced=" + replaced
078: + ", has to match!"); // NOI18N
079: }
080:
081: // This is no longer true: user can override via CSS "display" property:
082: //assert tag.isInlineTag() == inline;
083: if (tag.isHiddenTag()) {
084: // That was easy! Do nothing - don't even process its children
085: return null;
086: }
087:
088: CssBox box = createBox(context, tag, webform, element, boxType,
089: inline, replaced);
090:
091: if (box != null) {
092: box.tag = tag;
093: }
094:
095: return box;
096: }
097:
098: private static CssBox createBox(CreateContext context, HtmlTag tag,
099: WebForm webform, Element element, BoxType boxType,
100: boolean inline, boolean replaced) {
101: char c = tag.getTagName().charAt(0);
102:
103: switch (c) {
104: case 't':
105:
106: if (tag == HtmlTag.TABLE) {
107: CssBox box = TableBox.getTableBox(webform, element,
108: boxType, inline, replaced);
109:
110: return box;
111: } else if (tag == HtmlTag.TEXTAREA) { // XXX is this a blocktag?
112:
113: CssBox box = FormComponentBox.getBox(webform, element,
114: tag, boxType, inline, replaced);
115:
116: return box;
117: }
118:
119: break;
120:
121: case 'i':
122:
123: if (tag == HtmlTag.INPUT) {
124: CssBox box = FormComponentBox.getBox(webform, element,
125: tag, boxType, inline, replaced);
126:
127: return box;
128: } else if (tag == HtmlTag.IMG) {
129: CssBox box = ImageBox.getImageBox(webform, element,
130: webform.getPane(), boxType, inline);
131:
132: return box;
133: } else if (tag == HtmlTag.IFRAME) {
134: return FrameBox.getFrameBox(context, webform, element,
135: boxType, tag, inline);
136: }
137:
138: break;
139:
140: case 'b':
141:
142: if (tag == HtmlTag.BR) {
143: return new LineBreakBox(webform, element, tag);
144: } else if (tag == HtmlTag.BUTTON) {
145: CssBox box = FormComponentBox.getBox(webform, element,
146: tag, boxType, inline, replaced);
147:
148: return box;
149: }
150:
151: break;
152:
153: case 'j':
154:
155: if (tag == HtmlTag.JSPINCLUDE || tag == HtmlTag.JSPINCLUDEX) {
156: return JspIncludeBox.getJspIncludeBox(context, webform,
157: element, boxType, tag, inline);
158: }
159:
160: break;
161:
162: case 'f':
163:
164: if (tag == HtmlTag.FIELDSET) {
165: return FieldSetBox.getFieldSetBox(webform, element,
166: boxType, tag, inline);
167:
168: /* <frame> is only allowed as a child of a <frameset> so construction
169: of frames are moved there; any attempts to create frames here should
170: be suppressed because FRAME is recorded as a hidden tag
171: } else if (tag == HtmlTag.FRAME) {
172: // XXX Need separate class from iframe?
173: return FrameBox.getFrameBox(context, webform, element, boxType, tag, inline);
174: */
175: /*
176: <frameset> is only allowed as a body tag, or as a child
177: of the <frameset> tag
178: } else if (tag == HtmlTag.FRAMESET) {
179: return FrameSetBox.getFrameSetBox(webform.getPane(),
180: webform, element, boxType, tag, inline);
181: */
182: }
183:
184: break;
185:
186: case 'a':
187:
188: if (tag == HtmlTag.APPLET) {
189: return ObjectBox.getObjectBox(webform, element,
190: boxType, tag, inline);
191: }
192:
193: break;
194:
195: case 'o':
196:
197: if (tag == HtmlTag.OL) {
198: ListBox box = new ListBox(webform, element, boxType,
199: inline, replaced);
200:
201: return box;
202: } else if (tag == HtmlTag.OBJECT) {
203: return ObjectBox.getObjectBox(webform, element,
204: boxType, tag, inline);
205: }
206:
207: break;
208:
209: case 's':
210:
211: if (tag == HtmlTag.SELECT) {
212: CssBox box = FormComponentBox.getBox(webform, element,
213: tag, boxType, inline, replaced);
214:
215: return box;
216: }
217:
218: break;
219:
220: case 'u':
221:
222: if (tag == HtmlTag.UL) {
223: ListBox box = new ListBox(webform, element, boxType,
224: inline, replaced);
225:
226: return box;
227: }
228:
229: break;
230:
231: case 'm':
232:
233: if (tag == HtmlTag.MENU) {
234: ListBox box = new ListBox(webform, element, boxType,
235: inline, replaced);
236:
237: return box;
238: }
239:
240: break;
241:
242: case 'd':
243:
244: if (tag == HtmlTag.DIR) {
245: ListBox box = new ListBox(webform, element, boxType,
246: inline, replaced);
247:
248: return box;
249: }
250:
251: break;
252: }
253:
254: // Some tag which should just take on general box formatting
255: CssBox box = new ContainerBox(webform, element, boxType,
256: inline, replaced);
257:
258: return box;
259: }
260: }
|