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