01: package org.gui4j.core.swing;
02:
03: import java.awt.Component;
04: import java.awt.EventQueue;
05: import java.io.ByteArrayOutputStream;
06: import java.io.PrintStream;
07:
08: import javax.swing.JComponent;
09: import javax.swing.RepaintManager;
10:
11: public class ThreadCheckingRepaintManager extends RepaintManager {
12: private int tabCount = 0;
13: private boolean checkIsShowing = false;
14:
15: public ThreadCheckingRepaintManager() {
16: super ();
17: }
18:
19: public ThreadCheckingRepaintManager(boolean checkIsShowing) {
20: super ();
21: this .checkIsShowing = checkIsShowing;
22: }
23:
24: public synchronized void addInvalidComponent(JComponent jComponent) {
25: checkThread(jComponent);
26: super .addInvalidComponent(jComponent);
27: }
28:
29: private void checkThread(JComponent c) {
30: if (!EventQueue.isDispatchThread() && checkIsShowing(c)) {
31: System.out.println("----------Wrong Thread START");
32: System.out.println("Thread: "
33: + Thread.currentThread().getName());
34: System.out.println(getStracktraceAsString(new Exception()));
35: dumpComponentTree(c);
36: System.out.println("----------Wrong Thread END");
37: }
38: }
39:
40: private String getStracktraceAsString(Exception e) {
41: ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
42: PrintStream printStream = new PrintStream(byteArrayOutputStream);
43: e.printStackTrace(printStream);
44: printStream.flush();
45: return byteArrayOutputStream.toString();
46: }
47:
48: private boolean checkIsShowing(JComponent c) {
49: if (this .checkIsShowing == false) {
50: return true;
51: } else {
52: return c.isShowing();
53: }
54: }
55:
56: public synchronized void addDirtyRegion(JComponent jComponent,
57: int i, int i1, int i2, int i3) {
58: checkThread(jComponent);
59: super .addDirtyRegion(jComponent, i, i1, i2, i3);
60: }
61:
62: private void dumpComponentTree(Component c) {
63: System.out.println("----------Component Tree");
64: resetTabCount();
65: for (; c != null; c = c.getParent()) {
66: printTabIndent();
67: System.out.println(c);
68: printTabIndent();
69: System.out.println("Showing:" + c.isShowing()
70: + " Visible: " + c.isVisible());
71: incrementTabCount();
72: }
73: }
74:
75: private void resetTabCount() {
76: this .tabCount = 0;
77: }
78:
79: private void incrementTabCount() {
80: this .tabCount++;
81: }
82:
83: private void printTabIndent() {
84: for (int i = 0; i < this .tabCount; i++) {
85: System.out.print("\t");
86: }
87: }
88: }
|