Documentation update from hudson build on Thu 28 Aug 2008 09:25:08 PM CDT
[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 import org.eclipse.debug.ui.console.IConsoleLineTracker;
12 import org.eclipse.jface.text.BadLocationException;
13 import org.eclipse.jface.text.IDocument;
14 import org.eclipse.jface.text.IRegion;
15 import org.eclipse.ui.IEditorPart;
16 import org.eclipse.ui.PartInitException;
17 import org.eclipse.ui.texteditor.ITextEditor;
18
19 public class ConsoleLineTracker implements IConsoleLineTracker {
20
21         private static class JavadocConsoleHyperLink /*
22                                                                                                          * TODO XXX Disabled for 3.1
23                                                                                                          * implements
24                                                                                                          * IConsoleHyperlink
25                                                                                                          */{
26
27                 private IPath fExternalPath;
28
29                 private int fLineNumber;
30
31                 public JavadocConsoleHyperLink(IPath externalPath, int lineNumber) {
32                         fExternalPath = externalPath;
33                         fLineNumber = lineNumber;
34                 }
35
36                 /*
37                  * (non-Javadoc)
38                  * 
39                  * @see org.eclipse.debug.ui.console.IConsoleHyperlink#linkEntered()
40                  */
41                 public void linkEntered() {
42                 }
43
44                 /*
45                  * (non-Javadoc)
46                  * 
47                  * @see org.eclipse.debug.ui.console.IConsoleHyperlink#linkExited()
48                  */
49                 public void linkExited() {
50                 }
51
52                 /*
53                  * (non-Javadoc)
54                  * 
55                  * @see org.eclipse.debug.ui.console.IConsoleHyperlink#linkActivated()
56                  */
57                 public void linkActivated() {
58                         try {
59                                 IFile[] files = ResourcesPlugin.getWorkspace().getRoot()
60                                                 .findFilesForLocation(fExternalPath);
61                                 if (files.length > 0) {
62                                         for (int i = 0; i < files.length; i++) {
63                                                 IFile curr = files[0];
64                                                 IEditorPart part = EditorUtility.openInEditor(curr,
65                                                                 true);
66                                                 if (part != null) {
67                                                         if (part instanceof ITextEditor) {
68                                                                 revealLine((ITextEditor) part, fLineNumber);
69                                                         }
70                                                         return;
71                                                 }
72                                         }
73                                 }
74                         } catch (BadLocationException e) {
75                                 PHPLaunchingPlugin.log(e);
76                         } catch (PartInitException e) {
77                                 PHPLaunchingPlugin.log(e);
78                         } catch (JavaModelException e) {
79                                 PHPLaunchingPlugin.log(e);
80                         }
81                 }
82
83                 private void revealLine(ITextEditor editor, int lineNumber)
84                                 throws BadLocationException {
85                         IDocument document = editor.getDocumentProvider().getDocument(
86                                         editor.getEditorInput());
87                         IRegion region = document.getLineInformation(lineNumber - 1);
88                         editor.selectAndReveal(region.getOffset(), 0);
89                 }
90
91         }
92
93         private IConsole fConsole;
94
95         public ConsoleLineTracker() {
96                 super();
97         }
98
99         /*
100          * (non-Javadoc)
101          * 
102          * @see org.eclipse.debug.ui.console.IConsoleLineTracker#init(org.eclipse.debug.ui.console.IConsole)
103          */
104         public void init(IConsole console) {
105                 fConsole = console;
106         }
107
108         /*
109          * (non-Javadoc)
110          * 
111          * @see org.eclipse.debug.ui.console.IConsoleLineTracker#lineAppended(org.eclipse.jface.text.IRegion)
112          */
113         public void lineAppended(IRegion line) {
114                 try {
115                         int offset = line.getOffset();
116                         int length = line.getLength();
117                         String text = fConsole.getDocument().get(offset, length);
118
119                         int index1 = text.indexOf(':');
120                         if (index1 == -1) {
121                                 return;
122                         }
123
124                         int lineNumber = -1;
125                         IPath path = null;
126                         int index2 = text.indexOf(':', index1 + 1);
127                         while ((index2 != -1) && (path == null)) {
128                                 if (index1 < index2) {
129                                         try {
130                                                 String substr = text.substring(index1 + 1, index2);
131                                                 lineNumber = Integer.parseInt(substr);
132                                                 path = new Path(text.substring(0, index1));
133                                         } catch (NumberFormatException e) {
134                                                 // ignore
135                                         }
136                                 }
137                                 index1 = index2;
138                                 index2 = text.indexOf(':', index1 + 1);
139                         }
140
141                         if (lineNumber != -1) {
142                                 JavadocConsoleHyperLink link = new JavadocConsoleHyperLink(
143                                                 path, lineNumber);
144                                 // TODO XXX Disabled for 3.1 fConsole.addLink(link,
145                                 // line.getOffset(), index1);
146
147                         }
148                 } catch (BadLocationException e) {
149                         // ignore
150                 }
151         }
152
153         /*
154          * (non-Javadoc)
155          * 
156          * @see org.eclipse.debug.ui.console.IConsoleLineTracker#dispose()
157          */
158         public void dispose() {
159                 fConsole = null;
160         }
161
162 }