1 11 package org.eclipse.pde.internal.ui.search; 12 13 import org.eclipse.jface.text.BadLocationException; 14 import org.eclipse.jface.text.FindReplaceDocumentAdapter; 15 import org.eclipse.jface.text.IDocument; 16 import org.eclipse.jface.text.IRegion; 17 import org.eclipse.jface.text.Region; 18 import org.eclipse.pde.core.plugin.IFragment; 19 import org.eclipse.pde.core.plugin.IPlugin; 20 import org.eclipse.pde.core.plugin.IPluginExtension; 21 import org.eclipse.pde.core.plugin.IPluginExtensionPoint; 22 import org.eclipse.pde.core.plugin.IPluginImport; 23 import org.eclipse.pde.core.plugin.IPluginObject; 24 import org.eclipse.pde.internal.ui.editor.plugin.ManifestEditor; 25 import org.eclipse.search.ui.text.Match; 26 import org.eclipse.ui.IEditorPart; 27 import org.eclipse.ui.PartInitException; 28 29 30 public class ManifestEditorOpener { 31 32 public static IEditorPart open(Match match, boolean activate) throws PartInitException { 33 IEditorPart editorPart = null; 34 editorPart = ManifestEditor.open(match.getElement(), true); 35 if (editorPart != null && editorPart instanceof ManifestEditor) { 36 ManifestEditor editor = (ManifestEditor)editorPart; 37 IDocument doc = editor.getDocument(match); 38 if (doc != null) { 39 Match exact = findExactMatch(doc, match); 40 editor.openToSourcePage(match.getElement(), exact.getOffset(), exact.getLength()); 41 } 42 } 43 return editorPart; 44 } 45 46 public static Match findExactMatch(IDocument document, Match match) { 47 if (match.getOffset() == -1 && match.getBaseUnit() == Match.UNIT_LINE) 48 return new Match(match.getElement(), Match.UNIT_CHARACTER, 0,0); 49 IPluginObject element = (IPluginObject)match.getElement(); 50 String name = null; 51 String value = null; 52 if (element instanceof IPluginExtension) { 53 name = "point"; value = ((IPluginExtension)element).getPoint(); 55 } else if (element instanceof IPluginExtensionPoint) { 56 name = "id"; value = ((IPluginExtensionPoint)element).getId(); 58 } else if (element instanceof IPluginImport) { 59 name = "plugin"; value = ((IPluginImport)element).getId(); 61 } else if (element instanceof IPlugin) { 62 name = "id"; value = ((IPlugin)element).getId(); 64 } else if (element instanceof IFragment) { 65 name = "id"; value = ((IFragment)element).getId(); 67 } 68 IRegion region = getAttributeRegion(document, name, value, match.getOffset()); 69 if (region != null) { 70 return new Match(element, Match.UNIT_CHARACTER, region.getOffset(), region.getLength()); 71 } 72 return match; 73 } 74 75 private static IRegion getAttributeRegion(IDocument document, String name, String value, int line) { 76 try { 77 int offset = document.getLineOffset(line) + document.getLineLength(line); 78 FindReplaceDocumentAdapter findReplaceAdapter = new FindReplaceDocumentAdapter(document); 79 IRegion nameRegion = findReplaceAdapter.find(offset, name+"\\s*=\\s*\""+value, false, false, false, true); if (nameRegion != null) { 81 if (document.get(nameRegion.getOffset() + nameRegion.getLength() - value.length(), value.length()).equals(value)) 82 return new Region(nameRegion.getOffset() + nameRegion.getLength() - value.length(), value.length()); 83 } 84 } catch (BadLocationException e) { 85 } 86 return null; 87 } 88 89 90 } 91 | Popular Tags |