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.NotConnectedException;
12 import com.quantum.model.Schema;
14 public class BookmarkNode extends TreeNode implements PropertyChangeListener {
15 private Bookmark bookmark;
17 private QuickListNode quickListNode;
18 private QueryListNode queryListNode;
20 public BookmarkNode(TreeNode parent, Bookmark bookmark) {
22 this.bookmark = bookmark;
23 this.bookmark.addPropertyChangeListener(this);
26 public Object[] getChildren() throws NotConnectedException, SQLException {
27 if (bookmark.isConnected() && this.children.isEmpty()) {
30 if (this.bookmark.isConnected()) {
31 return (TreeNode[]) this.children.toArray(new TreeNode[this.children.size()]);
33 return BookmarkListNode.EMPTY_ARRAY;
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);
47 this.children.clear();
48 if (this.quickListNode == null) {
49 this.quickListNode = new QuickListNode(this);
51 if (this.queryListNode == null) {
52 this.queryListNode = new QueryListNode(this);
54 this.children.add(this.quickListNode);
55 this.children.add(this.queryListNode);
56 Bookmark bookmark = getBookmark();
58 Schema[] schemas = bookmark.getSchemas();
59 for (int i = 0, length = (schemas == null) ? 0 : schemas.length;
62 SchemaNode node = (SchemaNode) temp.remove(schemas[i].getDisplayName());
64 this.children.add(new SchemaNode(this, schemas[i]));
67 this.children.add(node);
71 for (Iterator i = temp.values().iterator(); i.hasNext(); ) {
72 ((TreeNode) i.next()).dispose();
76 if (temp.size() > 0 || changed ) {
77 firePropertyChange("children", null, null);
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()) {
85 } else if (!bookmark.isConnected()) {
87 } else if (children != null && children.size() > 0) {
93 protected void dispose() {
95 this.bookmark.removePropertyChangeListener(this);
96 if (this.bookmark.isConnected()) {
97 this.bookmark.disconnect();
99 } catch (SQLException e) {
104 * @see com.quantum.model.TreeNode#getName()
106 public String getName() {
107 return this.bookmark == null ? "<<new>>" : this.bookmark.getName();
113 public Bookmark getBookmark() {
114 return this.bookmark;
117 protected String getImageName() {
118 return this.bookmark.isConnected() ? "connected.gif" : "bookmarks.gif";
122 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
124 public void propertyChange(PropertyChangeEvent event) {
125 if ("connected".equals(event.getPropertyName())) {
126 if (Boolean.FALSE.equals(event.getNewValue())) {
129 firePropertyChange("connected", event.getOldValue(), event.getNewValue());
130 } else if ("schemas".equals(event.getPropertyName())) {
132 initializeChildren();
133 } catch (NotConnectedException e) {
134 this.children.clear();
135 } catch (SQLException e) {
136 this.children.clear();
138 firePropertyChange("children", event.getOldValue(), event.getNewValue());
139 } else if ("name".equals(event.getPropertyName())) {
140 firePropertyChange("name", event.getOldValue(), event.getNewValue());
144 protected void removeAllChildren() {
145 if (this.quickListNode != null) {
146 this.quickListNode.dispose();
147 this.quickListNode = null;
149 if (this.queryListNode != null) {
150 this.queryListNode.dispose();
151 this.queryListNode = null;
153 super.removeAllChildren();
156 public String getLabelDecorations(LabelDecorationInstructions labelDecorationInstructions) {
157 if (!labelDecorationInstructions.isDatabaseDataVisible()) {
159 } else if (!this.bookmark.isConnected()) {
163 String decoration = this.bookmark.getDatabase().getInformation();
164 return decoration == null ? null : "[" + decoration + "]";
165 } catch (NotConnectedException e) {
167 } catch (SQLException e) {