KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > windows > RecentViewList


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.core.windows;
22
23 import java.lang.ref.Reference JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Set JavaDoc;
26 import org.openide.windows.TopComponent;
27 import org.openide.windows.WindowManager;
28
29 import java.beans.PropertyChangeEvent JavaDoc;
30 import java.beans.PropertyChangeListener JavaDoc;
31 import java.lang.ref.WeakReference JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.List JavaDoc;
34
35 /**
36  * Instance of this class keeps list of (weak references to) recently activated TopComponents.
37  *
38  * @author Marek Slama
39  */

40 final class RecentViewList implements PropertyChangeListener JavaDoc {
41
42     private static RecentViewList instance;
43     
44     /** List of TopComponents (weak references are used). First is most recently
45      * activated. */

46     private List JavaDoc<Reference JavaDoc<TopComponent>> tcWeakList = new ArrayList JavaDoc<Reference JavaDoc<TopComponent>>(20);
47     
48     public RecentViewList (WindowManager wm) {
49         // Starts listening on Registry to be notified about activated TopComponent.
50
wm.getRegistry().addPropertyChangeListener(this);
51     }
52
53     
54     /** Used to get array for view and for persistence */
55     public TopComponent [] getTopComponents() {
56         List JavaDoc<TopComponent> tcList = new ArrayList JavaDoc<TopComponent>(tcWeakList.size());
57         clean();
58         for (int i = 0; i < tcWeakList.size(); i++) {
59             Reference JavaDoc<TopComponent> w = tcWeakList.get(i);
60             TopComponent tc = w.get();
61             if ((tc != null) && tc.isOpened()) {
62                 tcList.add(tc);
63             }
64         }
65         return tcList.toArray(new TopComponent[tcList.size()]);
66     }
67     
68     /** Used to set initial values from persistence */
69     public void setTopComponents(TopComponent [] tcs) {
70         tcWeakList.clear();
71         for (int i = 0; i < tcs.length; i++) {
72             Reference JavaDoc<TopComponent> wr = new WeakReference JavaDoc<TopComponent>(tcs[i]);
73             tcWeakList.add(wr);
74         }
75     }
76     
77     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
78         if (TopComponent.Registry.PROP_ACTIVATED.equals(evt.getPropertyName())) {
79             TopComponent tc = (TopComponent) evt.getNewValue();
80             if (tc != null) {
81                 //Update list
82
clean();
83                 Reference JavaDoc<TopComponent> w = find(tc);
84                 if (w != null) {
85                     //Rearrange, put to first place
86
tcWeakList.remove(w);
87                     tcWeakList.add(0,w);
88                 } else {
89                     Reference JavaDoc<TopComponent> wr = new WeakReference JavaDoc<TopComponent>(tc);
90                     tcWeakList.add(0,wr);
91                 }
92                 // #69486: ensure all components are listed
93
fillList(TopComponent.getRegistry().getOpened());
94             }
95         }
96     }
97     
98     /** Clean gc'ed TopComponents from list */
99     private void clean () {
100         int i = 0;
101         while (i < tcWeakList.size()) {
102             WeakReference JavaDoc w = (WeakReference JavaDoc) tcWeakList.get(i);
103             TopComponent tc = (TopComponent) w.get();
104             //TopComponent was gc'ed
105
if (tc == null) {
106                 tcWeakList.remove(w);
107             } else {
108                 i++;
109             }
110         }
111     }
112     
113     /** Returns weak reference to given TopComponent if present.
114      * Otherwise returns null. */

115     private Reference JavaDoc<TopComponent> find (TopComponent tc) {
116         for (int i = 0; i < tcWeakList.size(); i++) {
117             Reference JavaDoc<TopComponent> w = tcWeakList.get(i);
118             TopComponent c = w.get();
119             if (tc == c) {
120                 return w;
121             }
122         }
123         return null;
124     }
125
126     /** Fills list of weak references with TCs that are in given
127      * input list but are not yet contained in list of weak references.
128      */

129     private void fillList(Set JavaDoc<TopComponent> openedTCs) {
130         Reference JavaDoc<TopComponent> wr;
131         for (TopComponent curTC: openedTCs) {
132             if (find(curTC) == null) {
133                 if (tcWeakList.size() > 1) {
134                     wr = new WeakReference JavaDoc<TopComponent>(curTC);
135                     tcWeakList.add(1,wr);
136                 } else {
137                     wr = new WeakReference JavaDoc<TopComponent>(curTC);
138                     tcWeakList.add(wr);
139                 }
140             }
141         }
142     }
143     
144 }
145
Popular Tags