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