added button to start tests.
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / view / subset / SubsetContentProvider.java
1 package com.quantum.view.subset;
2
3 import java.util.Vector;
4
5 import com.quantum.Messages;
6 import com.quantum.sql.metadata.MetaDataXMLInterface;
7 import com.quantum.view.bookmark.ColumnNode;
8 import com.quantum.view.bookmark.TreeNode;
9
10 import org.eclipse.jface.viewers.ITreeContentProvider;
11 import org.eclipse.jface.viewers.Viewer;
12 import org.w3c.dom.Element;
13 import org.w3c.dom.NodeList;
14
15 public class SubsetContentProvider implements ITreeContentProvider {
16         private Vector subsets = new Vector();
17         private static SubsetContentProvider instance = null;
18         private boolean hasChanged = false;
19
20    /**
21     * Singleton accessor
22     */
23         public static synchronized SubsetContentProvider getInstance() {
24                 if (instance == null) {
25                         instance = new SubsetContentProvider();
26                 }
27                 return instance;
28         }
29
30         private SubsetContentProvider() {
31         }
32
33         public void exportXML(Element root) {
34                 System.out.println("Subsets: Saving to Element"); //$NON-NLS-1$
35                 Element subsetRoot = MetaDataXMLInterface.createElementText(root,Messages.getString("ExportXMLAction.Subsets"), ""); //$NON-NLS-1$ //$NON-NLS-2$
36                 for (int i = 0; i < subsets.size(); i++) {
37                         SubsetNode current = (SubsetNode) subsets.get(i);
38                         current.exportXML(subsetRoot);
39                 }
40         }
41         
42         /**
43          * Imports the data from an XML file to the tree. Does not set the tree to
44          * changed because it's usually invoked from the initial import routine.
45          * @param root
46          */
47         public void importXML(Element root){
48                 System.out.println("Bookmarks: Loading from Element"); //$NON-NLS-1$
49                 Vector newSubsets = new Vector();
50                 NodeList nodes = root.getElementsByTagName(Messages.getString("ExportXMLAction.Subset")); //$NON-NLS-1$
51                 for (int i = 0; i < nodes.getLength(); i++) {
52                         SubsetNode subset = new SubsetNode();
53                         Element node = (Element) nodes.item(i);
54                         subset.importXML(node,true);
55                         newSubsets.add(subset);
56                 }
57                 subsets = newSubsets;
58         }
59         
60
61         public Object[] getChildren(Object parentElement) {
62                 if (parentElement.equals(SubsetRoot.ROOT)) {
63                         return subsets.toArray();
64                 } else if (parentElement instanceof TreeNode) {
65                         TreeNode node = (TreeNode) parentElement;
66                         return node.getChildren();
67                 }
68                 return SubsetRoot.EMPTY_ARRAY;
69         }
70
71         public Object[] getElements(Object inputElement) {
72                 return getChildren(inputElement);
73         }
74
75         public Object getParent(Object element) {
76                 if (element.equals(SubsetRoot.ROOT)) {
77                         return null;
78                 } else if (element instanceof TreeNode) {
79                         TreeNode node = (TreeNode) element;
80                         return node.getParent();
81                 }
82                 return null;
83         }
84
85         public boolean hasChildren(Object element) {
86                 if (element.equals(SubsetRoot.ROOT)) {
87                         return true;
88                 } else if (element instanceof TreeNode) {
89                         TreeNode node = (TreeNode) element;
90                         return node.hasChildren();
91                 }
92                 return false;
93         }
94
95     public void setChildren(SubsetNode b, Vector tables) {
96         b.setChildren(tables);
97     }
98
99         public void addSubset(SubsetNode b) {
100                 hasChanged = true;
101                 if (!subsets.contains(b)) {
102                         subsets.addElement(b);
103                 }
104         }
105         public void removeSubset(SubsetNode b) {
106                 hasChanged = true;
107                 if (subsets.contains(b)) {
108                         subsets.removeElement(b);
109                 }
110         }
111         
112         public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
113         }
114         
115         public void dispose() {
116         }
117         
118         public boolean hasChanged() {
119                 return hasChanged;
120         }
121         
122         public int getSize() {
123                 return subsets.size();
124         }
125         
126         /**
127          * @param b
128          */
129         public void setHasChanged(boolean b) {
130                 hasChanged = b;
131         }
132
133         /**
134          * @param column
135          */
136         public void deleteColumn(ColumnNode column) {
137         // TODO: reinstate this
138 //              TreeNode node = (TreeNode) column.getParent();
139 //              if (!(node instanceof HasMetaData)) return;
140 //              ObjectMetaData metadata = ((HasMetaData)node).getMetaData();
141 //              if (metadata == null) return;
142 //              metadata.dropColumn(column.getName());
143 //              setHasChanged(true);
144 //              
145         }
146
147         /**
148          * @param object
149          */
150         public void deleteObject(ObjectNode object) {
151                 SubsetNode node = (SubsetNode) object.getParent();
152                 if (!(node instanceof SubsetNode)) return;
153                 node.remove(object);
154                 setHasChanged(true);
155                 
156         }
157
158 }