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