001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.core.gui.base;
019:
020: import java.awt.Component;
021: import java.io.ByteArrayOutputStream;
022: import java.io.PrintStream;
023:
024: import javax.swing.JComponent;
025: import javax.swing.RepaintManager;
026: import javax.swing.SwingUtilities;
027:
028: /**
029: * Debugging calls to swing gui elements which are accessed outside the java
030: * awt-event dispatcher thread.
031: *
032: * @author fdietz
033: */
034: public class DebugRepaintManager extends RepaintManager {
035:
036: private int tabCount = 0;
037:
038: private boolean checkIsShowing = true;
039:
040: public DebugRepaintManager() {
041: super ();
042: }
043:
044: public DebugRepaintManager(boolean checkIsShowing) {
045: super ();
046: this .checkIsShowing = checkIsShowing;
047: }
048:
049: public synchronized void addInvalidComponent(JComponent jComponent) {
050: checkThread(jComponent);
051: super .addInvalidComponent(jComponent);
052: }
053:
054: private void checkThread(JComponent c) {
055: if (!SwingUtilities.isEventDispatchThread()
056: && checkIsShowing(c)) {
057: System.err.println("----------Wrong Thread START"); //$NON-NLS-1$
058: System.err.println(getStracktraceAsString(new Exception()));
059: dumpComponentTree(c);
060: System.err.println("----------Wrong Thread END"); //$NON-NLS-1$
061: }
062: }
063:
064: private String getStracktraceAsString(Exception e) {
065: ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
066: PrintStream printStream = new PrintStream(byteArrayOutputStream);
067: e.printStackTrace(printStream);
068: printStream.flush();
069: return byteArrayOutputStream.toString();
070: }
071:
072: private boolean checkIsShowing(JComponent c) {
073: if (this .checkIsShowing == false) {
074: return true;
075: } else {
076: return c.isShowing();
077: }
078: }
079:
080: public synchronized void addDirtyRegion(JComponent jComponent,
081: int i, int i1, int i2, int i3) {
082: checkThread(jComponent);
083: super .addDirtyRegion(jComponent, i, i1, i2, i3);
084: }
085:
086: private void dumpComponentTree(Component c) {
087: System.err.println("----------Component Tree"); //$NON-NLS-1$
088: resetTabCount();
089: for (; c != null; c = c.getParent()) {
090: printTabIndent();
091: System.err.println(c.toString());
092: printTabIndent();
093: System.err
094: .println("Showing:" + c.isShowing() + " Visible: " + c.isVisible()); //$NON-NLS-2$
095: incrementTabCount();
096: }
097: }
098:
099: private void resetTabCount() {
100: this .tabCount = 0;
101: }
102:
103: private void incrementTabCount() {
104: this .tabCount++;
105: }
106:
107: private void printTabIndent() {
108: for (int i = 0; i < this .tabCount; i++) {
109: System.err.print("\t");
110: }
111: }
112: }
|