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