Skip to main content
summaryrefslogtreecommitdiffstats
blob: d6f56d05002c52f0c20fbc393ac85c228a7d87ac (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/**
 * Copyright (c) 2008 Oracle Corporation and others.
 * 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:
 *    Oracle Corporation - initial API and implementation
 */
package org.eclipse.jst.jsf.apache.trinidad.tagsupport.converter.operations;

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

import org.eclipse.jst.jsf.apache.trinidad.tagsupport.ITrinidadConstants;
import org.eclipse.jst.jsf.apache.trinidad.tagsupport.Messages;
import org.eclipse.jst.jsf.apache.trinidad.tagsupport.TrinidadUtils;
import org.eclipse.jst.pagedesigner.converter.ConvertPosition;
import org.eclipse.jst.pagedesigner.dtmanager.converter.ITransformOperation;
import org.eclipse.jst.pagedesigner.dtmanager.converter.operations.TransformOperationFactory;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 * ITransformOperation implementation specifically for the "panelTabbed" JSF
 * 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 PanelTabbedOperation extends AbstractTrinidadTransformOperation {

	private static final int SEP_POS_BETWEEN = -1;
	private static final int SEP_POS_START = 0;
	private static final int SEP_POS_END = 1;

	/* (non-Javadoc)
	 * @see org.eclipse.jst.pagedesigner.dtmanager.converter.operations.AbstractTransformOperation#transform(org.w3c.dom.Element, org.w3c.dom.Element)
	 */
	@Override
	public Element transform(Element srcElement, Element curElement) {
		//create outer span element and set style and class attributes
		Element spanElement = createElement("span"); //$NON-NLS-1$
		ITransformOperation operation =
			TransformOperationFactory.getInstance().getTransformOperation(
					TransformOperationFactory.OP_CopyAttributeWithRenameOperation,
					new String[]{"styleClass", "class"}); //$NON-NLS-1$  //$NON-NLS-2$
		operation.transform(srcElement, spanElement);
		operation =
			TransformOperationFactory.getInstance().getTransformOperation(
					TransformOperationFactory.OP_CopyAttributeWithRenameOperation,
					new String[]{"inlineStyle", "style"}); //$NON-NLS-1$  //$NON-NLS-2$
		operation.transform(srcElement, spanElement);

		//get child showDetailItem elements
		@SuppressWarnings("unchecked")
		List<Node> showDetailItems = getChildElements(
				srcElement, "showDetailItem"); //$NON-NLS-1$
		if (showDetailItems.size() > 0) {

			//determine tabs position ("both", "above", or "below" - default "both")
			String tabsPosition = srcElement.getAttribute("position"); //$NON-NLS-1$
			if (tabsPosition == null ||
					!(tabsPosition.equalsIgnoreCase("above") || //$NON-NLS-1$
					tabsPosition.equalsIgnoreCase("below"))) { //$NON-NLS-1$
				tabsPosition = "both"; //$NON-NLS-1$
			}

			//need to track where showDetailItem is in relation to "tabs"
			int showDetailItemConvertPosition = 0;

			//write tabs "above" if specified
			if ("above".equalsIgnoreCase(tabsPosition) || //$NON-NLS-1$
					"both".equalsIgnoreCase(tabsPosition)) { //$NON-NLS-1$
				appendTabs(srcElement, showDetailItems, spanElement, true);
				showDetailItemConvertPosition++;
			}

			//copy current child showDetailItem
			int currentEditorItem =
				getCurrentShowDetailItem(srcElement, showDetailItems);
			int curItem = 0;
			Iterator<Node> itItems = showDetailItems.iterator();
			while (itItems.hasNext()) {
				Node nodeItem = itItems.next();
				if (currentEditorItem == curItem) {
					if (nodeItem instanceof Element) {
						Element elemItem = (Element)nodeItem;
						tagConverterContext.addChild(
								elemItem,
								new ConvertPosition(
										spanElement,
										showDetailItemConvertPosition));
						break;
					}
				}
				curItem++;
			}

			//write tabs "below" if specified
			if ("below".equalsIgnoreCase(tabsPosition) || //$NON-NLS-1$
					"both".equalsIgnoreCase(tabsPosition)) { //$NON-NLS-1$
				appendTabs(srcElement, showDetailItems, spanElement, false);
			}
		} else {
			appendAttribute(
					spanElement,
					"style", //$NON-NLS-1$
					ITrinidadConstants.STYLE_EMPTYELEMENT);
			appendChildText(
					Messages.PanelTabbedOperation_EmptyPanelTabbedTag,
					spanElement);
		}
			
		return spanElement;
	}

	private void appendTabs(Element srcElement, List<Node> showDetailItems, Element spanElement, boolean above) {
		Element tableElement = appendChildElement("table", spanElement); //$NON-NLS-1$
		String tableStyle;
		if (above) {
			tableStyle = "background-color:#e9e8e8;border-color:#99cc99;text-align:center;border-style:solid;padding:2px 0px;margin:4px 0px;border-width:1px 0px 0px;"; //$NON-NLS-1$
		} else {
			tableStyle = "background-color:#e9e8e8;border-color:#99cc99;text-align:center;border-style:solid;padding:2px 0px;margin:4px 0px;border-width:0px 0px 1px;"; //$NON-NLS-1$
		}
		appendAttribute(tableElement, "style", tableStyle); //$NON-NLS-1$
		appendAttribute(tableElement, "cellpadding", "0"); //$NON-NLS-1$ //$NON-NLS-2$
		appendAttribute(tableElement, "cellspacing", "0"); //$NON-NLS-1$ //$NON-NLS-2$
		appendAttribute(tableElement, "border", "0"); //$NON-NLS-1$ //$NON-NLS-2$
		appendAttribute(tableElement, "width", "100%"); //$NON-NLS-1$ //$NON-NLS-2$
		appendAttribute(tableElement, "summary", ""); //$NON-NLS-1$ //$NON-NLS-2$
		Element trElement = appendChildElement("tr", tableElement);

		//append first separator
		appendSeparatorTD(trElement, SEP_POS_START);

		int currentItem = getCurrentShowDetailItem(srcElement, showDetailItems);
		int disclosedItem = calculateDisclosedShowDetailItem(showDetailItems);
		int curItem = 0;

		//iterate over showDetailItem elements
		Iterator<Node> itItems = showDetailItems.iterator();
		while (itItems.hasNext()) {
			Node nodeItem = itItems.next();
			if (nodeItem instanceof Element) {
				Element elemItem = (Element)nodeItem;
				appendShowDetailItemTD(
						trElement,
						elemItem,
						currentItem == curItem,
						disclosedItem == curItem);
				if (curItem < showDetailItems.size() - 1) {
					appendSeparatorTD(trElement);
				}
				curItem++;
			}
		}

		//append last separator
		appendSeparatorTD(trElement, SEP_POS_END);
	}

	private void appendSeparatorTD(Element trElement, int sepPosition) {
		Element tdElement = appendChildElement("td", trElement); //$NON-NLS-1$
		if (sepPosition == SEP_POS_START) {
			appendAttribute(tdElement, "style", "width:0%;"); //$NON-NLS-1$ //$NON-NLS-2$
		} else if (sepPosition == SEP_POS_END) {
			appendAttribute(tdElement, "style", "width:100%;"); //$NON-NLS-1$ //$NON-NLS-2$
		}
		Element bElement = appendChildElement("b", tdElement); //$NON-NLS-1$
		appendAttribute(bElement, "style", "margin-left:0px;"); //$NON-NLS-1$ //$NON-NLS-2$
	}

	private void appendSeparatorTD(Element trElement) {
		appendSeparatorTD(trElement, SEP_POS_BETWEEN);
	}

	private void appendShowDetailItemTD(Element trElement, Element showDetailItem, boolean isCurrent, boolean isDisclosed) {
		boolean isDisabled = false;
		String attrShowDetailItemDisabled = showDetailItem.getAttribute("disabled"); //$NON-NLS-1$
		if (Boolean.TRUE.toString().equalsIgnoreCase(attrShowDetailItemDisabled)) {
			isDisabled = true;
		}
		Element tdElement = appendChildElement("td", trElement); //$NON-NLS-1$
		appendAttribute(tdElement, "height", "1"); //$NON-NLS-1$ //$NON-NLS-2$
		appendAttribute(tdElement, "nowrap", "nowrap"); //$NON-NLS-1$ //$NON-NLS-2$
		String tdStyle;
		if (isDisclosed && !isDisabled) {
			tdStyle = "font-family:Arial,Helvetica,Geneva,sans-serif;font-size:10pt;font-weight:bold;padding:0px 8px;"; //$NON-NLS-1$
		} else {
			tdStyle = "font-family:Arial,Helvetica,Geneva,sans-serif;font-size:10pt;font-weight:normal;padding:0px 8px;"; //$NON-NLS-1$
		}
		appendAttribute(tdElement, "style", tdStyle); //$NON-NLS-1$
		Element aElement = appendChildElement("a", tdElement); //$NON-NLS-1$
		String aStyle;
		if (isDisabled) {
			appendAttribute(aElement, "name", "name"); //$NON-NLS-1$ //$NON-NLS-2$
			aStyle = "color:#999999;"; //$NON-NLS-1$
		} else {
			appendAttribute(aElement, "href", "#"); //$NON-NLS-1$ //$NON-NLS-2$
			if (isDisclosed) {
				aStyle = "color:#669966;text-decoration:none;"; //$NON-NLS-1$
			} else {
				aStyle = "color:#003333;"; //$NON-NLS-1$
			}
		}
		if (isCurrent) {
			aStyle += "border:1px solid #99cc99;padding:2px;"; //$NON-NLS-1$
		}
		String attrShowDetailItemInlineStyle = showDetailItem.getAttribute("inlineStyle"); //$NON-NLS-1$
		if (attrShowDetailItemInlineStyle != null &&
				attrShowDetailItemInlineStyle.length() > 0) {
			aStyle += attrShowDetailItemInlineStyle;
		}
		if (aStyle.length() > 0) {
			appendAttribute(aElement, "style", aStyle); //$NON-NLS-1$
		}
		ITransformOperation operation =
			TransformOperationFactory.getInstance().getTransformOperation(
					TransformOperationFactory.OP_CopyAttributeWithRenameOperation,
					new String[]{"styleClass", "class"}); //$NON-NLS-1$ //$NON-NLS-2$
		operation.transform(showDetailItem, aElement);
		String attrShowDetailItemTextAndAccessKey = showDetailItem.getAttribute("textAndAccessKey"); //$NON-NLS-1$
		if (attrShowDetailItemTextAndAccessKey != null &&
				attrShowDetailItemTextAndAccessKey.length() > 0) {
			appendChildText(attrShowDetailItemTextAndAccessKey, aElement);
		} else {
			String attrShowDetailItemText = showDetailItem.getAttribute("text"); //$NON-NLS-1$
			if (attrShowDetailItemText != null && attrShowDetailItemText.length() > 0) {
				appendChildText(attrShowDetailItemText, aElement);
			}
		}
	}

	private int getCurrentShowDetailItem(
			Element srcElement, List<Node> showDetailItems) {
		int disclosedItem = TrinidadUtils.getCurrentChildIndex(srcElement);
		if (disclosedItem == -1) {
			disclosedItem = calculateDisclosedShowDetailItem(showDetailItems);
			TrinidadUtils.setCurrentChildIndex(srcElement, disclosedItem);
		}
		return disclosedItem;
	}

	private int calculateDisclosedShowDetailItem(List<Node> showDetailItems) {
		int disclosedItem = -1;
		int curItem = 0;
		Iterator<Node> itItems = showDetailItems.iterator();
		while (itItems.hasNext()) {
			Node item = itItems.next();
			if (item instanceof Element) {
				Element elemItem = (Element)item;
				String attrDisclosedVal = elemItem.getAttribute("disclosed"); //$NON-NLS-1$
				if (Boolean.TRUE.toString().equalsIgnoreCase(
						attrDisclosedVal)) {
					disclosedItem = curItem;
					break;
				}
			}
			curItem++;
		}
		//if none explicitly disclosed, consider first non-disabled tab disclosed
		if (disclosedItem == -1) {
			curItem = 0;
			itItems = showDetailItems.iterator();
			while (itItems.hasNext()) {
				Node item = itItems.next();
				if (item instanceof Element) {
					Element elemItem = (Element)item;
					String attrDisabledVal = elemItem.getAttribute("disabled"); //$NON-NLS-1$
					if (!(Boolean.TRUE.toString().equalsIgnoreCase(attrDisabledVal))) {
						disclosedItem = curItem;
						break;
					}
				}
				curItem++;
			}
		}
		//if none explicitly disclosed and all disabled, consider first tab disclosed
		if (disclosedItem == -1) {
			disclosedItem = 0;
		}
		return disclosedItem;
	}

}

Back to the top