package net.sourceforge.phpdt.sql.view.bookmark; import java.net.MalformedURLException; import java.sql.Connection; import java.sql.SQLException; import org.eclipse.swt.graphics.Image; import net.sourceforge.phpdt.sql.adapters.AdapterFactory; import net.sourceforge.phpdt.sql.adapters.DatabaseAdapter; import net.sourceforge.phpdt.sql.adapters.NoSuchAdapterException; import net.sourceforge.phpdt.sql.model.Table; import net.sourceforge.phpdt.sql.sql.MultiSQLServer; import net.sourceforge.phpdt.sql.sql.metadata.ObjectMetaData; public class TableNode implements TreeNode, Comparable { private ObjectMetaData metadata = null; private TreeNode parent = null; private Table table; private boolean sizeVisible; public TableNode(TreeNode parent, boolean sizeVisible, Table table) { this.parent = parent; this.table = table; this.sizeVisible = sizeVisible; } public Object[] getChildren() { obtainMetaData(); if (metadata != null && ColumnMetaData.getColumnsMetaData(metadata, this) != null) { return ColumnMetaData.getColumnsMetaData(metadata, this).toArray(); } else { return Root.EMPTY_ARRAY; } } private void obtainMetaData() { Connection con = getBookmark().getConnection(); if (metadata == null) try { metadata = MultiSQLServer.getInstance().getObjectMetadata(con, this); } catch (SQLException e) { metadata = null; e.printStackTrace(); } } public Object getParent() { return parent; } public DatabaseAdapter getAdapter() throws NoSuchAdapterException { return AdapterFactory.getInstance().getAdapter(getBookmark().getType()); } public boolean hasChildren() { // If it has no metadata set, we suppose it can have some, and return true anyway if (metadata == null) return true; return (ColumnMetaData.getColumnsMetaData(metadata, this) != null && ColumnMetaData.getColumnsMetaData(metadata, this).size() > 0); } public String getName() { return this.table.getQualifiedName(); } public String toString() { return getName(); } public int compareTo(Object o) { if (o instanceof TableNode) { TableNode node = (TableNode) o; return getName().compareTo(node.getName()); } else if (o instanceof ViewNode || o instanceof SequenceNode) { return -1; } return 0; } public int getSize() { return this.table.getSize(); } public void setObjectMetadata(ObjectMetaData metadata) { this.metadata = metadata; } /** * @return */ public ObjectMetaData getMetaData() { if (metadata == null) obtainMetaData(); return metadata; } /** * @return an Image object to appear in the view * @throws MalformedURLException */ public Image getImage() throws MalformedURLException { if (parent instanceof GroupNode){ GroupNode group = (GroupNode) parent; return group.getImage(); } else return null; } /** * @return the associated BookmarkNode, by navigating upwards in the tree */ public BookmarkNode getBookmark() { TreeNode node = parent; while (!( node instanceof BookmarkNode)) { node = (TreeNode) node.getParent(); } return (BookmarkNode) node; } /** * @return */ public boolean isSizeVisible() { return this.sizeVisible; } public Table getTable() { return this.table; } }