latest quantum sources 2.3.2
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / view / bookmark / TreeNode.java
1 package com.quantum.view.bookmark;
2
3 import java.beans.PropertyChangeEvent;
4 import java.sql.SQLException;
5 import java.util.Iterator;
6 import java.util.Vector;
7
8 import com.quantum.QuantumPlugin;
9 import com.quantum.model.Bookmark;
10 import com.quantum.model.BookmarkHolder;
11 import com.quantum.model.NotConnectedException;
12
13 import org.eclipse.core.runtime.IAdaptable;
14 import org.eclipse.swt.graphics.Image;
15
16 /**
17  * Base class for all nodes of the internal tree of data. Basically allows navigation.
18  * @author root
19  */
20 public abstract class TreeNode 
21     implements BookmarkHolder, Comparable, IAdaptable {
22     
23     private TreeNode parent = null;
24     protected Vector children = new Vector();
25     private boolean disposed = false;
26
27     public TreeNode(TreeNode parent) {
28         this.parent = parent;
29     }
30     
31         public abstract Object[] getChildren() throws NotConnectedException, SQLException;
32         public TreeNode getParent() {
33         return this.parent;
34         }
35         public abstract boolean hasChildren();
36         public abstract String getName();
37
38     public Bookmark getBookmark() {
39         return getParent().getBookmark();
40     }
41     
42     public String getLabelName() {
43         return getName();
44     }
45
46     /**
47          * @return an Image object to appear in the view, null if not found
48          */
49     public Image getImage() {
50         return QuantumPlugin.getImage(getImageName());
51     }
52
53     /**
54      * @return
55      */
56     protected abstract String getImageName();
57     
58     
59     /**
60      * @see java.lang.Comparable#compareTo(java.lang.Object)
61      */
62     public int compareTo(Object object) {
63         TreeNode that = (TreeNode) object;
64         return this.getLabelName().compareTo(that.getLabelName());
65     }
66     
67     public String toString() {
68         return getLabelName();
69     }
70     
71     public String getLabelDecorations(LabelDecorationInstructions labelDecorationInstructions) {
72         return null;
73     }
74     /**
75      * @param propertyName
76      * @param oldValue
77      * @param newValue
78      */
79     protected void firePropertyChange(
80         String propertyName,
81         Object oldValue,
82         Object newValue) {
83             
84         firePropertyChange(new PropertyChangeEvent(this, propertyName, oldValue, newValue));
85     }
86
87     /**
88      * @param propertyName
89      * @param oldValue
90      * @param newValue
91      */
92     protected void firePropertyChange(PropertyChangeEvent event) {
93         TreeNode parent = getParent();
94         if (parent != null && !this.disposed) {
95             parent.firePropertyChange(event);
96         }
97     }
98     
99     protected void dispose() {
100         this.disposed = true;
101         removeAllChildren();
102     }
103     protected void removeAllChildren() {
104         for (Iterator i = this.children.iterator(); i.hasNext();) {
105             TreeNode element = (TreeNode) i.next();
106             element.dispose();
107         }
108     }
109     
110     protected boolean isInitialized() {
111         return !this.children.isEmpty();
112     }
113     
114     public Object getAdapter(Class adapter) {
115         return null;
116     }
117     protected abstract void initializeChildren() throws SQLException, NotConnectedException;
118     public void reload() throws NotConnectedException, SQLException {
119         if (isInitialized()) {
120             initializeChildren();
121             for (Iterator i = this.children.iterator(); i.hasNext(); ) {
122                 ((TreeNode) i.next()).reload();
123             }
124         }
125     }
126 }