KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > search > TextFetcher


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.search;
21
22 import java.awt.EventQueue JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.nio.channels.ClosedByInterruptException JavaDoc;
25 import org.netbeans.modules.search.types.TextDetail;
26 import org.openide.filesystems.FileObject;
27 import org.openide.filesystems.FileUtil;
28 import org.openide.util.RequestProcessor;
29
30 /**
31  * Fetches text from an Item off the event thread and passes it to a
32  * TextReceiever on the event thread.
33  *
34  * @author Tim Boudreau
35  */

36 final class TextFetcher implements Runnable JavaDoc {
37     
38     private final Item source;
39     private final TextDisplayer textDisplayer;
40     private final RequestProcessor.Task task;
41     
42     /** */
43     private TextDetail location; //accessed only from the event DT
44
/** */
45     private boolean done = false; //accessed only from the event DT
46
/** */
47     private boolean cancelled = false; //accessed only from the event DT
48

49     /** */
50     private volatile String JavaDoc text = null;
51     
52     /**
53      */

54     TextFetcher(Item source,
55                 TextDisplayer receiver,
56                 RequestProcessor rp) {
57         assert EventQueue.isDispatchThread();
58         
59         this.source = source;
60         this.textDisplayer = receiver;
61         this.location = source.getLocation();
62         task = rp.post(this, 50);
63     }
64
65     
66     void cancel() {
67         assert EventQueue.isDispatchThread();
68         
69         cancelled = true;
70         task.cancel();
71     }
72
73     public void run() {
74         if (EventQueue.isDispatchThread()) {
75             if (cancelled) {
76                 return;
77             }
78             
79             FileObject fob = FileUtil.toFileObject(
80                                                 source.matchingObj.getFile());
81             String JavaDoc mimeType = fob.getMIMEType();
82             //We don't want the swing html editor kit, and even if we
83
//do get it, it will frequently throw a random NPE
84
//in StyleSheet.removeHTMLTags that appears to be a swing bug
85
if ("text/html".equals(mimeType)) { //NOI18N
86
mimeType = "text/plain"; //NOI18N
87
}
88             textDisplayer.setText(text,
89                                   fob.getMIMEType(),
90                                   getLocation());
91             done = true;
92         } else {
93             
94             /* called from the request processor's thread */
95             
96             if (Thread.interrupted()) {
97                 return;
98             }
99             
100             String JavaDoc invalidityDescription
101                     = source.matchingObj.getInvalidityDescription();
102             if (invalidityDescription != null) {
103                 text = invalidityDescription;
104             } else {
105                 try {
106                     text = source.matchingObj.getText();
107                 } catch (ClosedByInterruptException JavaDoc cbie) {
108                     cancelled = true;
109                     return;
110                 } catch (IOException JavaDoc ioe) {
111                     text = ioe.getLocalizedMessage();
112                     
113     // cancel();
114
}
115             }
116             
117             if (Thread.interrupted()) {
118                 return;
119             }
120             
121             EventQueue.invokeLater(this);
122         }
123     }
124     
125     /**
126      * If a new request comes to display the same file, just possibly at a
127      * different location, simply change the location we're scheduled to
128      * display and return true, else return false (in which case we'll be
129      * cancelled and a new request will be scheduled).
130      *
131      * @param item item to be shown
132      * @param receiver displayer that will actually show the item in the UI
133      * @return {@code true} if the previous item has not been shown yet
134      * and we are about to show the same file, just at a possible
135      * different location;
136      * {@code false} otherwise
137      */

138     boolean replaceLocation(Item item, TextDisplayer textDisplayer) {
139         assert EventQueue.isDispatchThread();
140         
141         if (done || (textDisplayer != this.textDisplayer)) {
142             return false;
143         }
144         
145         boolean result = source.matchingObj.getFile()
146                          .equals(item.matchingObj.getFile());
147         if (result) {
148             setLocation(item.getLocation());
149             task.schedule(50);
150         }
151         return result;
152     }
153
154     private synchronized void setLocation(TextDetail location) {
155         assert EventQueue.isDispatchThread();
156         
157         this.location = location;
158     }
159
160     private synchronized TextDetail getLocation() {
161         assert EventQueue.isDispatchThread();
162         
163         return location;
164     }
165 }
Popular Tags