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