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 | |
17 | package net.sourceforge.hiveboard.util; |
18 | |
19 | import java.lang.ref.WeakReference; |
20 | |
21 | import javax.swing.JComponent; |
22 | import javax.swing.RepaintManager; |
23 | import javax.swing.SwingUtilities; |
24 | |
25 | import org.apache.commons.logging.Log; |
26 | import 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 | */ |
44 | public 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 | } |