added perspective shortcuts for debug and resource to php perspective
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / view / bookmark / BookmarkNode.java
1 package com.quantum.view.bookmark;
2
3 import java.beans.PropertyChangeEvent;
4 import java.beans.PropertyChangeListener;
5 import java.sql.SQLException;
6 import java.util.HashMap;
7 import java.util.Iterator;
8 import java.util.Map;
9
10 import com.quantum.model.Bookmark;
11 import com.quantum.model.NotConnectedException;
12 import com.quantum.model.Schema;
13
14 public class BookmarkNode extends TreeNode implements PropertyChangeListener {
15     private Bookmark bookmark;
16     
17     private QuickListNode quickListNode;
18     private QueryListNode queryListNode;
19
20     public BookmarkNode(TreeNode parent, Bookmark bookmark) {
21         super(parent);
22         this.bookmark = bookmark;
23         this.bookmark.addPropertyChangeListener(this);
24     }
25
26         public Object[] getChildren() throws NotConnectedException, SQLException {
27                 if (bookmark.isConnected() && this.children.isEmpty()) {
28             initializeChildren();
29                 }
30         if (this.bookmark.isConnected()) {
31             return (TreeNode[]) this.children.toArray(new TreeNode[this.children.size()]);
32         } else {
33             return BookmarkListNode.EMPTY_ARRAY;
34         }
35         }
36
37     protected void initializeChildren() throws NotConnectedException, SQLException {
38         boolean changed = false;
39         Map temp = new HashMap();
40         for (Iterator i = this.children.iterator(); i.hasNext(); ) {
41             TreeNode node = (TreeNode) i.next();
42             if (node instanceof SchemaNode) {
43                 temp.put(node.getName(), node);
44             }
45         }
46         
47         this.children.clear();
48         if (this.quickListNode == null) {
49             this.quickListNode = new QuickListNode(this);
50         }
51         if (this.queryListNode == null) {
52             this.queryListNode = new QueryListNode(this);
53         }
54         this.children.add(this.quickListNode);
55         this.children.add(this.queryListNode);
56         Bookmark bookmark = getBookmark();
57         
58         Schema[] schemas = bookmark.getSchemas();
59         for (int i = 0, length = (schemas == null) ? 0 : schemas.length;
60             i < length;
61             i++) {
62             SchemaNode node = (SchemaNode) temp.remove(schemas[i].getDisplayName());
63             if (node == null) {
64                 this.children.add(new SchemaNode(this, schemas[i]));
65             } else {
66                 changed = true;
67                 this.children.add(node);
68             }
69         }
70         
71         for (Iterator i = temp.values().iterator(); i.hasNext(); ) {
72             ((TreeNode) i.next()).dispose();
73             changed = true;
74         }
75         
76         if (temp.size() > 0 || changed ) {
77             firePropertyChange("children", null, null);
78         }
79     }
80
81         public boolean hasChildren() {
82                 // If the bookmark is connected but hasn't loaded the tables and views, we suppose it may have some
83                 if (bookmark.isConnected() && this.children.isEmpty()) {
84             return true;
85         } else if (!bookmark.isConnected()) {
86             return false;
87         } else if (children != null && children.size() > 0) {
88                         return true;
89                 }
90                 return false;
91         }
92
93         protected void dispose() {
94         try {
95             this.bookmark.removePropertyChangeListener(this);
96             if (this.bookmark.isConnected()) {
97                 this.bookmark.disconnect();
98             }
99         } catch (SQLException e) {
100         }
101         }
102
103     /**
104      * @see com.quantum.model.TreeNode#getName()
105      */
106     public String getName() {
107         return this.bookmark == null ? "<<new>>" : this.bookmark.getName();
108     }
109
110     /**
111      * @return
112      */
113     public Bookmark getBookmark() {
114         return this.bookmark;
115     }
116
117     protected String getImageName() {
118         return this.bookmark.isConnected() ? "connected.gif" : "bookmarks.gif";
119     }
120
121     /* (non-Javadoc)
122      * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
123      */
124     public void propertyChange(PropertyChangeEvent event) {
125         if ("connected".equals(event.getPropertyName())) {
126             if (Boolean.FALSE.equals(event.getNewValue())) {
127                 removeAllChildren();
128             }
129             firePropertyChange("connected", event.getOldValue(), event.getNewValue());
130         } else if ("schemas".equals(event.getPropertyName())) {
131                 try {
132                         initializeChildren();
133                 } catch (NotConnectedException e) {
134                         this.children.clear();
135                 } catch (SQLException e) {
136                         this.children.clear();
137                 }
138             firePropertyChange("children", event.getOldValue(), event.getNewValue());
139         } else if ("name".equals(event.getPropertyName())) {
140             firePropertyChange("name", event.getOldValue(), event.getNewValue());
141         }
142     }
143
144     protected void removeAllChildren() {
145         if (this.quickListNode != null) {
146             this.quickListNode.dispose();
147             this.quickListNode = null;
148         }
149         if (this.queryListNode != null) {
150             this.queryListNode.dispose();
151             this.queryListNode = null;
152         }
153         super.removeAllChildren();
154         
155     }
156     public String getLabelDecorations(LabelDecorationInstructions labelDecorationInstructions) {
157         if (!labelDecorationInstructions.isDatabaseDataVisible()) {
158             return null;
159         } else if (!this.bookmark.isConnected()) {
160             return null;
161         } else {
162             try {
163                 String decoration = this.bookmark.getDatabase().getInformation();
164                 return decoration == null ? null : "[" + decoration + "]";
165             } catch (NotConnectedException e) {
166                 return null;
167             } catch (SQLException e) {
168                 return null;
169             }
170         }
171     }
172 }