1 11 package org.eclipse.jdt.internal.debug.ui.console; 12 13 import java.io.BufferedInputStream ; 14 import java.io.File ; 15 import java.io.FileInputStream ; 16 import java.io.FileOutputStream ; 17 import java.io.IOException ; 18 import java.io.InputStream ; 19 import java.util.StringTokenizer ; 20 21 import org.eclipse.core.runtime.IProgressMonitor; 22 import org.eclipse.core.runtime.IStatus; 23 import org.eclipse.core.runtime.OperationCanceledException; 24 import org.eclipse.core.runtime.Status; 25 import org.eclipse.core.runtime.jobs.IJobManager; 26 import org.eclipse.core.runtime.jobs.Job; 27 import org.eclipse.debug.ui.IDebugUIConstants; 28 import org.eclipse.jdt.internal.debug.ui.IJavaDebugHelpContextIds; 29 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin; 30 import org.eclipse.jface.resource.JFaceResources; 31 import org.eclipse.jface.text.IDocument; 32 import org.eclipse.jface.text.rules.FastPartitioner; 33 import org.eclipse.jface.text.rules.RuleBasedPartitionScanner; 34 import org.eclipse.jface.util.IPropertyChangeListener; 35 import org.eclipse.jface.util.PropertyChangeEvent; 36 import org.eclipse.swt.custom.StyleRange; 37 import org.eclipse.swt.graphics.Font; 38 import org.eclipse.ui.console.IConsoleDocumentPartitioner; 39 import org.eclipse.ui.console.IConsoleView; 40 import org.eclipse.ui.console.TextConsole; 41 import org.eclipse.ui.part.IPageBookViewPage; 42 import org.eclipse.ui.progress.WorkbenchJob; 43 44 47 public class JavaStackTraceConsole extends TextConsole { 48 49 52 class JavaStackTraceConsolePartitioner extends FastPartitioner implements IConsoleDocumentPartitioner { 53 54 public JavaStackTraceConsolePartitioner() { 55 super(new RuleBasedPartitionScanner(), null); 56 getDocument().setDocumentPartitioner(this); 57 } 58 59 public boolean isReadOnly(int offset) { 60 return false; 61 } 62 63 public StyleRange[] getStyleRanges(int offset, int length) { 64 return null; 65 } 66 67 } 68 69 public final static String CONSOLE_TYPE = "javaStackTraceConsole"; public final static String FILE_NAME = JDIDebugUIPlugin.getDefault().getStateLocation().toOSString() + File.separator + "stackTraceConsole.txt"; 72 private JavaStackTraceConsolePartitioner partitioner = new JavaStackTraceConsolePartitioner(); 73 private IPropertyChangeListener propertyListener = new IPropertyChangeListener() { 74 public void propertyChange(PropertyChangeEvent event) { 75 String property = event.getProperty(); 76 if (property.equals(IDebugUIConstants.PREF_CONSOLE_FONT)) { 77 setFont(JFaceResources.getFont(IDebugUIConstants.PREF_CONSOLE_FONT)); 78 } 79 } 80 }; 81 82 85 public JavaStackTraceConsole() { 86 super(ConsoleMessages.JavaStackTraceConsoleFactory_0, CONSOLE_TYPE, null, true); 87 Font font = JFaceResources.getFont(IDebugUIConstants.PREF_CONSOLE_FONT); 88 setFont(font); 89 partitioner.connect(getDocument()); 90 } 91 92 95 void initializeDocument() { 96 File file = new File (FILE_NAME); 97 if (file.exists()) { 98 try { 99 int len = (int) file.length(); 100 byte[] b = new byte[len]; 101 InputStream fin = new BufferedInputStream (new FileInputStream (file)); 102 int read = 0; 103 while (read < len) { 104 read += fin.read(b); 105 } 106 getDocument().set(new String (b)); 107 fin.close(); 108 } catch (IOException e) { 109 } 110 } else { 111 getDocument().set(ConsoleMessages.JavaStackTraceConsole_0); 112 } 113 } 114 115 118 protected void init() { 119 JFaceResources.getFontRegistry().addListener(propertyListener); 120 } 121 122 125 protected void dispose() { 126 saveDocument(); 127 JFaceResources.getFontRegistry().removeListener(propertyListener); 128 super.dispose(); 129 } 130 131 134 void saveDocument() { 135 try { 136 IDocument document = getDocument(); 137 if (document != null) { 138 if (document.getLength() > 0) { 139 String contents = document.get(); 140 FileOutputStream fout = new FileOutputStream (FILE_NAME); 141 fout.write(contents.getBytes()); 142 fout.close(); 143 } else { 144 File file = new File (FILE_NAME); 145 file.delete(); 146 } 147 } 148 } 149 catch (IOException e) {} 150 } 151 152 155 protected IConsoleDocumentPartitioner getPartitioner() { 156 return partitioner; 157 } 158 159 162 public String getHelpContextId() { 163 return IJavaDebugHelpContextIds.STACK_TRACE_CONSOLE; 164 } 165 166 169 public IPageBookViewPage createPage(IConsoleView view) { 170 return new JavaStackTraceConsolePage(this, view); 171 } 172 173 176 public void format() { 177 WorkbenchJob job = new WorkbenchJob(ConsoleMessages.JavaStackTraceConsole_1) { 178 public IStatus runInUIThread(IProgressMonitor monitor) { 179 IJobManager jobManager = Job.getJobManager(); 180 try { 181 jobManager.join(this, monitor); 182 } catch (OperationCanceledException e1) { 183 return Status.CANCEL_STATUS; 184 } catch (InterruptedException e1) { 185 return Status.CANCEL_STATUS; 186 } 187 IDocument document = getDocument(); 188 String orig = document.get(); 189 if (orig != null && orig.length() > 0) { 190 document.set(format(orig)); 191 } 192 193 return Status.OK_STATUS; 194 } 195 }; 196 job.setSystem(true); 197 job.schedule(); 198 199 } 200 201 206 private String format(String trace) { 207 StringTokenizer tokenizer = new StringTokenizer (trace, " \t\n\r\f", true); StringBuffer formattedTrace = new StringBuffer (); 209 210 boolean insideAt = false; 211 boolean newLine = true; 212 int pendingSpaces = 0; 213 boolean antTrace = false; 214 215 while (tokenizer.hasMoreTokens()) { 216 String token = tokenizer.nextToken(); 217 if (token.length() == 0) 218 continue; char c = token.charAt(0); 220 switch (c) { 222 case ' ': 223 if (newLine) { 224 pendingSpaces++; 225 } else { 226 pendingSpaces = 1; 227 } 228 continue; 229 case '\t': 230 if (newLine) { 231 pendingSpaces += 4; 232 } else { 233 pendingSpaces = 1; 234 } 235 continue; 236 case '\n': 237 case '\r': 238 case '\f': 239 if (insideAt) { 240 pendingSpaces = 1; 241 } else { 242 pendingSpaces = 0; 243 newLine = true; 244 } 245 continue; 246 } 247 if (newLine || antTrace) { 250 if (c == '\"') { formattedTrace.append("\n\n"); } else if ("-".equals(token)) { formattedTrace.append("\n"); formattedTrace.append(" "); formattedTrace.append(token); 257 pendingSpaces = 0; 258 continue; 259 } else if ("at".equals(token)) { if (!antTrace) { 261 formattedTrace.append("\n"); formattedTrace.append(" "); } else { 264 formattedTrace.append(' '); 265 } 266 insideAt = true; 267 formattedTrace.append(token); 268 pendingSpaces = 0; 269 continue; 270 } else if (c == '[') { 271 if(antTrace) { 272 formattedTrace.append("\n"); } 274 formattedTrace.append(token); 275 pendingSpaces = 0; 276 newLine = false; 277 antTrace = true; 278 continue; 279 } 280 newLine = false; 281 } 282 if (pendingSpaces > 0) { 283 for (int i = 0; i < pendingSpaces; i++) { 284 formattedTrace.append(' '); 285 } 286 pendingSpaces = 0; 287 } 288 formattedTrace.append(token); 289 insideAt = false; 290 } 291 return formattedTrace.toString(); 292 } 293 } 294 | Popular Tags |