001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.web.jsf.impl.facesmodel;
043:
044: import java.util.Arrays;
045: import java.util.Collections;
046: import java.util.List;
047: import javax.xml.namespace.QName;
048: import org.netbeans.modules.web.jsf.api.facesmodel.JSFConfigComponent;
049: import org.netbeans.modules.web.jsf.api.facesmodel.JSFConfigModel;
050: import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
051: import org.netbeans.modules.xml.xam.dom.Attribute;
052: import org.w3c.dom.Element;
053: import org.w3c.dom.Node;
054: import org.w3c.dom.NodeList;
055:
056: /**
057: *
058: * @author Petr Pisl
059: */
060: public abstract class JSFConfigComponentImpl extends
061: AbstractDocumentComponent<JSFConfigComponent> implements
062: JSFConfigComponent {
063:
064: /** Creates a new instance of JSFConfigComponentImp */
065: public JSFConfigComponentImpl(JSFConfigModelImpl model,
066: Element element) {
067: super (model, element);
068: }
069:
070: public JSFConfigModelImpl getModel() {
071: return (JSFConfigModelImpl) super .getModel();
072: }
073:
074: protected void populateChildren(List<JSFConfigComponent> children) {
075: NodeList nodeList = getPeer().getChildNodes();
076: if (nodeList != null) {
077: for (int i = 0; i < nodeList.getLength(); i++) {
078: org.w3c.dom.Node node = nodeList.item(i);
079: if (node instanceof Element) {
080: JSFConfigModel model = getModel();
081: JSFConfigComponent comp = (JSFConfigComponent) model
082: .getFactory().create((Element) node, this );
083: if (comp != null) {
084: children.add(comp);
085: }
086: }
087: }
088: }
089: }
090:
091: /**
092: * Set the value of the text node from the child element with given QName.
093: * This method is use to implement mapping of "property" as component attribute.
094: * @param propertyName property change event name
095: * @param text the string to set value of the child element text node.
096: * @param qname QName of the child element to get text from.
097: */
098: protected void setChildElementText(String propertyName,
099: String text, QName qname) {
100: super .setChildElementText(propertyName, text, qname);
101: reorderChildren();
102: }
103:
104: protected void appendChild(String propertyName,
105: JSFConfigComponent child) {
106: super .appendChild(propertyName, child);
107: reorderChildren();
108: }
109:
110: protected Object getAttributeValueOf(Attribute attr,
111: String stringValue) {
112: return stringValue;
113: }
114:
115: protected List<String> getSortedListOfLocalNames() {
116: return Collections.EMPTY_LIST;
117: }
118:
119: protected void reorderChildren() {
120: NodeList nodes = getPeer().getChildNodes();
121: int length = nodes.getLength();
122:
123: Node node;
124: int orderNumber;
125: int lastRealOrderNumber = -1;
126: SortingItem[] sortingItems = new SortingItem[length];
127: for (int i = length - 1; i > -1; i--) {
128: node = nodes.item(i);
129: orderNumber = getSortedListOfLocalNames().indexOf(
130: node.getLocalName());
131: if (orderNumber == -1) {
132: if (lastRealOrderNumber == -1) {
133: orderNumber = getSortedListOfLocalNames().size() + 1;
134: } else {
135: orderNumber = lastRealOrderNumber;
136: }
137: } else {
138: lastRealOrderNumber = orderNumber;
139: }
140: sortingItems[i] = new SortingItem(i, orderNumber);
141: }
142:
143: Arrays.sort(sortingItems);
144:
145: int[] newIndexes = new int[length];
146: for (int i = 0; i < length; i++) {
147: newIndexes[sortingItems[i].getOriginalIndex()] = i;
148: }
149: getModel().getAccess().reorderChildren(getPeer(), newIndexes,
150: this );
151: }
152:
153: static public Element createElementNS(JSFConfigModel model,
154: JSFConfigQNames jsfqname) {
155: return model.getDocument().createElementNS(
156: model.getRootComponent().getPeer().getNamespaceURI(),
157: jsfqname.getQualifiedName(model.getVersion()));
158: }
159:
160: private class SortingItem implements Comparable<SortingItem> {
161: //index of the element, before sorting
162: int originalIndex;
163: //number, how it should be sorted
164: int orderNumber;
165:
166: public SortingItem(int originalIndex, int orderNumber) {
167: this .originalIndex = originalIndex;
168: this .orderNumber = orderNumber;
169: }
170:
171: public int getOriginalIndex() {
172: return originalIndex;
173: }
174:
175: public void setOriginalIndex(int originalIndex) {
176: this .originalIndex = originalIndex;
177: }
178:
179: public int getOrderNumber() {
180: return orderNumber;
181: }
182:
183: public void setOrderNumber(int orderNumber) {
184: this .orderNumber = orderNumber;
185: }
186:
187: public int compareTo(SortingItem item) {
188: int result;
189:
190: if (orderNumber == item.getOrderNumber()) {
191: result = 0;
192: } else if (orderNumber < item.getOrderNumber()) {
193: result = -1;
194: } else {
195: result = 1;
196: }
197: return result;
198: }
199: }
200:
201: }
|