1 19 20 package org.netbeans.modules.junit.output; 21 22 import java.awt.Color ; 23 import java.awt.EventQueue ; 24 import java.awt.event.ActionEvent ; 25 import java.awt.event.ActionListener ; 26 import javax.accessibility.AccessibleContext ; 27 import javax.swing.JScrollPane ; 28 import javax.swing.JTextPane ; 29 import javax.swing.Timer ; 30 import javax.swing.UIManager ; 31 import javax.swing.text.BadLocationException ; 32 import javax.swing.text.Style ; 33 import javax.swing.text.StyleConstants ; 34 import javax.swing.text.StyleContext ; 35 import javax.swing.text.StyledDocument ; 36 import org.openide.ErrorManager; 37 import org.openide.util.NbBundle; 38 39 43 final class ResultPanelOutput extends JScrollPane 44 implements ActionListener { 45 46 private static final boolean LOG = false; 47 48 static final Color selectedFg; 49 static final Color unselectedFg; 50 static final Color selectedErr; 51 static final Color unselectedErr; 52 53 private static final int UPDATE_DELAY = 300; 55 static { 56 57 61 62 Color color; 63 64 color = UIManager.getColor("nb.output.foreground.selected"); if (color == null) { 66 color = UIManager.getColor("textText"); if (color == null) { 68 color = Color.BLACK; 69 } 70 } 71 selectedFg = color; 72 73 color = UIManager.getColor("nb.output.foreground"); if (color == null) { 75 color = selectedFg; 76 } 77 unselectedFg = color; 78 79 color = UIManager.getColor("nb.output.err.foreground.selected"); if (color == null) { 81 color = new Color (164, 0, 0); 82 } 83 selectedErr = color; 84 85 color = UIManager.getColor("nb.output.err.foreground"); if (color == null) { 87 color = selectedErr; 88 } 89 unselectedErr = color; 90 } 91 92 93 private final Style outputStyle, errOutputStyle; 94 96 97 private final JTextPane textPane; 98 99 private final StyledDocument doc; 100 101 private final ResultDisplayHandler displayHandler; 102 103 104 boolean newLinePending = false; 105 106 private Timer timer = null; 107 108 112 private volatile boolean timerRunning = false; 113 114 117 ResultPanelOutput(ResultDisplayHandler displayHandler) { 118 super(); 119 if (LOG) { 120 System.out.println("ResultPanelOutput.<init>"); 121 } 122 123 textPane = new JTextPane (); 124 doc = textPane.getStyledDocument(); 125 textPane.setEditable(false); 126 setViewportView(textPane); 127 128 AccessibleContext accessibleContext = textPane.getAccessibleContext(); 129 accessibleContext.setAccessibleName( 130 NbBundle.getMessage(getClass(), "ACSN_OutputTextPane")); accessibleContext.setAccessibleDescription( 132 NbBundle.getMessage(getClass(), "ACSD_OutputTextPane")); 134 Style defaultStyle = StyleContext.getDefaultStyleContext() 135 .getStyle(StyleContext.DEFAULT_STYLE); 136 137 outputStyle = doc.addStyle("output", defaultStyle); StyleConstants.setFontFamily(outputStyle, "Monospaced"); StyleConstants.setForeground(outputStyle, unselectedFg); 140 141 errOutputStyle = doc.addStyle("error", outputStyle); 142 StyleConstants.setForeground(errOutputStyle, unselectedErr); 143 144 147 this.displayHandler = displayHandler; 148 } 149 150 152 public void addNotify() { 153 super.addNotify(); 154 155 final Object [] pendingOutput; 156 157 if (LOG) { 158 System.out.println("ResultPanelOutput.addNotify()"); 159 } 160 161 166 synchronized (displayHandler.getOutputQueueLock()) { 167 pendingOutput = displayHandler.consumeOutput(); 168 if (pendingOutput.length == 0) { 169 displayHandler.setOutputListener(this); 170 } 171 } 172 173 if (pendingOutput.length != 0) { 174 displayOutput(pendingOutput); 175 startTimer(); 176 } 177 } 178 179 181 void outputAvailable() { 182 183 184 185 if (LOG) { 186 System.out.println("ResultOutputPanel.outputAvailable() - called by the AntLogger"); 187 } 188 final Object [] pendingOutput = displayHandler.consumeOutput(); 190 assert pendingOutput.length != 0; 191 new OutputDisplayer(pendingOutput).run(); 192 displayHandler.setOutputListener(null); 193 if (!timerRunning) { 194 startTimer(); 195 } 196 } 197 198 final class OutputDisplayer implements Runnable { 199 private final Object [] output; 200 OutputDisplayer(Object [] output) { 201 this.output = output; 202 } 203 public void run() { 204 if (!EventQueue.isDispatchThread()) { 205 EventQueue.invokeLater(this); 206 return; 207 } 208 displayOutput(output); 209 } 210 } 211 212 215 public void actionPerformed(ActionEvent e) { 216 217 218 219 assert EventQueue.isDispatchThread(); 220 221 if (LOG) { 222 System.out.println("ResultOutputPanel.actionPerformed(...) - called by the timer"); 223 } 224 final Object [] pendingOutput = displayHandler.consumeOutput(); 225 if (pendingOutput.length != 0) { 226 displayOutput(pendingOutput); 227 } else { 228 synchronized (displayHandler.getOutputQueueLock()) { 229 stopTimer(); 230 displayHandler.setOutputListener(this); 231 } 232 } 233 } 234 235 237 private void startTimer() { 238 if (LOG) { 239 System.out.println("ResultPanelOutput.startTimer()"); 240 } 241 if (timer == null) { 242 timer = new Timer (UPDATE_DELAY, this); 243 } 244 timerRunning = true; 245 timer.start(); 246 } 247 248 250 private void stopTimer() { 251 if (LOG) { 252 System.out.println("ResultPanelOutput.stopTimer()"); 253 } 254 if (timer != null) { 255 timer.stop(); 256 timerRunning = false; 257 } 258 } 259 260 262 void displayOutput(final Object [] output) { 263 assert EventQueue.isDispatchThread(); 264 265 if (LOG) { 266 System.out.println("ResultPanelOutput.displayOutput(...):"); 267 for (int i = 0; output[i] != null; i++) { 268 System.out.println(" " + output[i]); 269 } 270 } 271 Object o; 272 int index = 0; 273 while ((o = output[index++]) != null) { 274 boolean errOutput = false; 275 if (o == Boolean.TRUE) { 276 o = output[index++]; 277 errOutput = true; 278 } 279 displayOutputLine(o.toString(), errOutput); 280 } 281 } 282 283 285 private void displayOutputLine(final String text, final boolean error) { 286 final Style textStyle = error ? errOutputStyle : outputStyle; 287 288 try { 289 if (newLinePending) { 290 doc.insertString(doc.getLength(), "\n", outputStyle); newLinePending = false; 292 } 293 doc.insertString(doc.getLength(), text, textStyle); 294 newLinePending = true; 295 } catch (BadLocationException ex) { 296 ErrorManager.getDefault().notify(ErrorManager.ERROR, ex); 297 } 298 } 299 300 341 343 345 private void clear() { 346 assert EventQueue.isDispatchThread(); 347 348 try { 349 doc.remove(0, doc.getLength()); 350 } catch (BadLocationException ex) { 351 ErrorManager.getDefault().notify(ErrorManager.ERROR, ex); 352 } 353 } 354 355 370 372 399 401 408 410 412 public boolean requestFocusInWindow() { 413 return textPane.requestFocusInWindow(); 414 } 415 416 } 417 | Popular Tags |