1) Added missing strings for italic, underline and strike through.
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.sql / src / net / sourceforge / phpdt / sql / view / bookmark / ViewNode.java
index 2398e1d..d178256 100644 (file)
@@ -1,33 +1,49 @@
 package net.sourceforge.phpdt.sql.view.bookmark;
 
-import java.util.Vector;
+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.View;
+import net.sourceforge.phpdt.sql.sql.MultiSQLServer;
+import net.sourceforge.phpdt.sql.sql.metadata.ObjectMetaData;
 
 public class ViewNode implements TreeNode, Comparable  {
-    private BookmarkNode parent;
-    private Vector metadata;
-    private String name;
-    private int size = -1;
-    public ViewNode(BookmarkNode parent, String tableName) {
-       this.parent = parent;
-       this.name = tableName;
-    }
+       private ObjectMetaData metadata = null;
+       private TreeNode parent;
+    private View view;
+    private boolean sizeVisible;
+       
+       public ViewNode(TreeNode parent, boolean sizeVisible, View view) {
+               this.parent = parent;
+               this.view = view;
+       }
 
        public Object getParent() {
                return parent;
        }
 
+       public DatabaseAdapter getAdapter()  throws NoSuchAdapterException {
+                       return AdapterFactory.getInstance().getAdapter(getBookmark().getType());
+       }
+
        public String getName() {
-               return name;
+               return this.view.getQualifiedName();
        }
        
        public String toString() {
-               return name;
+               return getName();
        }
 
        public int compareTo(Object o) {
                if (o instanceof ViewNode) {
                        ViewNode node = (ViewNode) o;
-                       return name.compareTo(node.getName());
+                       return getName().compareTo(node.getName());
                } else if (o instanceof SequenceNode) {
                        return -1;
                } else if (o instanceof TreeNode) {
@@ -37,26 +53,78 @@ public class ViewNode implements TreeNode, Comparable  {
        }
        
        public int getSize() {
-               return size;
-       }
-
-       public void setSize(int size) {
-               this.size = size;
+               return this.view.getSize();
        }
 
        public boolean hasChildren() {
-               return (metadata != null) && (metadata.size() > 0);
+               // If it has no metadata set, we suppose it can have some, and return true
+               if (metadata == null) return true;
+               return (ColumnMetaData.getColumnsMetaData(metadata, this) != null && 
+                               ColumnMetaData.getColumnsMetaData(metadata, this).size() > 0);
        }
 
        public Object[] getChildren() {
-               if (metadata != null) {
-                       return metadata.toArray();
+               obtainMetaData();
+               if (metadata != null && ColumnMetaData.getColumnsMetaData(metadata, this) != null) {
+                       return ColumnMetaData.getColumnsMetaData(metadata, this).toArray();
                } else {
                        return Root.EMPTY_ARRAY;
                }
        }
        
-       public void setMetadata(Vector metadata) {
+       public void setObjectMetadata(ObjectMetaData metadata) {
                this.metadata = metadata;
        }
+       
+       /**
+        * @return
+        */
+       public ObjectMetaData getMetaData() {
+               if (metadata == null) obtainMetaData();
+               return metadata;
+       }
+
+       /**
+        * Checks if there is metadata present, and if not, tries to get it
+        */
+       private void obtainMetaData() {
+               Connection con = getBookmark().getConnection();
+               if (metadata == null) try {
+                       metadata = MultiSQLServer.getInstance().getObjectMetadata(con, this);
+               } catch (SQLException e) {
+                       metadata = null;
+                       e.printStackTrace();
+               } 
+       }
+       /**
+        * @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 sizeVisible;
+    }
+
 }