1 package net.sourceforge.phpdt.sql.view.bookmark;
3 import java.net.MalformedURLException;
4 import java.sql.Connection;
5 import java.sql.SQLException;
7 import org.eclipse.swt.graphics.Image;
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;
16 public class TableNode implements TreeNode, Comparable {
17 private ObjectMetaData metadata = null;
18 private TreeNode parent = null;
20 private boolean sizeVisible;
22 public TableNode(TreeNode parent, boolean sizeVisible, Table table) {
25 this.sizeVisible = sizeVisible;
28 public Object[] getChildren() {
30 if (metadata != null && ColumnMetaData.getColumnsMetaData(metadata, this) != null) {
31 return ColumnMetaData.getColumnsMetaData(metadata, this).toArray();
33 return Root.EMPTY_ARRAY;
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) {
47 public Object getParent() {
51 public DatabaseAdapter getAdapter() throws NoSuchAdapterException {
52 return AdapterFactory.getInstance().getAdapter(getBookmark().getType());
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);
62 public String getName() {
63 return this.table.getQualifiedName();
66 public String toString() {
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) {
81 public int getSize() {
82 return this.table.getSize();
85 public void setObjectMetadata(ObjectMetaData metadata) {
86 this.metadata = metadata;
91 public ObjectMetaData getMetaData() {
92 if (metadata == null) obtainMetaData();
96 * @return an Image object to appear in the view
97 * @throws MalformedURLException
99 public Image getImage() throws MalformedURLException {
100 if (parent instanceof GroupNode){
101 GroupNode group = (GroupNode) parent;
102 return group.getImage();
108 * @return the associated BookmarkNode, by navigating upwards in the tree
110 public BookmarkNode getBookmark() {
111 TreeNode node = parent;
112 while (!( node instanceof BookmarkNode))
114 node = (TreeNode) node.getParent();
116 return (BookmarkNode) node;
121 public boolean isSizeVisible() {
122 return this.sizeVisible;
125 public Table getTable() {