1 package com.quantum.view.bookmark;
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;
10 import com.quantum.model.Bookmark;
11 import com.quantum.model.ConnectionException;
12 import com.quantum.model.NotConnectedException;
13 import com.quantum.model.Schema;
15 public class BookmarkNode extends TreeNode implements PropertyChangeListener {
16 private Bookmark bookmark;
18 private QuickListNode quickListNode;
19 private QueryListNode queryListNode;
21 public BookmarkNode(TreeNode parent, Bookmark bookmark) {
23 this.bookmark = bookmark;
24 this.bookmark.addPropertyChangeListener(this);
27 public Object[] getChildren() throws NotConnectedException, SQLException {
28 if (bookmark.isConnected() && this.children.isEmpty()) {
31 if (this.bookmark.isConnected()) {
32 return (TreeNode[]) this.children.toArray(new TreeNode[this.children.size()]);
34 return BookmarkListNode.EMPTY_ARRAY;
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);
48 this.children.clear();
49 if (this.quickListNode == null) {
50 this.quickListNode = new QuickListNode(this);
52 if (this.queryListNode == null) {
53 this.queryListNode = new QueryListNode(this);
55 this.children.add(this.quickListNode);
56 this.children.add(this.queryListNode);
57 Bookmark bookmark = getBookmark();
59 Schema[] schemas = bookmark.getSchemas();
60 for (int i = 0, length = (schemas == null) ? 0 : schemas.length;
63 SchemaNode node = (SchemaNode) temp.remove(schemas[i].getDisplayName());
65 this.children.add(new SchemaNode(this, schemas[i]));
68 this.children.add(node);
72 for (Iterator i = temp.values().iterator(); i.hasNext(); ) {
73 ((TreeNode) i.next()).dispose();
77 if (temp.size() > 0 || changed ) {
78 firePropertyChange("children", null, null);
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()) {
86 } else if (!bookmark.isConnected()) {
88 } else if (children != null && children.size() > 0) {
94 protected void dispose() {
96 this.bookmark.removePropertyChangeListener(this);
97 if (this.bookmark.isConnected()) {
98 this.bookmark.disconnect();
100 } catch (ConnectionException e) {
105 * @see com.quantum.model.TreeNode#getName()
107 public String getName() {
108 return this.bookmark == null ? "<<new>>" : this.bookmark.getName();
114 public Bookmark getBookmark() {
115 return this.bookmark;
118 protected String getImageName() {
119 return this.bookmark.isConnected() ? "connected.gif" : "bookmarks.gif";
123 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
125 public void propertyChange(PropertyChangeEvent event) {
126 if ("connected".equals(event.getPropertyName())) {
127 if (Boolean.FALSE.equals(event.getNewValue())) {
130 firePropertyChange("connected", event.getOldValue(), event.getNewValue());
131 } else if ("schemas".equals(event.getPropertyName())) {
133 initializeChildren();
134 } catch (NotConnectedException e) {
135 this.children.clear();
136 } catch (SQLException e) {
137 this.children.clear();
139 firePropertyChange("children", event.getOldValue(), event.getNewValue());
140 } else if ("name".equals(event.getPropertyName())) {
141 firePropertyChange("name", event.getOldValue(), event.getNewValue());
145 protected void removeAllChildren() {
146 if (this.quickListNode != null) {
147 this.quickListNode.dispose();
148 this.quickListNode = null;
150 if (this.queryListNode != null) {
151 this.queryListNode.dispose();
152 this.queryListNode = null;
154 super.removeAllChildren();
157 public String getLabelDecorations(LabelDecorationInstructions labelDecorationInstructions) {
158 if (!labelDecorationInstructions.isDatabaseDataVisible()) {
160 } else if (!this.bookmark.isConnected()) {
164 String decoration = this.bookmark.getDatabase().getInformation();
165 return decoration == null ? null : "[" + decoration + "]";
166 } catch (NotConnectedException e) {
168 } catch (SQLException e) {