1 19 20 21 package org.netbeans.core.windows; 22 23 import java.lang.ref.Reference ; 24 import java.util.Iterator ; 25 import java.util.Set ; 26 import org.openide.windows.TopComponent; 27 import org.openide.windows.WindowManager; 28 29 import java.beans.PropertyChangeEvent ; 30 import java.beans.PropertyChangeListener ; 31 import java.lang.ref.WeakReference ; 32 import java.util.ArrayList ; 33 import java.util.List ; 34 35 40 final class RecentViewList implements PropertyChangeListener { 41 42 private static RecentViewList instance; 43 44 46 private List <Reference <TopComponent>> tcWeakList = new ArrayList <Reference <TopComponent>>(20); 47 48 public RecentViewList (WindowManager wm) { 49 wm.getRegistry().addPropertyChangeListener(this); 51 } 52 53 54 55 public TopComponent [] getTopComponents() { 56 List <TopComponent> tcList = new ArrayList <TopComponent>(tcWeakList.size()); 57 clean(); 58 for (int i = 0; i < tcWeakList.size(); i++) { 59 Reference <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 69 public void setTopComponents(TopComponent [] tcs) { 70 tcWeakList.clear(); 71 for (int i = 0; i < tcs.length; i++) { 72 Reference <TopComponent> wr = new WeakReference <TopComponent>(tcs[i]); 73 tcWeakList.add(wr); 74 } 75 } 76 77 public void propertyChange(PropertyChangeEvent evt) { 78 if (TopComponent.Registry.PROP_ACTIVATED.equals(evt.getPropertyName())) { 79 TopComponent tc = (TopComponent) evt.getNewValue(); 80 if (tc != null) { 81 clean(); 83 Reference <TopComponent> w = find(tc); 84 if (w != null) { 85 tcWeakList.remove(w); 87 tcWeakList.add(0,w); 88 } else { 89 Reference <TopComponent> wr = new WeakReference <TopComponent>(tc); 90 tcWeakList.add(0,wr); 91 } 92 fillList(TopComponent.getRegistry().getOpened()); 94 } 95 } 96 } 97 98 99 private void clean () { 100 int i = 0; 101 while (i < tcWeakList.size()) { 102 WeakReference w = (WeakReference ) tcWeakList.get(i); 103 TopComponent tc = (TopComponent) w.get(); 104 if (tc == null) { 106 tcWeakList.remove(w); 107 } else { 108 i++; 109 } 110 } 111 } 112 113 115 private Reference <TopComponent> find (TopComponent tc) { 116 for (int i = 0; i < tcWeakList.size(); i++) { 117 Reference <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 129 private void fillList(Set <TopComponent> openedTCs) { 130 Reference <TopComponent> wr; 131 for (TopComponent curTC: openedTCs) { 132 if (find(curTC) == null) { 133 if (tcWeakList.size() > 1) { 134 wr = new WeakReference <TopComponent>(curTC); 135 tcWeakList.add(1,wr); 136 } else { 137 wr = new WeakReference <TopComponent>(curTC); 138 tcWeakList.add(wr); 139 } 140 } 141 } 142 } 143 144 } 145 | Popular Tags |