KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > console > JavaStackTraceConsole


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.debug.ui.console;
12
13 import java.io.BufferedInputStream JavaDoc;
14 import java.io.File JavaDoc;
15 import java.io.FileInputStream JavaDoc;
16 import java.io.FileOutputStream JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.io.InputStream JavaDoc;
19 import java.util.StringTokenizer JavaDoc;
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 /**
45  * Provides a stack trace console for Java stack traces
46  */

47 public class JavaStackTraceConsole extends TextConsole {
48     
49     /**
50      * Provides a partitioner for this console type
51      */

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 JavaDoc CONSOLE_TYPE = "javaStackTraceConsole"; //$NON-NLS-1$
70
public final static String JavaDoc FILE_NAME = JDIDebugUIPlugin.getDefault().getStateLocation().toOSString() + File.separator + "stackTraceConsole.txt"; //$NON-NLS-1$
71

72     private JavaStackTraceConsolePartitioner partitioner = new JavaStackTraceConsolePartitioner();
73     private IPropertyChangeListener propertyListener = new IPropertyChangeListener() {
74         public void propertyChange(PropertyChangeEvent event) {
75             String JavaDoc property = event.getProperty();
76             if (property.equals(IDebugUIConstants.PREF_CONSOLE_FONT)) {
77                 setFont(JFaceResources.getFont(IDebugUIConstants.PREF_CONSOLE_FONT));
78             }
79         }
80     };
81
82     /**
83      * Constructor
84      */

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     /**
93      * inits the document backing this console
94      */

95     void initializeDocument() {
96         File JavaDoc file = new File JavaDoc(FILE_NAME);
97         if (file.exists()) {
98             try {
99                 int len = (int) file.length();
100                 byte[] b = new byte[len];
101                 InputStream JavaDoc fin = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(file));
102                 int read = 0;
103                 while (read < len) {
104                     read += fin.read(b);
105                 }
106                 getDocument().set(new String JavaDoc(b));
107                 fin.close();
108             } catch (IOException JavaDoc e) {
109             }
110         } else {
111             getDocument().set(ConsoleMessages.JavaStackTraceConsole_0);
112         }
113     }
114
115     /**
116      * @see org.eclipse.ui.console.AbstractConsole#init()
117      */

118     protected void init() {
119         JFaceResources.getFontRegistry().addListener(propertyListener);
120     }
121
122     /**
123      * @see org.eclipse.ui.console.TextConsole#dispose()
124      */

125     protected void dispose() {
126         saveDocument();
127         JFaceResources.getFontRegistry().removeListener(propertyListener);
128         super.dispose();
129     }
130
131     /**
132      * Saves the backing document for this console
133      */

134     void saveDocument() {
135         try {
136             IDocument document = getDocument();
137             if (document != null) {
138                 if (document.getLength() > 0) {
139                     String JavaDoc contents = document.get();
140                     FileOutputStream JavaDoc fout = new FileOutputStream JavaDoc(FILE_NAME);
141                     fout.write(contents.getBytes());
142                     fout.close();
143                 } else {
144                     File JavaDoc file = new File JavaDoc(FILE_NAME);
145                     file.delete();
146                 }
147             }
148         }
149         catch (IOException JavaDoc e) {}
150     }
151
152     /**
153      * @see org.eclipse.ui.console.TextConsole#getPartitioner()
154      */

155     protected IConsoleDocumentPartitioner getPartitioner() {
156         return partitioner;
157     }
158
159     /**
160      * @see org.eclipse.ui.console.AbstractConsole#getHelpContextId()
161      */

162     public String JavaDoc getHelpContextId() {
163         return IJavaDebugHelpContextIds.STACK_TRACE_CONSOLE;
164     }
165     
166     /**
167      * @see org.eclipse.ui.console.TextConsole#createPage(org.eclipse.ui.console.IConsoleView)
168      */

169     public IPageBookViewPage createPage(IConsoleView view) {
170         return new JavaStackTraceConsolePage(this, view);
171     }
172     
173     /**
174      * performs the formatting of the stacktrace console
175      */

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 JavaDoc e1) {
185                     return Status.CANCEL_STATUS;
186                 }
187                 IDocument document = getDocument();
188                 String JavaDoc 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     /**
202      * Underlying format operation
203      * @param trace the stack trace to format
204      * @return the formatted stack trace for this console
205      */

206     private String JavaDoc format(String JavaDoc trace) {
207         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(trace, " \t\n\r\f", true); //$NON-NLS-1$
208
StringBuffer JavaDoc formattedTrace = new StringBuffer JavaDoc();
209         
210         boolean insideAt = false;
211         boolean newLine = true;
212         int pendingSpaces = 0;
213         boolean antTrace = false;
214         
215         while (tokenizer.hasMoreTokens()) {
216             String JavaDoc token = tokenizer.nextToken();
217             if (token.length() == 0)
218                 continue; // paranoid
219
char c = token.charAt(0);
220             // handle delimiters
221
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             // consider newlines only before token starting with char '\"' or
248
// token "at" or "-".
249
if (newLine || antTrace) {
250                 if (c == '\"') { // leading thread name, e.g. "Worker-124"
251
// prio=5
252
formattedTrace.append("\n\n"); //$NON-NLS-1$ print 2 lines to break between threads
253
} else if ("-".equals(token)) { //$NON-NLS-1$ - locked ...
254
formattedTrace.append("\n"); //$NON-NLS-1$
255
formattedTrace.append(" "); //$NON-NLS-1$
256
formattedTrace.append(token);
257                     pendingSpaces = 0;
258                     continue;
259                 } else if ("at".equals(token)) { //$NON-NLS-1$ at ...
260
if (!antTrace) {
261                         formattedTrace.append("\n"); //$NON-NLS-1$
262
formattedTrace.append(" "); //$NON-NLS-1$
263
} 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"); //$NON-NLS-1$
273
}
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