KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > gui > base > DebugRepaintManager


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.core.gui.base;
19
20 import java.awt.Component JavaDoc;
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import java.io.PrintStream JavaDoc;
23
24 import javax.swing.JComponent JavaDoc;
25 import javax.swing.RepaintManager JavaDoc;
26 import javax.swing.SwingUtilities JavaDoc;
27
28 /**
29  * Debugging calls to swing gui elements which are accessed outside the java
30  * awt-event dispatcher thread.
31  *
32  * @author fdietz
33  */

34 public class DebugRepaintManager extends RepaintManager JavaDoc {
35
36     private int tabCount = 0;
37
38     private boolean checkIsShowing = true;
39
40     public DebugRepaintManager() {
41         super();
42     }
43
44     public DebugRepaintManager(boolean checkIsShowing) {
45         super();
46         this.checkIsShowing = checkIsShowing;
47     }
48
49     public synchronized void addInvalidComponent(JComponent JavaDoc jComponent) {
50         checkThread(jComponent);
51         super.addInvalidComponent(jComponent);
52     }
53
54     private void checkThread(JComponent JavaDoc c) {
55         if (!SwingUtilities.isEventDispatchThread() && checkIsShowing(c)) {
56             System.err.println("----------Wrong Thread START"); //$NON-NLS-1$
57
System.err.println(getStracktraceAsString(new Exception JavaDoc()));
58             dumpComponentTree(c);
59             System.err.println("----------Wrong Thread END"); //$NON-NLS-1$
60
}
61     }
62
63     private String JavaDoc getStracktraceAsString(Exception JavaDoc e) {
64         ByteArrayOutputStream JavaDoc byteArrayOutputStream = new ByteArrayOutputStream JavaDoc();
65         PrintStream JavaDoc printStream = new PrintStream JavaDoc(byteArrayOutputStream);
66         e.printStackTrace(printStream);
67         printStream.flush();
68         return byteArrayOutputStream.toString();
69     }
70
71     private boolean checkIsShowing(JComponent JavaDoc c) {
72         if (this.checkIsShowing == false) {
73             return true;
74         } else {
75             return c.isShowing();
76         }
77     }
78
79     public synchronized void addDirtyRegion(JComponent JavaDoc jComponent, int i,
80             int i1, int i2, int i3) {
81         checkThread(jComponent);
82         super.addDirtyRegion(jComponent, i, i1, i2, i3);
83     }
84
85     private void dumpComponentTree(Component JavaDoc c) {
86         System.err.println("----------Component Tree"); //$NON-NLS-1$
87
resetTabCount();
88         for (; c != null; c = c.getParent()) {
89             printTabIndent();
90             System.err.println(c.toString());
91             printTabIndent();
92             System.err
93                     .println("Showing:" + c.isShowing() + " Visible: " + c.isVisible()); //$NON-NLS-2$
94
incrementTabCount();
95         }
96     }
97
98     private void resetTabCount() {
99         this.tabCount = 0;
100     }
101
102     private void incrementTabCount() {
103         this.tabCount++;
104     }
105
106     private void printTabIndent() {
107         for (int i = 0; i < this.tabCount; i++) {
108             System.err.print("\t");
109         }
110     }
111 }
Popular Tags