1 19 20 package org.netbeans.modules.junit.output; 21 22 import java.awt.Component ; 23 import java.awt.EventQueue ; 24 import java.lang.reflect.InvocationTargetException ; 25 import java.lang.reflect.Method ; 26 import java.util.ArrayList ; 27 import java.util.HashMap ; 28 import java.util.List ; 29 import java.util.Map ; 30 import javax.swing.JSplitPane ; 31 import org.openide.ErrorManager; 32 33 37 final class ResultDisplayHandler { 38 39 40 private static final String ID_TREE = "tree"; 42 private static final String ID_OUTPUT = "output"; 44 45 private ResultPanelTree treePanel; 46 47 private ResultPanelOutput outputListener; 48 49 private Component displayComp; 50 51 52 53 ResultDisplayHandler() { 54 } 55 56 58 Component getDisplayComponent() { 59 if (displayComp == null) { 60 displayComp = createDisplayComp(); 61 } 62 return displayComp; 63 } 64 65 67 private Component createDisplayComp() { 68 Component left = new StatisticsPanel(this); 69 Component right = new ResultPanelOutput(this); 70 JSplitPane splitPane 71 = new JSplitPane (JSplitPane.HORIZONTAL_SPLIT, left, right); 72 return splitPane; 73 } 74 75 77 void displayShown() { 78 } 82 83 85 void displayHidden() { 86 } 90 91 93 static final Object [] EMPTY_QUEUE = new Object [0]; 94 private final Object queueLock = new Object (); 95 private volatile Object [] outputQueue; 96 private volatile int outputQueueSize = 0; 97 private int outputQueueAvailSpace; 98 99 101 Object getOutputQueueLock() { 102 return queueLock; 103 } 104 105 107 void setOutputListener(ResultPanelOutput outputPanel) { 108 synchronized (queueLock) { 109 this.outputListener = outputPanel; 110 } 111 } 112 113 115 void displayOutput(final String text, final boolean error) { 116 117 118 119 synchronized (queueLock) { 120 if (outputQueue == null) { 121 outputQueue = new Object [40]; 122 outputQueueAvailSpace = outputQueue.length - 1; 123 outputQueueSize = 0; 124 } 125 final int itemSpace = error ? 2 : 1; 126 if ((outputQueueAvailSpace -= itemSpace) < 0) { 127 int newCapacity = (outputQueue.length < 640) 128 ? outputQueue.length * 2 129 : (outputQueue.length * 3) / 2; 130 Object [] oldQueue = outputQueue; 131 outputQueue = new Object [newCapacity]; 132 System.arraycopy(oldQueue, 0, outputQueue, 0, outputQueueSize); 133 134 outputQueueAvailSpace += outputQueue.length - oldQueue.length; 135 } 136 if (error) { 137 outputQueue[outputQueueSize++] = Boolean.TRUE; 138 } 139 outputQueue[outputQueueSize++] = text; 140 141 if (outputListener != null) { 142 outputListener.outputAvailable(); 143 } 144 } 145 } 146 147 149 Object [] consumeOutput() { 150 synchronized (queueLock) { 151 if (outputQueueSize == 0) { 152 return EMPTY_QUEUE; 153 } 154 Object [] passedQueue = outputQueue; 155 outputQueue = null; 156 outputQueueSize = 0; 157 return passedQueue; 158 } 159 } 160 161 164 static final String ANONYMOUS_SUITE = new String (); 165 169 private String runningSuite; 170 private List <Report> reports; 171 private String message; 172 173 178 void displaySuiteRunning(String suiteName) { 179 180 181 182 assert runningSuite == null; 183 184 suiteName = (suiteName != null) ? suiteName : ANONYMOUS_SUITE; 185 186 synchronized (this) { 187 if (treePanel == null) { 188 runningSuite = suiteName; 189 return; 190 } 191 } 192 193 displayInDispatchThread("displaySuiteRunning", suiteName); } 195 196 198 void displayReport(final Report report) { 199 200 201 202 synchronized (this) { 203 if (treePanel == null) { 204 if (reports == null) { 205 reports = new ArrayList <Report>(10); 206 } 207 reports.add(report); 208 runningSuite = null; 209 return; 210 } 211 } 212 213 displayInDispatchThread("displayReport", report); 215 assert runningSuite == null; 216 } 217 218 220 void displayMessage(final String msg) { 221 222 223 224 synchronized (this) { 225 if (treePanel == null) { 226 message = msg; 227 return; 228 } 229 } 230 231 displayInDispatchThread("displayMsg", msg); } 233 234 235 private Map <String ,Method > methodsMap; 236 237 244 private void displayInDispatchThread(final String methodName, 245 final Object param) { 246 assert methodName != null; 247 assert treePanel != null; 248 249 final Method method = prepareMethod(methodName); 250 if (method == null) { 251 return; 252 } 253 254 EventQueue.invokeLater(new Runnable () { 255 public void run() { 256 try { 257 method.invoke(treePanel, new Object [] {param}); 258 } catch (InvocationTargetException ex) { 259 ErrorManager.getDefault().notify(ex.getTargetException()); 260 } catch (Exception ex) { 261 ErrorManager.getDefault().notify(ErrorManager.ERROR, ex); 262 } 263 } 264 }); 265 } 266 267 269 private Method prepareMethod(final String methodName) { 270 Method method; 271 272 if (methodsMap == null) { 273 methodsMap = new HashMap <String ,Method >(4); 274 method = null; 275 } else { 276 method = methodsMap.get(methodName); 277 } 278 279 if ((method == null) && !methodsMap.containsKey(methodName)) { 280 final Class paramType; 281 if (methodName.equals("displayReport")) { paramType = Report.class; 283 } else { 284 assert methodName.equals("displayMsg") || methodName.equals("displaySuiteRunning"); paramType = String .class; 287 } 288 try { 289 method = ResultPanelTree.class 290 .getDeclaredMethod(methodName, new Class [] {paramType}); 291 } catch (Exception ex) { 292 method = null; 293 ErrorManager.getDefault().notify(ErrorManager.ERROR, ex); 294 } 295 methodsMap.put(methodName, method); 296 } 297 298 return method; 299 } 300 301 302 304 void setTreePanel(final ResultPanelTree treePanel) { 305 assert EventQueue.isDispatchThread(); 306 307 308 309 synchronized (this) { 310 if (this.treePanel != null) { 311 return; 312 } 313 314 this.treePanel = treePanel; 315 } 316 317 if (message != null) { 318 treePanel.displayMsg(message); 319 message = null; 320 } 321 if (reports != null) { 322 treePanel.displayReports(reports); 323 reports = null; 324 } 325 if (runningSuite != null) { 326 treePanel.displaySuiteRunning(runningSuite != ANONYMOUS_SUITE 327 ? runningSuite 328 : null); 329 runningSuite = null; 330 } 331 } 332 333 } 334 | Popular Tags |