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.View;
13 import net.sourceforge.phpdt.sql.sql.MultiSQLServer;
14 import net.sourceforge.phpdt.sql.sql.metadata.ObjectMetaData;
16 public class ViewNode implements TreeNode, Comparable {
17 private ObjectMetaData metadata = null;
18 private TreeNode parent;
20 private boolean sizeVisible;
22 public ViewNode(TreeNode parent, boolean sizeVisible, View view) {
27 public Object getParent() {
31 public DatabaseAdapter getAdapter() throws NoSuchAdapterException {
32 return AdapterFactory.getInstance().getAdapter(getBookmark().getType());
35 public String getName() {
36 return this.view.getQualifiedName();
39 public String toString() {
43 public int compareTo(Object o) {
44 if (o instanceof ViewNode) {
45 ViewNode node = (ViewNode) o;
46 return getName().compareTo(node.getName());
47 } else if (o instanceof SequenceNode) {
49 } else if (o instanceof TreeNode) {
55 public int getSize() {
56 return this.view.getSize();
59 public boolean hasChildren() {
60 // If it has no metadata set, we suppose it can have some, and return true
61 if (metadata == null) return true;
62 return (ColumnMetaData.getColumnsMetaData(metadata, this) != null &&
63 ColumnMetaData.getColumnsMetaData(metadata, this).size() > 0);
66 public Object[] getChildren() {
68 if (metadata != null && ColumnMetaData.getColumnsMetaData(metadata, this) != null) {
69 return ColumnMetaData.getColumnsMetaData(metadata, this).toArray();
71 return Root.EMPTY_ARRAY;
75 public void setObjectMetadata(ObjectMetaData metadata) {
76 this.metadata = metadata;
82 public ObjectMetaData getMetaData() {
83 if (metadata == null) obtainMetaData();
88 * Checks if there is metadata present, and if not, tries to get it
90 private void obtainMetaData() {
91 Connection con = getBookmark().getConnection();
92 if (metadata == null) try {
93 metadata = MultiSQLServer.getInstance().getObjectMetadata(con, this);
94 } catch (SQLException e) {
100 * @return an Image object to appear in the view
101 * @throws MalformedURLException
103 public Image getImage() throws MalformedURLException {
104 if (parent instanceof GroupNode){
105 GroupNode group = (GroupNode) parent;
106 return group.getImage();
112 * @return the associated BookmarkNode, by navigating upwards in the tree
114 public BookmarkNode getBookmark() {
115 TreeNode node = parent;
116 while (!( node instanceof BookmarkNode))
118 node = (TreeNode) node.getParent();
120 return (BookmarkNode) node;
126 public boolean isSizeVisible() {