1 19 20 package org.netbeans.modules.tasklist.providers; 21 22 import org.openide.loaders.DataObject; 23 import org.openide.cookies.EditorCookie; 24 import org.openide.filesystems.FileObject; 25 import org.openide.ErrorManager; 26 27 import javax.swing.text.Document ; 28 import javax.swing.text.BadLocationException ; 29 import java.io.IOException ; 30 31 38 public final class SuggestionContext { 39 40 private final DataObject dataObject; 41 42 private String cachedString; 43 44 private static boolean linkageError; 46 47 50 SuggestionContext(DataObject dataObject) { 51 this.dataObject = dataObject; 52 } 53 54 57 public CharSequence getCharSequence() { 58 59 if (cachedString == null) { 60 61 FileObject fo = getFileObject(); 62 if (linkageError == false && fo.hasExt("java")) { try { 66 cachedString = JavaSuggestionContext.getContent(fo); 68 return cachedString; 69 } catch (LinkageError link) { 70 link.printStackTrace(); 72 linkageError = true; 73 } 74 } 75 76 if (fo.hasExt("properties") && dataObject.isModified() == false) { cachedString = PropertiesSuggestionContext.getContent(fo); 78 return cachedString; 79 } 80 81 if ("xml".equalsIgnoreCase(fo.getExt()) && dataObject.isModified() == false) { cachedString = XMLSuggestionContext.getContent(fo); 83 if (cachedString != null) return cachedString; 84 } 85 86 EditorCookie edit = 87 (EditorCookie) dataObject.getCookie(EditorCookie.class); 88 if (edit != null) { 89 Document doc; 90 try { 91 doc = edit.openDocument(); cachedString = extractString(doc); 93 } catch (IOException e) { 94 ErrorManager.getDefault().notify(e); 95 } 96 } 97 } 98 return cachedString; 99 } 100 101 104 public Document getDocument() { 105 EditorCookie edit = 106 (EditorCookie) dataObject.getCookie(EditorCookie.class); 107 if (edit != null) { 108 try { 109 return edit.openDocument(); } catch (IOException e) { 111 e.printStackTrace(); 113 } 114 } 115 return null; 116 } 117 118 121 public FileObject getFileObject() { 122 return dataObject.getPrimaryFile(); 123 } 124 125 130 private static String extractString(final Document doc) { 131 final String text[] = new String [1]; 132 doc.render(new Runnable () { 133 public void run() { 134 try { 135 text[0] = doc.getText(0, doc.getLength()); 136 } catch (BadLocationException ex) { 137 assert false : ex; 138 } 139 } 140 }); 141 return text[0]; 142 } 143 144 } 145 | Popular Tags |