1 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 35 public BuildHyperlinkDetector(PDESourcePage page) { 36 fSourcePage = page; 37 } 38 39 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 name = entry.getName(); 56 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 value = doc.get(header.getOffset(), header.getLength()); 72 int offset = mainOffset - header.getOffset(); 73 if (skipChar(value.charAt(offset))) 74 return null; 75 76 String pre = value.substring(0, offset); 78 char[] preChars = pre.toCharArray(); 79 int start = pre.lastIndexOf(','); 80 if (start == -1) 81 if ((start = value.indexOf('=')) == 0) 83 return null; 84 85 while (++start < preChars.length) 87 if (!skipChar(preChars[start])) 88 break; 89 90 String post = value.substring(offset); 92 char[] postChars = post.toCharArray(); 93 int end = post.indexOf(','); 94 if (end == -1) 95 end = post.length(); 97 98 while (--end < postChars.length) 100 if (!skipChar(postChars[end])) 101 break; 102 end += 1; 103 104 String 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 |