Changed the UI Image handling.
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / view / subset / SubsetNode.java
1 package com.quantum.view.subset;
2
3 import java.util.Collections;
4 import java.util.Vector;
5
6 import com.quantum.Messages;
7 import com.quantum.sql.metadata.MetaDataXMLInterface;
8 import com.quantum.view.bookmark.TreeNode;
9
10 import org.w3c.dom.Element;
11 import org.w3c.dom.NodeList;
12
13 public class SubsetNode extends TreeNode {
14         private String name = null;
15     private Vector children = new Vector();
16
17         public SubsetNode(SubsetNode param) {
18         super(param.getParent());
19                 name = param.name;              
20                 children.addAll(param.children);
21         }
22         
23     public SubsetNode() {
24         super(SubsetRoot.ROOT);
25     }
26
27     public SubsetNode(String name) {
28         super(SubsetRoot.ROOT);
29         this.name = name;
30     }
31     
32         public void setName(String name) {
33                 this.name = name;
34         }
35         
36     public String getName(){
37         return name;
38     }
39
40         public Object[] getChildren() {
41                 return children.toArray();
42         }
43
44         public boolean hasChildren() {
45                 if (children != null && children.size() > 0) {
46                         return true;
47                 }
48                 return false;
49         }
50
51         public void setChildren(Vector children) {
52                 for (int i = 0; i <  children.size(); i++) {
53                         Object obj = children.elementAt(i);
54                         isValid(obj);
55                 }
56                 Collections.sort(children);
57                 this.children = children;
58         }
59         
60         public void isValid(Object child) {
61                 boolean valid = false;
62                 if (child instanceof ObjectNode ) {
63                         valid = true;
64                 }
65                 if (!valid) {
66                         throw new RuntimeException("Invalid SubsetNode child: " + child.getClass().getName()); //$NON-NLS-1$
67                 }
68         }
69         
70         
71         /**
72          * Finds a child of the SubsetNode with the said name 
73          * @param name
74          * @return the TreeNode found. null if none
75          */
76         public TreeNode find(String name){
77                 for (int i = 0; i <  children.size(); i++) {
78                         Object obj = children.elementAt(i);
79                         if (obj instanceof TreeNode){
80                                 TreeNode node = (TreeNode) obj;
81                                 if (name.equals(node.getName())) return node;                   
82                         }
83                 }
84                 return null;
85         }
86         
87         public boolean add(ObjectNode object){
88                 if (children.indexOf(object) >= 0) return false;
89                 children.add(object);
90                 return true;
91         }
92
93         public boolean addReplace(ObjectNode object){
94                 children.remove(object);
95                 return add(object);
96         }
97         
98         public boolean remove(ObjectNode object){
99                 return children.remove(object);
100         }
101         
102         public void exportXML(Element root) {
103                 Element sub = MetaDataXMLInterface.createElementText(root,Messages.getString("ExportXMLAction.Subset"), ""); //$NON-NLS-1$ //$NON-NLS-2$
104                 MetaDataXMLInterface.createElementText(sub,Messages.getString("ExportXMLAction.SubsetName"), getName()); //$NON-NLS-1$
105                 for (int i = 0; i <  children.size(); i++) {
106                         Object obj = children.elementAt(i);
107                         if (obj instanceof ObjectNode){
108                                 ((ObjectNode) obj).exportXML(sub);      
109                         }
110                 }
111                 
112         }
113
114         /**
115          * Imports a set of XML tags (parsed into a XMLDocument) into a Subset
116          * @param root  Document to get the data from
117          * @param replace       True if you want to replace already-existing elements with the same name, false if not
118          */
119         public void importXML(Element root, boolean replace) {
120                 if (replace) {
121                         String name = MetaDataXMLInterface.getElementText(root, Messages.getString("ExportXMLAction.SubsetName")); //$NON-NLS-1$
122                         if (name == "") return;  //$NON-NLS-1$
123                         setName(name);
124                 }
125                 importElementXML(root, replace, Messages.getString("ExportXMLAction.Table")); //$NON-NLS-1$
126                 importElementXML(root, replace, Messages.getString("ExportXMLAction.View")); //$NON-NLS-1$
127
128         }
129
130
131         /**
132          * 
133          * Imports one type of element from a XMLDocument (possibly a part of one) into a Subset
134          * @param root Document to get the data from
135          * @param replace True if you want to replace already-existing elements with the same name, false if not
136          * @param type The type of element to import, has to correspond with the XML tag
137          */
138         private void importElementXML(Element root, boolean replace, String type) {
139                 // We get all the tags named as the type
140                 NodeList tables = root.getElementsByTagName(type); 
141                 for (int i = 0; i < tables.getLength(); i++) {
142                         Element table = (Element) tables.item(i);
143                         ObjectNode objectNode = ObjectNode.importXML(table, this);
144                         if (replace)addReplace(objectNode);
145                         else add(objectNode);   
146                 }
147         }
148
149     /* (non-Javadoc)
150      * @see com.quantum.view.bookmark.TreeNode#getImageName()
151      */
152     protected String getImageName() {
153         return "subset.gif";
154     }
155
156     /* (non-Javadoc)
157      * @see com.quantum.view.bookmark.TreeNode#initializeChildren()
158      */
159     protected void initializeChildren() {
160     }
161
162 }