fixed Bug 1024299
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / view / bookmark / GroupNode.java
1 package com.quantum.view.bookmark;
2
3 import java.sql.SQLException;
4 import java.util.Collections;
5 import java.util.HashMap;
6 import java.util.Iterator;
7 import java.util.Map;
8
9 import com.quantum.Messages;
10 import com.quantum.model.Bookmark;
11 import com.quantum.model.Entity;
12 import com.quantum.model.Schema;
13 import com.quantum.model.SchemaHolder;
14
15 /**
16  * GroupNode represents a level of grouping in the BookmarkView hierarchy
17  * It will have categories like "TABLE", "VIEW" and so on, usually gotten from
18  * the JDBC driver.
19  * 
20  * @author panic
21  */
22 public class GroupNode extends TreeNode implements Comparable, SchemaHolder {
23         private String type = null;
24     private Schema schema = null;
25     private boolean initialized = false;
26
27         public GroupNode(TreeNode parent, Schema schema, String type) {
28         super(parent);
29         this.schema = schema;
30                 this.type = type;
31         }
32         
33         protected boolean isInitialized() {
34                 return this.initialized;
35         }
36         public boolean hasChildren() {
37                 if (!isInitialized()) {
38                         return true;
39                 } else {
40             return !this.children.isEmpty();
41                 }
42         }
43         public Object[] getChildren() {
44         if (!isInitialized() && getBookmark().isConnected()) {
45             initializeChildren();
46         }
47                 return (TreeNode[]) this.children.toArray(new TreeNode[this.children.size()]);
48         }
49     protected void initializeChildren() {
50         try {
51             boolean firstTimeInitialization = !isInitialized();
52             Map temp = new HashMap();
53             for (Iterator i = this.children.iterator(); i.hasNext();) {
54                 TreeNode treeNode = (TreeNode) i.next();
55                 temp.put(treeNode.getName(), treeNode);
56             }
57             this.children.clear();
58             
59             Bookmark bookmark = getBookmark();
60             Entity[] entities = bookmark.getEntitiesForSchema(schema, type);
61             for (int i = 0,
62                 length = (entities == null) ? 0 : entities.length;
63                 i < length;
64                 i++) {
65                 
66                 String name = entities[i].getName();
67                 EntityNode entityNode = (EntityNode) temp.remove(name);
68                 if (entityNode == null) {
69                     this.children.add(new EntityNode(this, entities[i]));
70                 } else {
71                         entityNode.setEntity(entities[i]);
72                     this.children.add(entityNode);
73                 }
74             }
75             for (Iterator i = temp.values().iterator(); i.hasNext();) {
76                                 ((TreeNode) i.next()).dispose();
77                         }
78             
79             Collections.sort(this.children);
80             if (!firstTimeInitialization) {
81                 firePropertyChange("children", null, null);
82             }
83             
84             this.initialized = true;
85         } catch (SQLException e) {
86         }
87     }
88         public String getName() {
89                 return Messages.getString(getClass().getName() + "." + this.type);
90         }
91         protected String getImageName() {
92                 return "entitygroup.gif"; //$NON-NLS-1$
93         }
94     /**
95      * @return
96      */
97     public Schema getSchema() {
98         return schema;
99     }
100         public String getType() {
101                 return this.type;
102         }
103 }