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
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.View;
13 import net.sourceforge.phpdt.sql.sql.MultiSQLServer;
14 import net.sourceforge.phpdt.sql.sql.metadata.ObjectMetaData;
15
16 public class ViewNode implements TreeNode, Comparable  {
17         private ObjectMetaData metadata = null;
18         private TreeNode parent;
19     private View view;
20     private boolean sizeVisible;
21         
22         public ViewNode(TreeNode parent, boolean sizeVisible, View view) {
23                 this.parent = parent;
24                 this.view = view;
25         }
26
27         public Object getParent() {
28                 return parent;
29         }
30
31         public DatabaseAdapter getAdapter()  throws NoSuchAdapterException {
32                         return AdapterFactory.getInstance().getAdapter(getBookmark().getType());
33         }
34
35         public String getName() {
36                 return this.view.getQualifiedName();
37         }
38         
39         public String toString() {
40                 return getName();
41         }
42
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) {
48                         return -1;
49                 } else if (o instanceof TreeNode) {
50                         return 1;
51                 }
52                 return 0;
53         }
54         
55         public int getSize() {
56                 return this.view.getSize();
57         }
58
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);
64         }
65
66         public Object[] getChildren() {
67                 obtainMetaData();
68                 if (metadata != null && ColumnMetaData.getColumnsMetaData(metadata, this) != null) {
69                         return ColumnMetaData.getColumnsMetaData(metadata, this).toArray();
70                 } else {
71                         return Root.EMPTY_ARRAY;
72                 }
73         }
74         
75         public void setObjectMetadata(ObjectMetaData metadata) {
76                 this.metadata = metadata;
77         }
78         
79         /**
80          * @return
81          */
82         public ObjectMetaData getMetaData() {
83                 if (metadata == null) obtainMetaData();
84                 return metadata;
85         }
86
87         /**
88          * Checks if there is metadata present, and if not, tries to get it
89          */
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) {
95                         metadata = null;
96                         e.printStackTrace();
97                 } 
98         }
99         /**
100          * @return an Image object to appear in the view
101          * @throws MalformedURLException
102          */
103         public Image getImage() throws MalformedURLException {
104                 if (parent instanceof GroupNode){
105                         GroupNode group = (GroupNode) parent;
106                         return group.getImage();
107                 } else
108                         return null;
109         }
110
111         /**
112          * @return the associated BookmarkNode, by navigating upwards in the tree 
113          */
114         public BookmarkNode getBookmark() {
115                 TreeNode node = parent;
116                 while (!( node instanceof BookmarkNode))
117                 {
118                         node = (TreeNode) node.getParent();
119                 }
120                 return (BookmarkNode) node;
121         }
122
123     /**
124      * @return
125      */
126     public boolean isSizeVisible() {
127         return sizeVisible;
128     }
129
130 }