EMMA Coverage Report (generated Wed Feb 13 07:49:24 ICT 2008)
[all classes][net.sourceforge.hiveboard.util]

COVERAGE SUMMARY FOR SOURCE FILE [CheckThreadViolationRepaintManager.java]

nameclass, %method, %block, %line, %
CheckThreadViolationRepaintManager.java100% (1/1)80%  (8/10)92%  (150/163)88%  (38.8/44)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class CheckThreadViolationRepaintManager100% (1/1)80%  (8/10)92%  (150/163)88%  (38.8/44)
isCompleteCheck (): boolean 0%   (0/1)0%   (0/3)0%   (0/1)
setCompleteCheck (boolean): void 0%   (0/1)0%   (0/4)0%   (0/2)
mustCheck (JComponent): boolean 100% (1/1)75%  (9/12)75%  (0.8/1)
checkThreadViolations (JComponent): void 100% (1/1)96%  (66/69)89%  (17/19)
<static initializer> 100% (1/1)100% (4/4)100% (1/1)
CheckThreadViolationRepaintManager (): void 100% (1/1)100% (4/4)100% (2/2)
CheckThreadViolationRepaintManager (boolean): void 100% (1/1)100% (9/9)100% (4/4)
addDirtyRegion (JComponent, int, int, int, int): void 100% (1/1)100% (11/11)100% (3/3)
addInvalidComponent (JComponent): void 100% (1/1)100% (7/7)100% (3/3)
logViolation (JComponent, StackTraceElement []): void 100% (1/1)100% (40/40)100% (8/8)

1/*
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
6 *
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
15 */
16 
17package net.sourceforge.hiveboard.util;
18 
19import java.lang.ref.WeakReference;
20 
21import javax.swing.JComponent;
22import javax.swing.RepaintManager;
23import javax.swing.SwingUtilities;
24 
25import org.apache.commons.logging.Log;
26import org.apache.commons.logging.LogFactory;
27 
28/**
29 * <p>This class is used to detect Event Dispatch Thread rule violations<br>
30 * See <a href="http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html">How to Use Threads</a>
31 * for more info</p>
32 * <p/>
33 * <p>This is a modification of original idea of Scott Delap<br>
34 * Initial version of ThreadCheckingRepaintManager can be found here<br>
35 * <a href="http://www.clientjava.com/blog/2004/08/20/1093059428000.html">Easily Find Swing Threading Mistakes</a>
36 * </p>
37 * <p>Modified by JF Poilpret to log everything to commons-logging</p>
38 *
39 * @author Scott Delap
40 * @author Alexander Potochkin
41 * 
42 * https://swinghelper.dev.java.net/
43 */
44public class CheckThreadViolationRepaintManager extends RepaintManager
45{
46        static private final Log        _logger = 
47                LogFactory.getLog(CheckThreadViolationRepaintManager.class);
48    // it is recommended to pass the complete check  
49    private boolean _completeCheck = true;
50    private WeakReference<JComponent> _lastComponent;
51 
52    public CheckThreadViolationRepaintManager(boolean completeCheck)
53    {
54        _completeCheck = completeCheck;
55    }
56 
57    public CheckThreadViolationRepaintManager()
58    {
59        this(true);
60    }
61 
62    public boolean isCompleteCheck() 
63    {
64        return _completeCheck;
65    }
66 
67    public void setCompleteCheck(boolean completeCheck) 
68    {
69        _completeCheck = completeCheck;
70    }
71 
72    @Override public synchronized void addInvalidComponent(JComponent component) 
73    {
74        checkThreadViolations(component);
75        super.addInvalidComponent(component);
76    }
77 
78    @Override public void addDirtyRegion(JComponent component, int x, int y, int w, int h) 
79    {
80        checkThreadViolations(component);
81        super.addDirtyRegion(component, x, y, w, h);
82    }
83 
84    private boolean mustCheck(JComponent c)
85    {
86        return !SwingUtilities.isEventDispatchThread() && (_completeCheck || c.isShowing());
87    }
88    
89    private void checkThreadViolations(JComponent c) 
90    {
91        if (mustCheck(c)) 
92        {
93            boolean repaint = false;
94            boolean fromSwing = false;
95            boolean imageUpdate = false;
96            StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
97            for (StackTraceElement st : stackTrace) 
98            {
99                if (repaint && st.getClassName().startsWith("javax.swing.")) 
100                {
101                    fromSwing = true;
102                }
103                if (repaint && "imageUpdate".equals(st.getMethodName())) 
104                {
105                    imageUpdate = true;
106                }
107                if ("repaint".equals(st.getMethodName())) 
108                {
109                    repaint = true;
110                    fromSwing = false;
111                }
112            }
113            if (imageUpdate) 
114            {
115                //assuming it is java.awt.image.ImageObserver.imageUpdate(...) 
116                //image was asynchronously updated, that's ok 
117                return;
118            }
119            if (repaint && !fromSwing) 
120            {
121                //no problems here, since repaint() is thread safe
122                return;
123            }
124            logViolation(c, stackTrace);
125        }
126    }
127    
128    private void        logViolation(JComponent c, StackTraceElement[] stackTrace)
129    {
130        //ignore the last processed component
131        if (_lastComponent != null && c == _lastComponent.get()) 
132        {
133            return;
134        }
135        _lastComponent = new WeakReference<JComponent>(c);
136        if (_logger.isWarnEnabled())
137        {
138                Exception e = new Exception("EDT violation");
139                e.setStackTrace(stackTrace);
140            _logger.warn("EDT violation on component <" + c + ">", e);
141        }
142    }
143}

[all classes][net.sourceforge.hiveboard.util]
EMMA 2.0.5312 (C) Vladimir Roubtsov