1 19 20 package org.netbeans.spi.editor.completion.support; 21 22 import javax.swing.SwingUtilities ; 23 import javax.swing.text.Document ; 24 import javax.swing.text.JTextComponent ; 25 import org.netbeans.spi.editor.completion.CompletionResultSet; 26 import org.netbeans.spi.editor.completion.CompletionTask; 27 import org.openide.util.RequestProcessor; 28 29 41 42 public final class AsyncCompletionTask implements CompletionTask, Runnable { 43 44 private final AsyncCompletionQuery query; 45 46 private final JTextComponent component; 47 48 private Document doc; 49 50 private int queryCaretOffset; 51 52 private CompletionResultSet queryResultSet; 53 54 private CompletionResultSet refreshResultSet; 55 56 private RequestProcessor.Task rpTask; 57 58 59 private boolean cancelled; 60 61 62 private boolean queryInvoked; 63 64 73 public AsyncCompletionTask(AsyncCompletionQuery query, JTextComponent component) { 74 assert (query != null) : "Query must be non-null"; 75 this.query = query; 76 this.component = component; 77 } 78 79 85 public AsyncCompletionTask(AsyncCompletionQuery query) { 86 this(query, null); 87 } 88 89 93 public void query(CompletionResultSet resultSet) { 94 assert (resultSet != null); 95 assert (SwingUtilities.isEventDispatchThread()); 96 if (component != null) { 97 doc = component.getDocument(); 98 } else { 99 doc = null; 100 } 101 queryInvoked = true; 102 103 synchronized (this) { 104 performQuery(resultSet); 105 } 106 } 107 108 115 public void refresh(CompletionResultSet resultSet) { 116 assert (SwingUtilities.isEventDispatchThread()); 117 assert !cancelled : "refresh() called on canceled task"; if (queryInvoked) { 119 assert (resultSet != null); 120 refreshResultSet = resultSet; 121 refreshImpl(); 122 } else { 123 query.preQueryUpdate(component); 124 } 125 } 126 127 130 public void cancel() { 131 cancelled = true; 132 synchronized (this) { 133 if (rpTask != null) { 134 rpTask.cancel(); 135 rpTask = null; 136 } 137 } 138 } 139 140 private void performQuery(CompletionResultSet resultSet) { 141 if (component != null) { 143 queryCaretOffset = component.getSelectionStart(); 144 } else { 145 queryCaretOffset = -1; 146 } 147 148 query.prepareQuery(component); 149 synchronized (this) { 150 queryResultSet = resultSet; 151 rpTask = RequestProcessor.getDefault().post(this); 152 } 153 } 154 155 void refreshImpl() { 156 CompletionResultSet resultSet; 158 boolean rpTaskFinished; 159 synchronized (this) { 160 rpTaskFinished = (rpTask == null); 161 resultSet = refreshResultSet; } 163 if (resultSet != null) { 164 if (rpTaskFinished) { synchronized (this) { 166 refreshResultSet = null; 167 } 168 if (query.canFilter(component)) { 170 query.filter(resultSet); 171 assert resultSet.isFinished() 172 : toString() + ": query.filter(): Result set not finished by resultSet.finish()"; } else { performQuery(resultSet); 175 } 176 177 } else { if (!query.canFilter(component)) { } } 183 } 184 } 185 186 190 public void run() { 191 CompletionResultSet resultSet = queryResultSet; 193 if (resultSet != null) { 194 query.query(resultSet, doc, queryCaretOffset); 196 assert resultSet.isFinished() 197 : toString() + ": query.query(): Result set not finished by resultSet.finish()"; } 199 200 synchronized (this) { 201 rpTask = null; 202 queryResultSet = null; 203 if (refreshResultSet != null) { 205 SwingUtilities.invokeLater(new Runnable () { 207 public void run() { 208 refreshImpl(); 209 } 210 }); 211 } 212 } 213 } 214 215 synchronized boolean isCancelled() { 216 return cancelled; 217 } 218 219 public String toString() { 220 return "AsyncCompletionTask: query=" + query; } 222 } 223 | Popular Tags |