KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > intro > impl > html > FormattedHTMLElement


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.intro.impl.html;
12 import java.util.Iterator JavaDoc;
13 import java.util.Map JavaDoc;
14 import java.util.Vector JavaDoc;
15 /**
16  * A FormattedHTMLElement is an HTMLElement that is indented and can have its
17  * content on a separate line from its start and end tags
18  */

19 public class FormattedHTMLElement extends HTMLElement {
20     // an integer representing how many tabs to insert when printing out this
21
// element and its content
22
private int indentLevel;
23     // indicates whether to print this element on a single line or on multiple
24
// lines
25
private boolean spanMultipleLines;
26     // indicates whether an end tag is required for this element. default is
27
// true, since most elements require an end tag
28
private boolean endTagRequired = true;
29     public FormattedHTMLElement(String JavaDoc name, int indentLevel,
30             boolean spanMultipleLines) {
31         super(name);
32         this.indentLevel = indentLevel;
33         this.spanMultipleLines = spanMultipleLines;
34         // default
35
endTagRequired = true;
36     }
37     public FormattedHTMLElement(String JavaDoc name, int indentLevel,
38             boolean spanMultipleLines, boolean endTagRequired) {
39         super(name);
40         this.indentLevel = indentLevel;
41         this.spanMultipleLines = spanMultipleLines;
42         this.endTagRequired = endTagRequired;
43     }
44     public FormattedHTMLElement(String JavaDoc name, Map JavaDoc attributes, Vector JavaDoc content,
45             int indentLevel, boolean spanMultipleLines) {
46         super(name, attributes, content);
47         this.indentLevel = indentLevel;
48         this.spanMultipleLines = spanMultipleLines;
49         endTagRequired = true;
50     }
51     /**
52      * Set whether the end tag is required for this element
53      *
54      * @param required
55      * true if end tag required, false otherwise
56      */

57     public void setEndTagRequired(boolean required) {
58         this.endTagRequired = required;
59     }
60     /**
61      * Set the indent level that should be applied to this element when printed
62      *
63      * @param indentLevel
64      * The indentLevel to set.
65      */

66     public void setIndentLevel(int indentLevel) {
67         this.indentLevel = indentLevel;
68     }
69     /**
70      * Set whether or not this element should be printed over multiple lines,
71      * or on a single line
72      *
73      * @param spanMultipleLines
74      * true if the element should be printed over multiple lines,
75      * false if it should be printed on a single line
76      */

77     public void setSpanMultipleLines(boolean spanMultipleLines) {
78         this.spanMultipleLines = spanMultipleLines;
79     }
80     /**
81      * Create a string of tabs to insert before the element is printed
82      *
83      * @param indentLevel
84      * the number of tabs to insert
85      * @return
86      */

87     private StringBuffer JavaDoc getIndent(int indentLevel) {
88         // figure out the tab width
89
StringBuffer JavaDoc indent = new StringBuffer JavaDoc();
90         for (int i = 0; i < indentLevel; i++) {
91             indent.append(IIntroHTMLConstants.SMALL_TAB);
92         }
93         return indent;
94     }
95     /*
96      * (non-Javadoc)
97      *
98      * @see java.lang.Object#toString()
99      */

100     public String JavaDoc toString() {
101         StringBuffer JavaDoc element = new StringBuffer JavaDoc();
102         // insert the indent
103
element.append(getIndent(indentLevel));
104         // add the start tag and attributes
105
element.append(HTMLUtil.createHTMLStartTag(getElementName(),
106                 getElementAttributes(), spanMultipleLines));
107         // if there is no content and an end tag is not required just
108
// return the element as is
109
if (getElementContent().isEmpty() && !endTagRequired) {
110             return element.toString();
111         }
112         // include the element's content, if there is any
113
for (Iterator JavaDoc it = getElementContent().iterator(); it.hasNext();) {
114             Object JavaDoc content = it.next();
115             element.append(content);
116         }
117         // indent the end tag if we're on a new line
118
if (indentLevel > 0 && spanMultipleLines)
119             element.append(getIndent(indentLevel));
120         // include an end tag
121
element.append(HTMLUtil.createHTMLEndTag(getElementName(), true));
122         return element.toString();
123     }
124 }
125
Popular Tags