1
/*******************************************************************************
2
* Copyright (c) 2006 Red Hat Inc. 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
9
* Kyu Lee <klee@redhat.com> - initial API and implementation
10
* Remy Chi Jian Suen <remy.suen@gmail.com> - clean up internal API references (bug #179389)
11
*******************************************************************************/
12
package org.eclipse.linuxtools.internal.changelog.core.editors;
14
import org.eclipse.compare.CompareEditorInput;
15
import org.eclipse.core.resources.IFile;
16
import org.eclipse.core.resources.IWorkspaceRoot;
17
import org.eclipse.core.resources.ResourcesPlugin;
18
import org.eclipse.core.runtime.Assert;
19
import org.eclipse.core.runtime.IPath;
20
import org.eclipse.core.runtime.Path;
21
import org.eclipse.jface.text.IDocument;
22
import org.eclipse.jface.text.IRegion;
23
import org.eclipse.jface.text.ITextViewer;
24
import org.eclipse.jface.text.ITypedRegion;
25
import org.eclipse.jface.text.Region;
26
import org.eclipse.jface.text.hyperlink.AbstractHyperlinkDetector;
27
import org.eclipse.jface.text.hyperlink.IHyperlink;
28
import org.eclipse.jface.text.rules.Token;
29
import org.eclipse.team.ui.synchronize.SyncInfoCompareInput;
30
import org.eclipse.ui.IEditorInput;
31
import org.eclipse.ui.IEditorPart;
32
import org.eclipse.ui.IFileEditorInput;
33
import org.eclipse.ui.editors.text.TextEditor;
34
import org.eclipse.ui.texteditor.ITextEditor;
38
* @author klee (Kyu Lee)
40
public class GNUHyperlinkDetector extends AbstractHyperlinkDetector {
42
private IPath documentLocation;
44
public GNUHyperlinkDetector() {
48
* Creates a new URL hyperlink detector for GNU Format changelogs.
50
* NOTE: It assumes that the path this ChangeLog is in, is root
51
* directory of path names in this ChangeLog.
53
* ex) ChangeLog is in /some/project and in ChangeLog, path names are like
54
* abc/file.java ghi/file2.java
56
* then absolute path of file.java and file2.java are
57
* /some/project/abc/file.java and /some/project/ghi/file2.java
59
* @param textViewer The text viewer in which to detect the hyperlink.
61
public GNUHyperlinkDetector(ITextViewer textViewer, TextEditor editor) {
62
Assert.isNotNull(textViewer);
64
documentLocation = getDocumentLocation(editor);
69
* Detector using RuleBasedScanner.
71
public IHyperlink[] detectHyperlinks(ITextViewer textViewer,
72
IRegion region, boolean canShowMultipleHyperlinks) {
73
if (documentLocation == null) {
74
ITextEditor ed = (ITextEditor) this.getAdapter(ITextEditor.class);
75
documentLocation = getDocumentLocation(ed);
78
IDocument thisDoc = textViewer.getDocument();
80
GNUElementScanner scanner = new GNUElementScanner();
82
scanner.setDefaultReturnToken(new Token("default"));
84
ITypedRegion partitionInfo = null;
87
partitionInfo = thisDoc.getPartition(region.getOffset());
88
} catch (org.eclipse.jface.text.BadLocationException e1) {
93
scanner.setRange(thisDoc, partitionInfo.getOffset(), partitionInfo.getLength());
95
Token tmpToken = (Token) scanner.nextToken();
97
String tokenStr = (String) tmpToken.getData();
99
if (tokenStr == null) {
103
// try to find non-default token containing region..if none, return null.
104
while (region.getOffset() < scanner.getTokenOffset() ||
105
region.getOffset() > scanner.getOffset() ||
106
tokenStr.equals("default")) {
107
tmpToken = (Token) scanner.nextToken();
108
tokenStr = (String) tmpToken.getData();
109
if (tokenStr == null)
113
Region tokenRegion = new Region(scanner.getTokenOffset(), scanner
119
.get(tokenRegion.getOffset(), tokenRegion.getLength());
120
} catch (org.eclipse.jface.text.BadLocationException e1) {
121
e1.printStackTrace();
126
if (tokenStr.equals(GNUElementScanner.FILE_NAME)) {
128
Region pathRegion = null;
132
// cut "* " if necessary
133
if (line.startsWith("* ")) {
135
line = line.substring(2);
137
// int trailingWhiteSpace;
138
// if (((trailingWhiteSpace = line.indexOf(":")) > 0)
139
// || ((trailingWhiteSpace = line.indexOf(" ")) > 0)) {
141
// line = line.substring(0, trailingWhiteSpace);
142
// pathRegion = new Region(tokenRegion.getOffset() + lineOffset,
143
// trailingWhiteSpace);
145
pathRegion = new Region(tokenRegion.getOffset() + lineOffset, line
150
if (documentLocation == null)
153
// Replace any escape characters added to name
154
line = line.replaceAll("\\\\(.)", "$1");
156
IPath filePath = documentLocation.append(line);
158
return new IHyperlink[] { new FileHyperlink(pathRegion,
159
ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(
167
private IWorkspaceRoot getWorkspaceRoot() {
168
return ResourcesPlugin.getWorkspace().getRoot();
172
* Get current directory that ChangeLog is in.
174
* @param currentEditor
175
* @return path that this ChangeLog is in
177
private IPath getDocumentLocation(IEditorPart currentEditor) {
178
IWorkspaceRoot myWorkspaceRoot = getWorkspaceRoot();
179
String WorkspaceRoot = myWorkspaceRoot.getLocation().toOSString();
180
IEditorInput cc = currentEditor.getEditorInput();
182
if (cc instanceof IFileEditorInput) {
183
IFileEditorInput test = (IFileEditorInput) cc;
184
IFile loc = test.getFile();
186
IPath docLoc = loc.getLocation();
187
docLoc = docLoc.removeLastSegments(1);
192
if ((cc instanceof SyncInfoCompareInput)
193
|| (cc instanceof CompareEditorInput)) {
195
CompareEditorInput test = (CompareEditorInput) cc;
196
if (test.getCompareResult() == null)
199
IPath docLoc = new Path(WorkspaceRoot
200
+ test.getCompareResult().toString());
201
docLoc = docLoc.removeLastSegments(1);