Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7ee81bf4b7cdbe455fd4abd24a83e3a5fa15c8d4 (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
201
202
203
204
/*******************************************************************************
 * 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.ArrayList;
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;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * ITransformOperation implementation specifically for the "panelGrid" 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 PanelGridOperation 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"); //$NON-NLS-1$
		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"}); //$NON-NLS-1$ //$NON-NLS-2$
		operation.transform(srcElement, tableElement);
		//get value of "columns" attribute
		int columns;
		try {
			columns = Integer.parseInt(srcElement.getAttribute("columns")); //$NON-NLS-1$
		} catch(NumberFormatException nfe) {
			columns = 1;
		}
		if (columns < 1) {
			columns = 1;
		}
		//check for "header" facet and render appropriately
		Element headerFacetElement = getChildFacetByName(srcElement, "header"); //$NON-NLS-1$
		if (headerFacetElement != null) {
			Element tHeadElement = appendChildElement("thead", tableElement); //$NON-NLS-1$
			Element trElement = appendChildElement("tr", tHeadElement); //$NON-NLS-1$
			Element thElement = appendChildElement("th", trElement); //$NON-NLS-1$
			String headerClass = srcElement.getAttribute("headerClass"); //$NON-NLS-1$
			if (headerClass != null && headerClass.length() > 0) {
				operation =
					TransformOperationFactory.getInstance().getTransformOperation(
							TransformOperationFactory.OP_CreateAttributeOperation,
							new String[]{"class", headerClass}); //$NON-NLS-1$
				operation.transform(srcElement, thElement);
			}
			operation =
				TransformOperationFactory.getInstance().getTransformOperation(
						TransformOperationFactory.OP_CreateAttributeOperation,
						new String[]{"colspan", String.valueOf(columns)}); //$NON-NLS-1$
			operation.transform(srcElement, thElement);
			tagConverterContext.addChild(headerFacetElement, new ConvertPosition(thElement, 0));
		}
		//check for "footer" facet and render appropriately
		Element footerFacetElement = getChildFacetByName(srcElement, "footer"); //$NON-NLS-1$
		if (footerFacetElement != null) {
			Element tFootElement = appendChildElement("tfoot", tableElement); //$NON-NLS-1$
			Element trElement = appendChildElement("tr", tFootElement); //$NON-NLS-1$
			Element tdElement = appendChildElement("td", trElement); //$NON-NLS-1$
			String footerClass = srcElement.getAttribute("footerClass"); //$NON-NLS-1$
			if (footerClass != null && footerClass.length() > 0) {
				operation =
					TransformOperationFactory.getInstance().getTransformOperation(
							TransformOperationFactory.OP_CreateAttributeOperation,
							new String[]{"class", footerClass}); //$NON-NLS-1$
				operation.transform(srcElement, tdElement);
			}
			operation =
				TransformOperationFactory.getInstance().getTransformOperation(
						TransformOperationFactory.OP_CreateAttributeOperation,
						new String[]{"colspan", String.valueOf(columns)}); //$NON-NLS-1$
			operation.transform(srcElement, tdElement);
			tagConverterContext.addChild(footerFacetElement, new ConvertPosition(tdElement, 0));
		}
		//get rowClasses and columnClasses for subsequent processing
        List rowClasses = new ArrayList();
        String rowClassesAttribute = srcElement.getAttribute("rowClasses"); //$NON-NLS-1$
        if (rowClassesAttribute != null && rowClassesAttribute.length() > 0) {
            StringTokenizer tokenizer = new StringTokenizer(rowClassesAttribute, ", "); //$NON-NLS-1$
            while (tokenizer.hasMoreTokens()) {
                rowClasses.add(tokenizer.nextToken());
            }
        }
        List columnClasses = new ArrayList();
        String columnClassAttribute = srcElement.getAttribute("columnClasses"); //$NON-NLS-1$
        if (columnClassAttribute != null) {
            StringTokenizer tokenizer = new StringTokenizer(columnClassAttribute, ", "); //$NON-NLS-1$
            while (tokenizer.hasMoreTokens()) {
                columnClasses.add(tokenizer.nextToken());
            }
        }
		//render children using appropriate number of columns and appropriate classes
        Element tBodyElement = appendChildElement("tbody", tableElement); //$NON-NLS-1$
        List childElements = getChildElementsSkipFacets(srcElement);
        Element trElement = null;
        int nextRow = 0;
        int curIndex = 0;
        Iterator itChildElements = childElements.iterator();
        while (itChildElements.hasNext()) {
        	int columnIndex = curIndex % columns;
        	if (columnIndex == 0) {
        		trElement = appendChildElement("tr", tBodyElement); //$NON-NLS-1$
        		if (!rowClasses.isEmpty()) {
    				operation =
    					TransformOperationFactory.getInstance().getTransformOperation(
    							TransformOperationFactory.OP_CreateAttributeOperation,
    							new String[]{"class", (String)rowClasses.get(nextRow)}); //$NON-NLS-1$
    				operation.transform(srcElement, trElement);
        			nextRow = (nextRow + 1) % rowClasses.size();
        		}
        	}
        	Element tdElement = appendChildElement("td", trElement); //$NON-NLS-1$
        	if (columnIndex < columnClasses.size()) {
				operation =
					TransformOperationFactory.getInstance().getTransformOperation(
							TransformOperationFactory.OP_CreateAttributeOperation,
							new String[]{"class", (String)columnClasses.get(columnIndex)}); //$NON-NLS-1$
				operation.transform(srcElement, tdElement);
        	}
        	tagConverterContext.addChild((Element)itChildElements.next(), new ConvertPosition(tdElement, 0));
        	curIndex++;
        }
        return tableElement;
	}

	/**
	 * 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"); //$NON-NLS-1$
		Iterator itFacets = facets.iterator();
		while (itFacets.hasNext()) {
			Element facet = (Element)itFacets.next();
			String facetAttrName = facet.getAttribute("name"); //$NON-NLS-1$
			if (facetAttrName != null && facetAttrName.equals(facetName)) {
				element = facet;
				break;
			}
		}
		return element;
	}

	/**
	 * Gets a list of child Elements of the specified parent Element, skipping
	 * any "facet" Elements.
	 * 
	 * @param srcElement Parent Element instance.
	 * @return List of child Elements of the specified parent Element that does
	 * not include any child "facet" Elements.
	 */
	private List getChildElementsSkipFacets(Element srcElement) {
		List childElementsList = new ArrayList();
		NodeList childNodes = srcElement.getChildNodes();
		for (int i = 0; i < childNodes.getLength(); i++) {
			Node childNode = childNodes.item(i);
			if (childNode.getNodeType() == Node.ELEMENT_NODE) {
				if (!childNode.getLocalName().equals("facet")) { //$NON-NLS-1$
					childElementsList.add(childNode);
				}
			}
		}
		return childElementsList;
	}

}

Back to the top