1 package org.columba.core.gui.base; 19 20 import java.awt.Component ; 21 import java.io.ByteArrayOutputStream ; 22 import java.io.PrintStream ; 23 24 import javax.swing.JComponent ; 25 import javax.swing.RepaintManager ; 26 import javax.swing.SwingUtilities ; 27 28 34 public class DebugRepaintManager extends RepaintManager { 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 jComponent) { 50 checkThread(jComponent); 51 super.addInvalidComponent(jComponent); 52 } 53 54 private void checkThread(JComponent c) { 55 if (!SwingUtilities.isEventDispatchThread() && checkIsShowing(c)) { 56 System.err.println("----------Wrong Thread START"); System.err.println(getStracktraceAsString(new Exception ())); 58 dumpComponentTree(c); 59 System.err.println("----------Wrong Thread END"); } 61 } 62 63 private String getStracktraceAsString(Exception e) { 64 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream (); 65 PrintStream printStream = new PrintStream (byteArrayOutputStream); 66 e.printStackTrace(printStream); 67 printStream.flush(); 68 return byteArrayOutputStream.toString(); 69 } 70 71 private boolean checkIsShowing(JComponent c) { 72 if (this.checkIsShowing == false) { 73 return true; 74 } else { 75 return c.isShowing(); 76 } 77 } 78 79 public synchronized void addDirtyRegion(JComponent 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 c) { 86 System.err.println("----------Component Tree"); 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()); 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 |