~ubuntu-branches/ubuntu/trusty/eclipse-linuxtools/trusty

« back to all changes in this revision

Viewing changes to changelog/org.eclipse.linuxtools.changelog.core/src/org/eclipse/linuxtools/internal/changelog/core/editors/GNUHyperlinkDetector.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2012-06-29 12:07:30 UTC
  • Revision ID: package-import@ubuntu.com-20120629120730-bfri1xys1i71dpn6
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
7
 *
 
8
 * Contributors:
 
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;
 
13
 
 
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;
 
35
 
 
36
/**
 
37
 * 
 
38
 * @author klee (Kyu Lee)
 
39
 */
 
40
public class GNUHyperlinkDetector extends AbstractHyperlinkDetector {
 
41
 
 
42
        private IPath documentLocation;
 
43
 
 
44
        public GNUHyperlinkDetector() {
 
45
        }
 
46
        
 
47
        /**
 
48
         * Creates a new URL hyperlink detector for GNU Format changelogs.
 
49
         * 
 
50
         * NOTE: It assumes that the path this ChangeLog is in, is root
 
51
         * directory of path names in this ChangeLog.
 
52
         * 
 
53
         * ex) ChangeLog is in /some/project and in ChangeLog, path names are like
 
54
         * abc/file.java ghi/file2.java
 
55
         * 
 
56
         * then absolute path of file.java and file2.java are
 
57
         * /some/project/abc/file.java and /some/project/ghi/file2.java
 
58
         * 
 
59
         * @param textViewer The text viewer in which to detect the hyperlink.
 
60
         */
 
61
        public GNUHyperlinkDetector(ITextViewer textViewer, TextEditor editor) {
 
62
                Assert.isNotNull(textViewer);
 
63
 
 
64
                documentLocation = getDocumentLocation(editor);
 
65
 
 
66
        }
 
67
 
 
68
        /**
 
69
         * Detector using RuleBasedScanner.
 
70
         */
 
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);
 
76
                }
 
77
 
 
78
                IDocument thisDoc = textViewer.getDocument();
 
79
 
 
80
                GNUElementScanner scanner = new GNUElementScanner();
 
81
 
 
82
                scanner.setDefaultReturnToken(new Token("default"));
 
83
 
 
84
                ITypedRegion partitionInfo = null;
 
85
 
 
86
                try {
 
87
                        partitionInfo = thisDoc.getPartition(region.getOffset());
 
88
                } catch (org.eclipse.jface.text.BadLocationException e1) {
 
89
                        e1.printStackTrace();
 
90
                        return null;
 
91
                }
 
92
 
 
93
                scanner.setRange(thisDoc, partitionInfo.getOffset(), partitionInfo.getLength());
 
94
 
 
95
                Token tmpToken = (Token) scanner.nextToken();
 
96
 
 
97
                String tokenStr = (String) tmpToken.getData();
 
98
 
 
99
                if (tokenStr == null) {
 
100
                        return null;
 
101
                }
 
102
 
 
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)
 
110
                                return null;
 
111
                }
 
112
 
 
113
                Region tokenRegion = new Region(scanner.getTokenOffset(), scanner
 
114
                                .getTokenLength());
 
115
 
 
116
                String line = "";
 
117
                try {
 
118
                        line = thisDoc
 
119
                                        .get(tokenRegion.getOffset(), tokenRegion.getLength());
 
120
                } catch (org.eclipse.jface.text.BadLocationException e1) {
 
121
                        e1.printStackTrace();
 
122
                        return null;
 
123
                }
 
124
 
 
125
                // process file link
 
126
                if (tokenStr.equals(GNUElementScanner.FILE_NAME)) {
 
127
 
 
128
                        Region pathRegion = null;
 
129
 
 
130
                        int lineOffset = 0;
 
131
                        
 
132
                        // cut "* " if necessary
 
133
                        if (line.startsWith("* ")) {
 
134
                                lineOffset = 2;
 
135
                                line = line.substring(2);
 
136
                        }
 
137
//                      int trailingWhiteSpace;
 
138
//                      if (((trailingWhiteSpace = line.indexOf(":")) > 0)
 
139
//                                      || ((trailingWhiteSpace = line.indexOf(" ")) > 0)) {
 
140
//
 
141
//                              line = line.substring(0, trailingWhiteSpace);
 
142
//                              pathRegion = new Region(tokenRegion.getOffset() + lineOffset,
 
143
//                                              trailingWhiteSpace);
 
144
//                      } else {
 
145
                                pathRegion = new Region(tokenRegion.getOffset() + lineOffset, line
 
146
                                                .length());
 
147
//                      }
 
148
                        
 
149
                        
 
150
                        if (documentLocation == null)
 
151
                                return null;
 
152
 
 
153
                        // Replace any escape characters added to name
 
154
                        line = line.replaceAll("\\\\(.)", "$1");
 
155
 
 
156
                        IPath filePath = documentLocation.append(line);
 
157
 
 
158
                        return new IHyperlink[] { new FileHyperlink(pathRegion,
 
159
                                        ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(
 
160
                                                        filePath)) };
 
161
 
 
162
                }
 
163
 
 
164
                return null;
 
165
        }
 
166
 
 
167
        private IWorkspaceRoot getWorkspaceRoot() {
 
168
                return ResourcesPlugin.getWorkspace().getRoot();
 
169
        }
 
170
 
 
171
        /**
 
172
         * Get current directory that ChangeLog is in.
 
173
         * 
 
174
         * @param currentEditor
 
175
         * @return path that this ChangeLog is in
 
176
         */
 
177
        private IPath getDocumentLocation(IEditorPart currentEditor) {
 
178
                IWorkspaceRoot myWorkspaceRoot = getWorkspaceRoot();
 
179
                String WorkspaceRoot = myWorkspaceRoot.getLocation().toOSString();
 
180
                IEditorInput cc = currentEditor.getEditorInput();
 
181
 
 
182
                if (cc instanceof IFileEditorInput) {
 
183
                        IFileEditorInput test = (IFileEditorInput) cc;
 
184
                        IFile loc = test.getFile();
 
185
 
 
186
                        IPath docLoc = loc.getLocation();
 
187
                        docLoc = docLoc.removeLastSegments(1);
 
188
                        return docLoc;
 
189
 
 
190
                }
 
191
 
 
192
                if ((cc instanceof SyncInfoCompareInput)
 
193
                                || (cc instanceof CompareEditorInput)) {
 
194
 
 
195
                        CompareEditorInput test = (CompareEditorInput) cc;
 
196
                        if (test.getCompareResult() == null)
 
197
                                return null;
 
198
 
 
199
                        IPath docLoc = new Path(WorkspaceRoot
 
200
                                        + test.getCompareResult().toString());
 
201
                        docLoc = docLoc.removeLastSegments(1);
 
202
                        return docLoc;
 
203
 
 
204
                }
 
205
 
 
206
                return null;
 
207
        }
 
208
 
 
209
}