/*------------------------------------------------------------------------------------
Copyright (c)2000 TogetherSoft Corporation. Patents pending. All rights reserved.
------------------------------------------------------------------------------------*/
package com.togethersoft.modules.xmleditor.utils;
       
import java.util.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.datatransfer.*;

import com.togethersoft.openapi.ide.message.*;


/**
 * @author TogetherSoft
 */
public class DebugTools 
{
  //--------------------------------------------------------------------
  public static void showException (Component parent, Throwable e)
  {
    StringWriter stringWriter = new StringWriter();
    e.printStackTrace (new PrintWriter (stringWriter, true));

    String strStackTrace = stringWriter.toString();
    strStackTrace = removeChars (strStackTrace, "\r\t");

    // transfer the text to clipboard
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    StringSelection contents = new StringSelection (strStackTrace);
    clipboard.setContents (contents, defaultClipboardOwner);  

    IdeMessageManagerAccess.printMessage (IdeMessageType.ERROR_MODAL,  
                      "Exception:\n" + e.toString() + 
                      "\nSee clipboard contents for detailed description...");
  }

  static class ClipboardObserver implements ClipboardOwner {
      public void lostOwnership(Clipboard clipboard, Transferable contents) { }
  }
  private static ClipboardOwner defaultClipboardOwner = new ClipboardObserver();

  public static String removeChars (String strSource, String chars)
  {
    int i;
    char[] strArray = strSource.toCharArray();
    String strResult = new String();

    for (i = 0; i < strArray.length; i ++)
    {
      char ch = strArray [i];
      if (chars.indexOf (ch) < 0)
        strResult += ch;
    }

    return strResult;
  }

}



