974dcd761e5e81da67c10f560dca1039ccecb426
[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.Collections;
9 import java.util.HashMap;
10 import java.util.Iterator;
11 import java.util.Map;
12
13 import com.quantum.Messages;
14 import com.quantum.model.Bookmark;
15 import com.quantum.model.Entity;
16 import com.quantum.model.Schema;
17 import com.quantum.model.SchemaHolder;
18
19 /**
20  * GroupNode represents a level of grouping in the BookmarkView hierarchy
21  * It will have categories like "TABLE", "VIEW" and so on, usually gotten from
22  * the JDBC driver.
23  * 
24  * @author panic
25  */
26 public class GroupNode extends TreeNode implements Comparable, SchemaHolder {
27         private String type = null;
28     private Schema schema = null;
29
30         public GroupNode(TreeNode parent, Schema schema, String type) {
31         super(parent);
32         this.schema = schema;
33                 this.type = type;
34         }
35         public boolean hasChildren() {
36                 if (!isInitialized()) {
37                         return true;
38                 } else {
39             return !this.children.isEmpty();
40                 }
41         }
42         public Object[] getChildren() {
43         if (!isInitialized() && getBookmark().isConnected()) {
44             initializeChildren();
45         }
46                 return (TreeNode[]) this.children.toArray(new TreeNode[this.children.size()]);
47         }
48     protected void initializeChildren() {
49         try {
50             boolean firstTimeInitialization = !isInitialized();
51             boolean changed = false;
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                     changed = true;
71                 } else {
72                     this.children.add(entityNode);
73                 }
74             }
75             Collections.sort(this.children);
76             if ((temp.size() > 0 || changed ) && !firstTimeInitialization) {
77                 firePropertyChange("children", null, null);
78             }
79         } catch (SQLException e) {
80         }
81     }
82         public String getName() {
83                 return Messages.getString(getClass().getName() + "." + this.type);
84         }
85         protected String getImageName() {
86                 return "entitygroup.gif"; //$NON-NLS-1$
87         }
88     /**
89      * @return
90      */
91     public Schema getSchema() {
92         return schema;
93     }
94 }