1 19 20 package org.netbeans.modules.tasklist.usertasks; 21 22 import java.io.IOException ; 23 import java.lang.ref.WeakReference ; 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import javax.swing.event.ChangeEvent ; 28 import javax.swing.event.ChangeListener ; 29 import javax.swing.event.EventListenerList ; 30 import org.netbeans.modules.tasklist.usertasks.util.AWTThreadAnnotation; 31 import org.openide.DialogDisplayer; 32 import org.openide.NotifyDescriptor; 33 import org.openide.NotifyDescriptor.Message; 34 import org.openide.filesystems.FileObject; 35 import org.openide.windows.TopComponent; 36 import org.openide.windows.WindowManager; 37 38 43 public class UserTaskViewRegistry { 44 private static UserTaskViewRegistry instance = 45 new UserTaskViewRegistry(); 46 47 52 public static UserTaskViewRegistry getInstance() { 53 return instance; 54 } 55 56 private UserTaskView defview = null; 57 58 62 private List <WeakReference <UserTaskView>> views = 63 new ArrayList <WeakReference <UserTaskView>>(); 64 65 private WeakReference <UserTaskView> lastActivated = null; 66 private EventListenerList listenerList = new EventListenerList (); 67 68 74 @AWTThreadAnnotation 75 public UserTaskView getDefault() { 76 if (defview == null) { 77 try { 78 defview = new UserTaskView( 79 UserTaskView.getDefaultFile(), true); 80 defview.showInMode(); 81 } catch (IOException ioe) { 82 DialogDisplayer.getDefault().notify(new Message( 83 ioe, NotifyDescriptor.ERROR_MESSAGE)); 84 } 85 } 86 return defview; 87 } 88 89 94 public UserTaskView[] getAll() { 95 synchronized(UserTaskView.class) { 96 WeakReference [] r = this.views.toArray( 97 new WeakReference [this.views.size()]); 98 List <UserTaskView> views = new ArrayList <UserTaskView>(); 99 for (int i = 0; i < r.length; i++) { 100 UserTaskView v = (UserTaskView) r[i].get(); 101 if (v != null) 102 views.add(v); 103 } 104 return views.toArray(new UserTaskView[views.size()]); 105 } 106 } 107 108 113 public UserTaskView getCurrent() { 114 TopComponent activated = WindowManager.getDefault(). 115 getRegistry().getActivated(); 116 if (activated instanceof UserTaskView) 117 return (UserTaskView) activated; 118 else 119 return null; 120 } 121 122 127 public UserTaskView getLastActivated() { 128 if (lastActivated == null) 129 return null; 130 UserTaskView v = (UserTaskView) lastActivated.get(); 131 if (v.isOpened()) 132 return v; 133 else 134 return null; 135 } 136 137 141 public UserTaskView findView(FileObject file) { 142 Iterator it = views.iterator(); 143 while (it.hasNext()) { 144 WeakReference wr = (WeakReference ) it.next(); 145 UserTaskView tlv = (UserTaskView) wr.get(); 146 if (tlv != null && tlv.getFile() == file) 147 return tlv; 148 } 149 return null; 150 } 151 152 157 public void setLastActivated(UserTaskView v) { 158 lastActivated = new WeakReference <UserTaskView>(v); 159 } 160 161 166 public void viewOpened(UserTaskView v) { 167 synchronized (UserTaskView.class) { 168 views.add(new WeakReference <UserTaskView>(v)); 169 } 170 fireChange(); 171 } 172 173 178 public void setDefaultView(UserTaskView v) { 179 defview = v; 180 } 181 182 187 public void viewClosed(UserTaskView v) { 188 if (defview == v) 189 defview = null; 190 191 Iterator it = views.iterator(); 192 while (it.hasNext()) { 193 WeakReference wr = (WeakReference ) it.next(); 194 UserTaskView tlv = (UserTaskView) wr.get(); 195 if (tlv == v) { 196 it.remove(); 197 break; 198 } 199 } 200 fireChange(); 201 } 202 203 209 public void addChangeListener(ChangeListener l) { 210 listenerList.add(ChangeListener .class, l); 211 } 212 213 218 public void removeChangeListener(ChangeListener l) { 219 listenerList.remove(ChangeListener .class, l); 220 } 221 222 225 private void fireChange() { 226 ChangeEvent event = null; 227 Object [] listeners = listenerList.getListenerList(); 228 for (int i = listeners.length - 2; i >= 0; i -= 2) { 229 if (listeners[i] == ChangeListener .class) { 230 if (event == null) 231 event = new ChangeEvent (this); 232 ((ChangeListener ) listeners[i + 1]).stateChanged(event); 233 } 234 } 235 } 236 237 240 private UserTaskViewRegistry() { 241 } 242 } 243 | Popular Tags |