1 19 20 package org.netbeans.modules.search; 21 22 import java.awt.EventQueue ; 23 import java.io.IOException ; 24 import java.nio.channels.ClosedByInterruptException ; 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 36 final class TextFetcher implements Runnable { 37 38 private final Item source; 39 private final TextDisplayer textDisplayer; 40 private final RequestProcessor.Task task; 41 42 43 private TextDetail location; 45 private boolean done = false; 47 private boolean cancelled = false; 49 50 private volatile String text = null; 51 52 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 mimeType = fob.getMIMEType(); 82 if ("text/html".equals(mimeType)) { mimeType = "text/plain"; } 88 textDisplayer.setText(text, 89 fob.getMIMEType(), 90 getLocation()); 91 done = true; 92 } else { 93 94 95 96 if (Thread.interrupted()) { 97 return; 98 } 99 100 String invalidityDescription 101 = source.matchingObj.getInvalidityDescription(); 102 if (invalidityDescription != null) { 103 text = invalidityDescription; 104 } else { 105 try { 106 text = source.matchingObj.getText(); 107 } catch (ClosedByInterruptException cbie) { 108 cancelled = true; 109 return; 110 } catch (IOException ioe) { 111 text = ioe.getLocalizedMessage(); 112 113 } 115 } 116 117 if (Thread.interrupted()) { 118 return; 119 } 120 121 EventQueue.invokeLater(this); 122 } 123 } 124 125 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 |