1) Added missing strings for italic, underline and strike through.
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.sql / src / net / sourceforge / phpdt / sql / view / bookmark / TableNode.java
1 package net.sourceforge.phpdt.sql.view.bookmark;
2
3 import java.net.MalformedURLException;
4 import java.sql.Connection;
5 import java.sql.SQLException;
6
7 import org.eclipse.swt.graphics.Image;
8
9 import net.sourceforge.phpdt.sql.adapters.AdapterFactory;
10 import net.sourceforge.phpdt.sql.adapters.DatabaseAdapter;
11 import net.sourceforge.phpdt.sql.adapters.NoSuchAdapterException;
12 import net.sourceforge.phpdt.sql.model.Table;
13 import net.sourceforge.phpdt.sql.sql.MultiSQLServer;
14 import net.sourceforge.phpdt.sql.sql.metadata.ObjectMetaData;
15
16 public class TableNode implements TreeNode, Comparable {
17         private ObjectMetaData metadata = null;
18         private TreeNode parent = null;
19     private Table table;
20     private boolean sizeVisible;
21         
22         public TableNode(TreeNode parent, boolean sizeVisible, Table table) {
23                 this.parent = parent;
24                 this.table = table;
25         this.sizeVisible = sizeVisible;
26         }
27
28         public Object[] getChildren() {
29                 obtainMetaData();
30                 if (metadata != null && ColumnMetaData.getColumnsMetaData(metadata, this) != null) {
31                         return ColumnMetaData.getColumnsMetaData(metadata, this).toArray();
32                 } else {
33                         return Root.EMPTY_ARRAY;
34                 }
35         }
36
37         private void obtainMetaData() {
38                 Connection con = getBookmark().getConnection();
39                 if (metadata == null) try {
40                         metadata = MultiSQLServer.getInstance().getObjectMetadata(con, this);
41                 } catch (SQLException e) {
42                         metadata = null;
43                         e.printStackTrace();
44                 } 
45         }
46
47         public Object getParent() {
48                 return parent;
49         }
50
51         public DatabaseAdapter getAdapter() throws NoSuchAdapterException {
52                         return AdapterFactory.getInstance().getAdapter(getBookmark().getType());
53         }
54
55         public boolean hasChildren() {
56                 // If it has no metadata set, we suppose it can have some, and return true anyway
57                 if (metadata == null) return true;
58                 return (ColumnMetaData.getColumnsMetaData(metadata, this) != null && 
59                                 ColumnMetaData.getColumnsMetaData(metadata, this).size() > 0);
60         }
61         
62         public String getName() {
63                 return this.table.getQualifiedName();
64         }
65         
66         public String toString() {
67                 return getName();
68         }
69
70         public int compareTo(Object o) {
71                 if (o instanceof TableNode) {
72                         TableNode node = (TableNode) o;
73                         return getName().compareTo(node.getName());
74                 } else if (o instanceof ViewNode ||
75                   o instanceof SequenceNode) {
76                         return -1;
77                 }
78                 return 0;
79         }
80         
81         public int getSize() {
82         return this.table.getSize();
83         }
84
85         public void setObjectMetadata(ObjectMetaData metadata) {
86                 this.metadata = metadata;
87         }
88         /**
89          * @return
90          */
91         public ObjectMetaData getMetaData() {
92                 if (metadata == null) obtainMetaData();
93                 return metadata;
94         }
95         /**
96          * @return an Image object to appear in the view
97          * @throws MalformedURLException
98          */
99         public Image getImage() throws MalformedURLException {
100                 if (parent instanceof GroupNode){
101                         GroupNode group = (GroupNode) parent;
102                         return group.getImage();
103                 } else
104                         return null;
105         }
106
107         /**
108          * @return the associated BookmarkNode, by navigating upwards in the tree 
109          */
110         public BookmarkNode getBookmark() {
111                 TreeNode node = parent;
112                 while (!( node instanceof BookmarkNode))
113                 {
114                         node = (TreeNode) node.getParent();
115                 }
116                 return (BookmarkNode) node;
117         }
118     /**
119      * @return
120      */
121     public boolean isSizeVisible() {
122         return this.sizeVisible;
123     }
124     
125     public Table getTable() {
126         return this.table;
127     }
128 }