Patch from Anders Betn�for initializing the PHP source locator.
[phpeclipse.git] / net.sourceforge.phpeclipse.launching / src / net / sourceforge / phpdt / internal / launching / ConsoleLineTracker.java
1 package net.sourceforge.phpdt.internal.launching;
2
3 import net.sourceforge.phpdt.core.JavaModelException;
4 import net.sourceforge.phpeclipse.phpeditor.EditorUtility;
5
6 import org.eclipse.core.resources.IFile;
7 import org.eclipse.core.resources.ResourcesPlugin;
8 import org.eclipse.core.runtime.IPath;
9 import org.eclipse.core.runtime.Path;
10 import org.eclipse.debug.ui.console.IConsole;
11 // TODO XXX Disabled for 3.1 import org.eclipse.debug.ui.console.IConsoleHyperlink;
12 import org.eclipse.debug.ui.console.IConsoleLineTracker;
13 import org.eclipse.jface.text.BadLocationException;
14 import org.eclipse.jface.text.IDocument;
15 import org.eclipse.jface.text.IRegion;
16 import org.eclipse.ui.IEditorPart;
17 import org.eclipse.ui.PartInitException;
18 import org.eclipse.ui.texteditor.ITextEditor;
19
20
21 public class ConsoleLineTracker implements IConsoleLineTracker {
22     
23         private static class JavadocConsoleHyperLink /* TODO XXX Disabled for 3.1 implements IConsoleHyperlink */ {
24                 
25                 private IPath fExternalPath;
26                 private int fLineNumber;
27
28                 public JavadocConsoleHyperLink(IPath externalPath, int lineNumber) {
29                         fExternalPath= externalPath;
30                         fLineNumber= lineNumber;
31                 }
32
33                 /* (non-Javadoc)
34                  * @see org.eclipse.debug.ui.console.IConsoleHyperlink#linkEntered()
35                  */
36                 public void linkEntered() {
37                 }
38
39                 /* (non-Javadoc)
40                  * @see org.eclipse.debug.ui.console.IConsoleHyperlink#linkExited()
41                  */
42                 public void linkExited() {
43                 }
44
45                 /* (non-Javadoc)
46                  * @see org.eclipse.debug.ui.console.IConsoleHyperlink#linkActivated()
47                  */
48                 public void linkActivated() {
49                         try {
50                                 IFile[] files= ResourcesPlugin.getWorkspace().getRoot().findFilesForLocation(fExternalPath);
51                                 if (files.length > 0) {
52                                         for (int i = 0; i < files.length; i++) {
53                                                 IFile curr= files[0];
54                                                 IEditorPart part= EditorUtility.openInEditor(curr, true);
55                                                 if (part != null) {
56                                                         if (part instanceof ITextEditor) {
57                                                                 revealLine((ITextEditor) part, fLineNumber);
58                                                         }
59                                                         return;
60                                                 }
61                                         }
62                                 }       
63                         } catch (BadLocationException e) {
64                                 PHPLaunchingPlugin.log(e);
65                         } catch (PartInitException e) {
66                           PHPLaunchingPlugin.log(e);
67                         } catch (JavaModelException e) {
68                           PHPLaunchingPlugin.log(e);
69                         }
70                 }
71                 
72                 private void revealLine(ITextEditor editor, int lineNumber) throws BadLocationException {
73                         IDocument document= editor.getDocumentProvider().getDocument(editor.getEditorInput());
74                         IRegion region= document.getLineInformation(lineNumber - 1);
75                         editor.selectAndReveal(region.getOffset(), 0);
76                 }
77                 
78         }
79         
80
81         private IConsole fConsole;
82         
83         public ConsoleLineTracker() {
84           super();
85         }
86         
87         /* (non-Javadoc)
88          * @see org.eclipse.debug.ui.console.IConsoleLineTracker#init(org.eclipse.debug.ui.console.IConsole)
89          */
90         public void init(IConsole console) {
91                 fConsole= console;
92         }
93
94         /* (non-Javadoc)
95          * @see org.eclipse.debug.ui.console.IConsoleLineTracker#lineAppended(org.eclipse.jface.text.IRegion)
96          */
97         public void lineAppended(IRegion line) {
98                 try {
99                         int offset = line.getOffset();
100                         int length = line.getLength();
101                         String text = fConsole.getDocument().get(offset, length);
102                         
103                         int index1= text.indexOf(':');
104                         if (index1 == -1) {
105                                 return;
106                         }
107                         
108                         int lineNumber= -1;
109                         IPath path= null;
110                         int index2= text.indexOf(':', index1 + 1);
111                         while ((index2 != -1) && (path == null)) {
112                                 if (index1 < index2) {
113                                         try {
114                                                 String substr= text.substring(index1 + 1, index2);
115                                                 lineNumber= Integer.parseInt(substr);
116                                                 path= new Path(text.substring(0, index1));
117                                         } catch (NumberFormatException e) {
118                                                 // ignore
119                                         }
120                                 }
121                                 index1= index2;
122                                 index2= text.indexOf(':', index1 + 1);
123                         }
124                         
125                         if (lineNumber != -1) {
126                                 JavadocConsoleHyperLink link= new JavadocConsoleHyperLink(path, lineNumber);
127                                 // TODO XXX Disabled for 3.1 fConsole.addLink(link, line.getOffset(), index1);
128
129                         }
130                 } catch (BadLocationException e) {
131                         // ignore
132                 }
133         }
134
135
136
137         /* (non-Javadoc)
138          * @see org.eclipse.debug.ui.console.IConsoleLineTracker#dispose()
139          */
140         public void dispose() {
141                 fConsole = null;
142         }
143
144 }