KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > javadocexport > JavadocConsoleLineTracker


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.ui.javadocexport;
12
13 import org.eclipse.core.runtime.IPath;
14 import org.eclipse.core.runtime.Path;
15
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.ResourcesPlugin;
18
19 import org.eclipse.jface.text.BadLocationException;
20 import org.eclipse.jface.text.IDocument;
21 import org.eclipse.jface.text.IRegion;
22
23 import org.eclipse.ui.IEditorPart;
24 import org.eclipse.ui.PartInitException;
25 import org.eclipse.ui.texteditor.ITextEditor;
26
27 import org.eclipse.ui.console.IHyperlink;
28
29 import org.eclipse.debug.ui.console.IConsole;
30 import org.eclipse.debug.ui.console.IConsoleLineTracker;
31
32 import org.eclipse.jdt.core.IJavaElement;
33 import org.eclipse.jdt.core.JavaCore;
34 import org.eclipse.jdt.core.JavaModelException;
35
36 import org.eclipse.jdt.ui.JavaUI;
37
38 import org.eclipse.jdt.internal.ui.JavaPlugin;
39
40 public class JavadocConsoleLineTracker implements IConsoleLineTracker {
41     
42     private static class JavadocConsoleHyperLink implements IHyperlink {
43         
44         private IPath fExternalPath;
45         private int fLineNumber;
46
47         public JavadocConsoleHyperLink(IPath externalPath, int lineNumber) {
48             fExternalPath= externalPath;
49             fLineNumber= lineNumber;
50         }
51
52         /* (non-Javadoc)
53          * @see org.eclipse.debug.ui.console.IConsoleHyperlink#linkEntered()
54          */

55         public void linkEntered() {
56         }
57
58         /* (non-Javadoc)
59          * @see org.eclipse.debug.ui.console.IConsoleHyperlink#linkExited()
60          */

61         public void linkExited() {
62         }
63
64         /* (non-Javadoc)
65          * @see org.eclipse.debug.ui.console.IConsoleHyperlink#linkActivated()
66          */

67         public void linkActivated() {
68             try {
69                 IFile[] files= ResourcesPlugin.getWorkspace().getRoot().findFilesForLocation(fExternalPath);
70                 if (files.length > 0) {
71                     for (int i = 0; i < files.length; i++) {
72                         IFile curr= files[0];
73                         IJavaElement element= JavaCore.create(curr);
74                         if (element != null && element.exists()) {
75                             IEditorPart part= JavaUI.openInEditor(element, true, false);
76                             if (part instanceof ITextEditor) {
77                                 revealLine((ITextEditor) part, fLineNumber);
78                             }
79                             return;
80                         }
81                     }
82                 }
83             } catch (BadLocationException e) {
84                 JavaPlugin.log(e);
85             } catch (PartInitException e) {
86                 JavaPlugin.log(e);
87             } catch (JavaModelException e) {
88                 JavaPlugin.log(e);
89             }
90         }
91         
92         private void revealLine(ITextEditor editor, int lineNumber) throws BadLocationException {
93             IDocument document= editor.getDocumentProvider().getDocument(editor.getEditorInput());
94             IRegion region= document.getLineInformation(lineNumber - 1);
95             editor.selectAndReveal(region.getOffset(), 0);
96         }
97         
98     }
99     
100
101     private IConsole fConsole;
102
103     /**
104      *
105      */

106     public JavadocConsoleLineTracker() {
107     }
108
109     /* (non-Javadoc)
110      * @see org.eclipse.debug.ui.console.IConsoleLineTracker#init(org.eclipse.debug.ui.console.IConsole)
111      */

112     public void init(IConsole console) {
113         fConsole= console;
114     }
115
116     /* (non-Javadoc)
117      * @see org.eclipse.debug.ui.console.IConsoleLineTracker#lineAppended(org.eclipse.jface.text.IRegion)
118      */

119     public void lineAppended(IRegion line) {
120         try {
121             int offset = line.getOffset();
122             int length = line.getLength();
123             String JavaDoc text = fConsole.getDocument().get(offset, length);
124             
125             int index1= text.indexOf(':');
126             if (index1 == -1) {
127                 return;
128             }
129             
130             int lineNumber= -1;
131             IPath path= null;
132             int index2= text.indexOf(':', index1 + 1);
133             while ((index2 != -1) && (path == null)) {
134                 if (index1 < index2) {
135                     try {
136                         String JavaDoc substr= text.substring(index1 + 1, index2);
137                         lineNumber= Integer.parseInt(substr);
138                         path= Path.fromOSString(text.substring(0, index1));
139                     } catch (NumberFormatException JavaDoc e) {
140                         // ignore
141
}
142                 }
143                 index1= index2;
144                 index2= text.indexOf(':', index1 + 1);
145             }
146             
147             if (lineNumber != -1) {
148                 JavadocConsoleHyperLink link= new JavadocConsoleHyperLink(path, lineNumber);
149                 fConsole.addLink(link, line.getOffset(), index1);
150
151             }
152         } catch (BadLocationException e) {
153             // ignore
154
}
155     }
156
157
158
159     /* (non-Javadoc)
160      * @see org.eclipse.debug.ui.console.IConsoleLineTracker#dispose()
161      */

162     public void dispose() {
163         fConsole = null;
164     }
165
166 }
167
Popular Tags