new build.xml
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / view / bookmark / BookmarkListNode.java
1 package com.quantum.view.bookmark;
2
3 import java.beans.PropertyChangeEvent;
4 import java.beans.PropertyChangeListener;
5 import java.beans.PropertyChangeSupport;
6 import java.util.Arrays;
7 import java.util.Collections;
8 import java.util.Hashtable;
9 import java.util.Iterator;
10 import java.util.Map;
11
12 import com.quantum.model.Bookmark;
13 import com.quantum.model.BookmarkCollection;
14
15
16 /**
17  * @author root
18  */
19 public final class BookmarkListNode extends TreeNode implements PropertyChangeListener {
20     final static BookmarkListNode instance = new BookmarkListNode();
21         final static TreeNode[] EMPTY_ARRAY = new TreeNode[0];
22     private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
23     private boolean initialized = false;
24    
25     private BookmarkListNode() {
26         super(null);
27     }
28     
29     public static BookmarkListNode getInstance() {
30         return BookmarkListNode.instance;
31     }
32     
33         /**
34          * @see com.quantum.view.bookmark.TreeNode#getChildren()
35          */
36         public Object[] getChildren() {
37         if (!initialized) {
38             initializeChildren();
39             BookmarkCollection.getInstance().addPropertyChangeListener(this);
40         }
41         BookmarkNode[] nodes = (BookmarkNode[]) this.children.toArray(
42             new BookmarkNode[this.children.size()]);
43         Arrays.sort(nodes);
44         return nodes;
45         }
46
47         /**
48          * @see com.quantum.view.bookmark.TreeNode#hasChildren()
49          */
50         public boolean hasChildren() {
51                 return true;
52         }
53         
54         public String getName() {
55                 return "ROOT"; //$NON-NLS-1$
56         }
57     protected String getImageName() {
58         return null;
59     }
60     /**
61      * @param listener
62      */
63     public synchronized void addPropertyChangeListener(PropertyChangeListener listener) {
64         this.propertyChangeSupport.addPropertyChangeListener(listener);
65     }
66
67     /**
68      * @param listener
69      */
70     public synchronized void removePropertyChangeListener(PropertyChangeListener listener) {
71         this.propertyChangeSupport.removePropertyChangeListener(listener);
72     }
73     protected void firePropertyChange(PropertyChangeEvent event) {
74         this.propertyChangeSupport.firePropertyChange(event);
75     }
76
77     protected synchronized void initializeChildren() {
78         boolean changed = false;
79         Map temp = new Hashtable();
80         for (Iterator i = this.children.iterator(); i.hasNext();) {
81             BookmarkNode bookmark = (BookmarkNode) i.next();
82             temp.put(bookmark.getName(), bookmark);
83         }
84         
85         this.children.clear();
86         
87         Bookmark[] bookmarks = BookmarkCollection.getInstance().getBookmarks();
88         for (int i = 0, length = (bookmarks == null) ? 0 : bookmarks.length; i < length; i++) {
89             
90             BookmarkNode node = (BookmarkNode) temp.remove(bookmarks[i].getName());
91             if (node == null) {
92                 this.children.add(new BookmarkNode(this, bookmarks[i]));
93                 changed = true;
94             } else {
95                 this.children.add(node);
96             }
97         }
98
99         for (Iterator i = temp.values().iterator(); i.hasNext(); ) {
100             ((TreeNode) i.next()).dispose();
101             changed = true;
102         }
103
104         Collections.sort(this.children);
105         this.initialized = true;
106         
107         if  (temp.size() > 0 || changed ) {
108             firePropertyChange("bookmarks", null, null);
109         }
110     }
111
112     /**
113      * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
114      */
115     public void propertyChange(PropertyChangeEvent event) {
116         if ("bookmarks".equals(event.getPropertyName())) {
117             initializeChildren();
118         }
119     }
120 }