Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: aff1770972abbb63e306608d8b5a26b007777072 (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
/**
 * Copyright (c) 2009 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 org.eclipse.jst.jsf.apache.trinidad.tagsupport.ITrinidadConstants;
import org.eclipse.jst.jsf.common.dom.TagIdentifier;
import org.eclipse.jst.jsf.core.internal.tld.IJSFConstants;
import org.eclipse.jst.jsf.core.internal.tld.TagIdentifierFactory;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 * ITransformOperation implementation specifically for the
 * "commandNavigationItem" 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 CommandNavigationItemOperation extends AbstractTrinidadTransformOperation {

	/* (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) {
		Element topElement = null;
		if (isDescendentOf(ITrinidadConstants.TAG_IDENTIFIER_BREADCRUMBS, srcElement)) {
			topElement = transformForBreadCrumbs(srcElement);
		} else if (isDescendentOf(ITrinidadConstants.TAG_IDENTIFIER_NAVIGATIONPANE, srcElement)) {
			Element navPane = getAncestor(ITrinidadConstants.TAG_IDENTIFIER_NAVIGATIONPANE, srcElement);
			if (navPane != null) {
				String navPaneHint = navPane.getAttribute("hint"); //$NON-NLS-1$
				if ("choice".equalsIgnoreCase(navPaneHint)) { //$NON-NLS-1$
					topElement = transformForNavigationPane_Choice(srcElement);
				} else {
					topElement = doDefaultTransform(srcElement);
				}
			}
		} else {
			topElement = doDefaultTransform(srcElement);
		}
		return topElement;
	}

	private Element transformForBreadCrumbs(Element srcElement) {
		Element anchor = createElement("a"); //$NON-NLS-1$
		if (!isDisabledOrLastCmdNavItem(srcElement)) {
			appendAttribute(anchor, "href", "#"); //$NON-NLS-1$ //$NON-NLS-2$
			appendAttribute(anchor, "class", "af_breadCrumbs_step"); //$NON-NLS-1$ //$NON-NLS-2$
		} else {
			appendAttribute(anchor, "class", "af_breadCrumbs_selected-step"); //$NON-NLS-1$ //$NON-NLS-2$
		}
		appendChildText(getText(srcElement), anchor);
		return anchor;
	}

	private Element transformForNavigationPane_Choice(Element srcElement) {
		Element option = createElement("option"); //$NON-NLS-1$
		if (isSelected(srcElement)) {
			appendAttribute(option, "selected", "selected"); //$NON-NLS-1$ //$NON-NLS-2$
		}
		appendChildText(getText(srcElement), option);
		return option;
	}

	private Element doDefaultTransform(Element srcElement) {
		Element anchor = createElement("a"); //$NON-NLS-1$
		if (!isDisabled(srcElement)) {
			appendAttribute(anchor, "href", "#"); //$NON-NLS-1$ //$NON-NLS-2$
		}
		appendChildText(getText(srcElement), anchor);
		return anchor;
	}

	private boolean isDescendentOf(TagIdentifier tagIdentifier, Element srcElement) {
		boolean isDescendent = false;
		if (tagIdentifier != null && srcElement != null) {
			Node parentNode = srcElement.getParentNode();
			if (parentNode instanceof Element) {
				if (tagIdentifier.isSameTagType(
						TagIdentifierFactory.createDocumentTagWrapper((Element)parentNode))) {
					isDescendent = true;
				} else if (IJSFConstants.TAG_IDENTIFIER_FACET.isSameTagType(
						TagIdentifierFactory.createDocumentTagWrapper((Element)parentNode))) {
					parentNode = parentNode.getParentNode();
					if (parentNode instanceof Element) {
						if (tagIdentifier.isSameTagType(
								TagIdentifierFactory.createDocumentTagWrapper((Element)parentNode))) {
							isDescendent = true;
						}
					}
				}
			}
		}
		return isDescendent;
	}

	private Element getAncestor(TagIdentifier tagIdentifier, Element srcElement) {
		Element ancestor = null;
		if (tagIdentifier != null && srcElement != null) {
			Node parentNode = srcElement.getParentNode();
			if (parentNode instanceof Element) {
				if (tagIdentifier.isSameTagType(
						TagIdentifierFactory.createDocumentTagWrapper((Element)parentNode))) {
					ancestor = (Element)parentNode;
				} else if (IJSFConstants.TAG_IDENTIFIER_FACET.isSameTagType(
						TagIdentifierFactory.createDocumentTagWrapper((Element)parentNode))) {
					parentNode = parentNode.getParentNode();
					if (parentNode instanceof Element) {
						if (tagIdentifier.isSameTagType(
								TagIdentifierFactory.createDocumentTagWrapper((Element)parentNode))) {
							ancestor = (Element)parentNode;
						}
					}
				}
			}
		}
		return ancestor;
	}

	private String getText(Element srcElement) {
		String text = "commandNavigationItem"; //$NON-NLS-1$
		if (srcElement != null) {
			String newText = srcElement.getAttribute(ITrinidadConstants.ATTR_TEXTANDACCESSKEY);
			if (newText != null && newText.length() > 0) {
				text = newText;
			} else {
				newText = srcElement.getAttribute(ITrinidadConstants.ATTR_TEXT);
				if (newText != null && newText.length() > 0) {
					text = newText;
				} else {
					newText = srcElement.getAttribute(ITrinidadConstants.ATTR_ACTION);
					if (newText != null && newText.length() > 0) {
						text = newText;
					} else {
						newText = srcElement.getAttribute(ITrinidadConstants.ATTR_DESTINATION);
						if (newText != null && newText.length() > 0) {
							text = newText;
						}
					}
				}
			}
		}
		return text;
	}

	private boolean isDisabled(Element srcElement) {
		String disabledAttr = srcElement.getAttribute(ITrinidadConstants.ATTR_DISABLED);
		return Boolean.TRUE.toString().equalsIgnoreCase(disabledAttr);
	}

	private boolean isDisabledOrLastCmdNavItem(Element srcElement) {
		boolean ret = false;
		String disabledAttr = srcElement.getAttribute(ITrinidadConstants.ATTR_DISABLED);
		if (Boolean.TRUE.toString().equalsIgnoreCase(disabledAttr)) {
			ret = true;
		} else {
			Node nextSibling = srcElement;
			while (nextSibling != null) {
				nextSibling = nextSibling.getNextSibling();
				if (nextSibling == null) {
					ret = true;
				} else {
					if (nextSibling instanceof Element) {
						if (TagIdentifierFactory.createDocumentTagWrapper((Element)nextSibling).isSameTagType(ITrinidadConstants.TAG_IDENTIFIER_COMMANDNAVIGATIONITEM)) {
							break;
						}
					}
				}
			}
		}
		return ret;
	}

	private boolean isSelected(Element srcElement) {
		String selectedAttr = srcElement.getAttribute(ITrinidadConstants.ATTR_SELECTED);
		return Boolean.TRUE.toString().equalsIgnoreCase(selectedAttr);
	}

}

Back to the top