Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3d0461b6585b83801573237ff9bfbcb9f221e6b6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*******************************************************************************
 * Copyright (c) 2005 Oracle Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Ian Trimble - initial API and implementation
 *******************************************************************************/ 
package org.eclipse.jst.pagedesigner.jsf.ui.converter.operations.jsf;

import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

import org.eclipse.jst.pagedesigner.converter.ConvertPosition;
import org.eclipse.jst.pagedesigner.dtmanager.converter.ITransformOperation;
import org.eclipse.jst.pagedesigner.dtmanager.converter.operations.AbstractTransformOperation;
import org.eclipse.jst.pagedesigner.dtmanager.converter.operations.TransformOperationFactory;
import org.w3c.dom.Element;

/**
 * ITransformOperation implementation specifically for the "dataTable" JSF
 * (HTML) Element. 
 * 
 * <br><b>Note:</b> requires ITransformOperation.setTagConverterContext(...) to
 * have been called to provide a valid ITagConverterContext instance prior to
 * a call to the transform(...) method.
 * 
 * @author Ian Trimble - Oracle
 */
public class DataTableOperation extends AbstractTransformOperation {

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.jst.pagedesigner.dtmanager.converter.operations.internal.provisional.AbstractTransformOperation#transform(org.w3c.dom.Element, org.w3c.dom.Element)
	 */
	public Element transform(Element srcElement, Element curElement) {
		Element tableElement = null;
		//create table element, copy all attributes, rename "styleClass" attribute to "class"
		tableElement = createElement("table");
		ITransformOperation operation =
			TransformOperationFactory.getInstance().getTransformOperation(
					TransformOperationFactory.OP_CopyAllAttributesOperation,
					new String[]{});
		operation.transform(srcElement, tableElement);
		operation =
			TransformOperationFactory.getInstance().getTransformOperation(
					TransformOperationFactory.OP_RenameAttributeOperation,
					new String[]{"styleClass", "class"});
		operation.transform(srcElement, tableElement);
		//build thead
		buildHeaderOrFooter(srcElement, tableElement, true);
		//build tbody
		buildBody(srcElement, tableElement);
		//build tfoot
		buildHeaderOrFooter(srcElement, tableElement, false);
		return tableElement;
	}

