001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.search;
011:
012: import org.eclipse.jface.text.BadLocationException;
013: import org.eclipse.jface.text.FindReplaceDocumentAdapter;
014: import org.eclipse.jface.text.IDocument;
015: import org.eclipse.jface.text.IRegion;
016: import org.eclipse.jface.text.Region;
017: import org.eclipse.pde.core.plugin.IFragment;
018: import org.eclipse.pde.core.plugin.IPlugin;
019: import org.eclipse.pde.core.plugin.IPluginExtension;
020: import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
021: import org.eclipse.pde.core.plugin.IPluginImport;
022: import org.eclipse.pde.core.plugin.IPluginModelBase;
023: import org.eclipse.pde.core.plugin.IPluginObject;
024: import org.eclipse.pde.internal.core.text.plugin.PluginObjectNode;
025: import org.eclipse.pde.internal.ui.editor.plugin.ManifestEditor;
026: import org.eclipse.search.ui.text.Match;
027: import org.eclipse.ui.IEditorPart;
028: import org.eclipse.ui.PartInitException;
029:
030: public class ManifestEditorOpener {
031:
032: public static IEditorPart open(Match match, boolean activate)
033: throws PartInitException {
034: IEditorPart editorPart = null;
035: editorPart = ManifestEditor.open(match.getElement(), true);
036: if (editorPart != null && editorPart instanceof ManifestEditor) {
037: ManifestEditor editor = (ManifestEditor) editorPart;
038: IDocument doc = editor.getDocument(match);
039: if (doc != null) {
040: Match exact = findExactMatch(doc, match, editor);
041: editor.openToSourcePage(match.getElement(), exact
042: .getOffset(), exact.getLength());
043: }
044: }
045: return editorPart;
046: }
047:
048: public static Match findExactMatch(IDocument document, Match match,
049: IEditorPart editor) {
050: if (match.getOffset() == -1
051: && match.getBaseUnit() == Match.UNIT_LINE)
052: return new Match(match.getElement(), Match.UNIT_CHARACTER,
053: 0, 0);
054: IPluginObject element = (IPluginObject) match.getElement();
055: String name = null;
056: String value = null;
057: IRegion region = null;
058: // since Extenion and Extension point matches don't contain line #'s, we need handle them differently (by trying to find matches in UI model)
059: if (editor instanceof ManifestEditor
060: && (element instanceof IPluginExtension || element instanceof IPluginExtensionPoint)) {
061: region = getAttributeMatch((ManifestEditor) editor,
062: element, document);
063: } else {
064: if (element instanceof IPluginImport) {
065: name = "plugin"; //$NON-NLS-1$
066: value = ((IPluginImport) element).getId();
067: } else if (element instanceof IPlugin) {
068: name = "id"; //$NON-NLS-1$
069: value = ((IPlugin) element).getId();
070: } else if (element instanceof IFragment) {
071: name = "id"; //$NON-NLS-1$
072: value = ((IFragment) element).getId();
073: }
074:
075: region = getAttributeRegionForLine(document, name, value,
076: match.getOffset());
077: }
078: if (region != null) {
079: return new Match(element, Match.UNIT_CHARACTER, region
080: .getOffset(), region.getLength());
081: }
082: return match;
083: }
084:
085: private static IRegion getAttributeRegionForLine(
086: IDocument document, String name, String value, int line) {
087: try {
088: int offset = document.getLineOffset(line)
089: + document.getLineLength(line);
090: return getAttributeRegion(document, name, value, offset);
091: } catch (BadLocationException e) {
092: }
093: return null;
094: }
095:
096: private static IRegion getAttributeRegion(IDocument document,
097: String name, String value, int offset) {
098: try {
099: FindReplaceDocumentAdapter findReplaceAdapter = new FindReplaceDocumentAdapter(
100: document);
101: IRegion nameRegion = findReplaceAdapter.find(offset, name
102: + "\\s*=\\s*\"" + value, false, false, false, true); //$NON-NLS-1$
103: if (nameRegion != null) {
104: if (document.get(
105: nameRegion.getOffset() + nameRegion.getLength()
106: - value.length(), value.length())
107: .equals(value))
108: return new Region(nameRegion.getOffset()
109: + nameRegion.getLength() - value.length(),
110: value.length());
111: }
112: } catch (BadLocationException e) {
113: }
114: return null;
115: }
116:
117: // Try to find a match for an Extension or Extension point by looking through the extensions/extension points in UI model for match.
118: private static IRegion getAttributeMatch(ManifestEditor editor,
119: IPluginObject object, IDocument document) {
120: IPluginObject[] elements = null;
121: // find equivalent models in UI text model
122: if (object instanceof IPluginExtension)
123: elements = ((IPluginModelBase) editor.getAggregateModel())
124: .getPluginBase().getExtensions();
125: else
126: elements = ((IPluginModelBase) editor.getAggregateModel())
127: .getPluginBase().getExtensionPoints();
128:
129: // iterate through the UI text models to find a match for a Search object.
130: for (int i = 0; i < elements.length; i++) {
131: if (object.equals(elements[i])) {
132: int offset = ((PluginObjectNode) elements[i])
133: .getOffset();
134: offset += ((PluginObjectNode) elements[i]).getLength();
135: String name = (object instanceof IPluginExtension) ? "point" : "id"; //$NON-NLS-1$ //$NON-NLS-2$
136: String value = (object instanceof IPluginExtension) ? ((IPluginExtension) object)
137: .getPoint()
138: : ((IPluginExtensionPoint) object).getId();
139: return getAttributeRegion(document, name, value, offset);
140: }
141: }
142: return null;
143: }
144:
145: }
|