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-2006 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.xml.xam.dom;
043:
044: import java.util.ArrayList;
045: import java.util.Collections;
046: import java.util.List;
047: import org.w3c.dom.Attr;
048: import org.w3c.dom.Element;
049: import org.w3c.dom.Node;
050:
051: /**
052: *
053: * @author Nam Nguyen
054: */
055: public class ChangeInfo {
056:
057: private Node changed;
058: private Element parent;
059: private DocumentComponent parentComponent;
060: private boolean domainElement;
061: private boolean added;
062: private List<Element> rootToParent;
063: /**
064: * List of non-domain element nodes beside the changed node.
065: * The order is increasing distance from root.
066: */
067: private List<Node> otherNonDomainElementNodes;
068:
069: /**
070: * Creates change info.
071: *
072: * @param parent parent node of changed
073: * @param changed added/removed domain element or first non-domain change node.
074: * @param isDomainElement is the changed node a domain element.
075: * @param rootToParent path from root to parent node, inclusively.
076: * @param otherNodes list of other nodes that are not domain elements beside the changed nodes.
077: */
078: public ChangeInfo(Element parent, Node changed,
079: boolean isDomainElement, List<Element> rootToParent,
080: List<Node> otherNodes) {
081: this .parent = parent;
082: this .changed = changed;
083: domainElement = isDomainElement;
084: if (!domainElement) {
085: otherNonDomainElementNodes = otherNodes;
086: }
087: this .rootToParent = rootToParent;
088: }
089:
090: public Element getParent() {
091: return parent;
092: }
093:
094: public Node getChangedNode() {
095: return changed;
096: }
097:
098: public Element getChangedElement() {
099: if (changed instanceof Element) {
100: return (Element) changed;
101: }
102: return null;
103: }
104:
105: public boolean isDomainElement() {
106: return domainElement;
107: }
108:
109: public void setDomainElement(boolean v) {
110: domainElement = v;
111: }
112:
113: public void setRootToParentPath(List<Element> path) {
114: rootToParent = path;
115: }
116:
117: public List<Element> getRootToParentPath() {
118: return rootToParent;
119: }
120:
121: public List<Element> getParentToRootPath() {
122: ArrayList<Element> ret = new ArrayList(rootToParent);
123: Collections.reverse(ret);
124: return ret;
125: }
126:
127: public boolean isDomainElementAdded() {
128: return domainElement && added;
129: }
130:
131: public void setAdded(boolean v) {
132: added = v;
133: }
134:
135: public boolean isAdded() {
136: return added;
137: }
138:
139: public void markParentAsChanged() {
140: assert parent != null;
141: changed = parent;
142:
143: assert rootToParent.size() > 1;
144: assert parent == rootToParent.get(rootToParent.size() - 1);
145: rootToParent.remove(rootToParent.size() - 1);
146: parent = rootToParent.get(rootToParent.size() - 1);
147: }
148:
149: public void setParentComponent(DocumentComponent component) {
150: parentComponent = component;
151: }
152:
153: public DocumentComponent getParentComponent() {
154: return parentComponent;
155: }
156:
157: public List<Node> getOtherNonDomainElementNodes() {
158: return otherNonDomainElementNodes;
159: }
160:
161: public Node getActualChangedNode() {
162: if (isDomainElement()) {
163: return changed;
164: } else {
165: if (otherNonDomainElementNodes == null
166: || otherNonDomainElementNodes.isEmpty()) {
167: return changed;
168: } else {
169: return otherNonDomainElementNodes
170: .get(otherNonDomainElementNodes.size() - 1);
171: }
172: }
173: }
174:
175: public void markNonDomainChildAsChanged() {
176: assert otherNonDomainElementNodes != null
177: && otherNonDomainElementNodes.size() > 0;
178: assert (changed instanceof Element);
179: rootToParent.add((Element) changed);
180: parent = (Element) changed;
181: changed = otherNonDomainElementNodes.remove(0);
182: parentComponent = null;
183: }
184:
185: public String toString() {
186: String op = added ? "ADD: " : "REMOVE: ";
187: if (changed instanceof Element) {
188: return op + ((Element) changed).getTagName();
189: } else if (changed instanceof Attr) {
190: return op + ((Attr) changed).getNodeName() + "="
191: + ((Attr) changed).getNodeValue();
192: } else {
193: return op + changed.getNodeValue();
194: }
195: }
196: }
|