KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > build > BuildHyperlinkDetector


1 /*******************************************************************************
2  * Copyright (c) 2006 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.pde.internal.ui.editor.build;
12
13 import org.eclipse.core.resources.IResource;
14 import org.eclipse.jface.text.BadLocationException;
15 import org.eclipse.jface.text.IDocument;
16 import org.eclipse.jface.text.IRegion;
17 import org.eclipse.jface.text.ITextViewer;
18 import org.eclipse.jface.text.Region;
19 import org.eclipse.jface.text.hyperlink.IHyperlink;
20 import org.eclipse.jface.text.hyperlink.IHyperlinkDetector;
21 import org.eclipse.pde.core.build.IBuildEntry;
22 import org.eclipse.pde.internal.core.text.IDocumentRange;
23 import org.eclipse.pde.internal.core.text.IEditingModel;
24 import org.eclipse.pde.internal.core.text.build.BuildEntry;
25 import org.eclipse.pde.internal.ui.editor.PDESourcePage;
26 import org.eclipse.pde.internal.ui.editor.text.ResourceHyperlink;
27
28 public class BuildHyperlinkDetector implements IHyperlinkDetector {
29
30     private PDESourcePage fSourcePage;
31     
32     /**
33      * @param editor the editor in which to detect the hyperlink
34      */

35     public BuildHyperlinkDetector(PDESourcePage page) {
36         fSourcePage = page;
37     }
38
39     /*
40      * @see org.eclipse.jface.text.hyperlink.IHyperlinkDetector#detectHyperlinks(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion, boolean)
41      */

42     public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
43         if (region == null || canShowMultipleHyperlinks)
44             return null;
45
46         IDocumentRange element = fSourcePage.getRangeElement(region.getOffset(), true);
47         if (!(element instanceof BuildEntry))
48             return null;
49         
50         BuildEntry entry = (BuildEntry)element;
51         if (!entry.getModel().isEditable() || !(entry.getModel() instanceof IEditingModel))
52             return null;
53         
54         
55         String JavaDoc name = entry.getName();
56         // as of now only scanning bin.includes, src.includes and source.* entries
57
if (!name.equals(IBuildEntry.BIN_INCLUDES) &&
58                 !name.equals(IBuildEntry.SRC_INCLUDES) &&
59                 !name.startsWith(IBuildEntry.JAR_PREFIX))
60             return null;
61         
62         if (region.getOffset() <= entry.getOffset() + entry.getName().length())
63             return null;
64         
65         return matchLinkFor(entry, region.getOffset());
66     }
67
68     private IHyperlink[] matchLinkFor(BuildEntry header, int mainOffset) {
69         try {
70             IDocument doc = ((IEditingModel)header.getModel()).getDocument();
71             String JavaDoc value = doc.get(header.getOffset(), header.getLength());
72             int offset = mainOffset - header.getOffset();
73             if (skipChar(value.charAt(offset)))
74                 return null;
75             
76             // all chars up to the offset
77
String JavaDoc pre = value.substring(0, offset);
78             char[] preChars = pre.toCharArray();
79             int start = pre.lastIndexOf(',');
80             if (start == -1)
81                 // we are looking at 1st entry, skip to ':'
82
if ((start = value.indexOf('=')) == 0)
83                     return null;
84             
85             // skip to 1st non whitespace char
86
while (++start < preChars.length)
87                 if (!skipChar(preChars[start]))
88                     break;
89             
90             // all chars past to ofset
91
String JavaDoc post = value.substring(offset);
92             char[] postChars = post.toCharArray();
93             int end = post.indexOf(',');
94             if (end == -1)
95                 // we are looking at last entry, skip to end
96
end = post.length();
97             
98             // move back to 1st non whitespace char
99
while (--end < postChars.length)
100                 if (!skipChar(postChars[end]))
101                     break;
102             end += 1;
103             
104             String JavaDoc match = value.substring(start, preChars.length + end);
105             if (match.length() == 0 || match.indexOf('*') != -1)
106                 return null;
107             
108             IResource res = header.getModel().getUnderlyingResource();
109             if (res == null)
110                 return null;
111             res = res.getProject().findMember(match);
112             return new IHyperlink[] {
113                 new ResourceHyperlink(
114                         new Region(header.getOffset() + start, match.length()),
115                         match, res)
116             };
117             
118         } catch (BadLocationException e) {
119         }
120         return null;
121     }
122     
123     private boolean skipChar(char c) {
124         return Character.isWhitespace(c) || c == '\\' || c == ',';
125     }
126     
127 }
128
Popular Tags