	private void buildHeaderOrFooter(Element srcElement, Element tableElement, boolean isHeader) {
		//setup vars depending on whether we are building thead or tfoot
		String facetName = isHeader ? "header" : "footer";
		String headerOrFooterTagName = isHeader ? "thead" : "tfoot";
		String thOrTdTagName = isHeader ? "th" : "td";
		String headerClassOrFooterClassAttrName = isHeader ? "headerClass" : "footerClass";

		//look for facet ("header" or "footer")
		Element facetElement = getChildFacetByName(srcElement, facetName);

		//get list of child "column" elements
		List columnElementList = getChildElements(srcElement, "column");

		//look for facet ("header" or "footer") on child "column" elements
		boolean hasColumnFacet = false;
		Iterator itColumnElementList = columnElementList.iterator();
		while (itColumnElementList.hasNext()) {
			Element columnElement = (Element)itColumnElementList.next();
			Element columnFacet = getChildFacetByName(columnElement, facetName);
			if (columnFacet != null) {
				hasColumnFacet = true;
				break;
			}
		}

		//test if we even need to build header or footer
		if (facetElement == null && !hasColumnFacet) {
			return;
		}

		//append "thead" or "tfoot" Element
		Element headerOrFooterElement = appendChildElement(headerOrFooterTagName, tableElement);

		//if facetElement exists, build appropriate thead or tfoot children
		if (facetElement != null) {
			//append "tr" Element
			Element trElement = appendChildElement("tr", headerOrFooterElement);
			//append "th" or "td" Element
			Element thOrTdElement = appendChildElement(thOrTdTagName, trElement);
			//set "class" attribute
			String headerClassOrFooterClassAttribute = srcElement.getAttribute(headerClassOrFooterClassAttrName);
			if (headerClassOrFooterClassAttribute != null && headerClassOrFooterClassAttribute.length() > 0) {
				ITransformOperation operation =
					TransformOperationFactory.getInstance().getTransformOperation(
							TransformOperationFactory.OP_CreateAttributeOperation,
							new String[]{"class", headerClassOrFooterClassAttribute});
				operation.transform(srcElement, thOrTdElement);
			}
			//set "colspan" attribute
			if (columnElementList.size() > 0) {
				ITransformOperation operation =
					TransformOperationFactory.getInstance().getTransformOperation(
							TransformOperationFactory.OP_CreateAttributeOperation,
							new String[]{"colspan", String.valueOf(columnElementList.size())});
				operation.transform(srcElement, thOrTdElement);
			}
			//add facet Element as child (to be processed further)
			tagConverterContext.addChild(facetElement, new ConvertPosition(thOrTdElement, 0));
		}

		//if any child column has "header" or "footer" facet, build "tr" element
		if (hasColumnFacet) {
			//append "tr" Element
			Element trElement = appendChildElement("tr", headerOrFooterElement);
			//iterate through columnElementList
			itColumnElementList = columnElementList.iterator();
			while (itColumnElementList.hasNext()) {
				Element columnElement = (Element)itColumnElementList.next();
				//get "header" or "footer" facet of column
				Element columnFacet = getChildFacetByName(columnElement, facetName);
				//append "th" or "td" Element
				Element thOrTdElement = appendChildElement(thOrTdTagName, trElement);
				//set "class" attribute
				String headerClassOrFooterClassAttribute = srcElement.getAttribute(headerClassOrFooterClassAttrName);
				if (headerClassOrFooterClassAttribute != null && headerClassOrFooterClassAttribute.length() > 0) {
					ITransformOperation operation =
						TransformOperationFactory.getInstance().getTransformOperation(
								TransformOperationFactory.OP_CreateAttributeOperation,
								new String[]{"class", headerClassOrFooterClassAttribute});
					operation.transform(srcElement, thOrTdElement);
				}
				//if facet exists, add facet Element as child (to be processed further)
				if (columnFacet != null) {
					tagConverterContext.addChild(columnFacet, new ConvertPosition(thOrTdElement, 0));
				}
			}
		}
	}

	private void buildBody(Element srcElement, Element tableElement) {
		//append "tbody" element
		Element tbodyElement = appendChildElement("tbody", tableElement);
		//append "tr" element
		Element trElement = appendChildElement("tr", tbodyElement);
		//parse "rowClasses" attribute and set "class" attribute
		String rowClassesAttribute = srcElement.getAttribute("rowClasses");
		if (rowClassesAttribute != null && rowClassesAttribute.length() > 0) {
			StringTokenizer tokenizer = new StringTokenizer(rowClassesAttribute, ", ");
			if (tokenizer.hasMoreTokens()) {
				ITransformOperation operation =
					TransformOperationFactory.getInstance().getTransformOperation(
							TransformOperationFactory.OP_CreateAttributeOperation,
							new String[]{"class", tokenizer.nextToken()});
				operation.transform(srcElement, trElement);
			}
		}
		//add child columns (to be processed further)
		List columnElementList = getChildElements(srcElement, "column");
		Iterator itColumnElementList = columnElementList.iterator();
		int index = 0;
		while (itColumnElementList.hasNext()) {
			Element columnElement = (Element)itColumnElementList.next();
			tagConverterContext.addChild(columnElement, new ConvertPosition(trElement, index++));
		}
	}

	/**
	 * Gets a child Element of the specified parent Element that has the node
	 * name "facet" and the specified value of the "name" attribute.
	 * 
	 * @param srcElement Parent Element instance.
	 * @param facetName Name of the facet Element for which to search.
	 * @return Child Element that is a facet with the specified name.
	 */
	private Element getChildFacetByName(Element srcElement, String facetName) {
		Element element = null;
		List facets = getChildElements(srcElement, "facet");
		Iterator itFacets = facets.iterator();
		while (itFacets.hasNext()) {
			Element facet = (Element)itFacets.next();
			if (facet.getAttribute("name").equals(facetName)) {
				element = facet;
				break;
			}
		}
		return element;
	}

}

Back to the top