reduce warnings due to core.runtime deprecations
[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() {
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() {
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             initializeChildren();
133             firePropertyChange("children", event.getOldValue(), event.getNewValue());
134         } else if ("name".equals(event.getPropertyName())) {
135             firePropertyChange("name", event.getOldValue(), event.getNewValue());
136         }
137     }
138
139     protected void removeAllChildren() {
140         if (this.quickListNode != null) {
141             this.quickListNode.dispose();
142             this.quickListNode = null;
143         }
144         if (this.queryListNode != null) {
145             this.queryListNode.dispose();
146             this.queryListNode = null;
147         }
148         super.removeAllChildren();
149         
150     }
151     public String getLabelDecorations(LabelDecorationInstructions labelDecorationInstructions) {
152         if (!labelDecorationInstructions.isDatabaseDataVisible()) {
153             return null;
154         } else if (!this.bookmark.isConnected()) {
155             return null;
156         } else {
157             try {
158                 String decoration = this.bookmark.getDatabase().getInformation();
159                 return decoration == null ? null : "[" + decoration + "]";
160             } catch (NotConnectedException e) {
161                 return null;
162             } catch (SQLException e) {
163                 return null;
164             }
165         }
166     }
167 }