KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xam > dom > ChangeInfo


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.xam.dom;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.List JavaDoc;
25 import org.w3c.dom.Attr JavaDoc;
26 import org.w3c.dom.Element JavaDoc;
27 import org.w3c.dom.Node JavaDoc;
28
29 /**
30  *
31  * @author Nam Nguyen
32  */

33 public class ChangeInfo {
34     
35         private Node JavaDoc changed;
36         private Element JavaDoc parent;
37         private DocumentComponent parentComponent;
38         private boolean domainElement;
39         private boolean added;
40         private List JavaDoc<Element JavaDoc> rootToParent;
41         /**
42          * List of non-domain element nodes beside the changed node.
43          * The order is increasing distance from root.
44          */

45         private List JavaDoc<Node JavaDoc> otherNonDomainElementNodes;
46         
47         /**
48          * Creates change info.
49          *
50          * @param parent parent node of changed
51          * @param changed added/removed domain element or first non-domain change node.
52          * @param isDomainElement is the changed node a domain element.
53          * @param rootToParent path from root to parent node, inclusively.
54          * @param otherNodes list of other nodes that are not domain elements beside the changed nodes.
55          */

56         public ChangeInfo(Element JavaDoc parent, Node JavaDoc changed, boolean isDomainElement, List JavaDoc<Element JavaDoc> rootToParent, List JavaDoc<Node JavaDoc> otherNodes) {
57             this.parent = parent;
58             this.changed = changed;
59             domainElement = isDomainElement;
60             if (! domainElement) {
61                 otherNonDomainElementNodes = otherNodes;
62             }
63             this.rootToParent = rootToParent;
64         }
65         public Element JavaDoc getParent() { return parent; }
66         public Node JavaDoc getChangedNode() { return changed; }
67         public Element JavaDoc getChangedElement() {
68             if (changed instanceof Element JavaDoc) {
69                 return (Element JavaDoc) changed;
70             }
71             return null;
72         }
73         public boolean isDomainElement() { return domainElement; }
74         public void setDomainElement(boolean v) { domainElement = v; }
75         public void setRootToParentPath(List JavaDoc<Element JavaDoc> path) {
76             rootToParent = path;
77         }
78         public List JavaDoc<Element JavaDoc> getRootToParentPath() {
79             return rootToParent;
80         }
81         public List JavaDoc<Element JavaDoc> getParentToRootPath() {
82             ArrayList JavaDoc<Element JavaDoc> ret = new ArrayList JavaDoc(rootToParent);
83             Collections.reverse(ret);
84             return ret;
85         }
86         public boolean isDomainElementAdded() {
87             return domainElement && added;
88         }
89         public void setAdded(boolean v) {
90             added = v;
91         }
92         public boolean isAdded() {
93             return added;
94         }
95         public void markParentAsChanged() {
96             assert parent != null;
97             changed = parent;
98
99             assert rootToParent.size() > 1;
100             assert parent == rootToParent.get(rootToParent.size()-1);
101             rootToParent.remove(rootToParent.size()-1);
102             parent = rootToParent.get(rootToParent.size()-1);
103         }
104         public void setParentComponent(DocumentComponent component) {
105             parentComponent = component;
106         }
107         public DocumentComponent getParentComponent() {
108             return parentComponent;
109         }
110         public List JavaDoc<Node JavaDoc> getOtherNonDomainElementNodes() {
111             return otherNonDomainElementNodes;
112         }
113         public Node JavaDoc getActualChangedNode() {
114             if (isDomainElement()) {
115                 return changed;
116             } else {
117                 if (otherNonDomainElementNodes == null || otherNonDomainElementNodes.isEmpty()) {
118                     return changed;
119                 } else {
120                     return otherNonDomainElementNodes.get(otherNonDomainElementNodes.size()-1);
121                 }
122             }
123         }
124         public void markNonDomainChildAsChanged() {
125             assert otherNonDomainElementNodes != null && otherNonDomainElementNodes.size() > 0;
126             assert(changed instanceof Element JavaDoc);
127             rootToParent.add((Element JavaDoc) changed);
128             parent = (Element JavaDoc) changed;
129             changed = otherNonDomainElementNodes.remove(0);
130             parentComponent = null;
131         }
132         
133         public String JavaDoc toString() {
134             String JavaDoc op = added ? "ADD: " : "REMOVE: ";
135             if (changed instanceof Element JavaDoc) {
136                 return op + ((Element JavaDoc)changed).getTagName();
137             } else if (changed instanceof Attr JavaDoc) {
138                 return op + ((Attr JavaDoc)changed).getNodeName()+"="+((Attr JavaDoc)changed).getNodeValue();
139             } else {
140                 return op + changed.getNodeValue();
141             }
142         }
143 }
Popular Tags