KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > usertasks > UserTaskViewRegistry


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 package org.netbeans.modules.tasklist.usertasks;
21
22 import java.io.IOException JavaDoc;
23 import java.lang.ref.WeakReference JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import javax.swing.event.ChangeEvent JavaDoc;
28 import javax.swing.event.ChangeListener JavaDoc;
29 import javax.swing.event.EventListenerList JavaDoc;
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 /**
39  * Registry for all open UserTaskViews.
40  *
41  * @author tl
42  */

43 public class UserTaskViewRegistry {
44     private static UserTaskViewRegistry instance =
45             new UserTaskViewRegistry();
46     
47     /**
48      * Returns the only instance of this class.
49      *
50      * @return the instance.
51      */

52     public static UserTaskViewRegistry getInstance() {
53         return instance;
54     }
55     
56     private UserTaskView defview = null;
57     
58     /**
59      * Keeps track of all UserTaskViews. Access should be synchronized on
60      * UserTaskView.class
61      */

62     private List JavaDoc<WeakReference JavaDoc<UserTaskView>> views =
63             new ArrayList JavaDoc<WeakReference JavaDoc<UserTaskView>>();
64     
65     private WeakReference JavaDoc<UserTaskView> lastActivated = null;
66     private EventListenerList JavaDoc listenerList = new EventListenerList JavaDoc();
67             
68     /**
69      * Returns the view with the default task list. The view will be opened if
70      * it was not.
71      *
72      * @return the default view or null if an error occured
73      */

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 JavaDoc ioe) {
82                 DialogDisplayer.getDefault().notify(new Message(
83                     ioe, NotifyDescriptor.ERROR_MESSAGE));
84             }
85     }
86     return defview;
87     }
88
89     /**
90      * Returns all opened views.
91      *
92      * @return array of all opened views
93      */

94     public UserTaskView[] getAll() {
95         synchronized(UserTaskView.class) {
96             WeakReference JavaDoc[] r = this.views.toArray(
97                 new WeakReference JavaDoc[this.views.size()]);
98             List JavaDoc<UserTaskView> views = new ArrayList JavaDoc<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     /**
109      * Return the currently active user task view, or null
110      *
111      * @return current view
112      */

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     /**
123      * Returns the last activated view.
124      *
125      * @return the view that was activated as the last one or null
126      */

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     /**
138      * Locate a particular view showing the given list
139      * @return found view or null
140      */

141     public UserTaskView findView(FileObject file) {
142     Iterator JavaDoc it = views.iterator();
143         while (it.hasNext()) {
144             WeakReference JavaDoc wr = (WeakReference JavaDoc) 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     /**
153      * Sets the last activated view.
154      *
155      * @param v the last activated view
156      */

157     public void setLastActivated(UserTaskView v) {
158         lastActivated = new WeakReference JavaDoc<UserTaskView>(v);
159     }
160
161     /**
162      * Registers a new view.
163      *
164      * @param v the view
165      */

166     public void viewOpened(UserTaskView v) {
167         synchronized (UserTaskView.class) {
168             views.add(new WeakReference JavaDoc<UserTaskView>(v));
169         }
170         fireChange();
171     }
172     
173     /**
174      * Sets default view.
175      *
176      * @param v the default view
177      */

178     public void setDefaultView(UserTaskView v) {
179         defview = v;
180     }
181     
182     /**
183      * A view was closed.
184      *
185      * @param v the view
186      */

187     public void viewClosed(UserTaskView v) {
188         if (defview == v)
189             defview = null;
190         
191     Iterator JavaDoc it = views.iterator();
192         while (it.hasNext()) {
193             WeakReference JavaDoc wr = (WeakReference JavaDoc) it.next();
194         UserTaskView tlv = (UserTaskView) wr.get();
195             if (tlv == v) {
196                 it.remove();
197                 break;
198             }
199         }
200         fireChange();
201     }
202     
203     /**
204      * Adds a listener that will be modified if a view was closed or a new view
205      * was opened.
206      *
207      * @param l a listener
208      */

209     public void addChangeListener(ChangeListener JavaDoc l) {
210         listenerList.add(ChangeListener JavaDoc.class, l);
211     }
212     
213     /**
214      * Removes a listener registered with addChangeListener.
215      *
216      * @param l the listener
217      */

218     public void removeChangeListener(ChangeListener JavaDoc l) {
219         listenerList.remove(ChangeListener JavaDoc.class, l);
220     }
221     
222     /**
223      * Fires a change.
224      */

225     private void fireChange() {
226         ChangeEvent JavaDoc event = null;
227         Object JavaDoc[] listeners = listenerList.getListenerList();
228         for (int i = listeners.length - 2; i >= 0; i -= 2) {
229             if (listeners[i] == ChangeListener JavaDoc.class) {
230                 if (event == null)
231                     event = new ChangeEvent JavaDoc(this);
232                 ((ChangeListener JavaDoc) listeners[i + 1]).stateChanged(event);
233             }
234         }
235     }
236     
237     /**
238      * Creates a new instance of UserTaskViewRegistry
239      */

240     private UserTaskViewRegistry() {
241     }
242 }
243
Popular Tags