KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > filesystems > RefreshRequest


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.openide.filesystems;
21
22 import java.lang.ref.Reference JavaDoc;
23 import java.lang.ref.WeakReference JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import org.openide.util.NbCollections;
26 import org.openide.util.RequestProcessor;
27
28 /** Request for parsing of an filesystem. Can be stopped.
29 *
30 * @author Jaroslav Tulach
31 */

32 final class RefreshRequest extends Object JavaDoc implements Runnable JavaDoc {
33     /** how much folders refresh at one request */
34     private static final int REFRESH_COUNT = 30;
35     private static RequestProcessor REFRESHER = new RequestProcessor("FS refresher"); // NOI18N
36

37     /** fs to work on */
38     private Reference JavaDoc<AbstractFileSystem> system;
39
40     /** enumeration of folders to process */
41     private Enumeration JavaDoc<AbstractFolder> en;
42
43     /** how often invoke itself */
44     private int refreshTime;
45
46     /** task to call us */
47     private RequestProcessor.Task task;
48
49     /** Constructor
50     * @param fs file system to refresh
51     * @param ms refresh time
52     */

53     public RefreshRequest(AbstractFileSystem fs, int ms) {
54         system = new WeakReference JavaDoc<AbstractFileSystem>(fs);
55         refreshTime = ms;
56         task = REFRESHER.post(this, ms, Thread.MIN_PRIORITY);
57     }
58
59     /** Getter for the time.
60     */

61     public int getRefreshTime() {
62         return refreshTime;
63     }
64
65     /** Stops the task.
66     */

67     public synchronized void stop() {
68         refreshTime = 0;
69
70         if (task == null) {
71             // null task means that the request processor is running =>
72
// wait for end of task execution
73
try {
74                 wait();
75             } catch (InterruptedException JavaDoc ex) {
76             }
77         }
78     }
79
80     /** Refreshes the system.
81     */

82     public void run() {
83         // this code is executed only in RequestProcessor thread
84
int ms;
85         RequestProcessor.Task t;
86
87         synchronized (this) {
88             // the synchronization is here to be sure
89
// that
90
ms = refreshTime;
91
92             if (ms <= 0) {
93                 // finish silently if already stopped
94
return;
95             }
96
97             t = task;
98
99             // by setting task to null we indicate that we are currently processing
100
// files and that any stop should wait till the processing is over
101
task = null;
102         }
103
104         try {
105             doLoop(ms);
106         } finally {
107             synchronized (this) {
108                 // reseting task variable back to indicate that
109
// the processing is over
110
task = t;
111
112                 notifyAll();
113             }
114
115             // plan the task for next execution
116
if ((system != null) && (system.get() != null)) {
117                 t.schedule(ms);
118             } else {
119                 refreshTime = 0;
120             }
121         }
122     }
123
124     private void doLoop(int ms) {
125         AbstractFileSystem system = this.system.get();
126
127         if (system == null) {
128             // end for ever the fs does not exist no more
129
return;
130         }
131
132         if ((en == null) || !en.hasMoreElements()) {
133             // start again from root
134
en = NbCollections.checkedEnumerationByFilter(existingFolders(system), AbstractFolder.class, true);
135         }
136
137         for (int i = 0; (i < REFRESH_COUNT) && en.hasMoreElements(); i++) {
138             AbstractFolder fo = en.nextElement();
139
140             if ((fo != null) && (!fo.isFolder() || fo.isInitialized())) {
141                 fo.refresh();
142             }
143
144             if (refreshTime <= 0) {
145                 // after each refresh check the current value of refreshTime
146
// again and if it goes to zero exit as fast a you can
147
return;
148             }
149         }
150
151         // clear the queue
152
if (!en.hasMoreElements()) {
153             en = null;
154         }
155     }
156
157     /** Existing folders for abstract file objects.
158     */

159     private static Enumeration JavaDoc<? extends FileObject> existingFolders(AbstractFileSystem fs) {
160         return fs.existingFileObjects(fs.getAbstractRoot());
161     }
162
163     /**
164      * Overridden for debugging/logging purposes.
165      */

166     public String JavaDoc toString() {
167         AbstractFileSystem fs = system.get();
168
169         return "RefreshRequest for " + // NOI18N
170
((fs == null) ? "gone FS" : fs); // NOI18N
171
}
172 }
173
Popular Tags