Fix #680.
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / view / bookmark / SchemaNode.java
1 package com.quantum.view.bookmark;
2
3 import java.sql.SQLException;
4 import java.util.HashMap;
5 import java.util.Iterator;
6 import java.util.Map;
7
8 import com.quantum.model.Bookmark;
9 import com.quantum.model.NotConnectedException;
10 import com.quantum.model.Schema;
11 import com.quantum.model.SchemaHolder;
12
13 /**
14  * @author BC
15  */
16 public class SchemaNode extends TreeNode implements SchemaHolder {
17
18     private Schema schema;
19     /**
20      * @param parent
21      */
22     public SchemaNode(TreeNode parent, Schema schema) {
23         super(parent);
24         this.schema = schema;
25     }
26
27     /**
28      * @see com.quantum.view.bookmark.TreeNode#getChildren()
29      */
30     public Object[] getChildren() {
31         Bookmark bookmark = getBookmark();
32         if (!bookmark.isConnected()) {
33             return BookmarkListNode.EMPTY_ARRAY;
34         } else {
35             if (this.children.isEmpty()) {
36                 initializeChildren();
37             }
38         }
39         return (TreeNode[]) this.children.toArray(new TreeNode[this.children.size()]);
40     }
41
42     protected void initializeChildren() {
43         boolean firstTimeInitialization = this.children.isEmpty();
44         boolean changed = false;
45         Map temp = new HashMap();
46         for (Iterator i = this.children.iterator(); i.hasNext();) {
47                         GroupNode element = (GroupNode) i.next();
48                         temp.put(element.getType(), element);
49                 }
50         this.children.clear();
51         
52         Bookmark bookmark = getBookmark();
53         try {
54             String[] types = bookmark.getDatabase().getEntityTypes();
55             for (int i = 0, length = (types == null) ? 0 : types.length;
56                 i < length;
57                 i++) {
58                 GroupNode node = (GroupNode) temp.remove(types[i]);
59                 if (node == null) {
60                         this.children.add(new GroupNode(this, this.schema, types[i]));
61                         changed = true;
62                 } else {
63                         this.children.add(node);
64                 }
65             }
66             for (Iterator i = temp.values().iterator(); i.hasNext();) {
67                                 ((GroupNode) i.next()).dispose();
68                                 changed = true;
69                         }
70             if (!firstTimeInitialization && changed) {
71                 firePropertyChange("children", null, null);
72             }
73         } catch (NotConnectedException e) {
74         } catch (SQLException e) {
75         }
76     }
77
78     /**
79      * @see com.quantum.view.bookmark.TreeNode#hasChildren()
80      */
81     public boolean hasChildren() {
82         return getBookmark().isConnected();
83     }
84
85     /**
86      * @see com.quantum.view.bookmark.TreeNode#getName()
87      */
88     public String getName() {
89         return this.schema.getDisplayName();
90     }
91
92     /**
93      * @see com.quantum.view.bookmark.TreeNode#getImageName()
94      */
95     protected String getImageName() {
96         return this.schema.exists() 
97                                 ? "schema.gif" 
98                                 : "missingschema.gif";
99     }
100
101     /**
102      * @see com.quantum.model.SchemaHolder#getSchema()
103      */
104     public Schema getSchema() {
105         return this.schema;
106     }
107 }