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() {
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() {
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())) {
132 initializeChildren();
133 firePropertyChange("children", event.getOldValue(), event.getNewValue());
134 } else if ("name".equals(event.getPropertyName())) {
135 firePropertyChange("name", event.getOldValue(), event.getNewValue());
139 protected void removeAllChildren() {
140 if (this.quickListNode != null) {
141 this.quickListNode.dispose();
142 this.quickListNode = null;
144 if (this.queryListNode != null) {
145 this.queryListNode.dispose();
146 this.queryListNode = null;
148 super.removeAllChildren();
151 public String getLabelDecorations(LabelDecorationInstructions labelDecorationInstructions) {
152 if (!labelDecorationInstructions.isDatabaseDataVisible()) {
154 } else if (!this.bookmark.isConnected()) {
158 String decoration = this.bookmark.getDatabase().getInformation();
159 return decoration == null ? null : "[" + decoration + "]";
160 } catch (NotConnectedException e) {
162 } catch (SQLException e) {