latest quantum sources 2.3.2
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / view / bookmark / EntityNode.java
1 package com.quantum.view.bookmark;
2
3 import java.sql.SQLException;
4
5 import com.quantum.model.Column;
6 import com.quantum.model.Entity;
7 import com.quantum.model.EntityHolder;
8 import com.quantum.model.NotConnectedException;
9 import com.quantum.model.SchemaHolder;
10 import com.quantum.model.Table;
11 import com.quantum.model.View;
12
13 /**
14  * @author BC
15  */
16 public class EntityNode extends TreeNode implements EntityHolder {
17
18     private Entity entity;
19     private boolean longFormName;
20     
21     public EntityNode(TreeNode parent, Entity entity) {
22         this(parent, entity, false);
23     }
24     public EntityNode(TreeNode parent, Entity entity, boolean longFormName) {
25         super(parent);
26         this.entity = entity;
27         this.longFormName = longFormName;
28         
29         if (parent instanceof SchemaHolder) {
30             SchemaHolder schemaHolder = (SchemaHolder) parent;
31             if (!schemaHolder.getSchema().getDisplayName().equals(entity.getSchema())) {
32                 this.longFormName = true;
33             }
34         }
35     }
36
37     public Object[] getChildren() throws NotConnectedException, SQLException {
38         initializeChildren();
39         if (this.children.size() > 0) {
40             return (ColumnNode[]) this.children.toArray(new ColumnNode[this.children.size()]);
41         } else {
42             return BookmarkListNode.EMPTY_ARRAY;
43         }
44     }
45
46     protected synchronized void initializeChildren() throws NotConnectedException, SQLException {
47         this.children.clear();
48         Column[] columns = this.entity.getColumns();
49         for (int i = 0, length = (columns == null) ? 0 : columns.length;
50             i < length;
51             i++) {
52             this.children.add(new ColumnNode(this, columns[i]));
53         }
54         // TODO: fire property change event
55     }
56
57     public boolean hasChildren() {
58         if (!isSequence()) {
59             return true;
60         } else {
61             return false;
62         }
63     }
64     
65     public Entity getEntity() {
66         return this.entity;
67     }
68     
69     public String getName() {
70         return this.entity.getName();
71     }
72     
73     public String getLabelName() {
74         return (this.longFormName) ? this.entity.getQualifiedName() : this.entity.getName();
75     }
76     
77     public boolean isTable() {
78         return Entity.TABLE_TYPE.equals(this.entity.getType());
79     }
80
81     public boolean isView() {
82         return Entity.VIEW_TYPE.equals(this.entity.getType());
83     }
84
85     public boolean isSequence() {
86         return Entity.SEQUENCE_TYPE.equals(this.entity.getType());
87     }
88
89     protected String getImageName() {
90         if (isSequence()) {
91             return "sequence.gif";
92         } else if (isView()) {
93             return "view.gif";
94         } else {
95             return (this.entity.exists() == null || this.entity.exists().booleanValue()) 
96                 ? "bigtable.gif" : "missingtable.gif";
97         }
98     }
99
100     public String getLabelDecorations(LabelDecorationInstructions instructions) {
101         String decoration = null;
102         if (instructions.isSizeVisible()) {
103             Integer size = getSize();
104             if (size != null) {
105                 decoration = ((decoration == null) ? "" : decoration) 
106                     + "[" + size + "]";
107             }
108         }
109         return decoration;
110     }
111     
112     private Integer getSize() {
113         if (isTable()) {
114             return ((Table) this.entity).getSize();
115         } else if (isView()) {
116             return ((View) this.entity).getSize();
117         } else {
118             return null;
119         }
120     }
121     
122     public int compareTo(Object o) {
123         if (o instanceof EntityNode) {
124             EntityNode that = (EntityNode) o;
125             return this.entity.getQualifiedName().compareTo(
126                 that.entity.getQualifiedName());
127         } else {
128             return super.compareTo(o);
129         }
130     }
131 }