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