]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/ui/org/eclipse/jdt/internal/ui/javadocexport/JavadocConsoleLineTracker.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / internal / ui / javadocexport / JavadocConsoleLineTracker.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2010 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.filesystem.URIUtil;
14
15 import org.eclipse.core.runtime.IPath;
16 import org.eclipse.core.runtime.Path;
17
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.resources.ResourcesPlugin;
20
21 import org.eclipse.jface.text.BadLocationException;
22 import org.eclipse.jface.text.IDocument;
23 import org.eclipse.jface.text.IRegion;
24
25 import org.eclipse.ui.IEditorPart;
26 import org.eclipse.ui.PartInitException;
27 import org.eclipse.ui.console.IHyperlink;
28
29 import org.eclipse.ui.texteditor.ITextEditor;
30
31 import org.eclipse.debug.ui.console.IConsole;
32 import org.eclipse.debug.ui.console.IConsoleLineTracker;
33
34 import org.eclipse.jdt.core.IJavaElement;
35 import org.eclipse.jdt.core.JavaCore;
36 import org.eclipse.jdt.core.JavaModelException;
37
38 import org.eclipse.jdt.ui.JavaUI;
39
40 import org.eclipse.jdt.internal.ui.JavaPlugin;
41
42
43 public class JavadocConsoleLineTracker implements IConsoleLineTracker {
44
45         private static class JavadocConsoleHyperLink implements IHyperlink {
46
47                 private IPath fExternalPath;
48                 private int fLineNumber;
49
50                 public JavadocConsoleHyperLink(IPath externalPath, int lineNumber) {
51                         fExternalPath= externalPath;
52                         fLineNumber= lineNumber;
53                 }
54
55                 /* (non-Javadoc)
56                  * @see org.eclipse.debug.ui.console.IConsoleHyperlink#linkEntered()
57                  */
58                 public void linkEntered() {
59                 }
60
61                 /* (non-Javadoc)
62                  * @see org.eclipse.debug.ui.console.IConsoleHyperlink#linkExited()
63                  */
64                 public void linkExited() {
65                 }
66
67                 /* (non-Javadoc)
68                  * @see org.eclipse.debug.ui.console.IConsoleHyperlink#linkActivated()
69                  */
70                 public void linkActivated() {
71                         try {
72                                 IFile[] files= ResourcesPlugin.getWorkspace().getRoot().findFilesForLocationURI(URIUtil.toURI(fExternalPath.makeAbsolute()));
73                                 if (files.length > 0) {
74                                         for (int i = 0; i < files.length; i++) {
75                                                 IFile curr= files[0];
76                                                 IJavaElement element= JavaCore.create(curr);
77                                                 if (element != null && element.exists()) {
78                                                         IEditorPart part= JavaUI.openInEditor(element, true, false);
79                                                         if (part instanceof ITextEditor) {
80                                                                 revealLine((ITextEditor) part, fLineNumber);
81                                                         }
82                                                         return;
83                                                 }
84                                         }
85                                 }
86                         } catch (BadLocationException e) {
87                                 JavaPlugin.log(e);
88                         } catch (PartInitException e) {
89                                 JavaPlugin.log(e);
90                         } catch (JavaModelException e) {
91                                 JavaPlugin.log(e);
92                         }
93                 }
94
95                 private void revealLine(ITextEditor editor, int lineNumber) throws BadLocationException {
96                         IDocument document= editor.getDocumentProvider().getDocument(editor.getEditorInput());
97                         IRegion region= document.getLineInformation(lineNumber - 1);
98                         editor.selectAndReveal(region.getOffset(), 0);
99                 }
100
101         }
102
103
104         private IConsole fConsole;
105
106         /**
107          *
108          */
109         public JavadocConsoleLineTracker() {
110         }
111
112         /* (non-Javadoc)
113          * @see org.eclipse.debug.ui.console.IConsoleLineTracker#init(org.eclipse.debug.ui.console.IConsole)
114          */
115         public void init(IConsole console) {
116                 fConsole= console;
117         }
118
119         /* (non-Javadoc)
120          * @see org.eclipse.debug.ui.console.IConsoleLineTracker#lineAppended(org.eclipse.jface.text.IRegion)
121          */
122         public void lineAppended(IRegion line) {
123                 try {
124                         int offset = line.getOffset();
125                         int length = line.getLength();
126                         String text = fConsole.getDocument().get(offset, length);
127
128                         int index1= text.indexOf(':');
129                         if (index1 == -1) {
130                                 return;
131                         }
132
133                         int lineNumber= -1;
134                         IPath path= null;
135                         int index2= text.indexOf(':', index1 + 1);
136                         while ((index2 != -1) && (path == null)) {
137                                 if (index1 < index2) {
138                                         try {
139                                                 String substr= text.substring(index1 + 1, index2);
140                                                 lineNumber= Integer.parseInt(substr);
141                                                 path= Path.fromOSString(text.substring(0, index1));
142                                         } catch (NumberFormatException e) {
143                                                 // ignore
144                                         }
145                                 }
146                                 index1= index2;
147                                 index2= text.indexOf(':', index1 + 1);
148                         }
149
150                         if (lineNumber != -1) {
151                                 JavadocConsoleHyperLink link= new JavadocConsoleHyperLink(path, lineNumber);
152                                 fConsole.addLink(link, line.getOffset(), index1);
153
154                         }
155                 } catch (BadLocationException e) {
156                         // ignore
157                 }
158         }
159
160
161
162         /* (non-Javadoc)
163          * @see org.eclipse.debug.ui.console.IConsoleLineTracker#dispose()
164          */
165         public void dispose() {
166                 fConsole = null;
167         }
168
169 }