initial quantum version
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / view / bookmark / TreeNode.java
diff --git a/archive/net.sourceforge.phpeclipse.quantum.sql/src/com/quantum/view/bookmark/TreeNode.java b/archive/net.sourceforge.phpeclipse.quantum.sql/src/com/quantum/view/bookmark/TreeNode.java
new file mode 100644 (file)
index 0000000..1d18d38
--- /dev/null
@@ -0,0 +1,124 @@
+package com.quantum.view.bookmark;
+
+import java.beans.PropertyChangeEvent;
+import java.util.Iterator;
+import java.util.Vector;
+
+import com.quantum.QuantumPlugin;
+import com.quantum.model.Bookmark;
+import com.quantum.model.BookmarkHolder;
+
+import org.eclipse.core.runtime.IAdaptable;
+import org.eclipse.swt.graphics.Image;
+
+/**
+ * Base class for all nodes of the internal tree of data. Basically allows navigation.
+ * @author root
+ */
+public abstract class TreeNode 
+    implements BookmarkHolder, Comparable, IAdaptable {
+    
+    private TreeNode parent = null;
+    protected Vector children = new Vector();
+    private boolean disposed = false;
+
+    public TreeNode(TreeNode parent) {
+        this.parent = parent;
+    }
+    
+       public abstract Object[] getChildren();
+       public TreeNode getParent() {
+        return this.parent;
+       }
+       public abstract boolean hasChildren();
+       public abstract String getName();
+
+    public Bookmark getBookmark() {
+        return getParent().getBookmark();
+    }
+    
+    public String getLabelName() {
+        return getName();
+    }
+
+    /**
+        * @return an Image object to appear in the view, null if not found
+        */
+    public Image getImage() {
+       return QuantumPlugin.getImage(getImageName());
+    }
+
+    /**
+     * @return
+     */
+    protected abstract String getImageName();
+    
+    
+    /**
+     * @see java.lang.Comparable#compareTo(java.lang.Object)
+     */
+    public int compareTo(Object object) {
+        TreeNode that = (TreeNode) object;
+        return this.getLabelName().compareTo(that.getLabelName());
+    }
+    
+    public String toString() {
+        return getLabelName();
+    }
+    
+    public String getLabelDecorations(LabelDecorationInstructions labelDecorationInstructions) {
+        return null;
+    }
+    /**
+     * @param propertyName
+     * @param oldValue
+     * @param newValue
+     */
+    protected void firePropertyChange(
+        String propertyName,
+        Object oldValue,
+        Object newValue) {
+            
+        firePropertyChange(new PropertyChangeEvent(this, propertyName, oldValue, newValue));
+    }
+
+    /**
+     * @param propertyName
+     * @param oldValue
+     * @param newValue
+     */
+    protected void firePropertyChange(PropertyChangeEvent event) {
+        TreeNode parent = getParent();
+        if (parent != null && !this.disposed) {
+            parent.firePropertyChange(event);
+        }
+    }
+    
+    protected void dispose() {
+        this.disposed = true;
+        removeAllChildren();
+    }
+    protected void removeAllChildren() {
+        for (Iterator i = this.children.iterator(); i.hasNext();) {
+            TreeNode element = (TreeNode) i.next();
+            element.dispose();
+        }
+    }
+    
+    protected boolean isInitialized() {
+        return !this.children.isEmpty();
+    }
+    
+    public Object getAdapter(Class adapter) {
+        return null;
+    }
+    protected abstract void initializeChildren();
+    public void reload() {
+        if (isInitialized()) {
+            initializeChildren();
+            for (Iterator i = this.children.iterator(); i.hasNext(); ) {
+                ((TreeNode) i.next()).reload();
+            }
+        }
+    }
+}