1 19 20 package org.netbeans.core; 21 22 import java.io.IOException ; 23 import java.io.PrintStream ; 24 import java.io.PrintWriter ; 25 import java.io.StringWriter ; 26 import java.util.ArrayList ; 27 import java.util.Date ; 28 import java.util.List ; 29 import java.util.concurrent.Callable ; 30 import java.util.logging.Level ; 31 import java.util.logging.LogRecord ; 32 import org.openide.util.Exceptions; 33 34 39 public final class UIExceptions { 40 41 44 private UIExceptions() { 45 } 46 47 public static void annotateUser( 48 Throwable t, 49 String msg, 50 String locMsg, 51 Throwable stackTrace, 52 Date date 53 ) { 54 AnnException ex = AnnException.findOrCreate(t, true); 55 LogRecord rec = new LogRecord (OwnLevel.USER, msg); 56 if (stackTrace != null) { 57 rec.setThrown(stackTrace); 58 } 59 ex.addRecord(rec); 60 61 if (locMsg != null) { 62 Exceptions.attachLocalizedMessage(t, locMsg); 63 } 64 } 65 private static final class OwnLevel extends Level { 66 public static final Level USER = new OwnLevel("USER", 1973); 68 private OwnLevel(String s, int i) { 69 super(s, i); 70 } 71 } private static final class AnnException extends Exception implements Callable <LogRecord []> { 73 private List <LogRecord > records; 74 75 public String getMessage() { 76 StringBuilder sb = new StringBuilder (); 77 String sep = ""; 78 for (LogRecord r : records) { 79 if (r.getMessage() != null) { 80 sb.append(sep); 81 sb.append(r.getMessage()); 82 sep = "\n"; 83 } 84 } 85 return sb.toString(); 86 } 87 88 static AnnException findOrCreate(Throwable t, boolean create) { 89 if (t instanceof AnnException) { 90 return (AnnException)t; 91 } 92 if (t.getCause() == null) { 93 if (create) { 94 t.initCause(new AnnException()); 95 } 96 return (AnnException)t.getCause(); 97 } 98 return findOrCreate(t.getCause(), create); 99 } 100 101 private AnnException() { 102 } 103 104 public synchronized void addRecord(LogRecord rec) { 105 if (records == null) { 106 records = new ArrayList <LogRecord >(); 107 } 108 records.add(rec); 109 } 110 111 public LogRecord [] call() { 112 List <LogRecord > r = records; 113 LogRecord [] empty = new LogRecord [0]; 114 return r == null ? empty : r.toArray(empty); 115 } 116 117 public void printStackTrace(PrintStream s) { 118 super.printStackTrace(s); 119 logRecords(s); 120 } 121 122 public void printStackTrace(PrintWriter s) { 123 super.printStackTrace(s); 124 logRecords(s); 125 } 126 127 public void printStackTrace() { 128 printStackTrace(System.err); 129 } 130 131 private void logRecords(Appendable a) { 132 List <LogRecord > r = records; 133 if (r == null) { 134 return; 135 } 136 try { 137 138 for (LogRecord log : r) { 139 if (log.getMessage() != null) { 140 a.append(log.getMessage()).append("\n");; 141 } 142 if (log.getThrown() != null) { 143 StringWriter w = new StringWriter (); 144 log.getThrown().printStackTrace(new PrintWriter (w)); 145 a.append(w.toString()).append("\n"); 146 } 147 } 148 } catch (IOException ex) { 149 ex.printStackTrace(); 150 } 151 } 152 } } 154 155 | Popular Tags |